|
| 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 "envmodules.h" |
| 20 | + |
| 21 | +#define MAX_ENTRIES 32 |
| 22 | +#define MAX_NAME_LEN 200 |
| 23 | + |
| 24 | +static void |
| 25 | +cleanupDir( |
| 26 | + const char *dir) |
| 27 | +{ |
| 28 | + DIR *d; |
| 29 | + struct dirent *de; |
| 30 | + char fpath[PATH_MAX]; |
| 31 | + |
| 32 | + d = opendir(dir); |
| 33 | + if (d == NULL) { |
| 34 | + return; |
| 35 | + } |
| 36 | + while ((de = readdir(d)) != NULL) { |
| 37 | + if (strcmp(de->d_name, ".") != 0 && strcmp(de->d_name, "..") != 0) { |
| 38 | + snprintf(fpath, sizeof(fpath), "%s/%s", dir, de->d_name); |
| 39 | + unlink(fpath); |
| 40 | + } |
| 41 | + } |
| 42 | + closedir(d); |
| 43 | +} |
| 44 | + |
| 45 | +int |
| 46 | +LLVMFuzzerTestOneInput( |
| 47 | + const uint8_t *data, |
| 48 | + size_t size) |
| 49 | +{ |
| 50 | + char dirtemplate[] = "/tmp/cflite_gfid.XXXXXX"; |
| 51 | + char *dir; |
| 52 | + size_t start, i; |
| 53 | + int created = 0; |
| 54 | + Tcl_Interp *interp; |
| 55 | + Tcl_Obj *objv[3]; |
| 56 | + |
| 57 | + if (size < 1) { |
| 58 | + return 0; |
| 59 | + } |
| 60 | + |
| 61 | + dir = mkdtemp(dirtemplate); |
| 62 | + if (dir == NULL) { |
| 63 | + return 0; |
| 64 | + } |
| 65 | + |
| 66 | + /* Split the input (past the leading flag byte) on newlines into |
| 67 | + * candidate file names. */ |
| 68 | + start = 1; |
| 69 | + for (i = 1; i <= size && created < MAX_ENTRIES; i++) { |
| 70 | + if (i == size || data[i] == '\n') { |
| 71 | + size_t len = i - start; |
| 72 | + if (len > 0 && len < MAX_NAME_LEN) { |
| 73 | + char name[MAX_NAME_LEN + 1]; |
| 74 | + char fpath[PATH_MAX]; |
| 75 | + int fd; |
| 76 | + |
| 77 | + memcpy(name, data + start, len); |
| 78 | + name[len] = '\0'; |
| 79 | + if (strchr(name, '/') == NULL && strcmp(name, ".") != 0 && |
| 80 | + strcmp(name, "..") != 0) { |
| 81 | + snprintf(fpath, sizeof(fpath), "%s/%s", dir, name); |
| 82 | + fd = open(fpath, O_CREAT | O_WRONLY, 0600); |
| 83 | + if (fd != -1) { |
| 84 | + close(fd); |
| 85 | + created++; |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + start = i + 1; |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + interp = Tcl_CreateInterp(); |
| 94 | + |
| 95 | + objv[0] = Tcl_NewStringObj("getFilesInDirectory", -1); |
| 96 | + objv[1] = Tcl_NewStringObj(dir, -1); |
| 97 | + objv[2] = Tcl_NewBooleanObj(data[0] & 1); |
| 98 | + Tcl_IncrRefCount(objv[0]); |
| 99 | + Tcl_IncrRefCount(objv[1]); |
| 100 | + Tcl_IncrRefCount(objv[2]); |
| 101 | + |
| 102 | + Envmodules_GetFilesInDirectoryObjCmd(NULL, interp, 3, objv); |
| 103 | + |
| 104 | + Tcl_DecrRefCount(objv[0]); |
| 105 | + Tcl_DecrRefCount(objv[1]); |
| 106 | + Tcl_DecrRefCount(objv[2]); |
| 107 | + Tcl_DeleteInterp(interp); |
| 108 | + |
| 109 | + cleanupDir(dir); |
| 110 | + rmdir(dir); |
| 111 | + |
| 112 | + return 0; |
| 113 | +} |
0 commit comments