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