]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/udev/udev-node.c
Add a helper for /dev/block/major:minor paths
[thirdparty/systemd.git] / src / udev / udev-node.c
1 /* SPDX-License-Identifier: GPL-2.0+ */
2 /*
3 * Copyright (C) 2003-2013 Kay Sievers <kay@vrfy.org>
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #include <errno.h>
20 #include <fcntl.h>
21 #include <stdbool.h>
22 #include <stddef.h>
23 #include <stdio.h>
24 #include <string.h>
25 #include <sys/stat.h>
26 #include <unistd.h>
27
28 #include "device-nodes.h"
29 #include "dirent-util.h"
30 #include "format-util.h"
31 #include "fs-util.h"
32 #include "selinux-util.h"
33 #include "smack-util.h"
34 #include "stdio-util.h"
35 #include "string-util.h"
36 #include "udev.h"
37
38 static int node_symlink(struct udev_device *dev, const char *node, const char *slink) {
39 struct stat stats;
40 char target[UTIL_PATH_SIZE];
41 char *s;
42 size_t l;
43 char slink_tmp[UTIL_PATH_SIZE + 32];
44 int i = 0;
45 int tail = 0;
46 int err = 0;
47
48 /* use relative link */
49 target[0] = '\0';
50 while (node[i] && (node[i] == slink[i])) {
51 if (node[i] == '/')
52 tail = i+1;
53 i++;
54 }
55 s = target;
56 l = sizeof(target);
57 while (slink[i] != '\0') {
58 if (slink[i] == '/')
59 l = strpcpy(&s, l, "../");
60 i++;
61 }
62 l = strscpy(s, l, &node[tail]);
63 if (l == 0) {
64 err = -EINVAL;
65 goto exit;
66 }
67
68 /* preserve link with correct target, do not replace node of other device */
69 if (lstat(slink, &stats) == 0) {
70 if (S_ISBLK(stats.st_mode) || S_ISCHR(stats.st_mode)) {
71 log_error("conflicting device node '%s' found, link to '%s' will not be created", slink, node);
72 goto exit;
73 } else if (S_ISLNK(stats.st_mode)) {
74 char buf[UTIL_PATH_SIZE];
75 int len;
76
77 len = readlink(slink, buf, sizeof(buf));
78 if (len > 0 && len < (int)sizeof(buf)) {
79 buf[len] = '\0';
80 if (streq(target, buf)) {
81 log_debug("preserve already existing symlink '%s' to '%s'", slink, target);
82 label_fix(slink, true, false);
83 utimensat(AT_FDCWD, slink, NULL, AT_SYMLINK_NOFOLLOW);
84 goto exit;
85 }
86 }
87 }
88 } else {
89 log_debug("creating symlink '%s' to '%s'", slink, target);
90 do {
91 err = mkdir_parents_label(slink, 0755);
92 if (!IN_SET(err, 0, -ENOENT))
93 break;
94 mac_selinux_create_file_prepare(slink, S_IFLNK);
95 err = symlink(target, slink);
96 if (err != 0)
97 err = -errno;
98 mac_selinux_create_file_clear();
99 } while (err == -ENOENT);
100 if (err == 0)
101 goto exit;
102 }
103
104 log_debug("atomically replace '%s'", slink);
105 strscpyl(slink_tmp, sizeof(slink_tmp), slink, ".tmp-", udev_device_get_id_filename(dev), NULL);
106 unlink(slink_tmp);
107 do {
108 err = mkdir_parents_label(slink_tmp, 0755);
109 if (!IN_SET(err, 0, -ENOENT))
110 break;
111 mac_selinux_create_file_prepare(slink_tmp, S_IFLNK);
112 err = symlink(target, slink_tmp);
113 if (err != 0)
114 err = -errno;
115 mac_selinux_create_file_clear();
116 } while (err == -ENOENT);
117 if (err != 0) {
118 log_error_errno(errno, "symlink '%s' '%s' failed: %m", target, slink_tmp);
119 goto exit;
120 }
121 err = rename(slink_tmp, slink);
122 if (err != 0) {
123 log_error_errno(errno, "rename '%s' '%s' failed: %m", slink_tmp, slink);
124 unlink(slink_tmp);
125 }
126 exit:
127 return err;
128 }
129
130 /* find device node of device with highest priority */
131 static const char *link_find_prioritized(struct udev_device *dev, bool add, const char *stackdir, char *buf, size_t bufsize) {
132 struct udev *udev = udev_device_get_udev(dev);
133 DIR *dir;
134 struct dirent *dent;
135 int priority = 0;
136 const char *target = NULL;
137
138 if (add) {
139 priority = udev_device_get_devlink_priority(dev);
140 strscpy(buf, bufsize, udev_device_get_devnode(dev));
141 target = buf;
142 }
143
144 dir = opendir(stackdir);
145 if (dir == NULL)
146 return target;
147 FOREACH_DIRENT_ALL(dent, dir, break) {
148 struct udev_device *dev_db;
149
150 if (dent->d_name[0] == '\0')
151 break;
152 if (dent->d_name[0] == '.')
153 continue;
154
155 log_debug("found '%s' claiming '%s'", dent->d_name, stackdir);
156
157 /* did we find ourself? */
158 if (streq(dent->d_name, udev_device_get_id_filename(dev)))
159 continue;
160
161 dev_db = udev_device_new_from_device_id(udev, dent->d_name);
162 if (dev_db != NULL) {
163 const char *devnode;
164
165 devnode = udev_device_get_devnode(dev_db);
166 if (devnode != NULL) {
167 if (target == NULL || udev_device_get_devlink_priority(dev_db) > priority) {
168 log_debug("'%s' claims priority %i for '%s'",
169 udev_device_get_syspath(dev_db), udev_device_get_devlink_priority(dev_db), stackdir);
170 priority = udev_device_get_devlink_priority(dev_db);
171 strscpy(buf, bufsize, devnode);
172 target = buf;
173 }
174 }
175 udev_device_unref(dev_db);
176 }
177 }
178 closedir(dir);
179 return target;
180 }
181
182 /* manage "stack of names" with possibly specified device priorities */
183 static void link_update(struct udev_device *dev, const char *slink, bool add) {
184 char name_enc[UTIL_PATH_SIZE];
185 char filename[UTIL_PATH_SIZE * 2];
186 char dirname[UTIL_PATH_SIZE];
187 const char *target;
188 char buf[UTIL_PATH_SIZE];
189
190 util_path_encode(slink + strlen("/dev"), name_enc, sizeof(name_enc));
191 strscpyl(dirname, sizeof(dirname), "/run/udev/links/", name_enc, NULL);
192 strscpyl(filename, sizeof(filename), dirname, "/", udev_device_get_id_filename(dev), NULL);
193
194 if (!add && unlink(filename) == 0)
195 rmdir(dirname);
196
197 target = link_find_prioritized(dev, add, dirname, buf, sizeof(buf));
198 if (target == NULL) {
199 log_debug("no reference left, remove '%s'", slink);
200 if (unlink(slink) == 0)
201 rmdir_parents(slink, "/");
202 } else {
203 log_debug("creating link '%s' to '%s'", slink, target);
204 node_symlink(dev, target, slink);
205 }
206
207 if (add) {
208 int err;
209
210 do {
211 int fd;
212
213 err = mkdir_parents(filename, 0755);
214 if (!IN_SET(err, 0, -ENOENT))
215 break;
216 fd = open(filename, O_WRONLY|O_CREAT|O_CLOEXEC|O_TRUNC|O_NOFOLLOW, 0444);
217 if (fd >= 0)
218 close(fd);
219 else
220 err = -errno;
221 } while (err == -ENOENT);
222 }
223 }
224
225 void udev_node_update_old_links(struct udev_device *dev, struct udev_device *dev_old) {
226 struct udev_list_entry *list_entry;
227
228 /* update possible left-over symlinks */
229 udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(dev_old)) {
230 const char *name = udev_list_entry_get_name(list_entry);
231 struct udev_list_entry *list_entry_current;
232 int found;
233
234 /* check if old link name still belongs to this device */
235 found = 0;
236 udev_list_entry_foreach(list_entry_current, udev_device_get_devlinks_list_entry(dev)) {
237 const char *name_current = udev_list_entry_get_name(list_entry_current);
238
239 if (streq(name, name_current)) {
240 found = 1;
241 break;
242 }
243 }
244 if (found)
245 continue;
246
247 log_debug("update old name, '%s' no longer belonging to '%s'",
248 name, udev_device_get_devpath(dev));
249 link_update(dev, name, false);
250 }
251 }
252
253 static int node_permissions_apply(struct udev_device *dev, bool apply,
254 mode_t mode, uid_t uid, gid_t gid,
255 struct udev_list *seclabel_list) {
256 const char *devnode = udev_device_get_devnode(dev);
257 dev_t devnum = udev_device_get_devnum(dev);
258 struct stat stats;
259 struct udev_list_entry *entry;
260 int err = 0;
261
262 if (streq(udev_device_get_subsystem(dev), "block"))
263 mode |= S_IFBLK;
264 else
265 mode |= S_IFCHR;
266
267 if (lstat(devnode, &stats) != 0) {
268 err = log_debug_errno(errno, "can not stat() node '%s' (%m)", devnode);
269 goto out;
270 }
271
272 if (((stats.st_mode & S_IFMT) != (mode & S_IFMT)) || (stats.st_rdev != devnum)) {
273 err = -EEXIST;
274 log_debug("found node '%s' with non-matching devnum %s, skip handling",
275 udev_device_get_devnode(dev), udev_device_get_id_filename(dev));
276 goto out;
277 }
278
279 if (apply) {
280 bool selinux = false;
281 bool smack = false;
282
283 if ((stats.st_mode & 0777) != (mode & 0777) || stats.st_uid != uid || stats.st_gid != gid) {
284 log_debug("set permissions %s, %#o, uid=%u, gid=%u", devnode, mode, uid, gid);
285 err = chmod(devnode, mode);
286 if (err < 0)
287 log_warning_errno(errno, "setting mode of %s to %#o failed: %m", devnode, mode);
288 err = chown(devnode, uid, gid);
289 if (err < 0)
290 log_warning_errno(errno, "setting owner of %s to uid=%u, gid=%u failed: %m", devnode, uid, gid);
291 } else {
292 log_debug("preserve permissions %s, %#o, uid=%u, gid=%u", devnode, mode, uid, gid);
293 }
294
295 /* apply SECLABEL{$module}=$label */
296 udev_list_entry_foreach(entry, udev_list_get_entry(seclabel_list)) {
297 const char *name, *label;
298 int r;
299
300 name = udev_list_entry_get_name(entry);
301 label = udev_list_entry_get_value(entry);
302
303 if (streq(name, "selinux")) {
304 selinux = true;
305
306 r = mac_selinux_apply(devnode, label);
307 if (r < 0)
308 log_error_errno(r, "SECLABEL: failed to set SELinux label '%s': %m", label);
309 else
310 log_debug("SECLABEL: set SELinux label '%s'", label);
311
312 } else if (streq(name, "smack")) {
313 smack = true;
314
315 r = mac_smack_apply(devnode, SMACK_ATTR_ACCESS, label);
316 if (r < 0)
317 log_error_errno(r, "SECLABEL: failed to set SMACK label '%s': %m", label);
318 else
319 log_debug("SECLABEL: set SMACK label '%s'", label);
320
321 } else
322 log_error("SECLABEL: unknown subsystem, ignoring '%s'='%s'", name, label);
323 }
324
325 /* set the defaults */
326 if (!selinux)
327 mac_selinux_fix(devnode, true, false);
328 if (!smack)
329 mac_smack_apply(devnode, SMACK_ATTR_ACCESS, NULL);
330 }
331
332 /* always update timestamp when we re-use the node, like on media change events */
333 utimensat(AT_FDCWD, devnode, NULL, 0);
334 out:
335 return err;
336 }
337
338 void udev_node_add(struct udev_device *dev, bool apply,
339 mode_t mode, uid_t uid, gid_t gid,
340 struct udev_list *seclabel_list) {
341 char filename[DEV_NUM_PATH_MAX];
342 struct udev_list_entry *list_entry;
343
344 log_debug("handling device node '%s', devnum=%s, mode=%#o, uid="UID_FMT", gid="GID_FMT,
345 udev_device_get_devnode(dev), udev_device_get_id_filename(dev), mode, uid, gid);
346
347 if (node_permissions_apply(dev, apply, mode, uid, gid, seclabel_list) < 0)
348 return;
349
350 /* always add /dev/{block,char}/$major:$minor */
351 xsprintf_dev_num_path(filename,
352 streq(udev_device_get_subsystem(dev), "block") ? "block" : "char",
353 udev_device_get_devnum(dev));
354 node_symlink(dev, udev_device_get_devnode(dev), filename);
355
356 /* create/update symlinks, add symlinks to name index */
357 udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(dev))
358 link_update(dev, udev_list_entry_get_name(list_entry), true);
359 }
360
361 void udev_node_remove(struct udev_device *dev) {
362 struct udev_list_entry *list_entry;
363 char filename[DEV_NUM_PATH_MAX];
364
365 /* remove/update symlinks, remove symlinks from name index */
366 udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(dev))
367 link_update(dev, udev_list_entry_get_name(list_entry), false);
368
369 /* remove /dev/{block,char}/$major:$minor */
370 xsprintf_dev_num_path(filename,
371 streq(udev_device_get_subsystem(dev), "block") ? "block" : "char",
372 udev_device_get_devnum(dev));
373 unlink(filename);
374 }