| 1 |
/* |
| 2 |
* ClusterFuzzLite target for Envmodules_ReadFileObjCmd, which opens, |
| 3 |
* reads and closes a file while looking for the "#%Module" magic cookie |
| 4 |
* on its first line. |
| 5 |
* |
| 6 |
* The fuzzer input is written to a memfd instead of a real filesystem |
| 7 |
* path so each run stays off disk; the readFile command still receives |
| 8 |
* an ordinary path, via /proc/self/fd/<n>. |
| 9 |
*/ |
| 10 |
|
| 11 |
#define _GNU_SOURCE |
| 12 |
#include <stdint.h> |
| 13 |
#include <stddef.h> |
| 14 |
#include <stdio.h> |
| 15 |
#include <sys/mman.h> |
| 16 |
#include <unistd.h> |
| 17 |
#include "config.h" |
| 18 |
#include "envmodules.h" |
| 19 |
|
| 20 |
int |
| 21 |
LLVMFuzzerTestOneInput( |
| 22 |
const uint8_t *data, |
| 23 |
size_t size) |
| 24 |
{ |
| 25 |
int fd; |
| 26 |
char path[64]; |
| 27 |
Tcl_Interp *interp; |
| 28 |
Tcl_Obj *objv[4]; |
| 29 |
|
| 30 |
if (size < 1) { |
| 31 |
return 0; |
| 32 |
} |
| 33 |
|
| 34 |
/* First input byte selects the firstline/must_have_cookie flags; the |
| 35 |
* remaining bytes become the file content read back by readFile. */ |
| 36 |
fd = memfd_create("cflite_readfile", 0); |
| 37 |
if (fd == -1) { |
| 38 |
return 0; |
| 39 |
} |
| 40 |
if (write(fd, data + 1, size - 1) != (ssize_t) (size - 1)) { |
| 41 |
close(fd); |
| 42 |
return 0; |
| 43 |
} |
| 44 |
snprintf(path, sizeof(path), "/proc/self/fd/%d", fd); |
| 45 |
|
| 46 |
interp = Tcl_CreateInterp(); |
| 47 |
|
| 48 |
objv[0] = Tcl_NewStringObj("readFile", -1); |
| 49 |
objv[1] = Tcl_NewStringObj(path, -1); |
| 50 |
objv[2] = Tcl_NewBooleanObj(data[0] & 1); |
| 51 |
objv[3] = Tcl_NewBooleanObj((data[0] >> 1) & 1); |
| 52 |
Tcl_IncrRefCount(objv[0]); |
| 53 |
Tcl_IncrRefCount(objv[1]); |
| 54 |
Tcl_IncrRefCount(objv[2]); |
| 55 |
Tcl_IncrRefCount(objv[3]); |
| 56 |
|
| 57 |
Envmodules_ReadFileObjCmd(NULL, interp, 4, objv); |
| 58 |
|
| 59 |
Tcl_DecrRefCount(objv[0]); |
| 60 |
Tcl_DecrRefCount(objv[1]); |
| 61 |
Tcl_DecrRefCount(objv[2]); |
| 62 |
Tcl_DecrRefCount(objv[3]); |
| 63 |
Tcl_DeleteInterp(interp); |
| 64 |
close(fd); |
| 65 |
|
| 66 |
return 0; |
| 67 |
} |