diff options
author | Bartosz Golaszewski <bgolaszewski@baylibre.com> | 2019-09-03 12:00:25 +0200 |
---|---|---|
committer | Bartosz Golaszewski <bgolaszewski@baylibre.com> | 2019-09-03 13:33:45 +0200 |
commit | f08b9ee70cd2758e30fbe56d6674e9406e7ce5c1 (patch) | |
tree | 8c19f9f4e9283bf81ab082b3c78ed0a1a1a011ec | |
parent | d52e5845cb439c42f0a80c3029d100bff58e2125 (diff) | |
download | libgpiod-f08b9ee70cd2758e30fbe56d6674e9406e7ce5c1.tar.gz |
core: fix the major:minor number comparison between the device and sysfs
Current code will incorrectly conclude that a device "1:1" matches
a sysfs device "1:10" as the length it reads from sysfsp is based on
the devstr length, which in this example is 3.
Always read the whole sysfs attribute and compare the string generated
from actual major:minor numbers against it (minus the trailing newline).
Fixes: d9b1c1f14c6b ("core: harden gpiod_chip_open()")
Reported-by: Kent Gibson <warthog618@gmail.com>
Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
-rw-r--r-- | lib/core.c | 6 |
1 files changed, 4 insertions, 2 deletions
@@ -120,12 +120,14 @@ static bool is_gpiochip_cdev(const char *path) goto out_free_sysfsp; memset(sysfsdev, 0, sizeof(sysfsdev)); - rd = read(fd, sysfsdev, strlen(devstr)); + rd = read(fd, sysfsdev, sizeof(sysfsdev) - 1); close(fd); if (rd < 0) goto out_free_sysfsp; - if (strcmp(sysfsdev, devstr) != 0) { + rd--; /* Ignore trailing newline. */ + if ((size_t)rd != strlen(devstr) || + strncmp(sysfsdev, devstr, rd) != 0) { errno = ENODEV; goto out_free_sysfsp; } |