Skip to contentSkip to conversation

Input: uinput - fix undefined behavior in uinput_validate_absinfo()

PATCH In review Dmitry Torokhov 2 participants 3 messages Jan 14, 2019

Subsystems (2)
lore
Share & tools
lore

Dmitry Torokhov

author maintainer
#0 lore raw

PATCH 1/1 Input: uinput - fix undefined behavior in uinput_validate_absinfo()

An integer overflow may arise in uinput_validate_absinfo() if "max - min" can't be represented by an "int". We should check for overflow before trying to use the result.

---
 drivers/input/misc/uinput.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/input/misc/uinput.c +3 -2
index 8ec483e8688b..26ec603fe220 100644
@@ -39,6 +39,7 @@
 #include <linux/init.h>
 #include <linux/fs.h>
 #include <linux/miscdevice.h>
+#include <linux/overflow.h>
 #include <linux/input/mt.h>
 #include "../input-compat.h"
 
@@ -405,7 +406,7 @@ static int uinput_open(struct inode *inode, struct file *file)
 static int uinput_validate_absinfo(struct input_dev *dev, unsigned int code,
 				   const struct input_absinfo *abs)
 {
-	int min, max;
+	int min, max, range;
 
 	min = abs->minimum;
 	max = abs->maximum;
@@ -417,7 +418,7 @@ static int uinput_validate_absinfo(struct input_dev *dev, unsigned int code,
 		return -EINVAL;
 	}
 
-	if (abs->flat > max - min) {
+	if (check_sub_overflow(max, min, &range) && abs->flat > range) {
 		printk(KERN_DEBUG
 		       "%s: abs_flat #%02x out of range: %d (min:%d/max:%d)\n",
 		       UINPUT_NAME, code, abs->flat, min, max);
Signature · 5 lines
2.20.1.97.g81188d93c3-goog


-- 
Dmitry
author maintainer
#1 lore raw
Quoted 33 lines · Dmitry Torokhov

An integer overflow may arise in uinput_validate_absinfo() if "max - min" can't be represented by an "int". We should check for overflow before trying to use the result.

---
 drivers/input/misc/uinput.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/input/misc/uinput.c +2 -1
index 8ec483e8688b..26ec603fe220 100644
@@ -39,6 +39,7 @@
 #include <linux/init.h>
 #include <linux/fs.h>
 #include <linux/miscdevice.h>
+#include <linux/overflow.h>
 #include <linux/input/mt.h>
 #include "../input-compat.h"
 
@@ -405,7 +406,7 @@ static int uinput_open(struct inode *inode, struct file *file)
 static int uinput_validate_absinfo(struct input_dev *dev, unsigned int code,
 				   const struct input_absinfo *abs)
 {
-	int min, max;
+	int min, max, range;
 
 	min = abs->minimum;
 	max = abs->maximum;

drivers/input/misc/uinput.c static int uinput_validate_absinfo(struct input_dev *dev, unsigned int code, · line 421

Quoted 5 lines
drivers/input/misc/uinput.c +1 -1
@@ -417,7 +418,7 @@ static int uinput_validate_absinfo(struct input_dev *dev, unsigned int code,
 		return -EINVAL;
 	}
 
-	if (abs->flat > max - min) {
+	if (check_sub_overflow(max, min, &range) && abs->flat > range) {

This should be !check_sub_overflow(...) of course.

Quoted 9 lines
drivers/input/misc/uinput.c +0 -0
 		printk(KERN_DEBUG
 		       "%s: abs_flat #%02x out of range: %d (min:%d/max:%d)\n",
 		       UINPUT_NAME, code, abs->flat, min, max);
Signature · 5 lines
2.20.1.97.g81188d93c3-goog


-- 
Dmitry
Signature · 1 line
Dmitry
Reviewed-by
#2 lore raw
Quoted 37 lines · Dmitry Torokhov
Quoted 36 lines · Dmitry Torokhov

An integer overflow may arise in uinput_validate_absinfo() if "max - min" can't be represented by an "int". We should check for overflow before trying to use the result.

---
 drivers/input/misc/uinput.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/input/misc/uinput.c +2 -1
index 8ec483e8688b..26ec603fe220 100644
@@ -39,6 +39,7 @@
 #include <linux/init.h>
 #include <linux/fs.h>
 #include <linux/miscdevice.h>
+#include <linux/overflow.h>
 #include <linux/input/mt.h>
 #include "../input-compat.h"
 
@@ -405,7 +406,7 @@ static int uinput_open(struct inode *inode, struct file *file)
 static int uinput_validate_absinfo(struct input_dev *dev, unsigned int code,
 				   const struct input_absinfo *abs)
 {
-	int min, max;
+	int min, max, range;
 
 	min = abs->minimum;
 	max = abs->maximum;
@@ -417,7 +418,7 @@ static int uinput_validate_absinfo(struct input_dev *dev, unsigned int code,
 		return -EINVAL;
 	}
 
Quoted 4 lines

drivers/input/misc/uinput.c static int uinput_validate_absinfo(struct input_dev *dev, unsigned int code, · line 421

Quoted 2 lines
drivers/input/misc/uinput.c +1 -1
-	if (abs->flat > max - min) {
+	if (check_sub_overflow(max, min, &range) && abs->flat > range) {

This should be !check_sub_overflow(...) of course.

thanks.

Cheers,
Peter

Quoted 12 lines
Quoted 9 lines
drivers/input/misc/uinput.c +0 -0
 		printk(KERN_DEBUG
 		       "%s: abs_flat #%02x out of range: %d (min:%d/max:%d)\n",
 		       UINPUT_NAME, code, abs->flat, min, max);
Signature · 5 lines
2.20.1.97.g81188d93c3-goog


-- 
Dmitry
Signature · 1 line
Dmitry