Commit 8de244c
committed
input/drivers: fix gx_input UAF on mouse realloc failure and ps3 threads malloc NULL-deref
Three bugs across two input drivers reached while sweeping
input/drivers/ for alloc-failure issues.
=== gx_input.c: UAF on mouse realloc failure (Wii/RVL only) ===
rvl_input_poll grows/shrinks the mouse array when the detected
Wiimote count changes:
gx_input_mouse_t *tmp = (gx_input_mouse_t*)realloc(
gx->mouse, count * sizeof(gx_input_mouse_t));
if (!tmp)
free(gx->mouse);
else
{
gx->mouse = tmp;
gx->mouse_max = count;
...
}
for (i = 0; i < gx->mouse_max; i++)
{
gx->mouse[i].x_last = gx->mouse[i].x_abs;
...
}
On realloc failure the old code free'd gx->mouse but left the
dangling pointer and stale gx->mouse_max in place. The
subsequent loop then iterated through freed memory via
gx->mouse[i].x_last = gx->mouse[i].x_abs - a use-after-free
at every poll until the driver was torn down, with the upper
gate 'if (gx && gx->mouse)' still accepting the freed-but-
non-NULL pointer on the next call.
Fix: on realloc failure clear gx->mouse = NULL and
gx->mouse_max = 0. The outer gate at the top of
rvl_input_poll then rejects subsequent polls, and the inner
per-poll update loop is now wrapped in 'if (gx->mouse)' so
it skips on OOM rather than iterating through NULL.
On the happy path behaviour is unchanged.
=== gx_input.c: NULL-deref on mouse calloc failure at init ===
gx_input_init unconditionally returned the gx handle even if
the per-Wiimote mouse array calloc at init time had failed:
gx->mouse_max = 1;
gx->mouse = (gx_input_mouse_t*)calloc(
gx->mouse_max, sizeof(gx_input_mouse_t));
return gx;
The RETRO_DEVICE_MOUSE input handler later does
int x = (gx->mouse[joy_idx].x_abs
- gx->mouse[joy_idx].x_last) * x_scale;
unconditionally - no 'if (gx->mouse)' guard. On OOM during
init, the very first mouse event NULL-derefs.
Fix: NULL-check the calloc and fail the whole driver init
(free(gx) + return NULL) if it fails. A Wii build running
without any mouse support is strictly better than crashing
on first Wiimote motion.
=== ps3_input.c: NULL-deref on SPU thread list malloc ===
ps3_init_spurs allocates a thread-list buffer and immediately
hands it to the SPURS SDK for population:
ps3->threads = (sys_spu_thread_t *)malloc(
sizeof(sys_spu_thread_t) * nthread);
if ((ret = spursGetSpuThreadId(ps3->spurs,
ps3->threads, &nthread)))
return ret;
malloc was unchecked. spursGetSpuThreadId writes the thread
IDs into the buffer - behaviour with NULL is
implementation-defined and at best returns an error code but
more realistically crashes inside the SDK.
Fix: NULL-check the malloc and return -1 on OOM. Matches the
return-code convention of the malloc-failure branch in
ps3_init_gem at line 364 below, which also returns -1. The
teardown path ps3_end_spurs does free(ps3->threads) which is
NULL-safe via free(NULL).
=== Swept-clean in the same pass ===
Verified via visual inspection that all other calloc/malloc/
realloc call sites in input/drivers/ are already NULL-checked:
- udev_input.c : 6 sites (3 touch state allocs, device/
realloc/driver-init) all guarded.
- winraw_input.c : 4 sites all guarded.
- rwebinput_input.c: 3 sites all guarded (realloc pattern fixed
in an earlier commit).
- qnx_input.c : 2 sites guarded.
- psp_input.c : 2 sites guarded.
- dinput.c : 2 sites guarded.
- Single-alloc drivers x11_input / switch_input / sdl_input /
ps4_input / linuxraw_input / cocoa_input / android_input :
all single init callocs guarded.
Reachability: all three fixes are reachable on OOM. The gx
UAF is the most concerning - it's triggered by routine
Wiimote hotplug on a Wii in low-memory conditions and results
in use-after-free every poll thereafter. The gx init crash
requires OOM during driver construction. The ps3 SPURS
crash requires OOM in the PS3 audio init path.
Thread-safety: all three sites are on the main input thread;
no concurrency concerns.
Scope: local to the two files modified; no API changes, no
header changes. The gx_input driver's external contract is
unchanged (init still returns NULL on failure, the poll
function still silently degrades when mouse state is
unavailable); the ps3_init_spurs function signature and error
convention are unchanged.1 parent 06106cb commit 8de244c
2 files changed
Lines changed: 40 additions & 5 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
202 | 202 | | |
203 | 203 | | |
204 | 204 | | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
205 | 217 | | |
206 | 218 | | |
207 | 219 | | |
| |||
239 | 251 | | |
240 | 252 | | |
241 | 253 | | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
242 | 264 | | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
243 | 268 | | |
244 | 269 | | |
245 | 270 | | |
| |||
254 | 279 | | |
255 | 280 | | |
256 | 281 | | |
257 | | - | |
| 282 | + | |
258 | 283 | | |
259 | | - | |
260 | | - | |
261 | | - | |
262 | | - | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
263 | 291 | | |
264 | 292 | | |
265 | 293 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
291 | 291 | | |
292 | 292 | | |
293 | 293 | | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
294 | 301 | | |
295 | 302 | | |
296 | 303 | | |
| |||
0 commit comments