forked from stevegrubb/fsfuzzer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fstest.c
367 lines (316 loc) · 6.95 KB
/
fstest.c
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/* fstest.c --
* Copyright 2006-12 Red Hat Inc., Durham, North Carolina.
* All Rights Reserved.
*
* 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 2 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, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Authors:
* Steve Grubb <[email protected]>
*/
#include "config.h"
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/vfs.h>
#include <dirent.h>
#include <sys/stat.h>
#include <sys/sendfile.h>
#include <sys/socket.h>
#include <netdb.h>
#include <signal.h>
#include <sys/file.h>
#include <sys/mman.h>
#include <sys/stat.h>
#define LOGGIT 1
#define CHECK_XATTR 1
#ifdef CHECK_XATTR
#include <attr/xattr.h>
#define MAXLISTBUF 65536
#endif
char origin[PATH_MAX];
const char *orig_dpath = NULL;
char *lbuf = NULL;
struct statfs sfbuf;
struct stat sbuf;
int dev_null = -1, sfd = -1;
struct addrinfo *res=NULL, hints;
static void mlog(const char *msg)
{
#ifdef LOGGIT
puts(msg);
#else
msg = NULL;
#endif
}
static void check_statfs(void)
{
mlog("+statfs");
statfs(orig_dpath, &sfbuf);
}
static void setup_socket(void)
{ // setup connection to xinetd discard
if ((sfd < 0) && res) {
struct addrinfo *ptr;
for (ptr = res; ptr; ptr = ptr->ai_next) {
if (ptr->ai_socktype != hints.ai_socktype)
continue;
sfd = socket(ptr->ai_family,
ptr->ai_socktype, ptr->ai_protocol);
if (sfd < 0)
continue;
if (connect(sfd, ptr->ai_addr, ptr->ai_addrlen) < 0) {
close(sfd);
sfd = -1;
continue;
} else
break;
}
}
}
static void file_read_checks(const char *path)
{
char buf1[16],buf2[8],buf3[32];
struct iovec vec[3];
int rc, f, cnt;
off_t i;
mlog("+open");
f = open(path, O_RDONLY);
if (f < 0)
return;
// first look at the file's attributes
mlog("+fstat");
rc = fstat(f, &sbuf);
#ifdef CHECK_XATTR
mlog("+flistxattr");
flistxattr(f, lbuf, MAXLISTBUF);
mlog("+fgetxattr");
fgetxattr(f, "selinux", lbuf, MAXLISTBUF);
#endif
// readahead(f, NULL
// Check reading
mlog("+read");
cnt = 0;
while((read(f, buf3, sizeof(buf3)) > 0) && cnt < 1000)
cnt++;
// lseek around
mlog("+lseek");
if (rc == 0)
lseek(f, sbuf.st_size, SEEK_SET);
lseek(f, 100000, SEEK_SET);
lseek(f, 0, SEEK_SET);
// vectored reads
vec[0].iov_base = (void*)&buf1;
vec[0].iov_len = sizeof(buf1);
vec[1].iov_base = (void*)&buf2;
vec[1].iov_len = sizeof(buf2);
vec[2].iov_base = (void*)&buf3;
vec[2].iov_len = sizeof(buf3);
mlog("+readv");
cnt = 0;
while((readv(f, vec, 3) > 0) && cnt < 1000)
cnt++;
// check out pread syscall
i = 0;
mlog("+pread");
cnt = 0;
while ((pread(f, buf1, sizeof(buf1), i) > 0) && cnt < 1000) {
i += sizeof(buf1)*2;
cnt++;
}
// flock
mlog("+flock");
flock(f, LOCK_SH|LOCK_NB);
flock(f, LOCK_UN);
// fcntl ?
// sendfile to localhost:discard
setup_socket();
if (sfd >= 0) {
mlog("+sendfile");
lseek(f, 0, SEEK_SET);
sendfile(sfd, f, NULL, rc ? 100000 : sbuf.st_size);
close(sfd);
sfd = -1;
}
// mmap each file
if (rc == 0) {
char *src;
mlog("+mmap");
src = mmap(0, sbuf.st_size, PROT_READ, MAP_FILE | MAP_SHARED,
f, 0);
if (src != (char *)-1) {
// Don't touch memory...or Mr. SIGBUS will visit you
mlog("+munmap");
munmap(src, sbuf.st_size);
}
}
close(f);
}
static void check_dir(const char *dpath, int level)
{
DIR *d;
struct dirent *e;
char path[PATH_MAX];
int count = 0, dfd;
if (level >= 4) {
puts("excessive recursion detected");
return;
}
// get directory listing
mlog("+opendir");
d = opendir(dpath);
if (d == NULL) {
printf("opendir failed: %s\n", strerror(errno));
return;
}
// fstatfs
mlog("+fstatfs");
dfd = dirfd(d);
fstatfs(dfd, &sfbuf);
while ((e = readdir(d))) {
int rc = -1;
if (strcmp(e->d_name, ".") == 0)
continue;
if (strcmp(e->d_name, "..") == 0)
continue;
count++;
if (count > 64) {
puts("excessive entries detected");
break;
}
snprintf(path, sizeof(path), "%s/%s", dpath, e->d_name);
// stat each
mlog("+stat");
rc = stat(path, &sbuf);
if (rc == 0 && S_ISDIR(sbuf.st_mode)) {
mlog("+chdir");
if (chdir(path) == 0) {
mlog("+getcwd");
getcwd(path, sizeof(path));
mlog("+fchdir");
fchdir(dirfd(d));
chdir(origin);
check_dir(path, level+1);
}
}
mlog("+fstatat");
rc = fstatat(dfd, path, &sbuf, AT_SYMLINK_NOFOLLOW);
// access each
mlog("+access");
access(path, R_OK|W_OK|X_OK);
mlog("+faccessat");
faccessat(dfd, path, R_OK|W_OK|X_OK,
AT_EACCESS|AT_SYMLINK_NOFOLLOW);
// lstat each
mlog("+lstat");
lstat(path, &sbuf);
// Check xattrs
#ifdef CHECK_XATTR
mlog("+listxattr");
listxattr(path, lbuf, MAXLISTBUF);
mlog("+llistxattr");
llistxattr(path, lbuf, MAXLISTBUF);
mlog("+getxattr");
getxattr(path, "selinux", lbuf, MAXLISTBUF);
mlog("+lgetxattr");
lgetxattr(path, "selinux", lbuf, MAXLISTBUF);
#endif
if ( !(S_ISFIFO(sbuf.st_mode)||S_ISSOCK(sbuf.st_mode)) ) {
int fd;
mlog("+openat");
fd = openat(dfd, path, O_RDONLY);
close(fd);
file_read_checks(path);
mlog("+readlink");
readlink(path, lbuf, MAXLISTBUF);
mlog("+readlinkat");
readlinkat(dfd, path, lbuf, MAXLISTBUF);
}
}
closedir(d);
}
static void dir_tests(const char *dpath)
{
dpath = dpath; // shut up compiler
// new file tests
// mkdir
// mknod
// rename
}
// Consider attr, read, write, full in config file
/* We should be passed the directory to be tested */
int main(int argc, char *argv[])
{
int rc;
if (argc != 2)
exit(-1);
// OK start the tests
orig_dpath = argv[1];
getcwd(origin, sizeof(origin));
#ifdef CHECK_XATTR
lbuf = malloc(MAXLISTBUF+1);
if (!lbuf)
exit(-1);
#endif
dev_null = open("/dev/null", O_RDWR);
if (dev_null < 0)
exit(-1);
signal(SIGPIPE, SIG_IGN);
memset(&hints, 0, sizeof(hints));
hints.ai_family = PF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
// Setup address for later use
rc = getaddrinfo("localhost", "discard", &hints, &res);
// fs tests
check_statfs();
// directory tests
check_dir(orig_dpath, 0);
dir_tests(orig_dpath);
/*
file write tests
15) creat
truncate
ftruncate
write
16) writev
17) pwrite
16) fsync
17) fdatasync
file attr tests
chown
lchown
chmod
lchmod
quotactl
setxattr
fsetxattr
lsetxattr
removexattr
fremovexattr
lremovexattr
*/
if (rc == 0)
freeaddrinfo(res);
if (sfd >= 0)
close(sfd);
if (dev_null >= 0)
close(dev_null);
free(lbuf);
puts("++++ Tests finished");
return 0;
}