| 1 |
/* |
| 2 |
* ClusterFuzzLite target for Envmodules_GetFilesInDirectoryObjCmd, which |
| 3 |
* lists a directory while special-casing .modulerc/.version and hidden |
| 4 |
* entries. Module directories can live on shared, multi-tenant |
| 5 |
* filesystems, so this exercises the entry-name handling with |
| 6 |
* fuzzer-controlled file names rather than only fuzzer-controlled paths. |
| 7 |
*/ |
| 8 |
|
| 9 |
#define _GNU_SOURCE |
| 10 |
#include <stdint.h> |
| 11 |
#include <stddef.h> |
| 12 |
#include <stdlib.h> |
| 13 |
#include <string.h> |
| 14 |
#include <limits.h> |
| 15 |
#include <dirent.h> |
| 16 |
#include <unistd.h> |
| 17 |
#include <fcntl.h> |
| 18 |
#include <stdio.h> |
| 19 |
#include "config.h" |
| 20 |
#include "envmodules.h" |
| 21 |
|
| 22 |
#define MAX_ENTRIES 32 |
| 23 |
#define MAX_NAME_LEN 200 |
| 24 |
|
| 25 |
static void |
| 26 |
cleanupDir( |
| 27 |
const char *dir) |
| 28 |
{ |
| 29 |
DIR *d; |
| 30 |
struct dirent *de; |
| 31 |
char fpath[PATH_MAX]; |
| 32 |
|
| 33 |
d = opendir(dir); |
| 34 |
if (d == NULL) { |
| 35 |
return; |
| 36 |
} |
| 37 |
while ((de = readdir(d)) != NULL) { |
| 38 |
if (strcmp(de->d_name, ".") != 0 && strcmp(de->d_name, "..") != 0) { |
| 39 |
snprintf(fpath, sizeof(fpath), "%s/%s", dir, de->d_name); |
| 40 |
unlink(fpath); |
| 41 |
} |
| 42 |
} |
| 43 |
closedir(d); |
| 44 |
} |
| 45 |
|
| 46 |
int |
| 47 |
LLVMFuzzerTestOneInput( |
| 48 |
const uint8_t *data, |
| 49 |
size_t size) |
| 50 |
{ |
| 51 |
char dirtemplate[] = "/tmp/cflite_gfid.XXXXXX"; |
| 52 |
char *dir; |
| 53 |
size_t start, i; |
| 54 |
int created = 0; |
| 55 |
Tcl_Interp *interp; |
| 56 |
Tcl_Obj *objv[3]; |
| 57 |
|
| 58 |
if (size < 1) { |
| 59 |
return 0; |
| 60 |
} |
| 61 |
|
| 62 |
dir = mkdtemp(dirtemplate); |
| 63 |
if (dir == NULL) { |
| 64 |
return 0; |
| 65 |
} |
| 66 |
|
| 67 |
/* Split the input (past the leading flag byte) on newlines into |
| 68 |
* candidate file names. */ |
| 69 |
start = 1; |
| 70 |
for (i = 1; i <= size && created < MAX_ENTRIES; i++) { |
| 71 |
if (i == size || data[i] == '\n') { |
| 72 |
size_t len = i - start; |
| 73 |
if (len > 0 && len < MAX_NAME_LEN) { |
| 74 |
char name[MAX_NAME_LEN + 1]; |
| 75 |
char fpath[PATH_MAX]; |
| 76 |
int fd; |
| 77 |
|
| 78 |
memcpy(name, data + start, len); |
| 79 |
name[len] = '\0'; |
| 80 |
if (strchr(name, '/') == NULL && strcmp(name, ".") != 0 && |
| 81 |
strcmp(name, "..") != 0) { |
| 82 |
snprintf(fpath, sizeof(fpath), "%s/%s", dir, name); |
| 83 |
fd = open(fpath, O_CREAT | O_WRONLY, 0600); |
| 84 |
if (fd != -1) { |
| 85 |
close(fd); |
| 86 |
created++; |
| 87 |
} |
| 88 |
} |
| 89 |
} |
| 90 |
start = i + 1; |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
interp = Tcl_CreateInterp(); |
| 95 |
|
| 96 |
objv[0] = Tcl_NewStringObj("getFilesInDirectory", -1); |
| 97 |
objv[1] = Tcl_NewStringObj(dir, -1); |
| 98 |
objv[2] = Tcl_NewBooleanObj(data[0] & 1); |
| 99 |
Tcl_IncrRefCount(objv[0]); |
| 100 |
Tcl_IncrRefCount(objv[1]); |
| 101 |
Tcl_IncrRefCount(objv[2]); |
| 102 |
|
| 103 |
Envmodules_GetFilesInDirectoryObjCmd(NULL, interp, 3, objv); |
| 104 |
|
| 105 |
Tcl_DecrRefCount(objv[0]); |
| 106 |
Tcl_DecrRefCount(objv[1]); |
| 107 |
Tcl_DecrRefCount(objv[2]); |
| 108 |
Tcl_DeleteInterp(interp); |
| 109 |
|
| 110 |
cleanupDir(dir); |
| 111 |
rmdir(dir); |
| 112 |
|
| 113 |
return 0; |
| 114 |
} |