Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

Input: joydev - prevent potential read overflow in ioctl

The problem here is that "len" might be less than "joydev->nabs" so the
loops which verfy abspam[i] and keypam[] might read beyond the buffer.

Fixes: 999b874f4aa3 ("Input: joydev - validate axis/button maps before clobbering current ones")
Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>
Link: https://lore.kernel.org/r/YCyzR8WvFRw4HWw6@mwanda
[dtor: additional check for len being even in joydev_handle_JSIOCSBTNMAP]
Cc: stable@vger.kernel.org
Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>

authored by

Dan Carpenter and committed by
Dmitry Torokhov
182d679b 0958351e

+5 -2
+5 -2
drivers/input/joydev.c
··· 456 456 if (IS_ERR(abspam)) 457 457 return PTR_ERR(abspam); 458 458 459 - for (i = 0; i < joydev->nabs; i++) { 459 + for (i = 0; i < len && i < joydev->nabs; i++) { 460 460 if (abspam[i] > ABS_MAX) { 461 461 retval = -EINVAL; 462 462 goto out; ··· 480 480 int i; 481 481 int retval = 0; 482 482 483 + if (len % sizeof(*keypam)) 484 + return -EINVAL; 485 + 483 486 len = min(len, sizeof(joydev->keypam)); 484 487 485 488 /* Validate the map. */ ··· 490 487 if (IS_ERR(keypam)) 491 488 return PTR_ERR(keypam); 492 489 493 - for (i = 0; i < joydev->nkey; i++) { 490 + for (i = 0; i < (len / 2) && i < joydev->nkey; i++) { 494 491 if (keypam[i] > KEY_MAX || keypam[i] < BTN_MISC) { 495 492 retval = -EINVAL; 496 493 goto out;