linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 0/2] gpiolib: improvements in linehandle_create()
@ 2017-10-16  9:32 Bartosz Golaszewski
  2017-10-16  9:32 ` [PATCH 1/2] gpiolib: only check line handle flags once Bartosz Golaszewski
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Bartosz Golaszewski @ 2017-10-16  9:32 UTC (permalink / raw)
  To: Linus Walleij; +Cc: linux-gpio, linux-kernel, Bartosz Golaszewski

Two minor fixes for creating user space line handles. The first patch
simplifies checking the request flags. The second adds an additional
sanitizing check.

Bartosz Golaszewski (2):
  gpiolib: only check line handle flags once
  gpiolib: don't allow OPEN_DRAIN & OPEN_SOURCE flags for input

 drivers/gpio/gpiolib.c | 20 +++++++++++++-------
 1 file changed, 13 insertions(+), 7 deletions(-)

-- 
2.13.2

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH 1/2] gpiolib: only check line handle flags once
  2017-10-16  9:32 [PATCH 0/2] gpiolib: improvements in linehandle_create() Bartosz Golaszewski
@ 2017-10-16  9:32 ` Bartosz Golaszewski
  2017-10-16  9:32 ` [PATCH 2/2] gpiolib: don't allow OPEN_DRAIN & OPEN_SOURCE flags for input Bartosz Golaszewski
  2017-10-16 11:47 ` [PATCH 0/2] gpiolib: improvements in linehandle_create() Linus Walleij
  2 siblings, 0 replies; 4+ messages in thread
From: Bartosz Golaszewski @ 2017-10-16  9:32 UTC (permalink / raw)
  To: Linus Walleij; +Cc: linux-gpio, linux-kernel, Bartosz Golaszewski

There's no need to check the validity of handle request flags more
than once, right after copying the data from user. Move the check
out of the for loop and simplify the error path by bailing out before
allocating any resources.

Signed-off-by: Bartosz Golaszewski <brgl@bgdev.pl>
---
 drivers/gpio/gpiolib.c | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)

diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c
index eb80dac4e26a..5d98345c9dc8 100644
--- a/drivers/gpio/gpiolib.c
+++ b/drivers/gpio/gpiolib.c
@@ -444,12 +444,19 @@ static int linehandle_create(struct gpio_device *gdev, void __user *ip)
 	struct linehandle_state *lh;
 	struct file *file;
 	int fd, i, ret;
+	u32 lflags;
 
 	if (copy_from_user(&handlereq, ip, sizeof(handlereq)))
 		return -EFAULT;
 	if ((handlereq.lines == 0) || (handlereq.lines > GPIOHANDLES_MAX))
 		return -EINVAL;
 
+	lflags = handlereq.flags;
+
+	/* Return an error if an unknown flag is set */
+	if (lflags & ~GPIOHANDLE_REQUEST_VALID_FLAGS)
+		return -EINVAL;
+
 	lh = kzalloc(sizeof(*lh), GFP_KERNEL);
 	if (!lh)
 		return -ENOMEM;
@@ -470,7 +477,6 @@ static int linehandle_create(struct gpio_device *gdev, void __user *ip)
 	/* Request each GPIO */
 	for (i = 0; i < handlereq.lines; i++) {
 		u32 offset = handlereq.lineoffsets[i];
-		u32 lflags = handlereq.flags;
 		struct gpio_desc *desc;
 
 		if (offset >= gdev->ngpio) {
@@ -478,12 +484,6 @@ static int linehandle_create(struct gpio_device *gdev, void __user *ip)
 			goto out_free_descs;
 		}
 
-		/* Return an error if a unknown flag is set */
-		if (lflags & ~GPIOHANDLE_REQUEST_VALID_FLAGS) {
-			ret = -EINVAL;
-			goto out_free_descs;
-		}
-
 		desc = &gdev->descs[offset];
 		ret = gpiod_request(desc, lh->label);
 		if (ret)
-- 
2.13.2

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH 2/2] gpiolib: don't allow OPEN_DRAIN & OPEN_SOURCE flags for input
  2017-10-16  9:32 [PATCH 0/2] gpiolib: improvements in linehandle_create() Bartosz Golaszewski
  2017-10-16  9:32 ` [PATCH 1/2] gpiolib: only check line handle flags once Bartosz Golaszewski
@ 2017-10-16  9:32 ` Bartosz Golaszewski
  2017-10-16 11:47 ` [PATCH 0/2] gpiolib: improvements in linehandle_create() Linus Walleij
  2 siblings, 0 replies; 4+ messages in thread
From: Bartosz Golaszewski @ 2017-10-16  9:32 UTC (permalink / raw)
  To: Linus Walleij; +Cc: linux-gpio, linux-kernel, Bartosz Golaszewski

OPEN_DRAIN and OPEN_SOURCE flags only affect the way we drive a GPIO
line, so they only make sense for output mode. Just as we only allow
input mode for event handle requests, don't allow passing open-drain
and open-source flags for any other mode than explicit output.

Signed-off-by: Bartosz Golaszewski <brgl@bgdev.pl>
---
 drivers/gpio/gpiolib.c | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c
index 5d98345c9dc8..eac86ead6cfa 100644
--- a/drivers/gpio/gpiolib.c
+++ b/drivers/gpio/gpiolib.c
@@ -457,6 +457,12 @@ static int linehandle_create(struct gpio_device *gdev, void __user *ip)
 	if (lflags & ~GPIOHANDLE_REQUEST_VALID_FLAGS)
 		return -EINVAL;
 
+	/* OPEN_DRAIN and OPEN_SOURCE flags only make sense for output mode. */
+	if (!(lflags & GPIOHANDLE_REQUEST_OUTPUT) &&
+	    ((lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN) ||
+	     (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE)))
+		return -EINVAL;
+
 	lh = kzalloc(sizeof(*lh), GFP_KERNEL);
 	if (!lh)
 		return -ENOMEM;
-- 
2.13.2

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH 0/2] gpiolib: improvements in linehandle_create()
  2017-10-16  9:32 [PATCH 0/2] gpiolib: improvements in linehandle_create() Bartosz Golaszewski
  2017-10-16  9:32 ` [PATCH 1/2] gpiolib: only check line handle flags once Bartosz Golaszewski
  2017-10-16  9:32 ` [PATCH 2/2] gpiolib: don't allow OPEN_DRAIN & OPEN_SOURCE flags for input Bartosz Golaszewski
@ 2017-10-16 11:47 ` Linus Walleij
  2 siblings, 0 replies; 4+ messages in thread
From: Linus Walleij @ 2017-10-16 11:47 UTC (permalink / raw)
  To: Bartosz Golaszewski; +Cc: linux-gpio, linux-kernel

On Mon, Oct 16, 2017 at 11:32 AM, Bartosz Golaszewski <brgl@bgdev.pl> wrote:

> Two minor fixes for creating user space line handles. The first patch
> simplifies checking the request flags. The second adds an additional
> sanitizing check.

Patches makes all kind of sense, so applied.

Yours,
Linus Walleij

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2017-10-16 11:47 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-10-16  9:32 [PATCH 0/2] gpiolib: improvements in linehandle_create() Bartosz Golaszewski
2017-10-16  9:32 ` [PATCH 1/2] gpiolib: only check line handle flags once Bartosz Golaszewski
2017-10-16  9:32 ` [PATCH 2/2] gpiolib: don't allow OPEN_DRAIN & OPEN_SOURCE flags for input Bartosz Golaszewski
2017-10-16 11:47 ` [PATCH 0/2] gpiolib: improvements in linehandle_create() Linus Walleij

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).