From e448a1c3a3eac4d73261bf8648110fc9f7aae806 Mon Sep 17 00:00:00 2001 From: Tom Gundersen Date: Tue, 23 Jun 2015 16:57:57 +0200 Subject: udev: bulitin-hwdb - fix memory leak This fixes CID#1292782. --- src/udev/udev-builtin-hwdb.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/udev/udev-builtin-hwdb.c b/src/udev/udev-builtin-hwdb.c index 7dfc74e6fa..b656204c46 100644 --- a/src/udev/udev-builtin-hwdb.c +++ b/src/udev/udev-builtin-hwdb.c @@ -26,6 +26,7 @@ #include "sd-hwdb.h" #include "hwdb-util.h" +#include "udev-util.h" static sd_hwdb *hwdb; @@ -87,6 +88,9 @@ static int udev_builtin_hwdb_search(struct udev_device *dev, struct udev_device assert(dev); + if (!srcdev) + srcdev = dev; + for (d = srcdev; d && !last; d = udev_device_get_parent(d)) { const char *dsubsys; const char *modalias = NULL; @@ -133,7 +137,7 @@ static int builtin_hwdb(struct udev_device *dev, int argc, char *argv[], bool te const char *device = NULL; const char *subsystem = NULL; const char *prefix = NULL; - struct udev_device *srcdev; + _cleanup_udev_device_unref_ struct udev_device *srcdev = NULL; if (!hwdb) return EXIT_FAILURE; @@ -176,8 +180,7 @@ static int builtin_hwdb(struct udev_device *dev, int argc, char *argv[], bool te srcdev = udev_device_new_from_device_id(udev_device_get_udev(dev), device); if (!srcdev) return EXIT_FAILURE; - } else - srcdev = dev; + } if (udev_builtin_hwdb_search(dev, srcdev, subsystem, prefix, filter, test) > 0) return EXIT_SUCCESS; -- cgit v1.2.1 From f6e0a35376dcab1455af698cf3f3be5e810c0f20 Mon Sep 17 00:00:00 2001 From: Tom Gundersen Date: Tue, 23 Jun 2015 17:03:19 +0200 Subject: udev: event - check return code of dup2() This fixes CID#1304688. --- src/udev/udev-event.c | 38 ++++++++++++++++++++++++++++---------- 1 file changed, 28 insertions(+), 10 deletions(-) diff --git a/src/udev/udev-event.c b/src/udev/udev-event.c index 5516a792eb..1092071e26 100644 --- a/src/udev/udev-event.c +++ b/src/udev/udev-event.c @@ -389,26 +389,44 @@ static int spawn_exec(struct udev_event *event, const char *cmd, char *const argv[], char **envp, int fd_stdout, int fd_stderr) { _cleanup_close_ int fd = -1; + int r; /* discard child output or connect to pipe */ fd = open("/dev/null", O_RDWR); if (fd >= 0) { - dup2(fd, STDIN_FILENO); - if (fd_stdout < 0) - dup2(fd, STDOUT_FILENO); - if (fd_stderr < 0) - dup2(fd, STDERR_FILENO); + r = dup2(fd, STDIN_FILENO); + if (r < 0) + log_warning_errno(errno, "redirecting stdin failed: %m"); + + if (fd_stdout < 0) { + r = dup2(fd, STDOUT_FILENO); + if (r < 0) + log_warning_errno(errno, "redirecting stdout failed: %m"); + } + + if (fd_stderr < 0) { + r = dup2(fd, STDERR_FILENO); + if (r < 0) + log_warning_errno(errno, "redirecting stderr failed: %m"); + } } else - log_error_errno(errno, "open /dev/null failed: %m"); + log_warning_errno(errno, "open /dev/null failed: %m"); /* connect pipes to std{out,err} */ if (fd_stdout >= 0) { - dup2(fd_stdout, STDOUT_FILENO); - safe_close(fd_stdout); + r = dup2(fd_stdout, STDOUT_FILENO); + if (r < 0) + log_warning_errno(errno, "redirecting stdout failed: %m"); + + fd_stdout = safe_close(fd_stdout); } + if (fd_stderr >= 0) { - dup2(fd_stderr, STDERR_FILENO); - safe_close(fd_stderr); + r = dup2(fd_stderr, STDERR_FILENO); + if (r < 0) + log_warning_errno(errno, "redirecting stdout failed: %m"); + + fd_stderr = safe_close(fd_stderr); } /* terminate child in case parent goes away */ -- cgit v1.2.1 From b6aab8ef9c4ac7555229fc741ea807bee093df31 Mon Sep 17 00:00:00 2001 From: Tom Gundersen Date: Tue, 23 Jun 2015 17:07:40 +0200 Subject: udev: worker - check return value of udev_monitor_enable_receiving() Fixes CID#1297430. --- src/udev/udevd.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/udev/udevd.c b/src/udev/udevd.c index ff15664cb4..cf15ddf641 100644 --- a/src/udev/udevd.c +++ b/src/udev/udevd.c @@ -327,6 +327,7 @@ static void worker_spawn(Manager *manager, struct event *event) { struct udev *udev = event->udev; _cleanup_udev_monitor_unref_ struct udev_monitor *worker_monitor = NULL; pid_t pid; + int r = 0; /* listen for new events */ worker_monitor = udev_monitor_new_from_netlink(udev, NULL); @@ -334,7 +335,9 @@ static void worker_spawn(Manager *manager, struct event *event) { return; /* allow the main daemon netlink address to send devices to the worker */ udev_monitor_allow_unicast_sender(worker_monitor, manager->monitor); - udev_monitor_enable_receiving(worker_monitor); + r = udev_monitor_enable_receiving(worker_monitor); + if (r < 0) + log_error_errno(r, "worker: could not enable receiving of device: %m"); pid = fork(); switch (pid) { @@ -346,7 +349,6 @@ static void worker_spawn(Manager *manager, struct event *event) { struct epoll_event ep_signal = { .events = EPOLLIN }; struct epoll_event ep_monitor = { .events = EPOLLIN }; sigset_t mask; - int r = 0; /* take initial device from queue */ dev = event->dev; @@ -528,7 +530,6 @@ out: default: { struct worker *worker; - int r; r = worker_new(&worker, manager, worker_monitor, pid); if (r < 0) -- cgit v1.2.1 From 56b13bcc992f296a061d9ae39d9369a8427bca73 Mon Sep 17 00:00:00 2001 From: Tom Gundersen Date: Tue, 23 Jun 2015 17:16:28 +0200 Subject: udevadm: trigger - check return values Fixes CID#1296243. --- src/udev/udevadm-trigger.c | 62 ++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 51 insertions(+), 11 deletions(-) diff --git a/src/udev/udevadm-trigger.c b/src/udev/udevadm-trigger.c index 11e83f355f..7af9665f8a 100644 --- a/src/udev/udevadm-trigger.c +++ b/src/udev/udevadm-trigger.c @@ -116,7 +116,7 @@ static int adm_trigger(struct udev *udev, int argc, char *argv[]) { } device_type = TYPE_DEVICES; const char *action = "change"; _cleanup_udev_enumerate_unref_ struct udev_enumerate *udev_enumerate = NULL; - int c; + int c, r; udev_enumerate = udev_enumerate_new(udev); if (udev_enumerate == NULL) @@ -153,28 +153,56 @@ static int adm_trigger(struct udev *udev, int argc, char *argv[]) { break; case 's': - udev_enumerate_add_match_subsystem(udev_enumerate, optarg); + r = udev_enumerate_add_match_subsystem(udev_enumerate, optarg); + if (r < 0) { + log_error_errno(r, "could not add subsystem match '%s': %m", optarg); + return 2; + } break; case 'S': - udev_enumerate_add_nomatch_subsystem(udev_enumerate, optarg); + r = udev_enumerate_add_nomatch_subsystem(udev_enumerate, optarg); + if (r < 0) { + log_error_errno(r, "could not add negative subsystem match '%s': %m", optarg); + return 2; + } break; case 'a': key = keyval(optarg, &val, buf, sizeof(buf)); - udev_enumerate_add_match_sysattr(udev_enumerate, key, val); + r = udev_enumerate_add_match_sysattr(udev_enumerate, key, val); + if (r < 0) { + log_error_errno(r, "could not add sysattr match '%s=%s': %m", key, val); + return 2; + } break; case 'A': key = keyval(optarg, &val, buf, sizeof(buf)); - udev_enumerate_add_nomatch_sysattr(udev_enumerate, key, val); + r = udev_enumerate_add_nomatch_sysattr(udev_enumerate, key, val); + if (r < 0) { + log_error_errno(r, "could not add negative sysattr match '%s=%s': %m", key, val); + return 2; + } break; case 'p': key = keyval(optarg, &val, buf, sizeof(buf)); - udev_enumerate_add_match_property(udev_enumerate, key, val); + r = udev_enumerate_add_match_property(udev_enumerate, key, val); + if (r < 0) { + log_error_errno(r, "could not add property match '%s=%s': %m", key, val); + return 2; + } break; case 'g': - udev_enumerate_add_match_tag(udev_enumerate, optarg); + r = udev_enumerate_add_match_tag(udev_enumerate, optarg); + if (r < 0) { + log_error_errno(r, "could not add tag match '%s': %m", optarg); + return 2; + } break; case 'y': - udev_enumerate_add_match_sysname(udev_enumerate, optarg); + r = udev_enumerate_add_match_sysname(udev_enumerate, optarg); + if (r < 0) { + log_error_errno(r, "could not add sysname match '%s': %m", optarg); + return 2; + } break; case 'b': { _cleanup_udev_device_unref_ struct udev_device *dev; @@ -185,7 +213,11 @@ static int adm_trigger(struct udev *udev, int argc, char *argv[]) { return 2; } - udev_enumerate_add_match_parent(udev_enumerate, dev); + r = udev_enumerate_add_match_parent(udev_enumerate, dev); + if (r < 0) { + log_error_errno(r, "could not add parent match '%s': %m", optarg); + return 2; + } break; } @@ -198,7 +230,11 @@ static int adm_trigger(struct udev *udev, int argc, char *argv[]) { return 2; } - udev_enumerate_add_match_parent(udev_enumerate, dev); + r = udev_enumerate_add_match_parent(udev_enumerate, dev); + if (r < 0) { + log_error_errno(r, "could not add parent match '%s': %m", optarg); + return 2; + } break; } @@ -221,7 +257,11 @@ static int adm_trigger(struct udev *udev, int argc, char *argv[]) { return 2; } - udev_enumerate_add_match_parent(udev_enumerate, dev); + r = udev_enumerate_add_match_parent(udev_enumerate, dev); + if (r < 0) { + log_error_errno(r, "could not add tag match '%s': %m", optarg); + return 2; + } } switch (device_type) { -- cgit v1.2.1