richardphibel / rpms / systemd

Forked from rpms/systemd 2 years ago
Clone
698723
From 897b4d1e19c706d9198b9308125df57a5d469a6b Mon Sep 17 00:00:00 2001
4bf4e7
From: Michal Sekletar <msekleta@redhat.com>
4bf4e7
Date: Mon, 17 May 2021 15:50:31 +0200
4bf4e7
Subject: [PATCH] Revert "udev: make algorithm that selects highest priority
4bf4e7
 devlink less susceptible to race conditions"
4bf4e7
4bf4e7
This reverts commit 1d5f966c1758eb620755fcae54abd07a1ac36d3d.
4bf4e7
698723
Related: #1942299
4bf4e7
---
4bf4e7
 src/udev/udev-event.c |  71 +++++-------
4bf4e7
 src/udev/udev-node.c  | 244 ++++++++++++------------------------------
4bf4e7
 2 files changed, 99 insertions(+), 216 deletions(-)
4bf4e7
4bf4e7
diff --git a/src/udev/udev-event.c b/src/udev/udev-event.c
4bf4e7
index 9004634f65..fd8406d959 100644
4bf4e7
--- a/src/udev/udev-event.c
4bf4e7
+++ b/src/udev/udev-event.c
4bf4e7
@@ -833,41 +833,6 @@ static int rename_netif(struct udev_event *event) {
4bf4e7
         return 0;
4bf4e7
 }
4bf4e7
 
4bf4e7
-static void update_devnode(struct udev_event *event) {
4bf4e7
-        struct udev_device *dev = event->dev;
4bf4e7
-
4bf4e7
-        if (major(udev_device_get_devnum(dev)) > 0) {
4bf4e7
-                bool apply;
4bf4e7
-
4bf4e7
-                /* remove/update possible left-over symlinks from old database entry */
4bf4e7
-                if (event->dev_db != NULL)
4bf4e7
-                        udev_node_update_old_links(dev, event->dev_db);
4bf4e7
-
4bf4e7
-                if (!event->owner_set)
4bf4e7
-                        event->uid = udev_device_get_devnode_uid(dev);
4bf4e7
-
4bf4e7
-                if (!event->group_set)
4bf4e7
-                        event->gid = udev_device_get_devnode_gid(dev);
4bf4e7
-
4bf4e7
-                if (!event->mode_set) {
4bf4e7
-                        if (udev_device_get_devnode_mode(dev) > 0) {
4bf4e7
-                                /* kernel supplied value */
4bf4e7
-                                event->mode = udev_device_get_devnode_mode(dev);
4bf4e7
-                        } else if (event->gid > 0) {
4bf4e7
-                                /* default 0660 if a group is assigned */
4bf4e7
-                                event->mode = 0660;
4bf4e7
-                        }
4bf4e7
-                        else {
4bf4e7
-                                /* default 0600 */
4bf4e7
-                                event->mode = 0600;
4bf4e7
-                        }
4bf4e7
-                }
4bf4e7
-
4bf4e7
-                apply = streq(udev_device_get_action(dev), "add") || event->owner_set || event->group_set || event->mode_set;
4bf4e7
-                udev_node_add(dev, apply, event->mode, event->uid, event->gid, &event->seclabel_list);
4bf4e7
-        }
4bf4e7
-}
4bf4e7
-
4bf4e7
 void udev_event_execute_rules(struct udev_event *event,
4bf4e7
                               usec_t timeout_usec, usec_t timeout_warn_usec,
4bf4e7
                               struct udev_list *properties_list,
4bf4e7
@@ -926,7 +891,35 @@ void udev_event_execute_rules(struct udev_event *event,
4bf4e7
                         }
4bf4e7
                 }
4bf4e7
 
4bf4e7
-                update_devnode(event);
4bf4e7
+                if (major(udev_device_get_devnum(dev)) > 0) {
4bf4e7
+                        bool apply;
4bf4e7
+
4bf4e7
+                        /* remove/update possible left-over symlinks from old database entry */
4bf4e7
+                        if (event->dev_db != NULL)
4bf4e7
+                                udev_node_update_old_links(dev, event->dev_db);
4bf4e7
+
4bf4e7
+                        if (!event->owner_set)
4bf4e7
+                                event->uid = udev_device_get_devnode_uid(dev);
4bf4e7
+
4bf4e7
+                        if (!event->group_set)
4bf4e7
+                                event->gid = udev_device_get_devnode_gid(dev);
4bf4e7
+
4bf4e7
+                        if (!event->mode_set) {
4bf4e7
+                                if (udev_device_get_devnode_mode(dev) > 0) {
4bf4e7
+                                        /* kernel supplied value */
4bf4e7
+                                        event->mode = udev_device_get_devnode_mode(dev);
4bf4e7
+                                } else if (event->gid > 0) {
4bf4e7
+                                        /* default 0660 if a group is assigned */
4bf4e7
+                                        event->mode = 0660;
4bf4e7
+                                } else {
4bf4e7
+                                        /* default 0600 */
4bf4e7
+                                        event->mode = 0600;
4bf4e7
+                                }
4bf4e7
+                        }
4bf4e7
+
4bf4e7
+                        apply = streq(udev_device_get_action(dev), "add") || event->owner_set || event->group_set || event->mode_set;
4bf4e7
+                        udev_node_add(dev, apply, event->mode, event->uid, event->gid, &event->seclabel_list);
4bf4e7
+                }
4bf4e7
 
4bf4e7
                 /* preserve old, or get new initialization timestamp */
4bf4e7
                 udev_device_ensure_usec_initialized(event->dev, event->dev_db);
4bf4e7
@@ -934,12 +927,6 @@ void udev_event_execute_rules(struct udev_event *event,
4bf4e7
                 /* (re)write database file */
4bf4e7
                 udev_device_tag_index(dev, event->dev_db, true);
4bf4e7
                 udev_device_update_db(dev);
4bf4e7
-
4bf4e7
-                /* Yes, we run update_devnode() twice, because in the first invocation, that is before update of udev database,
4bf4e7
-                 * it could happen that two contenders are replacing each other's symlink. Hence we run it again to make sure
4bf4e7
-                 * symlinks point to devices that claim them with the highest priority. */
4bf4e7
-                update_devnode(event);
4bf4e7
-
4bf4e7
                 udev_device_set_is_initialized(dev);
4bf4e7
 
4bf4e7
                 event->dev_db = udev_device_unref(event->dev_db);
4bf4e7
diff --git a/src/udev/udev-node.c b/src/udev/udev-node.c
4bf4e7
index 2eeeccdd3a..333dcae6b9 100644
4bf4e7
--- a/src/udev/udev-node.c
4bf4e7
+++ b/src/udev/udev-node.c
4bf4e7
@@ -13,27 +13,19 @@
4bf4e7
 #include <unistd.h>
4bf4e7
 
4bf4e7
 #include "device-nodes.h"
4bf4e7
-#include "device-private.h"
4bf4e7
 #include "dirent-util.h"
4bf4e7
-#include "fd-util.h"
4bf4e7
 #include "format-util.h"
4bf4e7
 #include "fs-util.h"
4bf4e7
-#include "sd-device.h"
4bf4e7
 #include "selinux-util.h"
4bf4e7
 #include "smack-util.h"
4bf4e7
-#include "stat-util.h"
4bf4e7
 #include "stdio-util.h"
4bf4e7
 #include "string-util.h"
4bf4e7
 #include "udev.h"
4bf4e7
-#include "libudev-device-internal.h"
4bf4e7
 
4bf4e7
-#define LINK_UPDATE_MAX_RETRIES 128
4bf4e7
-
4bf4e7
-static int node_symlink(sd_device *dev, const char *node, const char *slink) {
4bf4e7
+static int node_symlink(struct udev_device *dev, const char *node, const char *slink) {
4bf4e7
         struct stat stats;
4bf4e7
         char target[UTIL_PATH_SIZE];
4bf4e7
         char *s;
4bf4e7
-        const char *id_filename;
4bf4e7
         size_t l;
4bf4e7
         char slink_tmp[UTIL_PATH_SIZE + 32];
4bf4e7
         int i = 0;
4bf4e7
@@ -97,10 +89,7 @@ static int node_symlink(sd_device *dev, const char *node, const char *slink) {
4bf4e7
         }
4bf4e7
 
4bf4e7
         log_debug("atomically replace '%s'", slink);
4bf4e7
-        err = device_get_id_filename(dev, &id_filename);
4bf4e7
-        if (err < 0)
4bf4e7
-                return log_error_errno(err, "Failed to get id_filename: %m");
4bf4e7
-        strscpyl(slink_tmp, sizeof(slink_tmp), slink, ".tmp-", id_filename, NULL);
4bf4e7
+        strscpyl(slink_tmp, sizeof(slink_tmp), slink, ".tmp-", udev_device_get_id_filename(dev), NULL);
4bf4e7
         unlink(slink_tmp);
4bf4e7
         do {
4bf4e7
                 err = mkdir_parents_label(slink_tmp, 0755);
4bf4e7
@@ -120,187 +109,104 @@ static int node_symlink(sd_device *dev, const char *node, const char *slink) {
4bf4e7
         if (err != 0) {
4bf4e7
                 log_error_errno(errno, "rename '%s' '%s' failed: %m", slink_tmp, slink);
4bf4e7
                 unlink(slink_tmp);
4bf4e7
-        } else
4bf4e7
-                /* Tell caller that we replaced already existing symlink. */
4bf4e7
-                return 1;
4bf4e7
+        }
4bf4e7
 exit:
4bf4e7
         return err;
4bf4e7
 }
4bf4e7
 
4bf4e7
 /* find device node of device with highest priority */
4bf4e7
-static int link_find_prioritized(sd_device *dev, bool add, const char *stackdir, char **ret) {
4bf4e7
-        _cleanup_closedir_ DIR *dir = NULL;
4bf4e7
-        _cleanup_free_ char *target = NULL;
4bf4e7
+static const char *link_find_prioritized(struct udev_device *dev, bool add, const char *stackdir, char *buf, size_t bufsize) {
4bf4e7
+        struct udev *udev = udev_device_get_udev(dev);
4bf4e7
+        DIR *dir;
4bf4e7
         struct dirent *dent;
4bf4e7
-        int r, priority = 0;
4bf4e7
-
4bf4e7
-        assert(!add || dev);
4bf4e7
-        assert(stackdir);
4bf4e7
-        assert(ret);
4bf4e7
+        int priority = 0;
4bf4e7
+        const char *target = NULL;
4bf4e7
 
4bf4e7
         if (add) {
4bf4e7
-                const char *devnode;
4bf4e7
-
4bf4e7
-                r = device_get_devlink_priority(dev, &priority);
4bf4e7
-                if (r < 0)
4bf4e7
-                        return r;
4bf4e7
-
4bf4e7
-                r = sd_device_get_devname(dev, &devnode);
4bf4e7
-                if (r < 0)
4bf4e7
-                        return r;
4bf4e7
-
4bf4e7
-                target = strdup(devnode);
4bf4e7
-                if (!target)
4bf4e7
-                        return -ENOMEM;
4bf4e7
+                priority = udev_device_get_devlink_priority(dev);
4bf4e7
+                strscpy(buf, bufsize, udev_device_get_devnode(dev));
4bf4e7
+                target = buf;
4bf4e7
         }
4bf4e7
 
4bf4e7
         dir = opendir(stackdir);
4bf4e7
-        if (!dir) {
4bf4e7
-                if (target) {
4bf4e7
-                        *ret = TAKE_PTR(target);
4bf4e7
-                        return 0;
4bf4e7
-                }
4bf4e7
-
4bf4e7
-                return -errno;
4bf4e7
-        }
4bf4e7
-
4bf4e7
+        if (dir == NULL)
4bf4e7
+                return target;
4bf4e7
         FOREACH_DIRENT_ALL(dent, dir, break) {
4bf4e7
-                _cleanup_(sd_device_unrefp) sd_device *dev_db = NULL;
4bf4e7
-                const char *devnode, *id_filename;
4bf4e7
-                int db_prio = 0;
4bf4e7
+                struct udev_device *dev_db;
4bf4e7
 
4bf4e7
                 if (dent->d_name[0] == '\0')
4bf4e7
                         break;
4bf4e7
                 if (dent->d_name[0] == '.')
4bf4e7
                         continue;
4bf4e7
 
4bf4e7
-                log_debug("Found '%s' claiming '%s'", dent->d_name, stackdir);
4bf4e7
-
4bf4e7
-                if (device_get_id_filename(dev, &id_filename) < 0)
4bf4e7
-                        continue;
4bf4e7
+                log_debug("found '%s' claiming '%s'", dent->d_name, stackdir);
4bf4e7
 
4bf4e7
                 /* did we find ourself? */
4bf4e7
-                if (streq(dent->d_name, id_filename))
4bf4e7
-                        continue;
4bf4e7
-
4bf4e7
-                if (sd_device_new_from_device_id(&dev_db, dent->d_name) < 0)
4bf4e7
+                if (streq(dent->d_name, udev_device_get_id_filename(dev)))
4bf4e7
                         continue;
4bf4e7
 
4bf4e7
-                if (sd_device_get_devname(dev_db, &devnode) < 0)
4bf4e7
-                        continue;
4bf4e7
-
4bf4e7
-                if (device_get_devlink_priority(dev_db, &db_prio) < 0)
4bf4e7
-                        continue;
4bf4e7
-
4bf4e7
-                if (target && db_prio <= priority)
4bf4e7
-                        continue;
4bf4e7
-
4bf4e7
-                if (DEBUG_LOGGING) {
4bf4e7
-                        const char *syspath = NULL;
4bf4e7
-
4bf4e7
-                        (void) sd_device_get_syspath(dev_db, &syspath);
4bf4e7
-                        log_debug("Device '%s' claims priority %i for '%s'", strnull(syspath), db_prio, stackdir);
4bf4e7
+                dev_db = udev_device_new_from_device_id(udev, dent->d_name);
4bf4e7
+                if (dev_db != NULL) {
4bf4e7
+                        const char *devnode;
4bf4e7
+
4bf4e7
+                        devnode = udev_device_get_devnode(dev_db);
4bf4e7
+                        if (devnode != NULL) {
4bf4e7
+                                if (target == NULL || udev_device_get_devlink_priority(dev_db) > priority) {
4bf4e7
+                                        log_debug("'%s' claims priority %i for '%s'",
4bf4e7
+                                                  udev_device_get_syspath(dev_db), udev_device_get_devlink_priority(dev_db), stackdir);
4bf4e7
+                                        priority = udev_device_get_devlink_priority(dev_db);
4bf4e7
+                                        strscpy(buf, bufsize, devnode);
4bf4e7
+                                        target = buf;
4bf4e7
+                                }
4bf4e7
+                        }
4bf4e7
+                        udev_device_unref(dev_db);
4bf4e7
                 }
4bf4e7
-
4bf4e7
-                r = free_and_strdup(&target, devnode);
4bf4e7
-                if (r < 0)
4bf4e7
-                        return r;
4bf4e7
-                priority = db_prio;
4bf4e7
         }
4bf4e7
-
4bf4e7
-        if (!target)
4bf4e7
-                return -ENOENT;
4bf4e7
-
4bf4e7
-        *ret = TAKE_PTR(target);
4bf4e7
-        return 0;
4bf4e7
+        closedir(dir);
4bf4e7
+        return target;
4bf4e7
 }
4bf4e7
 
4bf4e7
-
4bf4e7
 /* manage "stack of names" with possibly specified device priorities */
4bf4e7
-static int link_update(sd_device *dev, const char *slink, bool add) {
4bf4e7
-        _cleanup_free_ char *filename = NULL, *dirname = NULL;
4bf4e7
-        char name_enc[PATH_MAX];
4bf4e7
-        const char *id_filename;
4bf4e7
-        int i, r, retries;
4bf4e7
-
4bf4e7
-        assert(dev);
4bf4e7
-        assert(slink);
4bf4e7
-
4bf4e7
-        r = device_get_id_filename(dev, &id_filename);
4bf4e7
-        if (r < 0)
4bf4e7
-                return log_debug_errno(r, "Failed to get id_filename: %m");
4bf4e7
+static void link_update(struct udev_device *dev, const char *slink, bool add) {
4bf4e7
+        char name_enc[UTIL_PATH_SIZE];
4bf4e7
+        char filename[UTIL_PATH_SIZE * 2];
4bf4e7
+        char dirname[UTIL_PATH_SIZE];
4bf4e7
+        const char *target;
4bf4e7
+        char buf[UTIL_PATH_SIZE];
4bf4e7
 
4bf4e7
         util_path_encode(slink + STRLEN("/dev"), name_enc, sizeof(name_enc));
4bf4e7
-        dirname = path_join(NULL, "/run/udev/links/", name_enc);
4bf4e7
-        if (!dirname)
4bf4e7
-                return log_oom();
4bf4e7
-        filename = path_join(NULL, dirname, id_filename);
4bf4e7
-        if (!filename)
4bf4e7
-                return log_oom();
4bf4e7
-
4bf4e7
-        if (!add) {
4bf4e7
-                if (unlink(filename) == 0)
4bf4e7
-                        (void) rmdir(dirname);
4bf4e7
-        } else
4bf4e7
-                for (;;) {
4bf4e7
-                        _cleanup_close_ int fd = -1;
4bf4e7
-
4bf4e7
-                        r = mkdir_parents(filename, 0755);
4bf4e7
-                        if (!IN_SET(r, 0, -ENOENT))
4bf4e7
-                                return r;
4bf4e7
-
4bf4e7
-                        fd = open(filename, O_WRONLY|O_CREAT|O_CLOEXEC|O_TRUNC|O_NOFOLLOW, 0444);
4bf4e7
-                        if (fd >= 0)
4bf4e7
-                                break;
4bf4e7
-                        if (errno != ENOENT)
4bf4e7
-                                return -errno;
4bf4e7
-                }
4bf4e7
-
4bf4e7
-        /* If the database entry is not written yet we will just do one iteration and possibly wrong symlink
4bf4e7
-         * will be fixed in the second invocation. */
4bf4e7
-        (void) sd_device_get_is_initialized(dev, &r);
4bf4e7
-        retries = r > 0 ? LINK_UPDATE_MAX_RETRIES : 1;
4bf4e7
+        strscpyl(dirname, sizeof(dirname), "/run/udev/links/", name_enc, NULL);
4bf4e7
+        strscpyl(filename, sizeof(filename), dirname, "/", udev_device_get_id_filename(dev), NULL);
4bf4e7
 
4bf4e7
-        for (i = 0; i < retries; i++) {
4bf4e7
-                _cleanup_free_ char *target = NULL;
4bf4e7
-                struct stat st1 = {}, st2 = {};
4bf4e7
+        if (!add && unlink(filename) == 0)
4bf4e7
+                rmdir(dirname);
4bf4e7
 
4bf4e7
-                r = stat(dirname, &st1;;
4bf4e7
-                if (r < 0 && errno != ENOENT)
4bf4e7
-                        return -errno;
4bf4e7
-
4bf4e7
-                r = link_find_prioritized(dev, add, dirname, &target);
4bf4e7
-                if (r == -ENOENT) {
4bf4e7
-                        log_debug("No reference left, removing '%s'", slink);
4bf4e7
-                        if (unlink(slink) == 0)
4bf4e7
-                                (void) rmdir_parents(slink, "/");
4bf4e7
-
4bf4e7
-                        break;
4bf4e7
-                } else if (r < 0)
4bf4e7
-                        return log_error_errno(r, "Failed to determine highest priority symlink: %m");
4bf4e7
+        target = link_find_prioritized(dev, add, dirname, buf, sizeof(buf));
4bf4e7
+        if (target == NULL) {
4bf4e7
+                log_debug("no reference left, remove '%s'", slink);
4bf4e7
+                if (unlink(slink) == 0)
4bf4e7
+                        rmdir_parents(slink, "/");
4bf4e7
+        } else {
4bf4e7
+                log_debug("creating link '%s' to '%s'", slink, target);
4bf4e7
+                node_symlink(dev, target, slink);
4bf4e7
+        }
4bf4e7
 
4bf4e7
-                r = node_symlink(dev, target, slink);
4bf4e7
-                if (r < 0) {
4bf4e7
-                        (void) unlink(filename);
4bf4e7
-                        break;
4bf4e7
-                } else if (r == 1)
4bf4e7
-                        /* We have replaced already existing symlink, possibly there is some other device trying
4bf4e7
-                         * to claim the same symlink. Let's do one more iteration to give us a chance to fix
4bf4e7
-                         * the error if other device actually claims the symlink with higher priority. */
4bf4e7
-                        continue;
4bf4e7
+        if (add) {
4bf4e7
+                int err;
4bf4e7
 
4bf4e7
-               /* Skip the second stat() if the first failed, stat_inode_unmodified() would return false regardless. */
4bf4e7
-                if ((st1.st_mode & S_IFMT) != 0) {
4bf4e7
-                        r = stat(dirname, &st2;;
4bf4e7
-                        if (r < 0 && errno != ENOENT)
4bf4e7
-                                return -errno;
4bf4e7
+                do {
4bf4e7
+                        int fd;
4bf4e7
 
4bf4e7
-                        if (stat_inode_unmodified(&st1, &st2))
4bf4e7
+                        err = mkdir_parents(filename, 0755);
4bf4e7
+                        if (!IN_SET(err, 0, -ENOENT))
4bf4e7
                                 break;
4bf4e7
-                }
4bf4e7
+                        fd = open(filename, O_WRONLY|O_CREAT|O_CLOEXEC|O_TRUNC|O_NOFOLLOW, 0444);
4bf4e7
+                        if (fd >= 0)
4bf4e7
+                                close(fd);
4bf4e7
+                        else
4bf4e7
+                                err = -errno;
4bf4e7
+                } while (err == -ENOENT);
4bf4e7
         }
4bf4e7
-
4bf4e7
-        return i < LINK_UPDATE_MAX_RETRIES ? 0 : -ELOOP;
4bf4e7
 }
4bf4e7
 
4bf4e7
 void udev_node_update_old_links(struct udev_device *dev, struct udev_device *dev_old) {
4bf4e7
@@ -327,7 +233,7 @@ void udev_node_update_old_links(struct udev_device *dev, struct udev_device *dev
4bf4e7
 
4bf4e7
                 log_debug("update old name, '%s' no longer belonging to '%s'",
4bf4e7
                      name, udev_device_get_devpath(dev));
4bf4e7
-                link_update(dev->device, name, false);
4bf4e7
+                link_update(dev, name, false);
4bf4e7
         }
4bf4e7
 }
4bf4e7
 
4bf4e7
@@ -432,16 +338,11 @@ void udev_node_add(struct udev_device *dev, bool apply,
4bf4e7
         xsprintf_dev_num_path(filename,
4bf4e7
                               streq(udev_device_get_subsystem(dev), "block") ? "block" : "char",
4bf4e7
                               udev_device_get_devnum(dev));
4bf4e7
-        node_symlink(dev->device, udev_device_get_devnode(dev), filename);
4bf4e7
+        node_symlink(dev, udev_device_get_devnode(dev), filename);
4bf4e7
 
4bf4e7
         /* create/update symlinks, add symlinks to name index */
4bf4e7
-        udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(dev)) {
4bf4e7
-                int r;
4bf4e7
-
4bf4e7
-                r = link_update(dev->device, udev_list_entry_get_name(list_entry), true);
4bf4e7
-                if (r < 0)
4bf4e7
-                        log_info_errno(r, "Failed to update device symlinks: %m");
4bf4e7
-        }
4bf4e7
+        udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(dev))
4bf4e7
+                        link_update(dev, udev_list_entry_get_name(list_entry), true);
4bf4e7
 }
4bf4e7
 
4bf4e7
 void udev_node_remove(struct udev_device *dev) {
4bf4e7
@@ -449,13 +350,8 @@ void udev_node_remove(struct udev_device *dev) {
4bf4e7
         char filename[DEV_NUM_PATH_MAX];
4bf4e7
 
4bf4e7
         /* remove/update symlinks, remove symlinks from name index */
4bf4e7
-        udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(dev)) {
4bf4e7
-                int r;
4bf4e7
-
4bf4e7
-                r = link_update(dev->device, udev_list_entry_get_name(list_entry), false);
4bf4e7
-                if (r < 0)
4bf4e7
-                        log_info_errno(r, "Failed to update device symlinks: %m");
4bf4e7
-        }
4bf4e7
+        udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(dev))
4bf4e7
+                link_update(dev, udev_list_entry_get_name(list_entry), false);
4bf4e7
 
4bf4e7
         /* remove /dev/{block,char}/$major:$minor */
4bf4e7
         xsprintf_dev_num_path(filename,