2 * Copyright (C) 2003-2013 Kay Sievers <kay@vrfy.org>
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
30 #include <sys/types.h>
32 #include <attr/xattr.h>
35 #include "smack-util.h"
38 static int node_symlink(struct udev_device *dev, const char *node, const char *slink)
41 char target[UTIL_PATH_SIZE];
44 char slink_tmp[UTIL_PATH_SIZE + 32];
49 /* use relative link */
51 while (node[i] && (node[i] == slink[i])) {
58 while (slink[i] != '\0') {
60 l = strpcpy(&s, l, "../");
63 l = strscpy(s, l, &node[tail]);
69 /* preserve link with correct target, do not replace node of other device */
70 if (lstat(slink, &stats) == 0) {
71 if (S_ISBLK(stats.st_mode) || S_ISCHR(stats.st_mode)) {
72 log_error("conflicting device node '%s' found, link to '%s' will not be created\n", slink, node);
74 } else if (S_ISLNK(stats.st_mode)) {
75 char buf[UTIL_PATH_SIZE];
78 len = readlink(slink, buf, sizeof(buf));
79 if (len > 0 && len < (int)sizeof(buf)) {
81 if (streq(target, buf)) {
82 log_debug("preserve already existing symlink '%s' to '%s'\n", slink, target);
83 label_fix(slink, true, false);
84 utimensat(AT_FDCWD, slink, NULL, AT_SYMLINK_NOFOLLOW);
90 log_debug("creating symlink '%s' to '%s'\n", slink, target);
92 err = mkdir_parents_label(slink, 0755);
93 if (err != 0 && err != -ENOENT)
95 label_context_set(slink, S_IFLNK);
96 err = symlink(target, slink);
99 label_context_clear();
100 } while (err == -ENOENT);
105 log_debug("atomically replace '%s'\n", slink);
106 strscpyl(slink_tmp, sizeof(slink_tmp), slink, ".tmp-", udev_device_get_id_filename(dev), NULL);
109 err = mkdir_parents_label(slink_tmp, 0755);
110 if (err != 0 && err != -ENOENT)
112 label_context_set(slink_tmp, S_IFLNK);
113 err = symlink(target, slink_tmp);
116 label_context_clear();
117 } while (err == -ENOENT);
119 log_error("symlink '%s' '%s' failed: %m\n", target, slink_tmp);
122 err = rename(slink_tmp, slink);
124 log_error("rename '%s' '%s' failed: %m\n", slink_tmp, slink);
131 /* find device node of device with highest priority */
132 static const char *link_find_prioritized(struct udev_device *dev, bool add, const char *stackdir, char *buf, size_t bufsize)
134 struct udev *udev = udev_device_get_udev(dev);
137 const char *target = NULL;
140 priority = udev_device_get_devlink_priority(dev);
141 strscpy(buf, bufsize, udev_device_get_devnode(dev));
145 dir = opendir(stackdir);
149 struct udev_device *dev_db;
153 if (dent == NULL || dent->d_name[0] == '\0')
155 if (dent->d_name[0] == '.')
158 log_debug("found '%s' claiming '%s'\n", dent->d_name, stackdir);
160 /* did we find ourself? */
161 if (streq(dent->d_name, udev_device_get_id_filename(dev)))
164 dev_db = udev_device_new_from_device_id(udev, dent->d_name);
165 if (dev_db != NULL) {
168 devnode = udev_device_get_devnode(dev_db);
169 if (devnode != NULL) {
170 if (target == NULL || udev_device_get_devlink_priority(dev_db) > priority) {
171 log_debug("'%s' claims priority %i for '%s'\n",
172 udev_device_get_syspath(dev_db), udev_device_get_devlink_priority(dev_db), stackdir);
173 priority = udev_device_get_devlink_priority(dev_db);
174 strscpy(buf, bufsize, devnode);
178 udev_device_unref(dev_db);
185 /* manage "stack of names" with possibly specified device priorities */
186 static void link_update(struct udev_device *dev, const char *slink, bool add)
188 struct udev *udev = udev_device_get_udev(dev);
189 char name_enc[UTIL_PATH_SIZE];
190 char filename[UTIL_PATH_SIZE * 2];
191 char dirname[UTIL_PATH_SIZE];
193 char buf[UTIL_PATH_SIZE];
195 util_path_encode(slink + strlen("/dev"), name_enc, sizeof(name_enc));
196 strscpyl(dirname, sizeof(dirname), "/run/udev/links/", name_enc, NULL);
197 strscpyl(filename, sizeof(filename), dirname, "/", udev_device_get_id_filename(dev), NULL);
199 if (!add && unlink(filename) == 0)
202 target = link_find_prioritized(dev, add, dirname, buf, sizeof(buf));
203 if (target == NULL) {
204 log_debug("no reference left, remove '%s'\n", slink);
205 if (unlink(slink) == 0)
206 util_delete_path(udev, slink);
208 log_debug("creating link '%s' to '%s'\n", slink, target);
209 node_symlink(dev, target, slink);
218 err = mkdir_parents(filename, 0755);
219 if (err != 0 && err != -ENOENT)
221 fd = open(filename, O_WRONLY|O_CREAT|O_CLOEXEC|O_TRUNC|O_NOFOLLOW, 0444);
226 } while (err == -ENOENT);
230 void udev_node_update_old_links(struct udev_device *dev, struct udev_device *dev_old)
232 struct udev_list_entry *list_entry;
234 /* update possible left-over symlinks */
235 udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(dev_old)) {
236 const char *name = udev_list_entry_get_name(list_entry);
237 struct udev_list_entry *list_entry_current;
240 /* check if old link name still belongs to this device */
242 udev_list_entry_foreach(list_entry_current, udev_device_get_devlinks_list_entry(dev)) {
243 const char *name_current = udev_list_entry_get_name(list_entry_current);
245 if (streq(name, name_current)) {
253 log_debug("update old name, '%s' no longer belonging to '%s'\n",
254 name, udev_device_get_devpath(dev));
255 link_update(dev, name, false);
259 static int node_permissions_apply(struct udev_device *dev, bool apply,
260 mode_t mode, uid_t uid, gid_t gid,
261 struct udev_list *seclabel_list) {
262 const char *devnode = udev_device_get_devnode(dev);
263 dev_t devnum = udev_device_get_devnum(dev);
265 struct udev_list_entry *entry;
268 if (streq(udev_device_get_subsystem(dev), "block"))
273 if (lstat(devnode, &stats) != 0) {
275 log_debug("can not stat() node '%s' (%m)\n", devnode);
279 if (((stats.st_mode & S_IFMT) != (mode & S_IFMT)) || (stats.st_rdev != devnum)) {
281 log_debug("found node '%s' with non-matching devnum %s, skip handling\n",
282 udev_device_get_devnode(dev), udev_device_get_id_filename(dev));
287 bool selinux = false;
292 if ((stats.st_mode & 0777) != (mode & 0777) || stats.st_uid != uid || stats.st_gid != gid) {
293 log_debug("set permissions %s, %#o, uid=%u, gid=%u\n", devnode, mode, uid, gid);
294 chmod(devnode, mode);
295 chown(devnode, uid, gid);
297 log_debug("preserve permissions %s, %#o, uid=%u, gid=%u\n", devnode, mode, uid, gid);
300 /* apply SECLABEL{$module}=$label */
301 udev_list_entry_foreach(entry, udev_list_get_entry(seclabel_list)) {
302 const char *name, *label;
304 name = udev_list_entry_get_name(entry);
305 label = udev_list_entry_get_value(entry);
307 if (streq(name, "selinux")) {
309 if (label_apply(devnode, label) < 0)
310 log_error("SECLABEL: failed to set SELinux label '%s'", label);
312 log_debug("SECLABEL: set SELinux label '%s'", label);
315 } else if (streq(name, "smack") && use_smack()) {
317 if (lsetxattr(devnode, "security.SMACK64", label, strlen(label), 0) < 0)
318 log_error("SECLABEL: failed to set SMACK label '%s'", label);
320 log_debug("SECLABEL: set SMACK label '%s'", label);
324 log_error("SECLABEL: unknown subsystem, ignoring '%s'='%s'", name, label);
327 /* set the defaults */
329 label_fix(devnode, true, false);
331 if (!smack && use_smack())
332 lremovexattr(devnode, "security.SMACK64");
336 /* always update timestamp when we re-use the node, like on media change events */
337 utimensat(AT_FDCWD, devnode, NULL, 0);
342 void udev_node_add(struct udev_device *dev, bool apply,
343 mode_t mode, uid_t uid, gid_t gid,
344 struct udev_list *seclabel_list) {
345 char filename[UTIL_PATH_SIZE];
346 struct udev_list_entry *list_entry;
348 log_debug("handling device node '%s', devnum=%s, mode=%#o, uid=%d, gid=%d\n",
349 udev_device_get_devnode(dev), udev_device_get_id_filename(dev), mode, uid, gid);
351 if (node_permissions_apply(dev, apply, mode, uid, gid, seclabel_list) < 0)
354 /* always add /dev/{block,char}/$major:$minor */
355 snprintf(filename, sizeof(filename), "/dev/%s/%u:%u",
356 streq(udev_device_get_subsystem(dev), "block") ? "block" : "char",
357 major(udev_device_get_devnum(dev)), minor(udev_device_get_devnum(dev)));
358 node_symlink(dev, udev_device_get_devnode(dev), filename);
360 /* create/update symlinks, add symlinks to name index */
361 udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(dev))
362 link_update(dev, udev_list_entry_get_name(list_entry), true);
365 void udev_node_remove(struct udev_device *dev)
367 struct udev_list_entry *list_entry;
368 char filename[UTIL_PATH_SIZE];
370 /* remove/update symlinks, remove symlinks from name index */
371 udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(dev))
372 link_update(dev, udev_list_entry_get_name(list_entry), false);
374 /* remove /dev/{block,char}/$major:$minor */
375 snprintf(filename, sizeof(filename), "/dev/%s/%u:%u",
376 streq(udev_device_get_subsystem(dev), "block") ? "block" : "char",
377 major(udev_device_get_devnum(dev)), minor(udev_device_get_devnum(dev)));