chiark / gitweb /
security: rework selinux, smack, ima, apparmor detection logic
[elogind.git] / src / udev / udev-node.c
1 /*
2  * Copyright (C) 2003-2013 Kay Sievers <kay@vrfy.org>
3  *
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.
8  *
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/>.
16  */
17
18 #include <stdlib.h>
19 #include <string.h>
20 #include <stdio.h>
21 #include <stddef.h>
22 #include <stdbool.h>
23 #include <fcntl.h>
24 #include <unistd.h>
25 #include <errno.h>
26 #include <grp.h>
27 #include <dirent.h>
28 #include <sys/time.h>
29 #include <sys/stat.h>
30 #include <sys/types.h>
31 #ifdef HAVE_XATTR
32 #include <attr/xattr.h>
33 #endif
34
35 #include "smack-util.h"
36 #include "udev.h"
37
38 static int node_symlink(struct udev_device *dev, const char *node, const char *slink)
39 {
40         struct stat stats;
41         char target[UTIL_PATH_SIZE];
42         char *s;
43         size_t l;
44         char slink_tmp[UTIL_PATH_SIZE + 32];
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] == '/')
60                         l = strpcpy(&s, l, "../");
61                 i++;
62         }
63         l = strscpy(s, l, &node[tail]);
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)) {
72                         log_error("conflicting device node '%s' found, link to '%s' will not be created\n", slink, node);
73                         goto exit;
74                 } else if (S_ISLNK(stats.st_mode)) {
75                         char buf[UTIL_PATH_SIZE];
76                         int len;
77
78                         len = readlink(slink, buf, sizeof(buf));
79                         if (len > 0 && len < (int)sizeof(buf)) {
80                                 buf[len] = '\0';
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);
85                                         goto exit;
86                                 }
87                         }
88                 }
89         } else {
90                 log_debug("creating symlink '%s' to '%s'\n", slink, target);
91                 do {
92                         err = mkdir_parents_label(slink, 0755);
93                         if (err != 0 && err != -ENOENT)
94                                 break;
95                         label_context_set(slink, S_IFLNK);
96                         err = symlink(target, slink);
97                         if (err != 0)
98                                 err = -errno;
99                         label_context_clear();
100                 } while (err == -ENOENT);
101                 if (err == 0)
102                         goto exit;
103         }
104
105         log_debug("atomically replace '%s'\n", slink);
106         strscpyl(slink_tmp, sizeof(slink_tmp), slink, ".tmp-", udev_device_get_id_filename(dev), NULL);
107         unlink(slink_tmp);
108         do {
109                 err = mkdir_parents_label(slink_tmp, 0755);
110                 if (err != 0 && err != -ENOENT)
111                         break;
112                 label_context_set(slink_tmp, S_IFLNK);
113                 err = symlink(target, slink_tmp);
114                 if (err != 0)
115                         err = -errno;
116                 label_context_clear();
117         } while (err == -ENOENT);
118         if (err != 0) {
119                 log_error("symlink '%s' '%s' failed: %m\n", target, slink_tmp);
120                 goto exit;
121         }
122         err = rename(slink_tmp, slink);
123         if (err != 0) {
124                 log_error("rename '%s' '%s' failed: %m\n", slink_tmp, slink);
125                 unlink(slink_tmp);
126         }
127 exit:
128         return err;
129 }
130
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)
133 {
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);
141                 strscpy(buf, bufsize, udev_device_get_devnode(dev));
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
158                 log_debug("found '%s' claiming '%s'\n", dent->d_name, stackdir);
159
160                 /* did we find ourself? */
161                 if (streq(dent->d_name, udev_device_get_id_filename(dev)))
162                         continue;
163
164                 dev_db = udev_device_new_from_device_id(udev, dent->d_name);
165                 if (dev_db != NULL) {
166                         const char *devnode;
167
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);
175                                         target = buf;
176                                 }
177                         }
178                         udev_device_unref(dev_db);
179                 }
180         }
181         closedir(dir);
182         return target;
183 }
184
185 /* manage "stack of names" with possibly specified device priorities */
186 static void link_update(struct udev_device *dev, const char *slink, bool add)
187 {
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
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);
198
199         if (!add && unlink(filename) == 0)
200                 rmdir(dirname);
201
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);
207         } else {
208                 log_debug("creating link '%s' to '%s'\n", slink, target);
209                 node_symlink(dev, target, slink);
210         }
211
212         if (add) {
213                 int err;
214
215                 do {
216                         int fd;
217
218                         err = mkdir_parents(filename, 0755);
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         }
228 }
229
230 void udev_node_update_old_links(struct udev_device *dev, struct udev_device *dev_old)
231 {
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
245                         if (streq(name, name_current)) {
246                                 found = 1;
247                                 break;
248                         }
249                 }
250                 if (found)
251                         continue;
252
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);
256         }
257 }
258
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);
264         struct stat stats;
265         struct udev_list_entry *entry;
266         int err = 0;
267
268         if (streq(udev_device_get_subsystem(dev), "block"))
269                 mode |= S_IFBLK;
270         else
271                 mode |= S_IFCHR;
272
273         if (lstat(devnode, &stats) != 0) {
274                 err = -errno;
275                 log_debug("can not stat() node '%s' (%m)\n", devnode);
276                 goto out;
277         }
278
279         if (((stats.st_mode & S_IFMT) != (mode & S_IFMT)) || (stats.st_rdev != devnum)) {
280                 err = -EEXIST;
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));
283                 goto out;
284         }
285
286         if (apply) {
287                 bool selinux = false;
288 #ifdef HAVE_SMACK
289                 bool smack = false;
290 #endif
291
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                 }
299
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")) {
308                                 selinux = true;
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);
313
314 #ifdef HAVE_SMACK
315                         } else if (streq(name, "smack") && use_smack()) {
316                                 smack = true;
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                 }
326
327                 /* set the defaults */
328                 if (!selinux)
329                         label_fix(devnode, true, false);
330 #ifdef HAVE_SMACK
331                 if (!smack && use_smack())
332                         lremovexattr(devnode, "security.SMACK64");
333 #endif
334         }
335
336         /* always update timestamp when we re-use the node, like on media change events */
337         utimensat(AT_FDCWD, devnode, NULL, 0);
338 out:
339         return err;
340 }
341
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;
347
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);
350
351         if (node_permissions_apply(dev, apply, mode, uid, gid, seclabel_list) < 0)
352                 return;
353
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);
359
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);
363 }
364
365 void udev_node_remove(struct udev_device *dev)
366 {
367         struct udev_list_entry *list_entry;
368         char filename[UTIL_PATH_SIZE];
369
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);
373
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)));
378         unlink(filename);
379 }