]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/smack-util.c
tree-wide: beautify remaining copyright statements
[thirdparty/systemd.git] / src / basic / smack-util.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
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 <string.h>
11 #include <sys/stat.h>
12 #include <sys/xattr.h>
13 #include <unistd.h>
14
15 #include "alloc-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, true);
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 = fsetxattr(fd, smack_attr_to_string(attr), label, strlen(label), 0);
100 else
101 r = fremovexattr(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 = 0;
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, 0);
119 if (r < 0)
120 return r;
121
122 return r;
123 }
124
125 int mac_smack_fix(const char *path, LabelFixFlags flags) {
126 char procfs_path[STRLEN("/proc/self/fd/") + DECIMAL_STR_MAX(int)];
127 _cleanup_close_ int fd = -1;
128 const char *label;
129 struct stat st;
130 int r;
131
132 assert(path);
133
134 if (!mac_smack_use())
135 return 0;
136
137 /* Path must be in /dev. Note that this check is pretty sloppy, as we might be called with non-normalized paths
138 * and hence not detect all cases of /dev. */
139
140 if (path_is_absolute(path)) {
141 if (!path_startswith(path, "/dev"))
142 return 0;
143 } else {
144 _cleanup_free_ char *cwd = NULL;
145
146 r = safe_getcwd(&cwd);
147 if (r < 0)
148 return r;
149
150 if (!path_startswith(cwd, "/dev"))
151 return 0;
152 }
153
154 fd = open(path, O_NOFOLLOW|O_CLOEXEC|O_PATH);
155 if (fd < 0) {
156 if ((flags & LABEL_IGNORE_ENOENT) && errno == ENOENT)
157 return 0;
158
159 return -errno;
160 }
161
162 if (fstat(fd, &st) < 0)
163 return -errno;
164
165 /*
166 * Label directories and character devices "*".
167 * Label symlinks "_".
168 * Don't change anything else.
169 */
170
171 if (S_ISDIR(st.st_mode))
172 label = SMACK_STAR_LABEL;
173 else if (S_ISLNK(st.st_mode))
174 label = SMACK_FLOOR_LABEL;
175 else if (S_ISCHR(st.st_mode))
176 label = SMACK_STAR_LABEL;
177 else
178 return 0;
179
180 xsprintf(procfs_path, "/proc/self/fd/%i", fd);
181 if (setxattr(procfs_path, "security.SMACK64", label, strlen(label), 0) < 0) {
182 _cleanup_free_ char *old_label = NULL;
183
184 r = -errno;
185
186 /* If the FS doesn't support labels, then exit without warning */
187 if (r == -EOPNOTSUPP)
188 return 0;
189
190 /* It the FS is read-only and we were told to ignore failures caused by that, suppress error */
191 if (r == -EROFS && (flags & LABEL_IGNORE_EROFS))
192 return 0;
193
194 /* If the old label is identical to the new one, suppress any kind of error */
195 if (getxattr_malloc(procfs_path, "security.SMACK64", &old_label, false) >= 0 &&
196 streq(old_label, label))
197 return 0;
198
199 return log_debug_errno(r, "Unable to fix SMACK label of %s: %m", path);
200 }
201
202 return 0;
203 }
204
205 int mac_smack_copy(const char *dest, const char *src) {
206 int r = 0;
207 _cleanup_free_ char *label = NULL;
208
209 assert(dest);
210 assert(src);
211
212 r = mac_smack_read(src, SMACK_ATTR_ACCESS, &label);
213 if (r < 0)
214 return r;
215
216 r = mac_smack_apply(dest, SMACK_ATTR_ACCESS, label);
217 if (r < 0)
218 return r;
219
220 return r;
221 }
222
223 #else
224 bool mac_smack_use(void) {
225 return false;
226 }
227
228 int mac_smack_read(const char *path, SmackAttr attr, char **label) {
229 return -EOPNOTSUPP;
230 }
231
232 int mac_smack_read_fd(int fd, SmackAttr attr, char **label) {
233 return -EOPNOTSUPP;
234 }
235
236 int mac_smack_apply(const char *path, SmackAttr attr, const char *label) {
237 return 0;
238 }
239
240 int mac_smack_apply_fd(int fd, SmackAttr attr, const char *label) {
241 return 0;
242 }
243
244 int mac_smack_apply_pid(pid_t pid, const char *label) {
245 return 0;
246 }
247
248 int mac_smack_fix(const char *path, LabelFixFlags flags) {
249 return 0;
250 }
251
252 int mac_smack_copy(const char *dest, const char *src) {
253 return 0;
254 }
255 #endif