Commit 18e15ad
committed
libretro-common/vfs_cdrom: fix four bugs + regression test
Four real bugs in vfs_implementation_cdrom.c found during the
follow-up audit after the main vfs_implementation.c fixes.
Same bug class as the main vfs mmap read overflow already fixed.
vfs_cdrom: cuesheet read byte_pos wrap OOB
In retro_vfs_file_read_cdrom cuesheet branch:
if ((int64_t)len >= (int64_t)stream->cdrom.cue_len
- stream->cdrom.byte_pos)
len = stream->cdrom.cue_len - stream->cdrom.byte_pos - 1;
memcpy(s, stream->cdrom.cue_buf + stream->cdrom.byte_pos, len);
cue_len is size_t, byte_pos is int64_t. When byte_pos equals
or exceeds cue_len (reachable via seek, which does NOT clamp
byte_pos), the subtraction "cue_len - byte_pos - 1" executes as
unsigned size_t and wraps to SIZE_MAX. The memcpy then reads
SIZE_MAX bytes off the end of cue_buf.
Similarly a negative byte_pos (reachable by SEEK_CUR with a
negative offset when byte_pos was near zero) becomes a huge
size_t after the cast and wraps the same subtraction.
Reproduced under ASan:
"negative-size-param: (size=-1)" in memcpy at
vfs_implementation_cdrom.c:401
Fix: guard byte_pos against being outside [0, cue_len) and
clamp len against the remaining bytes using unsigned subtraction
against "remaining = cue_len - byte_pos".
vfs_cdrom: bin read byte_pos+len wrap OOB
Same bug class in the .bin branch:
if (stream->cdrom.byte_pos + len >
vfs_cdrom_toc.track[stream->cdrom.cur_track - 1].track_bytes)
len -= (stream->cdrom.byte_pos + len)
- vfs_cdrom_toc.track[stream->cdrom.cur_track - 1].track_bytes;
Attacker-controlled len near UINT64_MAX wraps "byte_pos + len"
past track_bytes, bypassing the bound check; the downstream
cdrom_read() is then called with the unclamped len.
Fix: clamp against "remaining = track_bytes - byte_pos" with
unsigned subtraction, never adding byte_pos and len.
vfs_cdrom: cur_track == 0 indexes track[-1]
If a user-controlled VFS path is crafted as
"drive1-track00.bin" (or the Windows-style
"d:/drive-track00.bin"), the two-digit parser produces
cur_track = 0. The read code then does
"track[cur_track - 1]" = track[-1], an out-of-bounds read into
the struct preceding vfs_cdrom_toc.track[].
Fix: the parser rejects values outside [1, 99] and leaves the
default cur_track = 1 in place on reject. The read paths also
defensively reject cur_track == 0 before indexing, so a
malicious caller that sets cur_track through another vector
still cannot trip the OOB.
vfs_cdrom: guard cur_track > 99 in bin read
TOC array is track[99] (indices 0..98). cur_track is
unsigned char so mathematically can reach 255. Defensive
check rejects values above 99 before indexing.
Reachability: all four bugs are reachable by a core or frontend
that has VFS access to a CD-ROM-scheme stream (cdrom://...).
Bugs 1 and 2 escalate a weak seek primitive (byte_pos not
clamped) into arbitrary OOB reads.
VC6 compatibility: the whole file is gated by HAVE_CDROM, which
only the Linux and Win32 (non-Xbox) branches set. VC6 is a Win32
target, so its branch compiles. The fix uses only size_t,
uint64_t, and standard comparisons -- all VC6-compatible via the
existing stdint shim.
Regression test: libretro-common/samples/file/vfs_cdrom/
True discriminator for bug #1. Five subtests covering
byte_pos==cue_len, byte_pos>cue_len, byte_pos<0, huge len, and
a baseline success. On unpatched code:
SEGV on byte_pos==cue_len (plain run)
ASan: "negative-size-param: (size=-1)" in memcpy
On patched code:
all five subtests pass cleanly; ASan clean; exit 0
Bug #2 (bin read) and bugs #3/#4 (cur_track parsing) don't get
a targeted regression test. Bug #2 would require linking a
full cdrom_toc setup which is platform-dependent; bugs #3/#4
are guarded by the parser fix so the failing input cannot reach
the OOB path, and writing a white-box test of the parser is
low value once the upstream fix is in.
CI: libretro-common samples workflow updated. Full dry-run
under the GHA shell contract:
Built: 14 Ran: 13 Failed: 01 parent b431965 commit 18e15ad
4 files changed
Lines changed: 277 additions & 14 deletions
File tree
- .github/workflows
- libretro-common
- samples/file/vfs_cdrom
- vfs
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
53 | 53 | | |
54 | 54 | | |
55 | 55 | | |
| 56 | + | |
56 | 57 | | |
57 | 58 | | |
58 | 59 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
Lines changed: 171 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 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 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 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 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
171 | 171 | | |
172 | 172 | | |
173 | 173 | | |
174 | | - | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
175 | 181 | | |
176 | 182 | | |
177 | 183 | | |
| |||
249 | 255 | | |
250 | 256 | | |
251 | 257 | | |
252 | | - | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
253 | 265 | | |
254 | 266 | | |
255 | 267 | | |
| |||
396 | 408 | | |
397 | 409 | | |
398 | 410 | | |
399 | | - | |
400 | | - | |
401 | | - | |
| 411 | + | |
| 412 | + | |
| 413 | + | |
| 414 | + | |
| 415 | + | |
| 416 | + | |
| 417 | + | |
| 418 | + | |
| 419 | + | |
| 420 | + | |
| 421 | + | |
| 422 | + | |
| 423 | + | |
| 424 | + | |
| 425 | + | |
| 426 | + | |
| 427 | + | |
| 428 | + | |
| 429 | + | |
| 430 | + | |
402 | 431 | | |
403 | 432 | | |
404 | 433 | | |
405 | 434 | | |
406 | 435 | | |
407 | 436 | | |
408 | 437 | | |
409 | | - | |
| 438 | + | |
410 | 439 | | |
411 | 440 | | |
412 | | - | |
| 441 | + | |
413 | 442 | | |
414 | 443 | | |
415 | 444 | | |
| |||
424 | 453 | | |
425 | 454 | | |
426 | 455 | | |
427 | | - | |
428 | | - | |
| 456 | + | |
| 457 | + | |
| 458 | + | |
| 459 | + | |
| 460 | + | |
| 461 | + | |
429 | 462 | | |
430 | | - | |
431 | | - | |
432 | | - | |
433 | | - | |
434 | | - | |
| 463 | + | |
| 464 | + | |
| 465 | + | |
| 466 | + | |
| 467 | + | |
| 468 | + | |
| 469 | + | |
| 470 | + | |
| 471 | + | |
| 472 | + | |
| 473 | + | |
| 474 | + | |
| 475 | + | |
| 476 | + | |
| 477 | + | |
435 | 478 | | |
436 | 479 | | |
437 | 480 | | |
| |||
0 commit comments