Debian Codegraph @

src/core/selinux-access.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "selinux-access.h"
4
5 #if HAVE_SELINUX
6
7 #include <unistd.h>
8
9 #include "sd-bus.h"
10 #include "sd-varlink.h"
11
12 #include "alloc-util.h"
13 #include "audit-fd.h"
14 #include "errno-util.h"
15 #include "format-util.h"
16 #include "libaudit-util.h"
17 #include "log.h"
18 #include "selinux-util.h"
19 #include "stdio-util.h"
20 #include "string-util.h"
21 #include "strv.h"
22 #include "unit.h"
23
24 static bool initialized = false;
25
26 struct audit_info {
27 sd_bus_creds *creds;
28 sd_varlink *link;
29 const char *path;
30 const char *cmdline;
31 const char *function;
32 };
33
34 /*
35 Any time an access gets denied this callback will be called
36 with the audit data. We then need to just copy the audit data into the msgbuf.
37 */
38 static int audit_callback(
39 void *auditdata,
40 security_class_t cls,
41 char *msgbuf,
42 size_t msgbufsize) {
43
44 const struct audit_info *audit = auditdata;
45 uid_t uid = 0, login_uid = 0;
46 gid_t gid = 0;
47 char login_uid_buf[DECIMAL_STR_MAX(uid_t) + 1] = "n/a";
48 char uid_buf[DECIMAL_STR_MAX(uid_t) + 1] = "n/a";
49 char gid_buf[DECIMAL_STR_MAX(gid_t) + 1] = "n/a";
50
51 if (audit->creds) {
52 /* DBus case */
53 if (sd_bus_creds_get_audit_login_uid(audit->creds, &login_uid) >= 0)
54 xsprintf(login_uid_buf, UID_FMT, login_uid);
55 if (sd_bus_creds_get_euid(audit->creds, &uid) >= 0)
56 xsprintf(uid_buf, UID_FMT, uid);
57 if (sd_bus_creds_get_egid(audit->creds, &gid) >= 0)
58 xsprintf(gid_buf, GID_FMT, gid);
59 }
60
61 if (audit->link) {
62 /* varlink */
63 if (sd_varlink_get_peer_uid(audit->link, &uid) >= 0)
64 xsprintf(uid_buf, UID_FMT, uid);
65 if (sd_varlink_get_peer_gid(audit->link, &gid) >= 0)
66 xsprintf(gid_buf, GID_FMT, gid);
67 }
68
69 (void) snprintf(msgbuf, msgbufsize,
70 "auid=%s uid=%s gid=%s%s%s%s%s%s%s%s%s%s",
71 login_uid_buf, uid_buf, gid_buf,
72 audit->path ? " path=\"" : "", strempty(audit->path), audit->path ? "\"" : "",
73 audit->cmdline ? " cmdline=\"" : "", strempty(audit->cmdline), audit->cmdline ? "\"" : "",
74 audit->function ? " function=\"" : "", strempty(audit->function), audit->function ? "\"" : "");
75
76 return 0;
77 }
78
79 static int callback_type_to_priority(int type) {
80 switch (type) {
81
82 case SELINUX_ERROR:
83 return LOG_ERR;
84
85 case SELINUX_WARNING:
86 return LOG_WARNING;
87
88 case SELINUX_INFO:
89 return LOG_INFO;
90
91 case SELINUX_AVC:
92 default:
93 return LOG_NOTICE;
94 }
95 }
96
97 /*
98 libselinux uses this callback when access gets denied or other
99 events happen. If audit is turned on, messages will be reported
100 using audit netlink, otherwise they will be logged using the usual
101 channels.
102
103 Code copied from dbus and modified.
104 */
105 _printf_(2, 3) static int log_callback(int type, const char *fmt, ...) {
106 va_list ap;
107 const char *fmt2;
108
109 #if HAVE_AUDIT
110 int fd = get_core_audit_fd();
111
112 if (fd >= 0) {
113 _cleanup_free_ char *buf = NULL;
114 int r;
115
116 va_start(ap, fmt);
117 r = vasprintf(&buf, fmt, ap);
118 va_end(ap);
119
120 if (r >= 0) {
121 if (type == SELINUX_AVC)
122 sym_audit_log_user_avc_message(fd, AUDIT_USER_AVC, buf, NULL, NULL, NULL, getuid());
123 else if (type == SELINUX_ERROR)
124 sym_audit_log_user_avc_message(fd, AUDIT_USER_SELINUX_ERR, buf, NULL, NULL, NULL, getuid());
125
126 return 0;
127 }
128 }
129 #endif
130
131 fmt2 = strjoina("selinux: ", fmt);
132
133 va_start(ap, fmt);
134
135 DISABLE_WARNING_FORMAT_NONLITERAL;
136 log_internalv(LOG_AUTH | callback_type_to_priority(type),
137 0, PROJECT_FILE, __LINE__, __func__,
138 fmt2, ap);
139 REENABLE_WARNING;
140 va_end(ap);
141
142 return 0;
143 }
144
145 static int access_init(sd_bus_error *reterr_error) {
146 int r;
147
148 if (!mac_selinux_use())
149 return 0;
150
151 if (initialized)
152 return 1;
153
154 if (sym_avc_open(NULL, 0) != 0) {
155 /* Passing errno to save original value for later */
156 r = log_selinux_enforcing_errno(errno, "Failed to open the SELinux AVC: %m");
157 if (r == 0)
158 /* log_selinux_enforcing_errno() can return 0 if the enforcement isn't on.
159 * In this case don't do any AVC checks.
160 * The warning we printed is hence all the admin will see. */
161 return 0;
162
163 /* Return an access denied error based on the original errno, if we couldn't load the AVC but
164 * enforcing mode was on, or we couldn't determine whether it is one. */
165 errno = -r;
166 return sd_bus_error_setf(reterr_error, SD_BUS_ERROR_ACCESS_DENIED, "Failed to open the SELinux AVC: %m");
167 }
168
169 sym_selinux_set_callback(SELINUX_CB_AUDIT, (union selinux_callback) { .func_audit = audit_callback });
170 sym_selinux_set_callback(SELINUX_CB_LOG, (union selinux_callback) { .func_log = log_callback });
171
172 initialized = true;
173 return 1;
174 }
175
176 static int get_our_contexts(const Unit *unit, const char **ret_acon, const char **ret_tclass, char **ret_fcon) {
177 _cleanup_freecon_ char *fcon = NULL;
178
179 assert(ret_acon);
180 assert(ret_tclass);
181 assert(ret_fcon);
182
183 if (unit && unit->access_selinux_context) {
184 /* Nice! The unit comes with a SELinux context read from the unit file */
185 *ret_acon = unit->access_selinux_context;
186 *ret_tclass = "service";
187 *ret_fcon = NULL;
188 return 0;
189 }
190
191 /* If no unit context is known, use our own */
192
193 /* Ideally, we should call mac_selinux_get_our_label() here because it
194 * does exactly the same - call getcon_raw(). However, it involves
195 * selinux_init() which opens label DB. It was not part of the
196 * original code. I don't want to change it for now. */
197 if (sym_getcon_raw(&fcon) < 0)
198 return log_debug_errno(errno, "SELinux getcon_raw() failed: %m");
199
200 if (!fcon)
201 return log_debug_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "SELinux returned no context of the current process");
202
203 *ret_acon = fcon;
204 *ret_tclass = "system";
205 *ret_fcon = TAKE_PTR(fcon);
206 return 0;
207 }
208
209 static int check_access(
210 const char *scon,
211 const char *tcon,
212 const char *tclass,
213 const char *permission,
214 struct audit_info *audit_info,
215 sd_bus_error *reterr_error) {
216
217 bool enforce = mac_selinux_enforcing();
218 int r;
219
220 assert(scon);
221 assert(tcon);
222 assert(tclass);
223 assert(permission);
224 assert(audit_info);
225 assert(audit_info->function);
226
227 r = sym_selinux_check_access(scon, tcon, tclass, permission, audit_info);
228 if (r < 0) {
229 errno = -(r = errno_or_else(EPERM));
230
231 if (enforce)
232 sd_bus_error_setf(reterr_error, SD_BUS_ERROR_ACCESS_DENIED, "SELinux policy denies access: %m");
233 }
234
235 return log_selinux_enforcing_errno(
236 r,
237 "SELinux access check scon=%s tcon=%s tclass=%s perm=%s state=%s function=%s path=%s cmdline=%s: %m",
238 scon,
239 tcon,
240 tclass,
241 permission,
242 enforce ? "enforcing" : "permissive",
243 audit_info->function,
244 empty_to_na(audit_info->path),
245 empty_to_na(audit_info->cmdline));
246 }
247
248 /*
249 This function communicates with the kernel to check whether or not it should
250 allow the access.
251 If the machine is in permissive mode it will return ok. Audit messages will
252 still be generated if the access would be denied in enforcing mode.
253 */
254 int mac_selinux_access_check_bus_internal(
255 sd_bus_message *message,
256 const Unit *unit,
257 const char *permission,
258 const char *function,
259 sd_bus_error *reterr_error) {
260
261 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
262 const char *tclass, *scon, *acon;
263 _cleanup_free_ char *cl = NULL;
264 _cleanup_freecon_ char *fcon = NULL;
265 char **cmdline = NULL;
266 bool enforce;
267 int r = 0;
268
269 assert(message);
270 assert(permission);
271 assert(function);
272
273 r = access_init(reterr_error);
274 if (r <= 0)
275 return r;
276
277 /* delay call until we checked in `access_init()` if SELinux is actually enabled */
278 enforce = mac_selinux_enforcing();
279
280 r = sd_bus_query_sender_creds(
281 message,
282 SD_BUS_CREDS_PID|SD_BUS_CREDS_EUID|SD_BUS_CREDS_EGID|
283 SD_BUS_CREDS_CMDLINE|SD_BUS_CREDS_AUDIT_LOGIN_UID|
284 SD_BUS_CREDS_SELINUX_CONTEXT|
285 SD_BUS_CREDS_AUGMENT /* get more bits from /proc */,
286 &creds);
287 if (r < 0)
288 return r;
289
290 /* The SELinux context is something we really should have gotten directly from the message or sender,
291 * and not be an augmented field. If it was augmented we cannot use it for authorization, since this
292 * is racy and vulnerable. Let's add an extra check, just in case, even though this really shouldn't
293 * be possible. */
294 assert_return((sd_bus_creds_get_augmented_mask(creds) & SD_BUS_CREDS_SELINUX_CONTEXT) == 0, -EPERM);
295
296 r = sd_bus_creds_get_selinux_context(creds, &scon);
297 if (r < 0)
298 return r;
299
300 r = get_our_contexts(unit, &acon, &tclass, &fcon);
301 if (r < 0) {
302 log_selinux_enforcing_errno(
303 r,
304 "Failed to retrieves SELinux context of current process (perm=%s)%s: %m",
305 permission,
306 enforce ? "" : ", ignoring");
307
308 if (!enforce)
309 return 0;
310
311 if (r == -EOPNOTSUPP)
312 return sd_bus_error_setf(reterr_error, SD_BUS_ERROR_ACCESS_DENIED, "We appear not to have any SELinux context: %m");
313
314 return sd_bus_error_setf(reterr_error, SD_BUS_ERROR_ACCESS_DENIED, "Failed to get current context: %m");
315 }
316
317 (void) sd_bus_creds_get_cmdline(creds, &cmdline);
318 cl = strv_join(cmdline, " ");
319
320 struct audit_info audit_info = {
321 .creds = creds,
322 .path = unit ? unit->fragment_path : NULL,
323 .cmdline = cl,
324 .function = function,
325 };
326
327 return check_access(scon, acon, tclass, permission, &audit_info, reterr_error);
328 }
329
330 int mac_selinux_access_check_varlink_internal(
331 sd_varlink *link,
332 const Unit *unit,
333 const char *permission,
334 const char *function) {
335
336 _cleanup_freecon_ char *fcon = NULL, *scon = NULL;
337 const char *tclass, *acon;
338 int r;
339
340 assert(link);
341 assert(permission);
342 assert(function);
343
344 r = access_init(/* reterr_error= */ NULL);
345 if (r <= 0)
346 /* access_init() does log_selinux_enforcing_errno() */
347 return r;
348
349 /* delay call until we checked in `access_init()` if SELinux is actually enabled */
350 bool enforce = mac_selinux_enforcing();
351
352 int fd = sd_varlink_get_fd(link);
353 if (fd < 0)
354 return log_selinux_enforcing_errno(fd, "Failed to get varlink peer fd: %m");
355
356 /* We should call mac_selinux_get_peer_label() here similarly to get_our_contexts().
357 * See the explanation there why not. */
358 if (sym_getpeercon_raw(fd, &scon) < 0)
359 return log_selinux_enforcing_errno(
360 errno,
361 "Failed to get peer SELinux context%s: %m",
362 enforce ? "" : ", ignoring");
363
364 if (!scon)
365 return log_selinux_enforcing_errno(
366 SYNTHETIC_ERRNO(EOPNOTSUPP),
367 "Peer does not have SELinux context");
368
369 r = get_our_contexts(unit, &acon, &tclass, &fcon);
370 if (r < 0)
371 return log_selinux_enforcing_errno(
372 r,
373 "Failed to retrieves SELinux context of current process (perm=%s)%s: %m",
374 permission,
375 enforce ? "" : ", ignoring");
376
377 struct audit_info audit_info = {
378 .link = link,
379 .path = unit ? unit->fragment_path : NULL,
380 .function = function,
381 };
382
383 return check_access(scon, acon, tclass, permission, &audit_info, /* reterr_error= */ NULL);
384 }
385
386 #else /* HAVE_SELINUX */
387
388 int mac_selinux_access_check_bus_internal(
389 sd_bus_message *message,
390 const Unit *unit,
391 const char *permission,
392 const char *function,
393 sd_bus_error *reterr_error) {
394
395 return 0;
396 }
397
398 int mac_selinux_access_check_varlink_internal(
399 sd_varlink *link,
400 const Unit *unit,
401 const char *permission,
402 const char *function) {
403 return 0;
404 }
405
406 #endif /* HAVE_SELINUX */