daandemeyer / rpms / systemd

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