Commit cf73f1a
committed
libretro-common/vfs: fix three real bugs + regression test
Three independent bugs in vfs_implementation.c, bundled with a
regression test (true discriminator for bug 1) and the CI hook to
run it.
vfs: fix mmap read integer overflow in retro_vfs_file_read_impl
Pre-patch:
if (stream->mappos + len > stream->mapsize)
len = stream->mapsize - stream->mappos;
memcpy(s, &stream->mapped[stream->mappos], len);
mappos and len are uint64_t. When len is attacker-controlled
(the VFS read interface is exposed to cores), picking len such
that (mappos + len) overflows uint64_t past zero defeats the
bound check: the wrapped small value is not > mapsize, the
clamp is skipped, and memcpy reads the full unclamped len bytes
off the end of the mapped region.
Concrete repro on a 16-byte file with mappos seeked to 10:
pick len = UINT64_MAX - 9. Sum = UINT64_MAX + 1 = 0 in
uint64_t. 0 > 16 is false, clamp bypassed, memcpy reads
(UINT64_MAX - 9) bytes from mapped[10]. Crash / OOB. ASan
reports "negative-size-param: (size=-10)" in memcpy.
Fix: clamp len via unsigned subtraction against the remaining
bytes (`remaining = mapsize - mappos; if (len > remaining) len
= remaining;`) before computing the sum. This cannot wrap.
Also tighten the at-EOF predicate from > to >= and return 0
(successful read at EOF) rather than -1.
Reachability: requires HAVE_MMAP + the frequent-access hint.
Linux builds, and any other build with mmap support, are
affected. Windows (VC6 and every other MSVC) is NOT affected
since HAVE_MMAP is not defined there.
vfs: saturate retro_vfs_stat_impl size on files > 2 GiB
Pre-patch *size = (int32_t)size64 silently truncated files
> 2 GiB. Worse, for files in (INT32_MAX, UINT32_MAX] the high
bit propagated and callers saw a negative size, which many
interpret as an error. Post-patch saturates to INT_MAX before
the cast.
INT_MAX from <limits.h> is used instead of INT32_MAX from
<stdint.h> for C89 / VC6 portability: INT32_MAX is a C99
addition and not available in VC6's stdint shim. int is
32-bit on all MSVC targets including VC6, so the two macros
are equal everywhere this code runs.
vfs: fix retro_vfs_file_truncate_impl silent truncation on Windows
_chsize takes a long (32-bit on Windows) and silently
truncates lengths > LONG_MAX. This affects every Windows CRT
including VC6. The Secure CRT (VS 2005+, _MSC_VER >= 1400)
added _chsize_s which takes __int64.
Post-patch:
_MSC_VER >= 1400: use _chsize_s with the full 64-bit length
_MSC_VER < 1400 (including VC6) and non-MSVC Windows:
reject lengths > LONG_MAX and fall back to _chsize for
shorter values. Returning an error for the unsupported
case is strictly better than silently truncating the file.
The _MSC_VER >= 1400 gate preserves VC6 (_MSC_VER = 1200)
buildability while still giving modern MSVC the correct
semantics.
Regression test: libretro-common/samples/file/vfs/
A true discriminator for bug #1. Creates a 16-byte mmap'd
file, seeks to offset 10, then calls the read function with
len = UINT64_MAX - 9.
unpatched: SEGV (or under ASan:
"negative-size-param: (size=-10)" in memcpy)
patched: both reads return sane clamped values; ASan
clean; exit 0
Bugs #2 and #3 have no targeted regression test -- both
require > 2 GiB input files, impractical for a self-contained
sample that runs in CI. The existing nbio_test and
config_file_test exercise non-huge-file paths through vfs, so
a regression that breaks the happy path would still surface.
CI: libretro-common samples workflow updated
vfs_read_overflow_test added to RUN_TARGETS. Full local
dry-run under the GHA shell contract reports:
Built: 13 Ran: 12 Failed: 01 parent 53a26bc commit cf73f1a
4 files changed
Lines changed: 236 additions & 11 deletions
File tree
- .github/workflows
- libretro-common
- samples/file/vfs
- vfs
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
52 | 52 | | |
53 | 53 | | |
54 | 54 | | |
| 55 | + | |
55 | 56 | | |
56 | 57 | | |
57 | 58 | | |
| |||
| 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 | + | |
Lines changed: 149 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 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
24 | 24 | | |
25 | 25 | | |
26 | 26 | | |
| 27 | + | |
27 | 28 | | |
28 | 29 | | |
29 | 30 | | |
| |||
711 | 712 | | |
712 | 713 | | |
713 | 714 | | |
714 | | - | |
| 715 | + | |
| 716 | + | |
| 717 | + | |
| 718 | + | |
| 719 | + | |
| 720 | + | |
| 721 | + | |
| 722 | + | |
| 723 | + | |
| 724 | + | |
| 725 | + | |
| 726 | + | |
| 727 | + | |
| 728 | + | |
| 729 | + | |
| 730 | + | |
| 731 | + | |
715 | 732 | | |
716 | 733 | | |
717 | 734 | | |
718 | 735 | | |
| 736 | + | |
719 | 737 | | |
720 | 738 | | |
721 | 739 | | |
| |||
792 | 810 | | |
793 | 811 | | |
794 | 812 | | |
795 | | - | |
| 813 | + | |
| 814 | + | |
| 815 | + | |
| 816 | + | |
| 817 | + | |
| 818 | + | |
| 819 | + | |
796 | 820 | | |
| 821 | + | |
797 | 822 | | |
798 | | - | |
799 | | - | |
| 823 | + | |
| 824 | + | |
| 825 | + | |
| 826 | + | |
| 827 | + | |
| 828 | + | |
| 829 | + | |
| 830 | + | |
| 831 | + | |
| 832 | + | |
800 | 833 | | |
801 | | - | |
| 834 | + | |
802 | 835 | | |
803 | 836 | | |
804 | | - | |
| 837 | + | |
805 | 838 | | |
806 | 839 | | |
807 | 840 | | |
| |||
1186 | 1219 | | |
1187 | 1220 | | |
1188 | 1221 | | |
1189 | | - | |
1190 | | - | |
1191 | | - | |
1192 | | - | |
| 1222 | + | |
| 1223 | + | |
| 1224 | + | |
| 1225 | + | |
| 1226 | + | |
| 1227 | + | |
| 1228 | + | |
| 1229 | + | |
| 1230 | + | |
| 1231 | + | |
| 1232 | + | |
1193 | 1233 | | |
1194 | | - | |
| 1234 | + | |
1195 | 1235 | | |
1196 | 1236 | | |
1197 | 1237 | | |
| |||
0 commit comments