chiark / gitweb /
tree-wide: beautify remaining copyright statements
[elogind.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 #if 0 /// UNNEEDED by elogind
38 static const char* const smack_attr_table[_SMACK_ATTR_MAX] = {
39         [SMACK_ATTR_ACCESS]     = "security.SMACK64",
40         [SMACK_ATTR_EXEC]       = "security.SMACK64EXEC",
41         [SMACK_ATTR_MMAP]       = "security.SMACK64MMAP",
42         [SMACK_ATTR_TRANSMUTE]  = "security.SMACK64TRANSMUTE",
43         [SMACK_ATTR_IPIN]       = "security.SMACK64IPIN",
44         [SMACK_ATTR_IPOUT]      = "security.SMACK64IPOUT",
45 };
46
47 DEFINE_STRING_TABLE_LOOKUP(smack_attr, SmackAttr);
48
49 int mac_smack_read(const char *path, SmackAttr attr, char **label) {
50         assert(path);
51         assert(attr >= 0 && attr < _SMACK_ATTR_MAX);
52         assert(label);
53
54         if (!mac_smack_use())
55                 return 0;
56
57         return getxattr_malloc(path, smack_attr_to_string(attr), label, true);
58 }
59
60 int mac_smack_read_fd(int fd, SmackAttr attr, char **label) {
61         assert(fd >= 0);
62         assert(attr >= 0 && attr < _SMACK_ATTR_MAX);
63         assert(label);
64
65         if (!mac_smack_use())
66                 return 0;
67
68         return fgetxattr_malloc(fd, smack_attr_to_string(attr), label);
69 }
70
71 int mac_smack_apply(const char *path, SmackAttr attr, const char *label) {
72         int r;
73
74         assert(path);
75         assert(attr >= 0 && attr < _SMACK_ATTR_MAX);
76
77         if (!mac_smack_use())
78                 return 0;
79
80         if (label)
81                 r = lsetxattr(path, smack_attr_to_string(attr), label, strlen(label), 0);
82         else
83                 r = lremovexattr(path, smack_attr_to_string(attr));
84         if (r < 0)
85                 return -errno;
86
87         return 0;
88 }
89
90 int mac_smack_apply_fd(int fd, SmackAttr attr, const char *label) {
91         int r;
92
93         assert(fd >= 0);
94         assert(attr >= 0 && attr < _SMACK_ATTR_MAX);
95
96         if (!mac_smack_use())
97                 return 0;
98
99         if (label)
100                 r = fsetxattr(fd, smack_attr_to_string(attr), label, strlen(label), 0);
101         else
102                 r = fremovexattr(fd, smack_attr_to_string(attr));
103         if (r < 0)
104                 return -errno;
105
106         return 0;
107 }
108
109 int mac_smack_apply_pid(pid_t pid, const char *label) {
110         const char *p;
111         int r = 0;
112
113         assert(label);
114
115         if (!mac_smack_use())
116                 return 0;
117
118         p = procfs_file_alloca(pid, "attr/current");
119         r = write_string_file(p, label, 0);
120         if (r < 0)
121                 return r;
122
123         return r;
124 }
125 #endif // 0
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 #if 0 /// UNNEEDED by elogind
208 int mac_smack_copy(const char *dest, const char *src) {
209         int r = 0;
210         _cleanup_free_ char *label = NULL;
211
212         assert(dest);
213         assert(src);
214
215         r = mac_smack_read(src, SMACK_ATTR_ACCESS, &label);
216         if (r < 0)
217                 return r;
218
219         r = mac_smack_apply(dest, SMACK_ATTR_ACCESS, label);
220         if (r < 0)
221                 return r;
222
223         return r;
224 }
225 #endif // 0
226
227 #else
228 bool mac_smack_use(void) {
229         return false;
230 }
231
232 #if 0 /// UNNEEDED by elogind
233 int mac_smack_read(const char *path, SmackAttr attr, char **label) {
234         return -EOPNOTSUPP;
235 }
236
237 int mac_smack_read_fd(int fd, SmackAttr attr, char **label) {
238         return -EOPNOTSUPP;
239 }
240
241 int mac_smack_apply(const char *path, SmackAttr attr, const char *label) {
242         return 0;
243 }
244
245 int mac_smack_apply_fd(int fd, SmackAttr attr, const char *label) {
246         return 0;
247 }
248
249 int mac_smack_apply_pid(pid_t pid, const char *label) {
250         return 0;
251 }
252 #endif // 0
253
254 int mac_smack_fix(const char *path, LabelFixFlags flags) {
255         return 0;
256 }
257
258 #if 0 /// UNNEEDED by elogind
259 int mac_smack_copy(const char *dest, const char *src) {
260         return 0;
261 }
262 #endif // 0
263 #endif