chiark / gitweb /
log: whitespace style fix
[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 "util.h"
27 #include "process-util.h"
28 #include "path-util.h"
29 #include "fileio.h"
30 #include "smack-util.h"
31
32 #ifdef HAVE_SMACK
33 bool mac_smack_use(void) {
34         static int cached_use = -1;
35
36         if (cached_use < 0)
37                 cached_use = access("/sys/fs/smackfs/", F_OK) >= 0;
38
39         return cached_use;
40 }
41
42 /// UNNEEDED by elogind
43 #if 0
44 static const char* const smack_attr_table[_SMACK_ATTR_MAX] = {
45         [SMACK_ATTR_ACCESS]     = "security.SMACK64",
46         [SMACK_ATTR_EXEC]       = "security.SMACK64EXEC",
47         [SMACK_ATTR_MMAP]       = "security.SMACK64MMAP",
48         [SMACK_ATTR_TRANSMUTE]  = "security.SMACK64TRANSMUTE",
49         [SMACK_ATTR_IPIN]       = "security.SMACK64IPIN",
50         [SMACK_ATTR_IPOUT]      = "security.SMACK64IPOUT",
51 };
52
53 DEFINE_STRING_TABLE_LOOKUP(smack_attr, SmackAttr);
54
55 int mac_smack_read(const char *path, SmackAttr attr, char **label) {
56         assert(path);
57         assert(attr >= 0 && attr < _SMACK_ATTR_MAX);
58         assert(label);
59
60         if (!mac_smack_use())
61                 return 0;
62
63         return getxattr_malloc(path, smack_attr_to_string(attr), label, true);
64 }
65
66 int mac_smack_read_fd(int fd, SmackAttr attr, char **label) {
67         assert(fd >= 0);
68         assert(attr >= 0 && attr < _SMACK_ATTR_MAX);
69         assert(label);
70
71         if (!mac_smack_use())
72                 return 0;
73
74         return fgetxattr_malloc(fd, smack_attr_to_string(attr), label);
75 }
76
77 int mac_smack_apply(const char *path, SmackAttr attr, const char *label) {
78         int r;
79
80         assert(path);
81         assert(attr >= 0 && attr < _SMACK_ATTR_MAX);
82
83         if (!mac_smack_use())
84                 return 0;
85
86         if (label)
87                 r = lsetxattr(path, smack_attr_to_string(attr), label, strlen(label), 0);
88         else
89                 r = lremovexattr(path, smack_attr_to_string(attr));
90         if (r < 0)
91                 return -errno;
92
93         return 0;
94 }
95
96 int mac_smack_apply_fd(int fd, SmackAttr attr, const char *label) {
97         int r;
98
99         assert(fd >= 0);
100         assert(attr >= 0 && attr < _SMACK_ATTR_MAX);
101
102         if (!mac_smack_use())
103                 return 0;
104
105         if (label)
106                 r = fsetxattr(fd, smack_attr_to_string(attr), label, strlen(label), 0);
107         else
108                 r = fremovexattr(fd, smack_attr_to_string(attr));
109         if (r < 0)
110                 return -errno;
111
112         return 0;
113 }
114
115 int mac_smack_apply_pid(pid_t pid, const char *label) {
116         const char *p;
117         int r = 0;
118
119         assert(label);
120
121         if (!mac_smack_use())
122                 return 0;
123
124         p = procfs_file_alloca(pid, "attr/current");
125         r = write_string_file(p, label, 0);
126         if (r < 0)
127                 return r;
128
129         return r;
130 }
131 #endif // 0
132
133 int mac_smack_fix(const char *path, bool ignore_enoent, bool ignore_erofs) {
134         struct stat st;
135         int r = 0;
136
137         assert(path);
138
139         if (!mac_smack_use())
140                 return 0;
141
142         /*
143          * Path must be in /dev and must exist
144          */
145         if (!path_startswith(path, "/dev"))
146                 return 0;
147
148         r = lstat(path, &st);
149         if (r >= 0) {
150                 const char *label;
151
152                 /*
153                  * Label directories and character devices "*".
154                  * Label symlinks "_".
155                  * Don't change anything else.
156                  */
157
158                 if (S_ISDIR(st.st_mode))
159                         label = SMACK_STAR_LABEL;
160                 else if (S_ISLNK(st.st_mode))
161                         label = SMACK_FLOOR_LABEL;
162                 else if (S_ISCHR(st.st_mode))
163                         label = SMACK_STAR_LABEL;
164                 else
165                         return 0;
166
167                 r = lsetxattr(path, "security.SMACK64", label, strlen(label), 0);
168
169                 /* If the FS doesn't support labels, then exit without warning */
170                 if (r < 0 && errno == EOPNOTSUPP)
171                         return 0;
172         }
173
174         if (r < 0) {
175                 /* Ignore ENOENT in some cases */
176                 if (ignore_enoent && errno == ENOENT)
177                         return 0;
178
179                 if (ignore_erofs && errno == EROFS)
180                         return 0;
181
182                 r = log_debug_errno(errno, "Unable to fix SMACK label of %s: %m", path);
183         }
184
185         return r;
186 }
187
188 /// UNNEEDED by elogind
189 #if 0
190 int mac_smack_copy(const char *dest, const char *src) {
191         int r = 0;
192         _cleanup_free_ char *label = NULL;
193
194         assert(dest);
195         assert(src);
196
197         r = mac_smack_read(src, SMACK_ATTR_ACCESS, &label);
198         if (r < 0)
199                 return r;
200
201         r = mac_smack_apply(dest, SMACK_ATTR_ACCESS, label);
202         if (r < 0)
203                 return r;
204
205         return r;
206 }
207 #endif // 0
208
209 #else
210 bool mac_smack_use(void) {
211         return false;
212 }
213
214 /// UNNEEDED by elogind
215 #if 0
216 int mac_smack_read(const char *path, SmackAttr attr, char **label) {
217         return -EOPNOTSUPP;
218 }
219
220 int mac_smack_read_fd(int fd, SmackAttr attr, char **label) {
221         return -EOPNOTSUPP;
222 }
223
224 int mac_smack_apply(const char *path, SmackAttr attr, const char *label) {
225         return 0;
226 }
227
228 int mac_smack_apply_fd(int fd, SmackAttr attr, const char *label) {
229         return 0;
230 }
231
232 int mac_smack_apply_pid(pid_t pid, const char *label) {
233         return 0;
234 }
235 #endif // 0
236
237 int mac_smack_fix(const char *path, bool ignore_enoent, bool ignore_erofs) {
238         return 0;
239 }
240
241 /// UNNEEDED by elogind
242 #if 0
243 int mac_smack_copy(const char *dest, const char *src) {
244         return 0;
245 }
246 #endif // 0
247 #endif