From: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
To: Bartosz Golaszewski <brgl@bgdev.pl>,
Kent Gibson <warthog618@gmail.com>,
Linus Walleij <linus.walleij@linaro.org>,
linux-gpio@vger.kernel.org,
Bartosz Golaszewski <bgolaszewski@baylibre.com>
Cc: Andy Shevchenko <andriy.shevchenko@linux.intel.com>,
Bartosz Golaszewski <bartekgola@gmail.com>
Subject: [PATCH v1] core: Relax gpiod_chip_open() for symbolic links
Date: Thu, 6 Feb 2020 20:13:58 +0200 [thread overview]
Message-ID: <20200206181358.12805-1-andriy.shevchenko@linux.intel.com> (raw)
User may ask device helper tool, for example, udev, to create a specific
symbolic link to a device node. GPIO chip character device node is not
exceptional. However, libgpiod in the commit d9b1c1f14c6b
("core: harden gpiod_chip_open()") went way too far in the hardening device
node check.
Relax that hardening for symbolic link to fix the regression.
Reproducer:
% gpioinfo /dev/gpiochip5
gpiochip5 - 16 lines:
line 0: "MUX33_DIR" "uart1-rx-oe" output active-high [used]
...
% ln -sf /dev/gpiochip5 /dev/MyGPIO_5
% gpioinfo /dev/MyGPIO_5
gpioinfo: looking up chip /dev/MyGPIO_5: Inappropriate ioctl for device
Link: https://stackoverflow.com/questions/60057494/gpio-issue-with-sym-link
Fixes: d9b1c1f14c6b ("core: harden gpiod_chip_open()")
Cc: Bartosz Golaszewski <bartekgola@gmail.com>
Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
---
lib/core.c | 32 ++++++++++++++++++++++----------
1 file changed, 22 insertions(+), 10 deletions(-)
diff --git a/lib/core.c b/lib/core.c
index 8352e18..32476ee 100644
--- a/lib/core.c
+++ b/lib/core.c
@@ -8,6 +8,7 @@
/* Low-level, core library code. */
#include <errno.h>
+#include <limits.h>
#include <fcntl.h>
#include <gpiod.h>
#include <linux/gpio.h>
@@ -75,7 +76,7 @@ struct gpiod_chip {
static bool is_gpiochip_cdev(const char *path)
{
- char *name, *pathcpy, *sysfsp, sysfsdev[16], devstr[16];
+ char *name, *realname, *sysfsp, sysfsdev[16], devstr[16];
struct stat statbuf;
bool ret = false;
int rv, fd;
@@ -85,6 +86,21 @@ static bool is_gpiochip_cdev(const char *path)
if (rv)
goto out;
+ /*
+ * Is it a symbolic link?
+ * We have to resolve symbolic link before checking the rest.
+ */
+ if (S_ISLNK(statbuf.st_mode))
+ realname = realpath(path, NULL);
+ else
+ realname = strdup(path);
+ if (realname == NULL)
+ goto out;
+
+ rv = stat(realname, &statbuf);
+ if (rv)
+ goto out_free_realname;
+
/* Is it a character device? */
if (!S_ISCHR(statbuf.st_mode)) {
/*
@@ -94,20 +110,16 @@ static bool is_gpiochip_cdev(const char *path)
* libgpiod from before the introduction of this routine.
*/
errno = ENOTTY;
- goto out;
+ goto out_free_realname;
}
/* Get the basename. */
- pathcpy = strdup(path);
- if (!pathcpy)
- goto out;
-
- name = basename(pathcpy);
+ name = basename(realname);
/* Do we have a corresponding sysfs attribute? */
rv = asprintf(&sysfsp, "/sys/bus/gpio/devices/%s/dev", name);
if (rv < 0)
- goto out_free_pathcpy;
+ goto out_free_realname;
if (access(sysfsp, R_OK) != 0) {
/*
@@ -149,8 +161,8 @@ static bool is_gpiochip_cdev(const char *path)
out_free_sysfsp:
free(sysfsp);
-out_free_pathcpy:
- free(pathcpy);
+out_free_realname:
+ free(realname);
out:
return ret;
}
--
2.25.0
next reply other threads:[~2020-02-06 18:14 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-02-06 18:13 Andy Shevchenko [this message]
2020-02-07 10:13 ` [PATCH v1] core: Relax gpiod_chip_open() for symbolic links Bartosz Golaszewski
2020-02-07 10:30 ` Andy Shevchenko
2020-02-07 11:01 ` Bartosz Golaszewski
2020-02-07 11:28 ` Andy Shevchenko
2020-02-07 12:05 ` Bartosz Gołaszewski
2020-02-07 13:00 ` Bartosz Golaszewski
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=20200206181358.12805-1-andriy.shevchenko@linux.intel.com \
--to=andriy.shevchenko@linux.intel.com \
--cc=bartekgola@gmail.com \
--cc=bgolaszewski@baylibre.com \
--cc=brgl@bgdev.pl \
--cc=linus.walleij@linaro.org \
--cc=linux-gpio@vger.kernel.org \
--cc=warthog618@gmail.com \
/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;
as well as URLs for NNTP newsgroup(s).