Linux GPIO subsystem development
 help / color / mirror / Atom feed
From: Alexander Stein <alexander.stein@systec-electronic.com>
To: Linus Walleij <linus.walleij@linaro.org>
Cc: "linux-gpio@vger.kernel.org" <linux-gpio@vger.kernel.org>,
	Alexandre Courbot <acourbot@nvidia.com>,
	Michael Welling <mwelling@ieee.org>,
	Markus Pargmann <mpa@pengutronix.de>,
	Lee Campbell <leecam@google.com>,
	Dmitry Torokhov <dmitry.torokhov@gmail.com>,
	Bamvor Jian Zhang <bamvor.zhangjian@linaro.org>,
	Grant Likely <grant.likely@linaro.org>,
	Arnd Bergmann <arnd@arndb.de>, Mark Brown <broonie@kernel.org>,
	Johan Hovold <johan@kernel.org>
Subject: Re: [PATCH 1/4] gpio: userspace ABI for reading/writing GPIO lines
Date: Wed, 29 Jun 2016 11:43:56 +0200	[thread overview]
Message-ID: <31233738.EiQ4DLngM8@ws-stein> (raw)
In-Reply-To: <CACRpkdbRG3w+=GvnJUN9b+Nbej7gfDR5duTwjmdxTLc-p03O7w@mail.gmail.com>

On Wednesday 29 June 2016 11:05:49, Linus Walleij wrote:
> On Wed, Jun 29, 2016 at 10:53 AM, Alexander Stein
> 
> <alexander.stein@systec-electronic.com> wrote:
> > On Thursday 02 June 2016 13:51:26, Linus Walleij wrote:
> >> +     fd = anon_inode_getfd("gpio-linehandle",
> >> +                           &linehandle_fileops,
> >> +                           lh,
> >> +                           O_RDONLY | O_CLOEXEC);
> > 
> > When will linehandle_release actually be called? When we explicitely call
> > close(req.fd) or when application exits and all FDs will be closed anyway?
> 
> As far as I understand: both.
> 
> And I don't think anything else makes sense? If an application is
> terminated, the operating environment will close all open file handles as
> far as I know, at least that is how I always thought it works.
> 
> But hey, it's userspace so what do I know...
> 
> Anyways if it doesn't work like so, there are a bunch of subsystems in
> the kernel using it so they would all have severe problems.

Fine, just wanted to make sure.

> >> +     if (fd < 0) {
> >> +             ret = fd;
> >> +             goto out_free_descs;
> >> +     }
> >> +
> >> +     handlereq.fd = fd;
> >> +     if (copy_to_user(ip, &handlereq, sizeof(handlereq)))
> >> +             return -EFAULT;
> > 
> > If I'm right above doesn't that then leak lh until application eventually
> > exits? Userspace won't receive the newly created fd. The same would apply
> > to patch 3/4.
> 
> I don't understand....
> 
> The userspace application reads the .fd fields of the handle request after
> issuing the ioctl() and it then contains the new file handle.

There is no valid .fd if copy_to_user() fails and immediately returning
-EFAULT. Thus the previously kzalloc'ed lh (well anything linehandle_release 
frees) is unaccessible. Userspace has no .fd for neither any ioctl nor for 
calling close(). This allocated memory will remain unavailable until the 
process exits. I think something like this should be added:

> @@ -486,8 +486,10 @@ static int linehandle_create(struct gpio_device *gdev,
> void __user *ip)> 
>  	}
>  	
>  	handlereq.fd = fd;
> 
> -	if (copy_to_user(ip, &handlereq, sizeof(handlereq)))
> -		return -EFAULT;
> +	if (copy_to_user(ip, &handlereq, sizeof(handlereq))) {
> +		ret = -EFAULT;
> +		goto out_free_descs;
> +	}
> 
>  	dev_dbg(&gdev->dev, "registered chardev handle for %d lines\n",
>  	
>  		lh->numdescs);

I hope this could make my concerns more clear. Opinions?

Best regards,
Alexander


  reply	other threads:[~2016-06-29  9:44 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-06-02 11:51 [PATCH 1/4] gpio: userspace ABI for reading/writing GPIO lines Linus Walleij
2016-06-02 11:51 ` [PATCH 2/4] tools/gpio: add the gpio-hammer tool Linus Walleij
2016-06-02 11:51 ` [PATCH 3/4] gpio: userspace ABI for reading GPIO line events Linus Walleij
2016-06-02 11:51 ` [PATCH 4/4] tools/gpio: add the gpio-event-mon tool Linus Walleij
2016-06-02 18:12 ` [PATCH 1/4] gpio: userspace ABI for reading/writing GPIO lines Michael Welling
2016-06-29  8:53 ` Alexander Stein
2016-06-29  9:05   ` Linus Walleij
2016-06-29  9:43     ` Alexander Stein [this message]
2016-07-04  9:55       ` Linus Walleij

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=31233738.EiQ4DLngM8@ws-stein \
    --to=alexander.stein@systec-electronic.com \
    --cc=acourbot@nvidia.com \
    --cc=arnd@arndb.de \
    --cc=bamvor.zhangjian@linaro.org \
    --cc=broonie@kernel.org \
    --cc=dmitry.torokhov@gmail.com \
    --cc=grant.likely@linaro.org \
    --cc=johan@kernel.org \
    --cc=leecam@google.com \
    --cc=linus.walleij@linaro.org \
    --cc=linux-gpio@vger.kernel.org \
    --cc=mpa@pengutronix.de \
    --cc=mwelling@ieee.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox