1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
|
/*
* Copyright (C) 2009 Lars Wirzenius
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#define _LARGEFILE64_SOURCE
#include <errno.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
/* Choose a random integer in [0..n-1]. */
static long choose(long n)
{
if (n == 0)
return 0;
long buckets = (RAND_MAX + 1L) / n;
long max = buckets * n;
long r;
do {
r = random();
} while (r >= max);
return r / buckets;
}
static int corrupt(const char *filename)
{
int fd;
struct stat st;
fd = open(filename, O_RDWR | O_LARGEFILE);
if (fd == -1) {
fprintf(stderr, "Can't open %s: %s\n", filename, strerror(errno));
return -1;
}
if (fstat(fd, &st) == -1) {
fprintf(stderr, "Can't find length of %s: %s", filename,
strerror(errno));
return -1;
}
long offset = choose(st.st_size);
long bit = choose(CHAR_BIT);
printf("%s: offset=%ld bit=%ld\n", filename, offset, bit);
if (lseek(fd, offset, SEEK_SET) == (off_t) -1) {
fprintf(stderr, "Can't seek in %s: %s", filename, strerror(errno));
return -1;
}
unsigned char byte;
if (read(fd, &byte, 1) == -1) {
fprintf(stderr, "Can't read %s: %s", filename, strerror(errno));
return -1;
}
/* This is where we toggle the bit. */
unsigned mask = 1 << bit; // All zeroes, except for the interesting bit.
unsigned interesting = byte & mask; // Only the interesting bit can be 1.
unsigned toggled = interesting ^ mask; // Interesting bit has been toggled.
unsigned others = byte & (~mask); // Interesting bit is 0.
byte = others | toggled;
/* Now write it back. */
if (lseek(fd, offset, SEEK_SET) == (off_t) -1) {
fprintf(stderr, "Can't seek in %s: %s", filename, strerror(errno));
return -1;
}
if (write(fd, &byte, 1) != 1) {
fprintf(stderr, "Can't write %s: %s", filename, strerror(errno));
return -1;
}
if (close(fd) == -1) {
fprintf(stderr, "Can't close %s: %s", filename, strerror(errno));
return -1;
}
return 0;
}
int main(int argc, char **argv)
{
int exit_code;
srandom(time(NULL) + getpid());
exit_code = 0;
for (int i = 1; i < argc; ++i)
if (corrupt(argv[i]) == -1)
exit_code = 1;
return exit_code;
}
|