chiark / gitweb /
a8f29acfa34ce132f1f5edc4d36275b08abb1ca4
[elogind.git] / src / basic / smack-util.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2013 Intel Corporation
7
8   Author: Auke Kok <auke-jan.h.kok@intel.com>
9
10   systemd is free software; you can redistribute it and/or modify it
11   under the terms of the GNU Lesser General Public License as published by
12   the Free Software Foundation; either version 2.1 of the License, or
13   (at your option) any later version.
14
15   systemd is distributed in the hope that it will be useful, but
16   WITHOUT ANY WARRANTY; without even the implied warranty of
17   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18   Lesser General Public License for more details.
19
20   You should have received a copy of the GNU Lesser General Public License
21   along with systemd; If not, see <http://www.gnu.org/licenses/>.
22 ***/
23
24 #include <sys/xattr.h>
25
26 #include "alloc-util.h"
27 #include "fileio.h"
28 #include "path-util.h"
29 #include "process-util.h"
30 #include "smack-util.h"
31 #include "string-table.h"
32 #include "util.h"
33 #include "xattr-util.h"
34
35 #ifdef HAVE_SMACK
36 bool mac_smack_use(void) {
37         static int cached_use = -1;
38
39         if (cached_use < 0)
40                 cached_use = access("/sys/fs/smackfs/", F_OK) >= 0;
41
42         return cached_use;
43 }
44
45 /// UNNEEDED by elogind
46 #if 0
47 static const char* const smack_attr_table[_SMACK_ATTR_MAX] = {
48         [SMACK_ATTR_ACCESS]     = "security.SMACK64",
49         [SMACK_ATTR_EXEC]       = "security.SMACK64EXEC",
50         [SMACK_ATTR_MMAP]       = "security.SMACK64MMAP",
51         [SMACK_ATTR_TRANSMUTE]  = "security.SMACK64TRANSMUTE",
52         [SMACK_ATTR_IPIN]       = "security.SMACK64IPIN",
53         [SMACK_ATTR_IPOUT]      = "security.SMACK64IPOUT",
54 };
55
56 DEFINE_STRING_TABLE_LOOKUP(smack_attr, SmackAttr);
57
58 int mac_smack_read(const char *path, SmackAttr attr, char **label) {
59         assert(path);
60         assert(attr >= 0 && attr < _SMACK_ATTR_MAX);
61         assert(label);
62
63         if (!mac_smack_use())
64                 return 0;
65
66         return getxattr_malloc(path, smack_attr_to_string(attr), label, true);
67 }
68
69 int mac_smack_read_fd(int fd, SmackAttr attr, char **label) {
70         assert(fd >= 0);
71         assert(attr >= 0 && attr < _SMACK_ATTR_MAX);
72         assert(label);
73
74         if (!mac_smack_use())
75                 return 0;
76
77         return fgetxattr_malloc(fd, smack_attr_to_string(attr), label);
78 }
79
80 int mac_smack_apply(const char *path, SmackAttr attr, const char *label) {
81         int r;
82
83         assert(path);
84         assert(attr >= 0 && attr < _SMACK_ATTR_MAX);
85
86         if (!mac_smack_use())
87                 return 0;
88
89         if (label)
90                 r = lsetxattr(path, smack_attr_to_string(attr), label, strlen(label), 0);
91         else
92                 r = lremovexattr(path, smack_attr_to_string(attr));
93         if (r < 0)
94                 return -errno;
95
96         return 0;
97 }
98
99 int mac_smack_apply_fd(int fd, SmackAttr attr, const char *label) {
100         int r;
101
102         assert(fd >= 0);
103         assert(attr >= 0 && attr < _SMACK_ATTR_MAX);
104
105         if (!mac_smack_use())
106                 return 0;
107
108         if (label)
109                 r = fsetxattr(fd, smack_attr_to_string(attr), label, strlen(label), 0);
110         else
111                 r = fremovexattr(fd, smack_attr_to_string(attr));
112         if (r < 0)
113                 return -errno;
114
115         return 0;
116 }
117
118 int mac_smack_apply_pid(pid_t pid, const char *label) {
119         const char *p;
120         int r = 0;
121
122         assert(label);
123
124         if (!mac_smack_use())
125                 return 0;
126
127         p = procfs_file_alloca(pid, "attr/current");
128         r = write_string_file(p, label, 0);
129         if (r < 0)
130                 return r;
131
132         return r;
133 }
134 #endif // 0
135
136 int mac_smack_fix(const char *path, bool ignore_enoent, bool ignore_erofs) {
137         struct stat st;
138         int r = 0;
139
140         assert(path);
141
142         if (!mac_smack_use())
143                 return 0;
144
145         /*
146          * Path must be in /dev and must exist
147          */
148         if (!path_startswith(path, "/dev"))
149                 return 0;
150
151         r = lstat(path, &st);
152         if (r >= 0) {
153                 const char *label;
154
155                 /*
156                  * Label directories and character devices "*".
157                  * Label symlinks "_".
158                  * Don't change anything else.
159                  */
160
161                 if (S_ISDIR(st.st_mode))
162                         label = SMACK_STAR_LABEL;
163                 else if (S_ISLNK(st.st_mode))
164                         label = SMACK_FLOOR_LABEL;
165                 else if (S_ISCHR(st.st_mode))
166                         label = SMACK_STAR_LABEL;
167                 else
168                         return 0;
169
170                 r = lsetxattr(path, "security.SMACK64", label, strlen(label), 0);
171
172                 /* If the FS doesn't support labels, then exit without warning */
173                 if (r < 0 && errno == EOPNOTSUPP)
174                         return 0;
175         }
176
177         if (r < 0) {
178                 /* Ignore ENOENT in some cases */
179                 if (ignore_enoent && errno == ENOENT)
180                         return 0;
181
182                 if (ignore_erofs && errno == EROFS)
183                         return 0;
184
185                 r = log_debug_errno(errno, "Unable to fix SMACK label of %s: %m", path);
186         }
187
188         return r;
189 }
190
191 /// UNNEEDED by elogind
192 #if 0
193 int mac_smack_copy(const char *dest, const char *src) {
194         int r = 0;
195         _cleanup_free_ char *label = NULL;
196
197         assert(dest);
198         assert(src);
199
200         r = mac_smack_read(src, SMACK_ATTR_ACCESS, &label);
201         if (r < 0)
202                 return r;
203
204         r = mac_smack_apply(dest, SMACK_ATTR_ACCESS, label);
205         if (r < 0)
206                 return r;
207
208         return r;
209 }
210 #endif // 0
211
212 #else
213 bool mac_smack_use(void) {
214         return false;
215 }
216
217 /// UNNEEDED by elogind
218 #if 0
219 int mac_smack_read(const char *path, SmackAttr attr, char **label) {
220         return -EOPNOTSUPP;
221 }
222
223 int mac_smack_read_fd(int fd, SmackAttr attr, char **label) {
224         return -EOPNOTSUPP;
225 }
226
227 int mac_smack_apply(const char *path, SmackAttr attr, const char *label) {
228         return 0;
229 }
230
231 int mac_smack_apply_fd(int fd, SmackAttr attr, const char *label) {
232         return 0;
233 }
234
235 int mac_smack_apply_pid(pid_t pid, const char *label) {
236         return 0;
237 }
238 #endif // 0
239
240 int mac_smack_fix(const char *path, bool ignore_enoent, bool ignore_erofs) {
241         return 0;
242 }
243
244 /// UNNEEDED by elogind
245 #if 0
246 int mac_smack_copy(const char *dest, const char *src) {
247         return 0;
248 }
249 #endif // 0
250 #endif