]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/udev/udev-node.c
security: rework selinux, smack, ima, apparmor detection logic
[thirdparty/systemd.git] / src / udev / udev-node.c
CommitLineData
ea733a2f 1/*
22582bb2 2 * Copyright (C) 2003-2013 Kay Sievers <kay@vrfy.org>
ea733a2f 3 *
55e9959b
KS
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.
ea733a2f 8 *
55e9959b
KS
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.
13 *
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/>.
ea733a2f
GKH
16 */
17
18#include <stdlib.h>
19#include <string.h>
20#include <stdio.h>
1aa1e248 21#include <stddef.h>
6c29f2b9 22#include <stdbool.h>
ea733a2f
GKH
23#include <fcntl.h>
24#include <unistd.h>
25#include <errno.h>
67f69ae1 26#include <grp.h>
24f0605c 27#include <dirent.h>
56073914 28#include <sys/time.h>
32ff5bca 29#include <sys/stat.h>
10950dfe 30#include <sys/types.h>
c26547d6
KS
31#ifdef HAVE_XATTR
32#include <attr/xattr.h>
33#endif
ea733a2f 34
d682b3a7 35#include "smack-util.h"
ea733a2f 36#include "udev.h"
9825617b 37
38d70045 38static int node_symlink(struct udev_device *dev, const char *node, const char *slink)
fa33d857 39{
912541b0
KS
40 struct stat stats;
41 char target[UTIL_PATH_SIZE];
42 char *s;
43 size_t l;
38d70045 44 char slink_tmp[UTIL_PATH_SIZE + 32];
912541b0
KS
45 int i = 0;
46 int tail = 0;
47 int err = 0;
48
49 /* use relative link */
50 target[0] = '\0';
51 while (node[i] && (node[i] == slink[i])) {
52 if (node[i] == '/')
53 tail = i+1;
54 i++;
55 }
56 s = target;
57 l = sizeof(target);
58 while (slink[i] != '\0') {
59 if (slink[i] == '/')
d5a89d7d 60 l = strpcpy(&s, l, "../");
912541b0
KS
61 i++;
62 }
d5a89d7d 63 l = strscpy(s, l, &node[tail]);
912541b0
KS
64 if (l == 0) {
65 err = -EINVAL;
66 goto exit;
67 }
68
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)) {
2be7287b
KS
72 log_error("conflicting device node '%s' found, link to '%s' will not be created\n", slink, node);
73 goto exit;
912541b0
KS
74 } else if (S_ISLNK(stats.st_mode)) {
75 char buf[UTIL_PATH_SIZE];
76 int len;
77
912541b0
KS
78 len = readlink(slink, buf, sizeof(buf));
79 if (len > 0 && len < (int)sizeof(buf)) {
80 buf[len] = '\0';
090be865 81 if (streq(target, buf)) {
baa30fbc 82 log_debug("preserve already existing symlink '%s' to '%s'\n", slink, target);
c9bc0764 83 label_fix(slink, true, false);
912541b0
KS
84 utimensat(AT_FDCWD, slink, NULL, AT_SYMLINK_NOFOLLOW);
85 goto exit;
86 }
87 }
88 }
89 } else {
baa30fbc 90 log_debug("creating symlink '%s' to '%s'\n", slink, target);
912541b0 91 do {
d2e54fae 92 err = mkdir_parents_label(slink, 0755);
912541b0
KS
93 if (err != 0 && err != -ENOENT)
94 break;
e9a5ef7c 95 label_context_set(slink, S_IFLNK);
912541b0
KS
96 err = symlink(target, slink);
97 if (err != 0)
98 err = -errno;
e9a5ef7c 99 label_context_clear();
912541b0
KS
100 } while (err == -ENOENT);
101 if (err == 0)
102 goto exit;
103 }
104
baa30fbc 105 log_debug("atomically replace '%s'\n", slink);
38d70045 106 strscpyl(slink_tmp, sizeof(slink_tmp), slink, ".tmp-", udev_device_get_id_filename(dev), NULL);
912541b0
KS
107 unlink(slink_tmp);
108 do {
d2e54fae 109 err = mkdir_parents_label(slink_tmp, 0755);
912541b0
KS
110 if (err != 0 && err != -ENOENT)
111 break;
e9a5ef7c 112 label_context_set(slink_tmp, S_IFLNK);
912541b0
KS
113 err = symlink(target, slink_tmp);
114 if (err != 0)
115 err = -errno;
e9a5ef7c 116 label_context_clear();
912541b0
KS
117 } while (err == -ENOENT);
118 if (err != 0) {
baa30fbc 119 log_error("symlink '%s' '%s' failed: %m\n", target, slink_tmp);
912541b0
KS
120 goto exit;
121 }
122 err = rename(slink_tmp, slink);
123 if (err != 0) {
baa30fbc 124 log_error("rename '%s' '%s' failed: %m\n", slink_tmp, slink);
912541b0
KS
125 unlink(slink_tmp);
126 }
fa33d857 127exit:
912541b0 128 return err;
fa33d857
KS
129}
130
6c29f2b9
KS
131/* find device node of device with highest priority */
132static const char *link_find_prioritized(struct udev_device *dev, bool add, const char *stackdir, char *buf, size_t bufsize)
24f0605c 133{
912541b0
KS
134 struct udev *udev = udev_device_get_udev(dev);
135 DIR *dir;
136 int priority = 0;
137 const char *target = NULL;
138
139 if (add) {
140 priority = udev_device_get_devlink_priority(dev);
d5a89d7d 141 strscpy(buf, bufsize, udev_device_get_devnode(dev));
912541b0
KS
142 target = buf;
143 }
144
145 dir = opendir(stackdir);
146 if (dir == NULL)
147 return target;
148 for (;;) {
149 struct udev_device *dev_db;
150 struct dirent *dent;
151
152 dent = readdir(dir);
153 if (dent == NULL || dent->d_name[0] == '\0')
154 break;
155 if (dent->d_name[0] == '.')
156 continue;
157
baa30fbc 158 log_debug("found '%s' claiming '%s'\n", dent->d_name, stackdir);
912541b0
KS
159
160 /* did we find ourself? */
090be865 161 if (streq(dent->d_name, udev_device_get_id_filename(dev)))
912541b0
KS
162 continue;
163
dbf61afb 164 dev_db = udev_device_new_from_device_id(udev, dent->d_name);
912541b0
KS
165 if (dev_db != NULL) {
166 const char *devnode;
167
168 devnode = udev_device_get_devnode(dev_db);
169 if (devnode != NULL) {
912541b0 170 if (target == NULL || udev_device_get_devlink_priority(dev_db) > priority) {
baa30fbc
KS
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);
912541b0 173 priority = udev_device_get_devlink_priority(dev_db);
d5a89d7d 174 strscpy(buf, bufsize, devnode);
912541b0
KS
175 target = buf;
176 }
177 }
178 udev_device_unref(dev_db);
179 }
180 }
181 closedir(dir);
182 return target;
aa8734ff
KS
183}
184
6c29f2b9
KS
185/* manage "stack of names" with possibly specified device priorities */
186static void link_update(struct udev_device *dev, const char *slink, bool add)
aa8734ff 187{
912541b0
KS
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];
192 const char *target;
193 char buf[UTIL_PATH_SIZE];
194
4cb72937 195 util_path_encode(slink + strlen("/dev"), name_enc, sizeof(name_enc));
d5a89d7d
KS
196 strscpyl(dirname, sizeof(dirname), "/run/udev/links/", name_enc, NULL);
197 strscpyl(filename, sizeof(filename), dirname, "/", udev_device_get_id_filename(dev), NULL);
912541b0 198
baa30fbc
KS
199 if (!add && unlink(filename) == 0)
200 rmdir(dirname);
912541b0
KS
201
202 target = link_find_prioritized(dev, add, dirname, buf, sizeof(buf));
203 if (target == NULL) {
baa30fbc 204 log_debug("no reference left, remove '%s'\n", slink);
912541b0
KS
205 if (unlink(slink) == 0)
206 util_delete_path(udev, slink);
207 } else {
baa30fbc 208 log_debug("creating link '%s' to '%s'\n", slink, target);
38d70045 209 node_symlink(dev, target, slink);
912541b0
KS
210 }
211
212 if (add) {
213 int err;
214
912541b0
KS
215 do {
216 int fd;
217
3cbd5f6b 218 err = mkdir_parents(filename, 0755);
912541b0
KS
219 if (err != 0 && err != -ENOENT)
220 break;
221 fd = open(filename, O_WRONLY|O_CREAT|O_CLOEXEC|O_TRUNC|O_NOFOLLOW, 0444);
222 if (fd >= 0)
223 close(fd);
224 else
225 err = -errno;
226 } while (err == -ENOENT);
227 }
24f0605c
KS
228}
229
ec2dd02e 230void udev_node_update_old_links(struct udev_device *dev, struct udev_device *dev_old)
24f0605c 231{
912541b0
KS
232 struct udev_list_entry *list_entry;
233
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;
238 int found;
239
240 /* check if old link name still belongs to this device */
241 found = 0;
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);
244
090be865 245 if (streq(name, name_current)) {
912541b0
KS
246 found = 1;
247 break;
248 }
249 }
250 if (found)
251 continue;
252
baa30fbc 253 log_debug("update old name, '%s' no longer belonging to '%s'\n",
912541b0 254 name, udev_device_get_devpath(dev));
8a173387 255 link_update(dev, name, false);
912541b0 256 }
24f0605c
KS
257}
258
c26547d6
KS
259static 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) {
912541b0
KS
262 const char *devnode = udev_device_get_devnode(dev);
263 dev_t devnum = udev_device_get_devnum(dev);
264 struct stat stats;
c26547d6 265 struct udev_list_entry *entry;
912541b0
KS
266 int err = 0;
267
090be865 268 if (streq(udev_device_get_subsystem(dev), "block"))
912541b0
KS
269 mode |= S_IFBLK;
270 else
271 mode |= S_IFCHR;
272
273 if (lstat(devnode, &stats) != 0) {
274 err = -errno;
baa30fbc 275 log_debug("can not stat() node '%s' (%m)\n", devnode);
912541b0
KS
276 goto out;
277 }
278
279 if (((stats.st_mode & S_IFMT) != (mode & S_IFMT)) || (stats.st_rdev != devnum)) {
280 err = -EEXIST;
baa30fbc
KS
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));
912541b0
KS
283 goto out;
284 }
285
22582bb2 286 if (apply) {
b7e2b764
KS
287 bool selinux = false;
288#ifdef HAVE_SMACK
289 bool smack = false;
290#endif
291
48a849ee
KS
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);
296 } else {
297 log_debug("preserve permissions %s, %#o, uid=%u, gid=%u\n", devnode, mode, uid, gid);
298 }
c26547d6 299
c26547d6
KS
300 /* apply SECLABEL{$module}=$label */
301 udev_list_entry_foreach(entry, udev_list_get_entry(seclabel_list)) {
302 const char *name, *label;
303
304 name = udev_list_entry_get_name(entry);
305 label = udev_list_entry_get_value(entry);
306
307 if (streq(name, "selinux")) {
b7e2b764 308 selinux = true;
463b5dbb
KS
309 if (label_apply(devnode, label) < 0)
310 log_error("SECLABEL: failed to set SELinux label '%s'", label);
311 else
312 log_debug("SECLABEL: set SELinux label '%s'", label);
c26547d6 313
b7e2b764 314#ifdef HAVE_SMACK
d682b3a7 315 } else if (streq(name, "smack") && use_smack()) {
b7e2b764 316 smack = true;
c26547d6
KS
317 if (lsetxattr(devnode, "security.SMACK64", label, strlen(label), 0) < 0)
318 log_error("SECLABEL: failed to set SMACK label '%s'", label);
319 else
320 log_debug("SECLABEL: set SMACK label '%s'", label);
321#endif
322
323 } else
324 log_error("SECLABEL: unknown subsystem, ignoring '%s'='%s'", name, label);
325 }
b7e2b764
KS
326
327 /* set the defaults */
328 if (!selinux)
329 label_fix(devnode, true, false);
330#ifdef HAVE_SMACK
d682b3a7 331 if (!smack && use_smack())
b7e2b764
KS
332 lremovexattr(devnode, "security.SMACK64");
333#endif
48a849ee 334 }
912541b0
KS
335
336 /* always update timestamp when we re-use the node, like on media change events */
337 utimensat(AT_FDCWD, devnode, NULL, 0);
220893b3 338out:
912541b0 339 return err;
220893b3
KS
340}
341
c26547d6
KS
342void 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) {
912541b0
KS
345 char filename[UTIL_PATH_SIZE];
346 struct udev_list_entry *list_entry;
912541b0 347
baa30fbc
KS
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);
912541b0 350
c26547d6 351 if (node_permissions_apply(dev, apply, mode, uid, gid, seclabel_list) < 0)
e7f32890 352 return;
912541b0
KS
353
354 /* always add /dev/{block,char}/$major:$minor */
4cb72937 355 snprintf(filename, sizeof(filename), "/dev/%s/%u:%u",
090be865 356 streq(udev_device_get_subsystem(dev), "block") ? "block" : "char",
912541b0 357 major(udev_device_get_devnum(dev)), minor(udev_device_get_devnum(dev)));
38d70045 358 node_symlink(dev, udev_device_get_devnode(dev), filename);
912541b0
KS
359
360 /* create/update symlinks, add symlinks to name index */
8a173387
KS
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);
ea733a2f
GKH
363}
364
e7f32890 365void udev_node_remove(struct udev_device *dev)
ff9a488d 366{
912541b0 367 struct udev_list_entry *list_entry;
912541b0 368 char filename[UTIL_PATH_SIZE];
912541b0
KS
369
370 /* remove/update symlinks, remove symlinks from name index */
371 udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(dev))
8a173387 372 link_update(dev, udev_list_entry_get_name(list_entry), false);
912541b0
KS
373
374 /* remove /dev/{block,char}/$major:$minor */
4cb72937 375 snprintf(filename, sizeof(filename), "/dev/%s/%u:%u",
090be865 376 streq(udev_device_get_subsystem(dev), "block") ? "block" : "char",
912541b0
KS
377 major(udev_device_get_devnum(dev)), minor(udev_device_get_devnum(dev)));
378 unlink(filename);
ea733a2f 379}