/** * @file mvsfmt.c * @brief libFuzzer target for the MVS decoders (src/mvsfmt.c). * * The first byte chooses the decoder; the rest is the buffer it gets: * a CSI work area, a PDS directory block, TIOT entries, a filter key, * the work area a fake IGGCSI00 returns, or a name field. Built and * run by `make fuzz` (clang, ASan, UBSan). */ #include #include #include #include "../src/mvsfmt.h" /** @brief CSIRESUM in src/mvsfmt.c: the byte that asks for a resume. */ #define CSI_RESUM 149 /** @brief Callback: touch every byte of the name so ASan sees it. */ static void sink(const char *name, char type, void *arg) { size_t *total = arg; *total += strlen(name) + (size_t)(unsigned char)type; } /* ---- a fake IGGCSI00, so csi_search's resume loop can be fuzzed ------ */ /** @brief Bytes the fake call fills the work area with. */ static const unsigned char *fake_src; /** @brief Their number. */ static size_t fake_len; /** @brief One fake IGGCSI00 call: hand back the fuzz bytes and maybe resume. */ static int fake_call(csi_area *a, void *ctx) { (void)ctx; memset(a->work, 0, a->worklen); memcpy(a->work, fake_src, fake_len < a->worklen ? fake_len : a->worklen); /* A blank byte here means "no more"; anything else resumes. */ a->field[CSI_RESUM] = fake_len && (fake_src[0] & 1) ? 0xe8 : 0x40; return fake_len && (fake_src[0] & 2) ? 4 : 0; } /** @brief Search with a work area of `worklen` the fake call fills. */ static void fuzz_search(const unsigned char *buf, size_t size, uint32_t worklen) { unsigned char reason[4] = {0}; unsigned char field[CSI_FIELD_LEN]; char *filter = malloc(size + 1); unsigned char *work = malloc(worklen); if (!filter || !work) { free(filter); free(work); return; } memcpy(filter, buf, size); filter[size] = '\0'; fake_src = buf; fake_len = size; csi_area a = {reason, field, work, worklen}; size_t total = 0; (void)csi_search(filter, &a, fake_call, NULL, sink, &total); free(work); free(filter); } /** @brief Fuzzer entry point. */ int LLVMFuzzerTestOneInput(const unsigned char *data, size_t size); int LLVMFuzzerTestOneInput(const unsigned char *data, size_t size) { if (size < 1) return 0; unsigned char which = data[0] % 6; data++; size--; /* An exact-size heap copy, so a read past the end is caught. */ unsigned char *buf = malloc(size ? size : 1); if (!buf) return 0; memcpy(buf, data, size); size_t total = 0; bool end = false; switch (which) { case 0: (void)csi_parse(buf, size, sink, &total); break; case 1: if (size >= PDS_DIRBLK) (void)pds_parse_block(buf, sink, &total, &end); break; case 2: (void)tiot_parse(buf, size, sink, &total); break; case 3: { char *key = malloc(size + 1); if (key) { memcpy(key, buf, size); key[size] = '\0'; if (csi_filter_ok(key)) { unsigned char field[CSI_FIELD_LEN]; csi_build(field, key); } (void)mvs_dsn_ok(key); free(key); } break; } case 4: /* The smallest work area csi_search accepts, and a larger one. */ fuzz_search(buf, size, CSI_WORK_MIN); fuzz_search(buf, size, CSI_WORK_MIN + 3); break; case 5: { /* A name field and both directions of the byte map, each in a * buffer exactly the size the function is allowed to touch. */ char *dst = malloc(size + 1); if (dst) { mvs_name(buf, size, dst); total += strlen(dst); free(dst); } mvs_from_ebcdic((char *)buf, size); mvs_to_ebcdic((char *)buf, size); break; } } free(buf); return 0; }