-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathlibfioinfo.c
470 lines (424 loc) · 12.6 KB
/
libfioinfo.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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
/*
* File I/O Functions Info
* This library provides the statistics what functions where used during the execution.
*
* Author: Marek Zmysłowski
* Version: 0.3
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
* http://www.apache.org/licenses/LICENSE-2.0
* This is the real deal: the program takes an instrumented binary and
* attempts a variety of basic fuzzing tricks, paying close attention to
* how they affect the execution path.
*
*/
#define _GNU_SOURCE
#include <dlfcn.h>
#include <stdlib.h>
#include <stdio.h>
#include "libfiodef.h"
//
// Variables holding statistics
//
// Standard
static int fopen_count;
static int fwrite_count;
static int fputc_count;
static int fputs_count;
static int fread_count;
static int fseek_count;
static int ftell_count;
static int fgetc_count;
static int fgets_count;
static int fclose_count;
// Nonlocking
static int fwrite_unlocked_count;
static int fputc_unlocked_count;
static int fputs_unlocked_count;
static int fread_unlocked_count;
static int fgetc_unlocked_count;
static int fgets_unlocked_count;
// POSIX
static int open_count;
static int read_count;
static int write_count;
static int lseek_count;
static int close_count;
/*
* fopen wrapper
*/
FILE *fopen(const char *path, const char *mode)
{
fopen_count++;
#ifdef DEBUG
printf("fopen - path:%s, mode:%s\n", path, mode);
#endif
return _libc_fopen(path, mode);
}
#if defined(_LARGEFILE64_SOURCE) && !defined(__APPLE__)
FILE *fopen64(const char *path, const char *mode)
{
fopen_count++;
#ifdef DEBUG
printf("fopen - path:%s, mode:%s\n", path, mode);
#endif
return _libc_fopen64(path, mode);
}
#endif
/****************************************************************************************************
*
* Standard stdio function wrappers
*
****************************************************************************************************/
/*
* fwrite wrapper
*/
size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream)
{
if (stream != stderr && stream != stdout)
fwrite_count++;
#ifdef DEBUG
printf("fwrite - stream:%p, buffer:%p, size:%ld, nmemb:%ld\n", stream, ptr, size, nmemb);
#endif
return _libc_fwrite(ptr, size, nmemb, stream);
}
/*
* fputc wrapper
*/
int fputc(int character, FILE *stream)
{
if (stream != stderr && stream != stdout)
fputc_count++;
#ifdef DEBUG
printf("fputc - stream:%p, character:%x \n", stream, character);
#endif
return _libc_fputc(character, stream);
}
/*
* fputs wrapper
*/
int fputs(const char *str, FILE *stream)
{
if (stream != stderr && stream != stdout)
fputs_count++;
#ifdef DEBUG
printf("fputs - stream:%p, buffer:%s\n", stream, str);
#endif
return _libc_fputs(str, stream);
}
/*
* fread wrapper
*/
size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream)
{
if (stream != stderr && stream != stdout)
fread_count++;
#ifdef DEBUG
printf("fread - stream:%p, buffer:%p, size:%ld, nmemb:%ld\n", stream, ptr, size, nmemb);
#endif
return _libc_fread(ptr, size, nmemb, stream);
}
/*
* fseek wrapper
*/
int fseek(FILE *stream, long offset, int whence)
{
if (stream != stderr && stream != stdout)
fseek_count++;
#ifdef DEBUG
printf("fseek - stream:%p, offest:%ld, whence:%d\n", stream, offset, whence);
#endif
return _libc_fseek(stream, offset, whence);
}
/*
* fgetc wrapper
*/
int fgetc(FILE *stream)
{
if (stream != stderr && stream != stdout)
fgetc_count++;
#ifdef DEBUG
printf("fgetc - stream:%p\n", stream);
#endif
return _libc_fgetc(stream);
}
/*
* fgets wrapper
*/
char *fgets(char *str, int num, FILE *stream)
{
if (stream != stderr && stream != stdout)
fgets_count++;
#ifdef DEBUG
printf("fgets - stream:%p, buffer:%s, size:%d\n", stream, str, num);
#endif
return _libc_fgets(str, num, stream);
}
/////////////////////////////////////////////////////////////////////////////////////////////////
/*
* ftell wrapper
*/
long ftell(FILE *stream)
{
if (stream != stderr && stream != stdout)
ftell_count++;
#ifdef DEBUG
printf("ftell - stream:%p\n", stream);
#endif
return _libc_ftell(stream);
}
off_t ftello(FILE *stream)
{
if (stream != stderr && stream != stdout)
ftell_count++;
#ifdef DEBUG
printf("ftello - stream:%p\n", stream);
#endif
return _libc_ftello(stream);
}
#if defined(_LARGEFILE64_SOURCE) && !defined(__APPLE__)
off64_t ftello64(FILE *stream)
{
if (stream != stderr && stream != stdout)
ftell_count++;
#ifdef DEBUG
printf("ftello64 - stream:%p\n", stream);
#endif
return _libc_ftello64(stream);
}
#endif
/////////////////////////////////////////////////////////////////////////////////////////////////
/*
* fclose wrapper
*/
int fclose(FILE *stream)
{
if (stream != stderr && stream != stdout)
fclose_count++;
#ifdef DEBUG
printf("fclose - stream:%p\n", stream);
#endif
return _libc_fclose(stream);
}
/****************************************************************************************************
*
* Nonlocking stdio function wrappers
*
****************************************************************************************************/
/*
* fwrite_unlocked wrapper
*/
#ifndef __OPTIMIZE__
size_t fwrite_unlocked(const void *ptr, size_t size, size_t nmemb, FILE *stream)
{
if (stream != stderr && stream != stdout)
fwrite_unlocked_count++;
#ifdef DEBUG
printf("fwrite_unlocked - stream:%p, buffer:%p, size:%ld, nmemb:%ld\n", stream, ptr, size, nmemb);
#endif
return _libc_fwrite_unlocked(ptr, size, nmemb, stream);
}
#endif
/*
* fputc_unlocked wrapper
*/
int fputc_unlocked(int character, FILE *stream)
{
if (stream != stderr && stream != stdout)
fputc_unlocked_count++;
#ifdef DEBUG
printf("fputs_unlocked - stream:%p, character:%c \n", stream, character);
#endif
return _libc_fputc_unlocked(character, stream);
}
/*
* fputs_unlocked wrapper
*/
int fputs_unlocked(const char *str, FILE *stream)
{
if (stream != stderr && stream != stdout)
fputs_unlocked_count++;
#ifdef DEBUG
printf("fputs_unlocked - stream:%p, buffer:%s\n", stream, str);
#endif
return _libc_fputs_unlocked(str, stream);
}
/*
* fread_unlocked wrapper
*/
#ifndef __OPTIMIZE__
size_t fread_unlocked(void *ptr, size_t size, size_t nmemb, FILE *stream)
{
if (stream != stderr && stream != stdout)
fread_unlocked_count++;
#ifdef DEBUG
printf("fread_unlocked - stream:%p, buffer:%p, size:%ld, nmemb:%ld\n", stream, ptr, size, nmemb);
#endif
return _libc_fread_unlocked(ptr, size, nmemb, stream);
}
#endif
/*
* fgetc_unlocked wrapper
*/
int fgetc_unlocked(FILE *stream)
{
if (stream != stderr && stream != stdout)
fgetc_unlocked_count++;
#ifdef DEBUG
printf("fgetc_unlocked - stream:%p\n", stream);
#endif
return _libc_fgetc_unlocked(stream);
}
/*
* fgets_unlocked wrapper
*/
char *fgets_unlocked(char *str, int num, FILE *stream)
{
if (stream != stderr && stream != stdout)
fgets_unlocked_count++;
#ifdef DEBUG
printf("fgets_unlocked - stream:%p, buffer:%s, size:%d\n", stream, str, num);
#endif
return _libc_fgets_unlocked(str, num, stream);
}
int open(const char *pathname, int flags, ...)
{
open_count++;
#ifdef DEBUG
printf("open - path:%s\n", pathname);
#endif
return _posix_open(pathname, flags);
}
ssize_t read(int fd, void *buf, size_t count)
{
read_count++;
#ifdef DEBUG
printf("read - fd %d, buf:%p, count:%ld\n", fd, buf, count);
#endif
return _posix_read(fd, buf, count);
}
ssize_t write(int fd, const void *buf, size_t count)
{
write_count++;
#ifdef DEBUG
printf("write - fd %d, buf:%p, count:%ld\n", fd, buf, count);
#endif
return _posix_write(fd, buf, count);
}
off_t lseek(int fd, off_t offset, int whence)
{
lseek_count++;
#ifdef DEBUG
printf("lseek - fd %d, offset:%ld, whence:%d\n", fd, offset, whence);
#endif
return _posix_lseek(fd, offset, whence);
}
int close(int fd)
{
close_count++;
#ifdef DEBUG
printf("close - fd %d\n", fd);
#endif
return _posix_close(fd);
}
/*
* Fuction prints the banner and the library info
*/
void show_banner()
{
printf("===========================================\n");
printf("\t\tlibfioinfo\n");
printf("Version: 0.3\n");
printf("Author: Marek Zmysłowski\n");
printf("===========================================\n\n");
}
/*
* Function that prints statistics of the function calls
*/
void show_results()
{
printf("===========================================\n");
printf("\tFile calls statistics:\n");
printf("fopen: %d\n", fopen_count);
printf("fwrite: %d - unlocked: %d\n", fwrite_count, fwrite_unlocked_count);
printf("fputc: %d - unlocked: %d\n", fputc_count, fputc_unlocked_count);
printf("fputs: %d - unlocked: %d\n", fputs_count, fputs_unlocked_count);
printf("fread: %d - unlocked: %d\n", fread_count, fread_unlocked_count);
printf("fgetc: %d - unlocked: %d\n", fgetc_count, fgetc_unlocked_count);
printf("fgets: %d - unlocked: %d\n", fgets_count, fgets_unlocked_count);
printf("fseek: %d\n", fseek_count);
printf("ftell: %d\n", ftell_count);
printf("fclose: %d\n", fclose_count);
printf ("\n");
printf("open: %d\n", open_count);
printf("read: %d\n", read_count);
printf("write: %d\n", write_count);
printf("lseek: %d\n", lseek_count);
printf("close: %d\n", close_count);
printf("===========================================\n");
}
/*
* Library constructor
*/
__attribute__((constructor)) static void init(void)
{
_libc_fopen = (FILE * (*)(const char *path, const char *mode)) dlsym(RTLD_NEXT, "fopen");
_libc_fwrite = (size_t (*)(const void *ptr, size_t size, size_t nmemb, FILE *stream))dlsym(RTLD_NEXT, "fwrite");
_libc_fputc = (int (*)(int character, FILE *fp))dlsym(RTLD_NEXT, "fputc");
_libc_fputs = (int (*)(const char *str, FILE *fp))dlsym(RTLD_NEXT, "fputs");
_libc_fread = (size_t (*)(void *ptr, size_t size, size_t nmemb, FILE *stream))dlsym(RTLD_NEXT, "fread");
_libc_fgetc = (int (*)(FILE * fp)) dlsym(RTLD_NEXT, "fgetc");
_libc_fgets = (char *(*)(char *str, int num, FILE *fp))dlsym(RTLD_NEXT, "fgets");
_libc_fseek = (int (*)(FILE * stream, long offset, int whence)) dlsym(RTLD_NEXT, "fseek");
_libc_fseeko = (int (*)(FILE * stream, off_t offset, int whence)) dlsym(RTLD_NEXT, "fseeko");
_libc_ftell = (long (*)(FILE * fp)) dlsym(RTLD_NEXT, "ftell");
_libc_ftello = (off_t (*)(FILE * fp)) dlsym(RTLD_NEXT, "ftello");
#if defined(_LARGEFILE64_SOURCE) && !defined(__APPLE__)
_libc_fopen64 = (FILE * (*)(const char *path, const char *mode)) dlsym(RTLD_NEXT, "fopen64");
_libc_ftello64 = (off64_t(*)(FILE * fp)) dlsym(RTLD_NEXT, "ftello64");
_libc_fseeko64 = (int (*)(FILE * stream, off64_t offset, int whence)) dlsym(RTLD_NEXT, "fseeko64");
#endif
_libc_fclose = (int (*)(FILE * fp)) dlsym(RTLD_NEXT, "fclose");
_libc_fwrite_unlocked = (size_t (*)(const void *ptr, size_t size, size_t nmemb, FILE *stream))dlsym(RTLD_NEXT, "fwrite_unlocked");
_libc_fputc_unlocked = (int (*)(int character, FILE *fp))dlsym(RTLD_NEXT, "fputc_unlocked");
_libc_fputs_unlocked = (int (*)(const char *str, FILE *fp))dlsym(RTLD_NEXT, "fputs_unlocked");
_libc_fread_unlocked = (size_t(*)(void *ptr, size_t size, size_t nmemb, FILE *stream))dlsym(RTLD_NEXT, "fread_unlocked");
_libc_fgetc_unlocked = (int (*)(FILE * fp)) dlsym(RTLD_NEXT, "fgetc_unlocked");
_libc_fgets_unlocked = (char *(*)(char *str, int num, FILE *fp))dlsym(RTLD_NEXT, "fgets_unlocked");
_posix_open = (int (*)(const char *pathname, int flags, ...))dlsym(RTLD_NEXT, "open");
_posix_read = (ssize_t (*)(int fd, void *buf, size_t count))dlsym(RTLD_NEXT, "read");
_posix_write = (ssize_t (*)(int fd, const void *buf, size_t count))dlsym(RTLD_NEXT, "write");
_posix_lseek = (off_t (*)(int fd, off_t offset, int whence))dlsym(RTLD_NEXT, "lseek");
_posix_close = (int (*)(int fd))dlsym(RTLD_NEXT, "close");
fopen_count = 0;
fwrite_count = 0;
fputc_count = 0;
fputs_count = 0;
fread_count = 0;
fgetc_count = 0;
fgets_count = 0;
fseek_count = 0;
ftell_count = 0;
fclose_count = 0;
fwrite_unlocked_count = 0;
fputc_unlocked_count = 0;
fputs_unlocked_count = 0;
fread_unlocked_count = 0;
fgetc_unlocked_count = 0;
fgets_unlocked_count = 0;
open_count = 0;
read_count = 0;
write_count = 0;
lseek_count = 0;
close_count = 0;
show_banner();
}
/*
* Library destructor
*/
__attribute__((destructor)) static void unload(void)
{
show_results();
}