Debian Codegraph @

src/shared/smack-util.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 /***
3 Copyright © 2013 Intel Corporation
4
5 Author: Auke Kok <[email protected]>
6 ***/
7
8 #include <fcntl.h>
9 #include <sys/stat.h>
10 #include <unistd.h>
11
12 #include "alloc-util.h"
13 #include "errno-util.h"
14 #include "fd-util.h"
15 #include "fileio.h"
16 #include "label-util.h"
17 #include "log.h"
18 #include "path-util.h"
19 #include "process-util.h"
20 #include "smack-util.h"
21 #include "string-table.h"
22 #include "xattr-util.h"
23
24 #define SMACK_FLOOR_LABEL "_"
25 #define SMACK_STAR_LABEL "*"
26
27 bool mac_smack_use(void) {
28 #if ENABLE_SMACK
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 #else
36 return false;
37 #endif
38 }
39
40 #if ENABLE_SMACK
41
42 static const char* const smack_attr_table[_SMACK_ATTR_MAX] = {
43 [SMACK_ATTR_ACCESS] = "security.SMACK64",
44 [SMACK_ATTR_EXEC] = "security.SMACK64EXEC",
45 [SMACK_ATTR_MMAP] = "security.SMACK64MMAP",
46 [SMACK_ATTR_TRANSMUTE] = "security.SMACK64TRANSMUTE",
47 [SMACK_ATTR_IPIN] = "security.SMACK64IPIN",
48 [SMACK_ATTR_IPOUT] = "security.SMACK64IPOUT",
49 };
50
51 DEFINE_PRIVATE_STRING_TABLE_LOOKUP_TO_STRING(smack_attr, SmackAttr);
52
53 #endif
54
55 int mac_smack_read_at(int fd, const char *path, SmackAttr attr, char **ret) {
56 #if ENABLE_SMACK
57 assert(fd >= 0 || fd == AT_FDCWD);
58 assert(attr >= 0 && attr < _SMACK_ATTR_MAX);
59 assert(ret);
60
61 if (!mac_smack_use()) {
62 *ret = NULL;
63 return 0;
64 }
65
66 return getxattr_at_malloc(fd, path, smack_attr_to_string(attr), /* at_flags= */ 0, ret, /* ret_size= */ NULL);
67 #else
68 return -EOPNOTSUPP;
69 #endif
70 }
71
72 int mac_smack_apply_at(int fd, const char *path, SmackAttr attr, const char *label) {
73 #if ENABLE_SMACK
74 assert(fd >= 0 || fd == AT_FDCWD);
75 assert(attr >= 0 && attr < _SMACK_ATTR_MAX);
76
77 if (!mac_smack_use())
78 return 0;
79
80 if (!label)
81 return xremovexattr(fd, path, /* at_flags= */ 0, smack_attr_to_string(attr));
82
83 return xsetxattr(fd, path, /* at_flags= */ 0, smack_attr_to_string(attr), label);
84 #else
85 return 0;
86 #endif
87 }
88
89 int mac_smack_apply_pid(pid_t pid, const char *label) {
90 #if ENABLE_SMACK
91 const char *p;
92
93 assert(pid >= 0);
94 assert(label);
95
96 if (!mac_smack_use())
97 return 0;
98
99 p = procfs_file_alloca(pid, "attr/current");
100 return write_string_file(p, label, WRITE_STRING_FILE_DISABLE_BUFFER);
101 #else
102 return 0;
103 #endif
104 }
105
106 #if ENABLE_SMACK
107
108 static int smack_fix_fd(
109 int fd,
110 const char *label_path,
111 LabelFixFlags flags) {
112
113 const char *label;
114 struct stat st;
115 int r;
116
117 /* The caller should have done the sanity checks. */
118 assert(fd >= 0);
119 assert(label_path);
120 assert(path_is_absolute(label_path));
121
122 /* Path must be in /dev. */
123 if (!path_startswith(label_path, "/dev"))
124 return 0;
125
126 if (fstat(fd, &st) < 0)
127 return -errno;
128
129 /*
130 * Label directories and character devices "*".
131 * Label symlinks "_".
132 * Don't change anything else.
133 */
134
135 if (S_ISDIR(st.st_mode))
136 label = SMACK_STAR_LABEL;
137 else if (S_ISLNK(st.st_mode))
138 label = SMACK_FLOOR_LABEL;
139 else if (S_ISCHR(st.st_mode))
140 label = SMACK_STAR_LABEL;
141 else
142 return 0;
143
144 r = xsetxattr(fd, /* path= */ NULL, AT_EMPTY_PATH, "security.SMACK64", label);
145 if (ERRNO_IS_NEG_NOT_SUPPORTED(r)) /* If the FS doesn't support labels, then exit without warning */
146 return 0;
147 if (r == -EROFS && FLAGS_SET(flags, LABEL_IGNORE_EROFS)) /* If the FS is read-only and we were told
148 to ignore failures caused by that,
149 suppress error */
150 return 0;
151 if (r < 0)
152 return log_debug_errno(r, "Unable to fix SMACK label of '%s': %m", label_path);
153
154 return 0;
155 }
156
157 #endif
158
159 int mac_smack_fix_full(
160 int atfd,
161 const char *inode_path,
162 const char *label_path,
163 LabelFixFlags flags) {
164
165 #if ENABLE_SMACK
166 _cleanup_close_ int opened_fd = -EBADF;
167 _cleanup_free_ char *p = NULL;
168 int r, inode_fd;
169
170 assert(atfd >= 0 || (atfd == AT_FDCWD && inode_path));
171
172 if (!mac_smack_use())
173 return 0;
174
175 if (inode_path) {
176 opened_fd = openat(atfd, inode_path, O_NOFOLLOW|O_CLOEXEC|O_PATH);
177 if (opened_fd < 0) {
178 if (errno == ENOENT && FLAGS_SET(flags, LABEL_IGNORE_ENOENT))
179 return 0;
180
181 return -errno;
182 }
183 inode_fd = opened_fd;
184 } else
185 inode_fd = atfd;
186
187 if (!label_path) {
188 if (path_is_absolute(inode_path))
189 label_path = inode_path;
190 else {
191 r = fd_get_path(inode_fd, &p);
192 if (r < 0)
193 return r;
194
195 label_path = p;
196 }
197 }
198
199 return smack_fix_fd(inode_fd, label_path, flags);
200 #else
201 return 0;
202 #endif
203 }
204
205 int mac_smack_fix(const char *path, LabelFixFlags flags) {
206 return mac_smack_fix_full(AT_FDCWD, path, path, flags);
207 }
208
209 int renameat_and_apply_smack_floor_label(int fdf, const char *from, int fdt, const char *to) {
210
211 assert(fdf >= 0 || fdf == AT_FDCWD);
212 assert(fdt >= 0 || fdt == AT_FDCWD);
213
214 if (renameat(fdf, from, fdt, to) < 0)
215 return -errno;
216
217 #if HAVE_SMACK_RUN_LABEL
218 return mac_smack_apply_at(fdt, to, SMACK_ATTR_ACCESS, SMACK_FLOOR_LABEL);
219 #else
220 return 0;
221 #endif
222 }
223
224 static int mac_smack_label_pre(int dir_fd, const char *path, mode_t mode) {
225 return 0;
226 }
227
228 static int mac_smack_label_post(int dir_fd, const char *path, bool created) {
229 if (!created)
230 return 0;
231
232 return mac_smack_fix_full(dir_fd, path, NULL, 0);
233 }
234
235 int mac_smack_init(void) {
236 static const LabelOps label_ops = {
237 .pre = mac_smack_label_pre,
238 .post = mac_smack_label_post,
239 };
240
241 if (!mac_smack_use())
242 return 0;
243
244 return label_ops_set(&label_ops);
245 }