Skip to contentSkip to conversation

gpiolib: sanitize flags before allocating memory in lineevent_create()

PATCH In review Bartosz Golaszewski 1 participant 2 messages Sep 16, 2019 – Oct 1, 2019 GPIO

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 gpiolib: sanitize flags before allocating memory in lineevent_create()

From: Bartosz Golaszewski <[email protected]>

Move all the flags sanitization before any memory allocation in lineevent_create() in order to remove a couple unneeded gotos.

---
 drivers/gpio/gpiolib.c | 42 ++++++++++++++++++------------------------
 1 file changed, 18 insertions(+), 24 deletions(-)
drivers/gpio/gpiolib.c +18 -24
index d9074191edef..194b0bcdcfb7 100644
@@ -899,6 +899,24 @@ static int lineevent_create(struct gpio_device *gdev, void __user *ip)
 
 	if (copy_from_user(&eventreq, ip, sizeof(eventreq)))
 		return -EFAULT;
+	
+	offset = eventreq.lineoffset;
+	lflags = eventreq.handleflags;
+	eflags = eventreq.eventflags;
+
+	if (offset >= gdev->ngpio)
+		return -EINVAL;
+
+	/* Return an error if a unknown flag is set */
+	if ((lflags & ~GPIOHANDLE_REQUEST_VALID_FLAGS) ||
+	    (eflags & ~GPIOEVENT_REQUEST_VALID_FLAGS))
+		return -EINVAL;
+
+	/* This is just wrong: we don't look for events on output lines */
+	if ((lflags & GPIOHANDLE_REQUEST_OUTPUT) ||
+	    (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN) ||
+	    (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE))
+		return -EINVAL;
 
 	le = kzalloc(sizeof(*le), GFP_KERNEL);
 	if (!le)
@@ -917,30 +935,6 @@ static int lineevent_create(struct gpio_device *gdev, void __user *ip)
 		}
 	}
 
-	offset = eventreq.lineoffset;
-	lflags = eventreq.handleflags;
-	eflags = eventreq.eventflags;
-
-	if (offset >= gdev->ngpio) {
-		ret = -EINVAL;
-		goto out_free_label;
-	}
-
-	/* Return an error if a unknown flag is set */
-	if ((lflags & ~GPIOHANDLE_REQUEST_VALID_FLAGS) ||
-	    (eflags & ~GPIOEVENT_REQUEST_VALID_FLAGS)) {
-		ret = -EINVAL;
-		goto out_free_label;
-	}
-
-	/* This is just wrong: we don't look for events on output lines */
-	if ((lflags & GPIOHANDLE_REQUEST_OUTPUT) ||
-	    (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN) ||
-	    (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE)) {
-		ret = -EINVAL;
-		goto out_free_label;
-	}
-
 	desc = &gdev->descs[offset];
 	ret = gpiod_request(desc, le->label);
 	if (ret)
Signature · 1 line
2.21.0
#1 lore raw

pon., 16 wrz 2019 o 11:46 Bartosz Golaszewski <[email protected]> napisał(a):

Quoted 72 lines

From: Bartosz Golaszewski <[email protected]>

Move all the flags sanitization before any memory allocation in lineevent_create() in order to remove a couple unneeded gotos.

---
 drivers/gpio/gpiolib.c | 42 ++++++++++++++++++------------------------
 1 file changed, 18 insertions(+), 24 deletions(-)
drivers/gpio/gpiolib.c +18 -25
index d9074191edef..194b0bcdcfb7 100644
@@ -899,6 +899,24 @@ static int lineevent_create(struct gpio_device *gdev, void __user *ip)
 
        if (copy_from_user(&eventreq, ip, sizeof(eventreq)))
                return -EFAULT;
+
+       offset = eventreq.lineoffset;
+       lflags = eventreq.handleflags;
+       eflags = eventreq.eventflags;
+
+       if (offset >= gdev->ngpio)
+               return -EINVAL;
+
+       /* Return an error if a unknown flag is set */
+       if ((lflags & ~GPIOHANDLE_REQUEST_VALID_FLAGS) ||
+           (eflags & ~GPIOEVENT_REQUEST_VALID_FLAGS))
+               return -EINVAL;
+
+       /* This is just wrong: we don't look for events on output lines */
+       if ((lflags & GPIOHANDLE_REQUEST_OUTPUT) ||
+           (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN) ||
+           (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE))
+               return -EINVAL;
 
        le = kzalloc(sizeof(*le), GFP_KERNEL);
        if (!le)
@@ -917,30 +935,6 @@ static int lineevent_create(struct gpio_device *gdev, void __user *ip)
                }
        }
 
-       offset = eventreq.lineoffset;
-       lflags = eventreq.handleflags;
-       eflags = eventreq.eventflags;
-
-       if (offset >= gdev->ngpio) {
-               ret = -EINVAL;
-               goto out_free_label;
-       }
-
-       /* Return an error if a unknown flag is set */
-       if ((lflags & ~GPIOHANDLE_REQUEST_VALID_FLAGS) ||
-           (eflags & ~GPIOEVENT_REQUEST_VALID_FLAGS)) {
-               ret = -EINVAL;
-               goto out_free_label;
-       }
-
-       /* This is just wrong: we don't look for events on output lines */
-       if ((lflags & GPIOHANDLE_REQUEST_OUTPUT) ||
-           (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN) ||
-           (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE)) {
-               ret = -EINVAL;
-               goto out_free_label;
-       }
-
        desc = &gdev->descs[offset];
        ret = gpiod_request(desc, le->label);
        if (ret)
--
Quoted 2 lines

2.21.0

Patch applied.

Bart