Coverage Report

Created: 2026-12-01 06:04

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/numactl/fuzz/fuzz_parse_bitmap.c
Line
Count
Source
1
/*
2
 * fuzz_parse_bitmap.c - libFuzzer target for libnuma's exported string
3
 * parsers beyond the two covered by fuzz_parse_str: the numa_parse_bitmap
4
 * symbol-version pair and the *_all node/cpu string variants.
5
 *
6
 * Threat model
7
 * ============
8
 * 1. Untrusted input: numa_parse_bitmap() is an exported libnuma API for
9
 *    parsing hex bitmask strings (the format used in /sys cpumasks); the
10
 *    numa_parse_nodestring_all()/numa_parse_cpustring_all() variants parse
11
 *    user-provided node/cpu list strings for callers that must consider all
12
 *    system (not task-allowed) nodes/cpus. Applications feed these strings
13
 *    from config files, job specs, monitoring output and user input.
14
 * 2. Trust boundary: the parse functions themselves - bytes-in, mask-out.
15
 * 3. Attacker-controlled: the whole string, plus (for the v1 bitmap variant)
16
 *    the declared cpu count ncpus that sizes the output array.
17
 * 4. Real bug here: OOB read/write in the bitmap walker (it walks the string
18
 *    backwards from an embedded newline, memmoves comma-separated 32-bit
19
 *    groups together, and bounds writes only against CPU_LONGS(ncpus)),
20
 *    integer conversion edges, and mask-index errors in the *_all variants
21
 *    (which validate against numa_possible_* instead of numa_all_* masks).
22
 *
23
 * Why this target (coverage rationale)
24
 * ====================================
25
 * Fuzz Introspector lists numa_parse_nodestring_all() (complexity 99) at 0%
26
 * coverage; numa_parse_bitmap_v1/v2 (libnuma.c ~1347-1416, exported per
27
 * versions.ldscript for both libnuma_1.1 and libnuma_1.2) have never been
28
 * fuzzed - the existing fuzz_parse_str only drives numa_parse_nodestring()
29
 * and numa_parse_cpustring(). The bitmap walker's backwards pointer walk
30
 * (`oldp--`, `memmove(p, p+1, oldp-p+1)`), its `p > line` boundary logic and
31
 * the comma/newline interplay are exactly the kind of arithmetic that yields
32
 * off-by-one and OOB findings under mutation.
33
 *
34
 * Design notes
35
 * ============
36
 * - The v1 symbol is bound via a .symver alias
37
 *   (numa_parse_bitmap@libnuma_1.1, 3-arg raw-array form) so both arity
38
 *   variants of the exported name are exercised; the default (2-arg) form
39
 *   binds to @libnuma_1.2 (v2).
40
 * - The line buffer is copied to writable stack storage per call: the parsers
41
 *   mutate the line in place (memmove).
42
 * - All masks are allocated with numa_bitmask_alloc() (n >= 1) and freed; the
43
 *   raw v1 array is sized CPU_LONGS(ncpus) for the declared ncpus - the
44
 *   documented caller contract this API expects.
45
 * - numa_node_to_cpus()/numa_node_of_cpu() round-trips are driven on the
46
 *   parsed masks to cover the convert-and-consume paths downstream of
47
 *   parsing.
48
 */
49
#include <stdint.h>
50
#include <stdlib.h>
51
#include <string.h>
52
#include <stdio.h>
53
#include "numa.h"
54
#include "numaint.h"
55
56
/* bind the 3-arg v1 export explicitly */
57
extern int numa_parse_bitmap_v1_ref(char *line, unsigned long *mask, int ncpus);
58
__asm__(".symver numa_parse_bitmap_v1_ref, numa_parse_bitmap@libnuma_1.1");
59
60
1.66k
#define MAX_LINE      256u
61
868
#define MAX_NCPUS     1024u
62
63
int LLVMFuzzerInitialize(int *argc, char ***argv)
64
2
{
65
2
        (void)argc;
66
2
        (void)argv;
67
2
        if (numa_available() < 0)
68
0
                return 0;
69
2
        return 0;
70
2
}
71
72
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
73
869
{
74
869
        if (size < 2)
75
1
                return 0;
76
77
        /* bitmap string: must contain a newline for the parser */
78
868
        char line[MAX_LINE + 1];
79
868
        size_t blen = size < MAX_LINE ? size : MAX_LINE;
80
868
        memcpy(line, data, blen);
81
868
        line[blen] = '\0';
82
868
        if (!strchr(line, '\n'))
83
793
                line[blen - 1] = '\n';   /* seed the terminator the parser needs */
84
85
868
        unsigned ncpus = (unsigned)data[0] % MAX_NCPUS + 1;
86
87
        /* v1: raw array sized for ncpus (caller contract) */
88
868
        {
89
868
                unsigned long raw[(MAX_NCPUS + 63) / 64];   /* == CPU_LONGS(MAX_NCPUS) */
90
868
                memset(raw, 0, sizeof(raw));
91
868
                char buf[MAX_LINE + 1];
92
868
                memcpy(buf, line, blen + 1);
93
868
                (void)numa_parse_bitmap_v1_ref(buf, raw, (int)ncpus);
94
868
        }
95
96
        /* v2: bitmask form */
97
868
        {
98
868
                struct bitmask *mask = numa_bitmask_alloc((unsigned)ncpus);
99
868
                if (mask) {
100
868
                        char buf[MAX_LINE + 1];
101
868
                        memcpy(buf, line, blen + 1);
102
868
                        (void)numa_parse_bitmap(buf, mask);
103
868
                        numa_bitmask_free(mask);
104
868
                }
105
868
        }
106
107
        /* *_all string variants on the remaining bytes */
108
868
        if (size > blen) {
109
793
                const char *s = (const char *)data + blen;
110
793
                size_t slen = size - blen;
111
793
                char str[128];
112
793
                size_t copy = slen < sizeof(str) - 1 ? slen : sizeof(str) - 1;
113
793
                memcpy(str, s, copy);
114
793
                str[copy] = '\0';
115
116
793
                struct bitmask *nodes = numa_parse_nodestring_all(str);
117
793
                if (nodes)
118
42
                        numa_bitmask_free(nodes);
119
793
                struct bitmask *cpus = numa_parse_cpustring_all(str);
120
793
                if (cpus) {
121
101
                        if (numa_available() >= 0) {
122
101
                                struct bitmask *cpubits = numa_allocate_cpumask();
123
101
                                if (cpubits) {
124
101
                                        (void)numa_node_to_cpus(0, cpubits);
125
101
                                        numa_bitmask_free(cpubits);
126
101
                                }
127
101
                        }
128
101
                        numa_bitmask_free(cpus);
129
101
                }
130
793
        }
131
868
        return 0;
132
869
}