Skip to content

Commit ec8b4b7

Browse files
skittdtor
authored andcommitted
Input: joydev - decouple axis and button map ioctls from input constants
The KEY_MAX change in 2.6.28 changed the values of the JSIOCSBTNMAP and JSIOCGBTNMAP constants; software compiled with the old values no longer works with kernels following 2.6.28, because the ioctl switch statement no longer matches the values given by the software. This patch handles these ioctls independently of the length of data specified, and applies the same treatment to JSIOCSAXMAP and JSIOCGAXMAP which currently depend on ABS_MAX. Signed-off-by: Stephen Kitt <steve@sk2.org> Signed-off-by: Dmitry Torokhov <dtor@mail.ru>
1 parent d82f1c3 commit ec8b4b7

1 file changed

Lines changed: 41 additions & 27 deletions

File tree

‎drivers/input/joydev.c‎

Lines changed: 41 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -456,8 +456,11 @@ static int joydev_ioctl_common(struct joydev *joydev,
456456
unsigned int cmd, void __user *argp)
457457
{
458458
struct input_dev *dev = joydev->handle.dev;
459+
size_t len;
459460
int i, j;
461+
const char *name;
460462

463+
/* Process fixed-sized commands. */
461464
switch (cmd) {
462465

463466
case JS_SET_CAL:
@@ -499,9 +502,22 @@ static int joydev_ioctl_common(struct joydev *joydev,
499502
return copy_to_user(argp, joydev->corr,
500503
sizeof(joydev->corr[0]) * joydev->nabs) ? -EFAULT : 0;
501504

502-
case JSIOCSAXMAP:
503-
if (copy_from_user(joydev->abspam, argp,
504-
sizeof(__u8) * (ABS_MAX + 1)))
505+
}
506+
507+
/*
508+
* Process variable-sized commands (the axis and button map commands
509+
* are considered variable-sized to decouple them from the values of
510+
* ABS_MAX and KEY_MAX).
511+
*/
512+
switch (cmd & ~IOCSIZE_MASK) {
513+
514+
case (JSIOCSAXMAP & ~IOCSIZE_MASK):
515+
len = min_t(size_t, _IOC_SIZE(cmd), sizeof(joydev->abspam));
516+
/*
517+
* FIXME: we should not copy into our axis map before
518+
* validating the data.
519+
*/
520+
if (copy_from_user(joydev->abspam, argp, len))
505521
return -EFAULT;
506522

507523
for (i = 0; i < joydev->nabs; i++) {
@@ -511,13 +527,17 @@ static int joydev_ioctl_common(struct joydev *joydev,
511527
}
512528
return 0;
513529

514-
case JSIOCGAXMAP:
515-
return copy_to_user(argp, joydev->abspam,
516-
sizeof(__u8) * (ABS_MAX + 1)) ? -EFAULT : 0;
517-
518-
case JSIOCSBTNMAP:
519-
if (copy_from_user(joydev->keypam, argp,
520-
sizeof(__u16) * (KEY_MAX - BTN_MISC + 1)))
530+
case (JSIOCGAXMAP & ~IOCSIZE_MASK):
531+
len = min_t(size_t, _IOC_SIZE(cmd), sizeof(joydev->abspam));
532+
return copy_to_user(argp, joydev->abspam, len) ? -EFAULT : 0;
533+
534+
case (JSIOCSBTNMAP & ~IOCSIZE_MASK):
535+
len = min_t(size_t, _IOC_SIZE(cmd), sizeof(joydev->keypam));
536+
/*
537+
* FIXME: we should not copy into our keymap before
538+
* validating the data.
539+
*/
540+
if (copy_from_user(joydev->keypam, argp, len))
521541
return -EFAULT;
522542

523543
for (i = 0; i < joydev->nkey; i++) {
@@ -529,25 +549,19 @@ static int joydev_ioctl_common(struct joydev *joydev,
529549

530550
return 0;
531551

532-
case JSIOCGBTNMAP:
533-
return copy_to_user(argp, joydev->keypam,
534-
sizeof(__u16) * (KEY_MAX - BTN_MISC + 1)) ? -EFAULT : 0;
552+
case (JSIOCGBTNMAP & ~IOCSIZE_MASK):
553+
len = min_t(size_t, _IOC_SIZE(cmd), sizeof(joydev->keypam));
554+
return copy_to_user(argp, joydev->keypam, len) ? -EFAULT : 0;
535555

536-
default:
537-
if ((cmd & ~IOCSIZE_MASK) == JSIOCGNAME(0)) {
538-
int len;
539-
const char *name = dev->name;
540-
541-
if (!name)
542-
return 0;
543-
len = strlen(name) + 1;
544-
if (len > _IOC_SIZE(cmd))
545-
len = _IOC_SIZE(cmd);
546-
if (copy_to_user(argp, name, len))
547-
return -EFAULT;
548-
return len;
549-
}
556+
case JSIOCGNAME(0):
557+
name = dev->name;
558+
if (!name)
559+
return 0;
560+
561+
len = min_t(size_t, _IOC_SIZE(cmd), strlen(name) + 1);
562+
return copy_to_user(argp, name, len) ? -EFAULT : len;
550563
}
564+
551565
return -EINVAL;
552566
}
553567

0 commit comments

Comments
 (0)