Commit 1a13965
committed
libretro-common/queues: bound fifo_write/read + reject SIZE_MAX init
Audit-pass on libretro-common/queues/fifo_queue.c found three
defensive gaps. Each is currently latent (audio + gfx widget
callers all gate on FIFO_WRITE_AVAIL / FIFO_READ_AVAIL before
calling fifo_write / fifo_read) but the function trusts @len
unconditionally, so any future caller that forgets the gate
silently corrupts memory rather than dropping the request.
* fifo_write: out-of-bounds write when @len exceeds available
space (libretro-common/queues/fifo_queue.c). The wrap-around
branch computes rest_write = len - first_write and memcpy()s
rest_write bytes starting at buffer->buffer; for any @len
exceeding @buffer->size, that second memcpy walks off the end
of the backing allocation. Worse, the gating test
`buffer->end + len > buffer->size` itself wraps in size_t for
huge @len, mis-routing the call into a single-memcpy branch
with @len bytes of unbounded write. Fix by capping @len at
FIFO_WRITE_AVAIL(buffer) on entry. No-op for current callers
(they already cap); turns the latent overrun into a defined
silent truncation for any future caller that forgets to gate.
* fifo_read: same out-of-bounds shape on the read side --
rest_read bytes copied past the end of @buffer->buffer when
@len exceeds the readable range. Same fix: cap @len at
FIFO_READ_AVAIL(buffer) on entry.
* fifo_initialize_internal: SIZE_MAX wrap (libretro-common/
queues/fifo_queue.c). The function reserves one ring slot
for the empty/full distinction, so the actual allocation is
(len + 1) bytes. Passing SIZE_MAX wraps that addition to 0;
calloc(1, 0) is allowed to satisfy with a non-NULL pointer
to a zero-byte allocation, so initialisation can succeed and
leave buf->size == 0. The next fifo_write would then divide
by zero on the `% buffer->size` end-pointer update. No
current caller asks for SIZE_MAX, so the rejection is purely
defensive.
Also documents the new caps in the public header (fifo_write /
fifo_read) so callers know that exceeding available space is a
silent drop, not a silent overrun.
Adds a regression test under libretro-common/samples/queues/
fifo_queue_bounds_test/. The test exercises:
- SIZE_MAX initialisation (must be rejected),
- over-write of 2048 bytes into a 100-byte ring (capped at 100,
no OOB write),
- over-read into a 64-byte buffer (capped at FIFO_READ_AVAIL,
trailing bytes untouched),
- SIZE_MAX @len passed to fifo_write with end != 0 (the
integer-overflow case in the original gating test),
- wrap-around correctness when the cap isn't engaged,
- zero-length write/read no-ops.
Wired into the existing libretro-common-samples CI workflow with
SANITIZER=address,undefined. Pre-patch the test fails: the
SIZE_MAX init succeeds (no rejection), and the over-write trips
ASan with a 1947-byte heap-buffer-overflow WRITE. Post-patch
all seven cases pass under ASan + UBSan + LSan.
Note on residual scope
----------------------
The cap protects @buffer->buffer (the destination ring) but not
@in_buf (the caller-supplied source). fifo_write reads exactly
@len bytes from @in_buf -- a buggy caller that passes
@len > sizeof(*in_buf) gets a source over-read that we cannot
detect from inside the function (no @in_buf size parameter).
That residual is unchanged by this commit; the surface area for
it is much smaller than the destination overrun (audio drivers
all pass full-size frame buffers). Any future hardening would
require an API break to add @in_buf_len, which is out of scope.1 parent 3281572 commit 1a13965
5 files changed
Lines changed: 374 additions & 3 deletions
File tree
- .github/workflows
- libretro-common
- include/queues
- queues
- samples/queues/fifo_queue_bounds_test
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
79 | 79 | | |
80 | 80 | | |
81 | 81 | | |
| 82 | + | |
82 | 83 | | |
83 | 84 | | |
84 | 85 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
126 | 126 | | |
127 | 127 | | |
128 | 128 | | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
129 | 135 | | |
130 | 136 | | |
131 | 137 | | |
| |||
135 | 141 | | |
136 | 142 | | |
137 | 143 | | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
138 | 150 | | |
139 | 151 | | |
140 | 152 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
31 | 31 | | |
32 | 32 | | |
33 | 33 | | |
34 | | - | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
35 | 49 | | |
36 | 50 | | |
37 | 51 | | |
| |||
91 | 105 | | |
92 | 106 | | |
93 | 107 | | |
94 | | - | |
| 108 | + | |
95 | 109 | | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
96 | 133 | | |
97 | 134 | | |
98 | 135 | | |
| |||
109 | 146 | | |
110 | 147 | | |
111 | 148 | | |
112 | | - | |
| 149 | + | |
113 | 150 | | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
114 | 165 | | |
115 | 166 | | |
116 | 167 | | |
| |||
Lines changed: 29 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 | + | |
0 commit comments