chiark / gitweb /
udev: support custom Linux Security Module labels for device nodes
[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 "udev.h"
36
37 static int node_symlink(struct udev_device *dev, const char *node, const char *slink)
38 {
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\n", 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'\n", 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'\n", slink, target);
90                 do {
91                         err = mkdir_parents_label(slink, 0755);
92                         if (err != 0 && err != -ENOENT)
93                                 break;
94                         label_context_set(slink, S_IFLNK);
95                         err = symlink(target, slink);
96                         if (err != 0)
97                                 err = -errno;
98                         label_context_clear();
99                 } while (err == -ENOENT);
100                 if (err == 0)
101                         goto exit;
102         }
103
104         log_debug("atomically replace '%s'\n", 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 (err != 0 && err != -ENOENT)
110                         break;
111                 label_context_set(slink_tmp, S_IFLNK);
112                 err = symlink(target, slink_tmp);
113                 if (err != 0)
114                         err = -errno;
115                 label_context_clear();
116         } while (err == -ENOENT);
117         if (err != 0) {
118                 log_error("symlink '%s' '%s' failed: %m\n", target, slink_tmp);
119                 goto exit;
120         }
121         err = rename(slink_tmp, slink);
122         if (err != 0) {
123                 log_error("rename '%s' '%s' failed: %m\n", 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 {
133         struct udev *udev = udev_device_get_udev(dev);
134         DIR *dir;
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         for (;;) {
148                 struct udev_device *dev_db;
149                 struct dirent *dent;
150
151                 dent = readdir(dir);
152                 if (dent == NULL || dent->d_name[0] == '\0')
153                         break;
154                 if (dent->d_name[0] == '.')
155                         continue;
156
157                 log_debug("found '%s' claiming '%s'\n", dent->d_name, stackdir);
158
159                 /* did we find ourself? */
160                 if (streq(dent->d_name, udev_device_get_id_filename(dev)))
161                         continue;
162
163                 dev_db = udev_device_new_from_device_id(udev, dent->d_name);
164                 if (dev_db != NULL) {
165                         const char *devnode;
166
167                         devnode = udev_device_get_devnode(dev_db);
168                         if (devnode != NULL) {
169                                 if (target == NULL || udev_device_get_devlink_priority(dev_db) > priority) {
170                                         log_debug("'%s' claims priority %i for '%s'\n",
171                                                   udev_device_get_syspath(dev_db), udev_device_get_devlink_priority(dev_db), stackdir);
172                                         priority = udev_device_get_devlink_priority(dev_db);
173                                         strscpy(buf, bufsize, devnode);
174                                         target = buf;
175                                 }
176                         }
177                         udev_device_unref(dev_db);
178                 }
179         }
180         closedir(dir);
181         return target;
182 }
183
184 /* manage "stack of names" with possibly specified device priorities */
185 static void link_update(struct udev_device *dev, const char *slink, bool add)
186 {
187         struct udev *udev = udev_device_get_udev(dev);
188         char name_enc[UTIL_PATH_SIZE];
189         char filename[UTIL_PATH_SIZE * 2];
190         char dirname[UTIL_PATH_SIZE];
191         const char *target;
192         char buf[UTIL_PATH_SIZE];
193
194         util_path_encode(slink + strlen("/dev"), name_enc, sizeof(name_enc));
195         strscpyl(dirname, sizeof(dirname), "/run/udev/links/", name_enc, NULL);
196         strscpyl(filename, sizeof(filename), dirname, "/", udev_device_get_id_filename(dev), NULL);
197
198         if (!add && unlink(filename) == 0)
199                 rmdir(dirname);
200
201         target = link_find_prioritized(dev, add, dirname, buf, sizeof(buf));
202         if (target == NULL) {
203                 log_debug("no reference left, remove '%s'\n", slink);
204                 if (unlink(slink) == 0)
205                         util_delete_path(udev, slink);
206         } else {
207                 log_debug("creating link '%s' to '%s'\n", slink, target);
208                 node_symlink(dev, target, slink);
209         }
210
211         if (add) {
212                 int err;
213
214                 do {
215                         int fd;
216
217                         err = mkdir_parents(filename, 0755);
218                         if (err != 0 && err != -ENOENT)
219                                 break;
220                         fd = open(filename, O_WRONLY|O_CREAT|O_CLOEXEC|O_TRUNC|O_NOFOLLOW, 0444);
221                         if (fd >= 0)
222                                 close(fd);
223                         else
224                                 err = -errno;
225                 } while (err == -ENOENT);
226         }
227 }
228
229 void udev_node_update_old_links(struct udev_device *dev, struct udev_device *dev_old)
230 {
231         struct udev_list_entry *list_entry;
232
233         /* update possible left-over symlinks */
234         udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(dev_old)) {
235                 const char *name = udev_list_entry_get_name(list_entry);
236                 struct udev_list_entry *list_entry_current;
237                 int found;
238
239                 /* check if old link name still belongs to this device */
240                 found = 0;
241                 udev_list_entry_foreach(list_entry_current, udev_device_get_devlinks_list_entry(dev)) {
242                         const char *name_current = udev_list_entry_get_name(list_entry_current);
243
244                         if (streq(name, name_current)) {
245                                 found = 1;
246                                 break;
247                         }
248                 }
249                 if (found)
250                         continue;
251
252                 log_debug("update old name, '%s' no longer belonging to '%s'\n",
253                      name, udev_device_get_devpath(dev));
254                 link_update(dev, name, false);
255         }
256 }
257
258 static int node_permissions_apply(struct udev_device *dev, bool apply,
259                                   mode_t mode, uid_t uid, gid_t gid,
260                                   struct udev_list *seclabel_list) {
261         const char *devnode = udev_device_get_devnode(dev);
262         dev_t devnum = udev_device_get_devnum(dev);
263         struct stat stats;
264         struct udev_list_entry *entry;
265         int err = 0;
266
267         if (streq(udev_device_get_subsystem(dev), "block"))
268                 mode |= S_IFBLK;
269         else
270                 mode |= S_IFCHR;
271
272         if (lstat(devnode, &stats) != 0) {
273                 err = -errno;
274                 log_debug("can not stat() node '%s' (%m)\n", devnode);
275                 goto out;
276         }
277
278         if (((stats.st_mode & S_IFMT) != (mode & S_IFMT)) || (stats.st_rdev != devnum)) {
279                 err = -EEXIST;
280                 log_debug("found node '%s' with non-matching devnum %s, skip handling\n",
281                           udev_device_get_devnode(dev), udev_device_get_id_filename(dev));
282                 goto out;
283         }
284
285         if (apply) {
286                 if ((stats.st_mode & 0777) != (mode & 0777) || stats.st_uid != uid || stats.st_gid != gid) {
287                         log_debug("set permissions %s, %#o, uid=%u, gid=%u\n", devnode, mode, uid, gid);
288                         chmod(devnode, mode);
289                         chown(devnode, uid, gid);
290                 } else {
291                         log_debug("preserve permissions %s, %#o, uid=%u, gid=%u\n", devnode, mode, uid, gid);
292                 }
293
294                 label_fix(devnode, true, false);
295
296                 /* apply SECLABEL{$module}=$label */
297                 udev_list_entry_foreach(entry, udev_list_get_entry(seclabel_list)) {
298                         const char *name, *label;
299
300                         name = udev_list_entry_get_name(entry);
301                         label = udev_list_entry_get_value(entry);
302
303                         if (streq(name, "selinux")) {
304                                 /* FIXME: hook up libselinux */
305                                 log_error("SECLABEL: failed to set selinux label '%s'", label);
306
307                         } else if (streq(name, "smack")) {
308 #ifdef HAVE_XATTR
309                                 if (lsetxattr(devnode, "security.SMACK64", label, strlen(label), 0) < 0)
310                                         log_error("SECLABEL: failed to set SMACK label '%s'", label);
311                                 else
312                                         log_debug("SECLABEL: set SMACK label '%s'", label);
313 #endif
314
315                         } else
316                                 log_error("SECLABEL: unknown subsystem, ignoring '%s'='%s'", name, label);
317                 }
318         }
319
320         /* always update timestamp when we re-use the node, like on media change events */
321         utimensat(AT_FDCWD, devnode, NULL, 0);
322 out:
323         return err;
324 }
325
326 void udev_node_add(struct udev_device *dev, bool apply,
327                    mode_t mode, uid_t uid, gid_t gid,
328                    struct udev_list *seclabel_list) {
329         char filename[UTIL_PATH_SIZE];
330         struct udev_list_entry *list_entry;
331
332         log_debug("handling device node '%s', devnum=%s, mode=%#o, uid=%d, gid=%d\n",
333                   udev_device_get_devnode(dev), udev_device_get_id_filename(dev), mode, uid, gid);
334
335         if (node_permissions_apply(dev, apply, mode, uid, gid, seclabel_list) < 0)
336                 return;
337
338         /* always add /dev/{block,char}/$major:$minor */
339         snprintf(filename, sizeof(filename), "/dev/%s/%u:%u",
340                  streq(udev_device_get_subsystem(dev), "block") ? "block" : "char",
341                  major(udev_device_get_devnum(dev)), minor(udev_device_get_devnum(dev)));
342         node_symlink(dev, udev_device_get_devnode(dev), filename);
343
344         /* create/update symlinks, add symlinks to name index */
345         udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(dev))
346                         link_update(dev, udev_list_entry_get_name(list_entry), true);
347 }
348
349 void udev_node_remove(struct udev_device *dev)
350 {
351         struct udev_list_entry *list_entry;
352         char filename[UTIL_PATH_SIZE];
353
354         /* remove/update symlinks, remove symlinks from name index */
355         udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(dev))
356                 link_update(dev, udev_list_entry_get_name(list_entry), false);
357
358         /* remove /dev/{block,char}/$major:$minor */
359         snprintf(filename, sizeof(filename), "/dev/%s/%u:%u",
360                  streq(udev_device_get_subsystem(dev), "block") ? "block" : "char",
361                  major(udev_device_get_devnum(dev)), minor(udev_device_get_devnum(dev)));
362         unlink(filename);
363 }