]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/smack-util.c
Merge pull request #9115 from yuwata/rfe-8491
[thirdparty/systemd.git] / src / basic / smack-util.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 This file is part of systemd.
4
5 Copyright 2013 Intel Corporation
6
7 Author: Auke Kok <auke-jan.h.kok@intel.com>
8 ***/
9
10 #include <errno.h>
11 #include <fcntl.h>
12 #include <string.h>
13 #include <sys/stat.h>
14 #include <sys/xattr.h>
15 #include <unistd.h>
16
17 #include "alloc-util.h"
18 #include "fd-util.h"
19 #include "fileio.h"
20 #include "log.h"
21 #include "macro.h"
22 #include "path-util.h"
23 #include "process-util.h"
24 #include "smack-util.h"
25 #include "stdio-util.h"
26 #include "string-table.h"
27 #include "xattr-util.h"
28
29 #if ENABLE_SMACK
30 bool mac_smack_use(void) {
31 static int cached_use = -1;
32
33 if (cached_use < 0)
34 cached_use = access("/sys/fs/smackfs/", F_OK) >= 0;
35
36 return cached_use;
37 }
38
39 static const char* const smack_attr_table[_SMACK_ATTR_MAX] = {
40 [SMACK_ATTR_ACCESS] = "security.SMACK64",
41 [SMACK_ATTR_EXEC] = "security.SMACK64EXEC",
42 [SMACK_ATTR_MMAP] = "security.SMACK64MMAP",
43 [SMACK_ATTR_TRANSMUTE] = "security.SMACK64TRANSMUTE",
44 [SMACK_ATTR_IPIN] = "security.SMACK64IPIN",
45 [SMACK_ATTR_IPOUT] = "security.SMACK64IPOUT",
46 };
47
48 DEFINE_STRING_TABLE_LOOKUP(smack_attr, SmackAttr);
49
50 int mac_smack_read(const char *path, SmackAttr attr, char **label) {
51 assert(path);
52 assert(attr >= 0 && attr < _SMACK_ATTR_MAX);
53 assert(label);
54
55 if (!mac_smack_use())
56 return 0;
57
58 return getxattr_malloc(path, smack_attr_to_string(attr), label, true);
59 }
60
61 int mac_smack_read_fd(int fd, SmackAttr attr, char **label) {
62 assert(fd >= 0);
63 assert(attr >= 0 && attr < _SMACK_ATTR_MAX);
64 assert(label);
65
66 if (!mac_smack_use())
67 return 0;
68
69 return fgetxattr_malloc(fd, smack_attr_to_string(attr), label);
70 }
71
72 int mac_smack_apply(const char *path, SmackAttr attr, const char *label) {
73 int r;
74
75 assert(path);
76 assert(attr >= 0 && attr < _SMACK_ATTR_MAX);
77
78 if (!mac_smack_use())
79 return 0;
80
81 if (label)
82 r = lsetxattr(path, smack_attr_to_string(attr), label, strlen(label), 0);
83 else
84 r = lremovexattr(path, smack_attr_to_string(attr));
85 if (r < 0)
86 return -errno;
87
88 return 0;
89 }
90
91 int mac_smack_apply_fd(int fd, SmackAttr attr, const char *label) {
92 int r;
93
94 assert(fd >= 0);
95 assert(attr >= 0 && attr < _SMACK_ATTR_MAX);
96
97 if (!mac_smack_use())
98 return 0;
99
100 if (label)
101 r = fsetxattr(fd, smack_attr_to_string(attr), label, strlen(label), 0);
102 else
103 r = fremovexattr(fd, smack_attr_to_string(attr));
104 if (r < 0)
105 return -errno;
106
107 return 0;
108 }
109
110 int mac_smack_apply_pid(pid_t pid, const char *label) {
111 const char *p;
112 int r = 0;
113
114 assert(label);
115
116 if (!mac_smack_use())
117 return 0;
118
119 p = procfs_file_alloca(pid, "attr/current");
120 r = write_string_file(p, label, 0);
121 if (r < 0)
122 return r;
123
124 return r;
125 }
126
127 int mac_smack_fix(const char *path, LabelFixFlags flags) {
128 char procfs_path[STRLEN("/proc/self/fd/") + DECIMAL_STR_MAX(int)];
129 _cleanup_close_ int fd = -1;
130 const char *label;
131 struct stat st;
132 int r;
133
134 assert(path);
135
136 if (!mac_smack_use())
137 return 0;
138
139 /* Path must be in /dev. Note that this check is pretty sloppy, as we might be called with non-normalized paths
140 * and hence not detect all cases of /dev. */
141
142 if (path_is_absolute(path)) {
143 if (!path_startswith(path, "/dev"))
144 return 0;
145 } else {
146 _cleanup_free_ char *cwd = NULL;
147
148 r = safe_getcwd(&cwd);
149 if (r < 0)
150 return r;
151
152 if (!path_startswith(cwd, "/dev"))
153 return 0;
154 }
155
156 fd = open(path, O_NOFOLLOW|O_CLOEXEC|O_PATH);
157 if (fd < 0) {
158 if ((flags & LABEL_IGNORE_ENOENT) && errno == ENOENT)
159 return 0;
160
161 return -errno;
162 }
163
164 if (fstat(fd, &st) < 0)
165 return -errno;
166
167 /*
168 * Label directories and character devices "*".
169 * Label symlinks "_".
170 * Don't change anything else.
171 */
172
173 if (S_ISDIR(st.st_mode))
174 label = SMACK_STAR_LABEL;
175 else if (S_ISLNK(st.st_mode))
176 label = SMACK_FLOOR_LABEL;
177 else if (S_ISCHR(st.st_mode))
178 label = SMACK_STAR_LABEL;
179 else
180 return 0;
181
182 xsprintf(procfs_path, "/proc/self/fd/%i", fd);
183 if (setxattr(procfs_path, "security.SMACK64", label, strlen(label), 0) < 0) {
184 _cleanup_free_ char *old_label = NULL;
185
186 r = -errno;
187
188 /* If the FS doesn't support labels, then exit without warning */
189 if (r == -EOPNOTSUPP)
190 return 0;
191
192 /* It the FS is read-only and we were told to ignore failures caused by that, suppress error */
193 if (r == -EROFS && (flags & LABEL_IGNORE_EROFS))
194 return 0;
195
196 /* If the old label is identical to the new one, suppress any kind of error */
197 if (getxattr_malloc(procfs_path, "security.SMACK64", &old_label, false) >= 0 &&
198 streq(old_label, label))
199 return 0;
200
201 return log_debug_errno(r, "Unable to fix SMACK label of %s: %m", path);
202 }
203
204 return 0;
205 }
206
207 int mac_smack_copy(const char *dest, const char *src) {
208 int r = 0;
209 _cleanup_free_ char *label = NULL;
210
211 assert(dest);
212 assert(src);
213
214 r = mac_smack_read(src, SMACK_ATTR_ACCESS, &label);
215 if (r < 0)
216 return r;
217
218 r = mac_smack_apply(dest, SMACK_ATTR_ACCESS, label);
219 if (r < 0)
220 return r;
221
222 return r;
223 }
224
225 #else
226 bool mac_smack_use(void) {
227 return false;
228 }
229
230 int mac_smack_read(const char *path, SmackAttr attr, char **label) {
231 return -EOPNOTSUPP;
232 }
233
234 int mac_smack_read_fd(int fd, SmackAttr attr, char **label) {
235 return -EOPNOTSUPP;
236 }
237
238 int mac_smack_apply(const char *path, SmackAttr attr, const char *label) {
239 return 0;
240 }
241
242 int mac_smack_apply_fd(int fd, SmackAttr attr, const char *label) {
243 return 0;
244 }
245
246 int mac_smack_apply_pid(pid_t pid, const char *label) {
247 return 0;
248 }
249
250 int mac_smack_fix(const char *path, LabelFixFlags flags) {
251 return 0;
252 }
253
254 int mac_smack_copy(const char *dest, const char *src) {
255 return 0;
256 }
257 #endif