Commit fd69102
committed
libretro-common/net: fix buffer overflow in string_parse_html_anchor + test
The parser underlying task_http's buildbot directory-listing
scrape had two unbounded memcpy calls in the same function.
Found while fixing an adjacent bit-rot warning in the regression
test.
net_http_parse: bound memcpy against caller buffer sizes
string_parse_html_anchor takes link_size and name_size for
the caller's output buffers but did not check either. Both
memcpy calls copied end-start bytes straight in:
memcpy(link, line, end - line);
*(link + (end - line)) = '\0';
memcpy(name, start + 2, end - start - 2);
*(name + (end - start - 2)) = '\0';
A long href or anchor text trivially overflows the caller's
buffer. Reachable through tasks/task_http.c's HTML directory
scraping path -- any server response with a long link URL or
long title trips it.
Reproduced under ASan: "stack-buffer-overflow" in memcpy at
net_http_parse.c:65 when link[] is smaller than the URL.
Fix: clamp the copy length to link_size-1 / name_size-1 before
the memcpy and write the NUL at the clamped position.
Truncates cleanly rather than overflowing. Callers that need
full URLs must pass adequately sized buffers, which is already
the contract (the size parameters exist for a reason).
net_http_parse_test: fix bit-rot + turn into true regression test
The sample triggered "implicit declaration of
string_parse_html_anchor" in every CI build -- it was
including compat/strcasestr.h instead of the correct
net/net_http_parse.h.
Rewrote the sample as four subtests:
1. happy path (verifies output)
2. no anchor in input (verifies error return)
3. undersized link buffer (stack canary + NUL-term check)
4. undersized name buffer (same)
Subtests 3 and 4 are true discriminators for the parser fix
above. On unpatched parser:
-O0 under ASan: stack-buffer-overflow in memcpy
-O2 plain run: 2 of 4 subtests report "output not NUL-
terminated" (the unclamped memcpy + NUL at offset N writes
the terminator past the end of the buffer, leaving the
buffer contents without any NUL inside it)
Post-patch: all 4 subtests pass, ASan clean.
Also cleaned up:
- int main(int argc, char *argv[]) -> int main(void)
(the original sample used neither)
CI: http_parse_test added to the libretro-common samples
RUN_TARGETS allowlist. Full local dry-run under the GHA
shell contract:
Built: 14 Ran: 14 Failed: 0
VC6 compatibility: net_http_parse.c is compiled on all
platforms. The fix uses only size_t and standard comparisons;
no new headers. Sample test uses stdint.h (already available
via the VC6 compat shim) and basic <string.h>.1 parent 39fcefe commit fd69102
3 files changed
Lines changed: 184 additions & 10 deletions
File tree
- .github/workflows
- libretro-common
- net
- samples/net
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
54 | 54 | | |
55 | 55 | | |
56 | 56 | | |
| 57 | + | |
57 | 58 | | |
58 | 59 | | |
59 | 60 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
58 | 58 | | |
59 | 59 | | |
60 | 60 | | |
| 61 | + | |
61 | 62 | | |
62 | 63 | | |
63 | 64 | | |
64 | 65 | | |
65 | | - | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
66 | 72 | | |
67 | | - | |
| 73 | + | |
| 74 | + | |
68 | 75 | | |
69 | 76 | | |
70 | 77 | | |
71 | 78 | | |
72 | 79 | | |
73 | 80 | | |
74 | 81 | | |
| 82 | + | |
75 | 83 | | |
76 | 84 | | |
77 | 85 | | |
78 | 86 | | |
79 | | - | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
80 | 91 | | |
81 | | - | |
| 92 | + | |
| 93 | + | |
82 | 94 | | |
83 | 95 | | |
84 | 96 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
21 | 21 | | |
22 | 22 | | |
23 | 23 | | |
24 | | - | |
| 24 | + | |
| 25 | + | |
25 | 26 | | |
26 | | - | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
27 | 30 | | |
28 | 31 | | |
29 | 32 | | |
30 | | - | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
31 | 89 | | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
32 | 94 | | |
33 | 95 | | |
34 | | - | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
35 | 99 | | |
36 | | - | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
37 | 192 | | |
38 | | - | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
39 | 200 | | |
0 commit comments