@@ -647,8 +647,10 @@ static int str_to_user(const char *str, unsigned int maxlen, void __user *p)
647647 return copy_to_user (p , str , len ) ? - EFAULT : len ;
648648}
649649
650+ #define OLD_KEY_MAX 0x1ff
650651static int handle_eviocgbit (struct input_dev * dev , unsigned int cmd , void __user * p , int compat_mode )
651652{
653+ static unsigned long keymax_warn_time ;
652654 unsigned long * bits ;
653655 int len ;
654656
@@ -665,9 +667,26 @@ static int handle_eviocgbit(struct input_dev *dev, unsigned int cmd, void __user
665667 case EV_SW : bits = dev -> swbit ; len = SW_MAX ; break ;
666668 default : return - EINVAL ;
667669 }
670+
671+ /*
672+ * Work around bugs in userspace programs that like to do
673+ * EVIOCGBIT(EV_KEY, KEY_MAX) and not realize that 'len'
674+ * should be in bytes, not in bits.
675+ */
676+ if ((_IOC_NR (cmd ) & EV_MAX ) == EV_KEY && _IOC_SIZE (cmd ) == OLD_KEY_MAX ) {
677+ len = OLD_KEY_MAX ;
678+ if (printk_timed_ratelimit (& keymax_warn_time , 10 * 1000 ))
679+ printk (KERN_WARNING
680+ "evdev.c(EVIOCGBIT): Suspicious buffer size %d, "
681+ "limiting output to %d bytes. See "
682+ "http://userweb.kernel.org/~dtor/eviocgbit-bug.html\n" ,
683+ OLD_KEY_MAX ,
684+ BITS_TO_LONGS (OLD_KEY_MAX ) * sizeof (long ));
685+ }
686+
668687 return bits_to_user (bits , len , _IOC_SIZE (cmd ), p , compat_mode );
669688}
670-
689+ #undef OLD_KEY_MAX
671690
672691static long evdev_do_ioctl (struct file * file , unsigned int cmd ,
673692 void __user * p , int compat_mode )
0 commit comments