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/>.
29 #include "smack-util.h"
30 #include "selinux-util.h"
32 static int node_symlink(struct udev_device *dev, const char *node, const char *slink) {
34 char target[UTIL_PATH_SIZE];
37 char slink_tmp[UTIL_PATH_SIZE + 32];
42 /* use relative link */
44 while (node[i] && (node[i] == slink[i])) {
51 while (slink[i] != '\0') {
53 l = strpcpy(&s, l, "../");
56 l = strscpy(s, l, &node[tail]);
62 /* preserve link with correct target, do not replace node of other device */
63 if (lstat(slink, &stats) == 0) {
64 if (S_ISBLK(stats.st_mode) || S_ISCHR(stats.st_mode)) {
65 log_error("conflicting device node '%s' found, link to '%s' will not be created", slink, node);
67 } else if (S_ISLNK(stats.st_mode)) {
68 char buf[UTIL_PATH_SIZE];
71 len = readlink(slink, buf, sizeof(buf));
72 if (len > 0 && len < (int)sizeof(buf)) {
74 if (streq(target, buf)) {
75 log_debug("preserve already existing symlink '%s' to '%s'", slink, target);
76 label_fix(slink, true, false);
77 utimensat(AT_FDCWD, slink, NULL, AT_SYMLINK_NOFOLLOW);
83 log_debug("creating symlink '%s' to '%s'", slink, target);
85 err = mkdir_parents_label(slink, 0755);
86 if (err != 0 && err != -ENOENT)
88 mac_selinux_create_file_prepare(slink, S_IFLNK);
89 err = symlink(target, slink);
92 mac_selinux_create_file_clear();
93 } while (err == -ENOENT);
98 log_debug("atomically replace '%s'", slink);
99 strscpyl(slink_tmp, sizeof(slink_tmp), slink, ".tmp-", udev_device_get_id_filename(dev), NULL);
102 err = mkdir_parents_label(slink_tmp, 0755);
103 if (err != 0 && err != -ENOENT)
105 mac_selinux_create_file_prepare(slink_tmp, S_IFLNK);
106 err = symlink(target, slink_tmp);
109 mac_selinux_create_file_clear();
110 } while (err == -ENOENT);
112 log_error_errno(errno, "symlink '%s' '%s' failed: %m", target, slink_tmp);
115 err = rename(slink_tmp, slink);
117 log_error_errno(errno, "rename '%s' '%s' failed: %m", slink_tmp, slink);
124 /* find device node of device with highest priority */
125 static const char *link_find_prioritized(struct udev_device *dev, bool add, const char *stackdir, char *buf, size_t bufsize) {
126 struct udev *udev = udev_device_get_udev(dev);
129 const char *target = NULL;
132 priority = udev_device_get_devlink_priority(dev);
133 strscpy(buf, bufsize, udev_device_get_devnode(dev));
137 dir = opendir(stackdir);
141 struct udev_device *dev_db;
145 if (dent == NULL || dent->d_name[0] == '\0')
147 if (dent->d_name[0] == '.')
150 log_debug("found '%s' claiming '%s'", dent->d_name, stackdir);
152 /* did we find ourself? */
153 if (streq(dent->d_name, udev_device_get_id_filename(dev)))
156 dev_db = udev_device_new_from_device_id(udev, dent->d_name);
157 if (dev_db != NULL) {
160 devnode = udev_device_get_devnode(dev_db);
161 if (devnode != NULL) {
162 if (target == NULL || udev_device_get_devlink_priority(dev_db) > priority) {
163 log_debug("'%s' claims priority %i for '%s'",
164 udev_device_get_syspath(dev_db), udev_device_get_devlink_priority(dev_db), stackdir);
165 priority = udev_device_get_devlink_priority(dev_db);
166 strscpy(buf, bufsize, devnode);
170 udev_device_unref(dev_db);
177 /* manage "stack of names" with possibly specified device priorities */
178 static void link_update(struct udev_device *dev, const char *slink, bool add) {
179 char name_enc[UTIL_PATH_SIZE];
180 char filename[UTIL_PATH_SIZE * 2];
181 char dirname[UTIL_PATH_SIZE];
183 char buf[UTIL_PATH_SIZE];
185 util_path_encode(slink + strlen("/dev"), name_enc, sizeof(name_enc));
186 strscpyl(dirname, sizeof(dirname), "/run/udev/links/", name_enc, NULL);
187 strscpyl(filename, sizeof(filename), dirname, "/", udev_device_get_id_filename(dev), NULL);
189 if (!add && unlink(filename) == 0)
192 target = link_find_prioritized(dev, add, dirname, buf, sizeof(buf));
193 if (target == NULL) {
194 log_debug("no reference left, remove '%s'", slink);
195 if (unlink(slink) == 0)
196 rmdir_parents(slink, "/");
198 log_debug("creating link '%s' to '%s'", slink, target);
199 node_symlink(dev, target, slink);
208 err = mkdir_parents(filename, 0755);
209 if (err != 0 && err != -ENOENT)
211 fd = open(filename, O_WRONLY|O_CREAT|O_CLOEXEC|O_TRUNC|O_NOFOLLOW, 0444);
216 } while (err == -ENOENT);
220 void udev_node_update_old_links(struct udev_device *dev, struct udev_device *dev_old) {
221 struct udev_list_entry *list_entry;
223 /* update possible left-over symlinks */
224 udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(dev_old)) {
225 const char *name = udev_list_entry_get_name(list_entry);
226 struct udev_list_entry *list_entry_current;
229 /* check if old link name still belongs to this device */
231 udev_list_entry_foreach(list_entry_current, udev_device_get_devlinks_list_entry(dev)) {
232 const char *name_current = udev_list_entry_get_name(list_entry_current);
234 if (streq(name, name_current)) {
242 log_debug("update old name, '%s' no longer belonging to '%s'",
243 name, udev_device_get_devpath(dev));
244 link_update(dev, name, false);
248 static int node_permissions_apply(struct udev_device *dev, bool apply,
249 mode_t mode, uid_t uid, gid_t gid,
250 struct udev_list *seclabel_list) {
251 const char *devnode = udev_device_get_devnode(dev);
252 dev_t devnum = udev_device_get_devnum(dev);
254 struct udev_list_entry *entry;
257 if (streq(udev_device_get_subsystem(dev), "block"))
262 if (lstat(devnode, &stats) != 0) {
264 log_debug_errno(errno, "can not stat() node '%s' (%m)", devnode);
268 if (((stats.st_mode & S_IFMT) != (mode & S_IFMT)) || (stats.st_rdev != devnum)) {
270 log_debug("found node '%s' with non-matching devnum %s, skip handling",
271 udev_device_get_devnode(dev), udev_device_get_id_filename(dev));
276 bool selinux = false;
279 if ((stats.st_mode & 0777) != (mode & 0777) || stats.st_uid != uid || stats.st_gid != gid) {
280 log_debug("set permissions %s, %#o, uid=%u, gid=%u", devnode, mode, uid, gid);
281 err = chmod(devnode, mode);
283 log_warning_errno(errno, "setting mode of %s to %#o failed: %m", devnode, mode);
284 err = chown(devnode, uid, gid);
286 log_warning_errno(errno, "setting owner of %s to uid=%u, gid=%u failed: %m", devnode, uid, gid);
288 log_debug("preserve permissions %s, %#o, uid=%u, gid=%u", devnode, mode, uid, gid);
291 /* apply SECLABEL{$module}=$label */
292 udev_list_entry_foreach(entry, udev_list_get_entry(seclabel_list)) {
293 const char *name, *label;
296 name = udev_list_entry_get_name(entry);
297 label = udev_list_entry_get_value(entry);
299 if (streq(name, "selinux")) {
302 r = mac_selinux_apply(devnode, label);
304 log_error_errno(r, "SECLABEL: failed to set SELinux label '%s': %m", label);
306 log_debug("SECLABEL: set SELinux label '%s'", label);
308 } else if (streq(name, "smack")) {
311 r = mac_smack_apply(devnode, label);
313 log_error_errno(r, "SECLABEL: failed to set SMACK label '%s': %m", label);
315 log_debug("SECLABEL: set SMACK label '%s'", label);
318 log_error("SECLABEL: unknown subsystem, ignoring '%s'='%s'", name, label);
321 /* set the defaults */
323 mac_selinux_fix(devnode, true, false);
325 mac_smack_apply(devnode, NULL);
328 /* always update timestamp when we re-use the node, like on media change events */
329 utimensat(AT_FDCWD, devnode, NULL, 0);
334 void udev_node_add(struct udev_device *dev, bool apply,
335 mode_t mode, uid_t uid, gid_t gid,
336 struct udev_list *seclabel_list) {
337 char filename[UTIL_PATH_SIZE];
338 struct udev_list_entry *list_entry;
340 log_debug("handling device node '%s', devnum=%s, mode=%#o, uid="UID_FMT", gid="GID_FMT,
341 udev_device_get_devnode(dev), udev_device_get_id_filename(dev), mode, uid, gid);
343 if (node_permissions_apply(dev, apply, mode, uid, gid, seclabel_list) < 0)
346 /* always add /dev/{block,char}/$major:$minor */
347 snprintf(filename, sizeof(filename), "/dev/%s/%u:%u",
348 streq(udev_device_get_subsystem(dev), "block") ? "block" : "char",
349 major(udev_device_get_devnum(dev)), minor(udev_device_get_devnum(dev)));
350 node_symlink(dev, udev_device_get_devnode(dev), filename);
352 /* create/update symlinks, add symlinks to name index */
353 udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(dev))
354 link_update(dev, udev_list_entry_get_name(list_entry), true);
357 void udev_node_remove(struct udev_device *dev) {
358 struct udev_list_entry *list_entry;
359 char filename[UTIL_PATH_SIZE];
361 /* remove/update symlinks, remove symlinks from name index */
362 udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(dev))
363 link_update(dev, udev_list_entry_get_name(list_entry), false);
365 /* remove /dev/{block,char}/$major:$minor */
366 snprintf(filename, sizeof(filename), "/dev/%s/%u:%u",
367 streq(udev_device_get_subsystem(dev), "block") ? "block" : "char",
368 major(udev_device_get_devnum(dev)), minor(udev_device_get_devnum(dev)));