Commit | Line | Data |
---|---|---|
898127c3 JJ |
1 | /* |
2 | * AppArmor security module | |
3 | * | |
4 | * This file contains AppArmor policy attachment and domain transitions | |
5 | * | |
6 | * Copyright (C) 2002-2008 Novell/SUSE | |
7 | * Copyright 2009-2010 Canonical Ltd. | |
8 | * | |
9 | * This program is free software; you can redistribute it and/or | |
10 | * modify it under the terms of the GNU General Public License as | |
11 | * published by the Free Software Foundation, version 2 of the | |
12 | * License. | |
13 | */ | |
14 | ||
15 | #include <linux/errno.h> | |
16 | #include <linux/fdtable.h> | |
17 | #include <linux/file.h> | |
18 | #include <linux/mount.h> | |
19 | #include <linux/syscalls.h> | |
20 | #include <linux/tracehook.h> | |
21 | #include <linux/personality.h> | |
22 | ||
23 | #include "include/audit.h" | |
24 | #include "include/apparmorfs.h" | |
25 | #include "include/context.h" | |
26 | #include "include/domain.h" | |
27 | #include "include/file.h" | |
28 | #include "include/ipc.h" | |
29 | #include "include/match.h" | |
30 | #include "include/path.h" | |
31 | #include "include/policy.h" | |
cff281f6 | 32 | #include "include/policy_ns.h" |
898127c3 JJ |
33 | |
34 | /** | |
35 | * aa_free_domain_entries - free entries in a domain table | |
36 | * @domain: the domain table to free (MAYBE NULL) | |
37 | */ | |
38 | void aa_free_domain_entries(struct aa_domain *domain) | |
39 | { | |
40 | int i; | |
41 | if (domain) { | |
42 | if (!domain->table) | |
43 | return; | |
44 | ||
45 | for (i = 0; i < domain->size; i++) | |
46 | kzfree(domain->table[i]); | |
47 | kzfree(domain->table); | |
48 | domain->table = NULL; | |
49 | } | |
50 | } | |
51 | ||
52 | /** | |
53 | * may_change_ptraced_domain - check if can change profile on ptraced task | |
898127c3 JJ |
54 | * @to_profile: profile to change to (NOT NULL) |
55 | * | |
51775fe7 | 56 | * Check if current is ptraced and if so if the tracing task is allowed |
898127c3 JJ |
57 | * to trace the new domain |
58 | * | |
59 | * Returns: %0 or error if change not allowed | |
60 | */ | |
51775fe7 | 61 | static int may_change_ptraced_domain(struct aa_profile *to_profile) |
898127c3 JJ |
62 | { |
63 | struct task_struct *tracer; | |
898127c3 JJ |
64 | struct aa_profile *tracerp = NULL; |
65 | int error = 0; | |
66 | ||
67 | rcu_read_lock(); | |
51775fe7 | 68 | tracer = ptrace_parent(current); |
3cfcc19e | 69 | if (tracer) |
898127c3 | 70 | /* released below */ |
3cfcc19e | 71 | tracerp = aa_get_task_profile(tracer); |
898127c3 JJ |
72 | |
73 | /* not ptraced */ | |
74 | if (!tracer || unconfined(tracerp)) | |
75 | goto out; | |
76 | ||
dd0c6e86 | 77 | error = aa_may_ptrace(tracerp, to_profile, PTRACE_MODE_ATTACH); |
898127c3 JJ |
78 | |
79 | out: | |
04fdc099 | 80 | rcu_read_unlock(); |
3cfcc19e | 81 | aa_put_profile(tracerp); |
898127c3 JJ |
82 | |
83 | return error; | |
84 | } | |
85 | ||
86 | /** | |
87 | * change_profile_perms - find permissions for change_profile | |
88 | * @profile: the current profile (NOT NULL) | |
89 | * @ns: the namespace being switched to (NOT NULL) | |
90 | * @name: the name of the profile to change to (NOT NULL) | |
91 | * @request: requested perms | |
92 | * @start: state to start matching in | |
93 | * | |
94 | * Returns: permission set | |
95 | */ | |
96 | static struct file_perms change_profile_perms(struct aa_profile *profile, | |
98849dff | 97 | struct aa_ns *ns, |
898127c3 JJ |
98 | const char *name, u32 request, |
99 | unsigned int start) | |
100 | { | |
101 | struct file_perms perms; | |
102 | struct path_cond cond = { }; | |
103 | unsigned int state; | |
104 | ||
105 | if (unconfined(profile)) { | |
106 | perms.allow = AA_MAY_CHANGE_PROFILE | AA_MAY_ONEXEC; | |
107 | perms.audit = perms.quiet = perms.kill = 0; | |
108 | return perms; | |
109 | } else if (!profile->file.dfa) { | |
110 | return nullperms; | |
111 | } else if ((ns == profile->ns)) { | |
112 | /* try matching against rules with out namespace prepended */ | |
113 | aa_str_perms(profile->file.dfa, start, name, &cond, &perms); | |
114 | if (COMBINED_PERM_MASK(perms) & request) | |
115 | return perms; | |
116 | } | |
117 | ||
118 | /* try matching with namespace name and then profile */ | |
119 | state = aa_dfa_match(profile->file.dfa, start, ns->base.name); | |
120 | state = aa_dfa_match_len(profile->file.dfa, state, ":", 1); | |
121 | aa_str_perms(profile->file.dfa, state, name, &cond, &perms); | |
122 | ||
123 | return perms; | |
124 | } | |
125 | ||
126 | /** | |
127 | * __attach_match_ - find an attachment match | |
128 | * @name - to match against (NOT NULL) | |
129 | * @head - profile list to walk (NOT NULL) | |
130 | * | |
131 | * Do a linear search on the profiles in the list. There is a matching | |
132 | * preference where an exact match is preferred over a name which uses | |
133 | * expressions to match, and matching expressions with the greatest | |
134 | * xmatch_len are preferred. | |
135 | * | |
136 | * Requires: @head not be shared or have appropriate locks held | |
137 | * | |
138 | * Returns: profile or NULL if no match found | |
139 | */ | |
140 | static struct aa_profile *__attach_match(const char *name, | |
141 | struct list_head *head) | |
142 | { | |
143 | int len = 0; | |
144 | struct aa_profile *profile, *candidate = NULL; | |
145 | ||
01e2b670 | 146 | list_for_each_entry_rcu(profile, head, base.list) { |
898127c3 JJ |
147 | if (profile->flags & PFLAG_NULL) |
148 | continue; | |
149 | if (profile->xmatch && profile->xmatch_len > len) { | |
150 | unsigned int state = aa_dfa_match(profile->xmatch, | |
151 | DFA_START, name); | |
152 | u32 perm = dfa_user_allow(profile->xmatch, state); | |
153 | /* any accepting state means a valid match. */ | |
154 | if (perm & MAY_EXEC) { | |
155 | candidate = profile; | |
156 | len = profile->xmatch_len; | |
157 | } | |
158 | } else if (!strcmp(profile->base.name, name)) | |
159 | /* exact non-re match, no more searching required */ | |
160 | return profile; | |
161 | } | |
162 | ||
163 | return candidate; | |
164 | } | |
165 | ||
166 | /** | |
167 | * find_attach - do attachment search for unconfined processes | |
168 | * @ns: the current namespace (NOT NULL) | |
169 | * @list: list to search (NOT NULL) | |
170 | * @name: the executable name to match against (NOT NULL) | |
171 | * | |
172 | * Returns: profile or NULL if no match found | |
173 | */ | |
98849dff | 174 | static struct aa_profile *find_attach(struct aa_ns *ns, |
898127c3 JJ |
175 | struct list_head *list, const char *name) |
176 | { | |
177 | struct aa_profile *profile; | |
178 | ||
01e2b670 | 179 | rcu_read_lock(); |
898127c3 | 180 | profile = aa_get_profile(__attach_match(name, list)); |
01e2b670 | 181 | rcu_read_unlock(); |
898127c3 JJ |
182 | |
183 | return profile; | |
184 | } | |
185 | ||
186 | /** | |
187 | * separate_fqname - separate the namespace and profile names | |
188 | * @fqname: the fqname name to split (NOT NULL) | |
189 | * @ns_name: the namespace name if it exists (NOT NULL) | |
190 | * | |
191 | * This is the xtable equivalent routine of aa_split_fqname. It finds the | |
192 | * split in an xtable fqname which contains an embedded \0 instead of a : | |
193 | * if a namespace is specified. This is done so the xtable is constant and | |
194 | * isn't re-split on every lookup. | |
195 | * | |
196 | * Either the profile or namespace name may be optional but if the namespace | |
197 | * is specified the profile name termination must be present. This results | |
198 | * in the following possible encodings: | |
199 | * profile_name\0 | |
200 | * :ns_name\0profile_name\0 | |
201 | * :ns_name\0\0 | |
202 | * | |
203 | * NOTE: the xtable fqname is pre-validated at load time in unpack_trans_table | |
204 | * | |
205 | * Returns: profile name if it is specified else NULL | |
206 | */ | |
207 | static const char *separate_fqname(const char *fqname, const char **ns_name) | |
208 | { | |
209 | const char *name; | |
210 | ||
211 | if (fqname[0] == ':') { | |
212 | /* In this case there is guaranteed to be two \0 terminators | |
213 | * in the string. They are verified at load time by | |
214 | * by unpack_trans_table | |
215 | */ | |
216 | *ns_name = fqname + 1; /* skip : */ | |
217 | name = *ns_name + strlen(*ns_name) + 1; | |
218 | if (!*name) | |
219 | name = NULL; | |
220 | } else { | |
221 | *ns_name = NULL; | |
222 | name = fqname; | |
223 | } | |
224 | ||
225 | return name; | |
226 | } | |
227 | ||
228 | static const char *next_name(int xtype, const char *name) | |
229 | { | |
230 | return NULL; | |
231 | } | |
232 | ||
233 | /** | |
234 | * x_table_lookup - lookup an x transition name via transition table | |
235 | * @profile: current profile (NOT NULL) | |
236 | * @xindex: index into x transition table | |
237 | * | |
238 | * Returns: refcounted profile, or NULL on failure (MAYBE NULL) | |
239 | */ | |
240 | static struct aa_profile *x_table_lookup(struct aa_profile *profile, u32 xindex) | |
241 | { | |
242 | struct aa_profile *new_profile = NULL; | |
98849dff | 243 | struct aa_ns *ns = profile->ns; |
898127c3 JJ |
244 | u32 xtype = xindex & AA_X_TYPE_MASK; |
245 | int index = xindex & AA_X_INDEX_MASK; | |
246 | const char *name; | |
247 | ||
248 | /* index is guaranteed to be in range, validated at load time */ | |
249 | for (name = profile->file.trans.table[index]; !new_profile && name; | |
250 | name = next_name(xtype, name)) { | |
98849dff | 251 | struct aa_ns *new_ns; |
898127c3 JJ |
252 | const char *xname = NULL; |
253 | ||
254 | new_ns = NULL; | |
255 | if (xindex & AA_X_CHILD) { | |
256 | /* release by caller */ | |
257 | new_profile = aa_find_child(profile, name); | |
258 | continue; | |
259 | } else if (*name == ':') { | |
260 | /* switching namespace */ | |
261 | const char *ns_name; | |
262 | xname = name = separate_fqname(name, &ns_name); | |
263 | if (!xname) | |
264 | /* no name so use profile name */ | |
265 | xname = profile->base.hname; | |
266 | if (*ns_name == '@') { | |
267 | /* TODO: variable support */ | |
268 | ; | |
269 | } | |
270 | /* released below */ | |
98849dff | 271 | new_ns = aa_find_ns(ns, ns_name); |
898127c3 JJ |
272 | if (!new_ns) |
273 | continue; | |
274 | } else if (*name == '@') { | |
275 | /* TODO: variable support */ | |
276 | continue; | |
277 | } else { | |
278 | /* basic namespace lookup */ | |
279 | xname = name; | |
280 | } | |
281 | ||
282 | /* released by caller */ | |
283 | new_profile = aa_lookup_profile(new_ns ? new_ns : ns, xname); | |
98849dff | 284 | aa_put_ns(new_ns); |
898127c3 JJ |
285 | } |
286 | ||
287 | /* released by caller */ | |
288 | return new_profile; | |
289 | } | |
290 | ||
291 | /** | |
292 | * x_to_profile - get target profile for a given xindex | |
293 | * @profile: current profile (NOT NULL) | |
294 | * @name: name to lookup (NOT NULL) | |
295 | * @xindex: index into x transition table | |
296 | * | |
297 | * find profile for a transition index | |
298 | * | |
299 | * Returns: refcounted profile or NULL if not found available | |
300 | */ | |
301 | static struct aa_profile *x_to_profile(struct aa_profile *profile, | |
302 | const char *name, u32 xindex) | |
303 | { | |
304 | struct aa_profile *new_profile = NULL; | |
98849dff | 305 | struct aa_ns *ns = profile->ns; |
898127c3 JJ |
306 | u32 xtype = xindex & AA_X_TYPE_MASK; |
307 | ||
308 | switch (xtype) { | |
309 | case AA_X_NONE: | |
310 | /* fail exec unless ix || ux fallback - handled by caller */ | |
311 | return NULL; | |
312 | case AA_X_NAME: | |
313 | if (xindex & AA_X_CHILD) | |
314 | /* released by caller */ | |
315 | new_profile = find_attach(ns, &profile->base.profiles, | |
316 | name); | |
317 | else | |
318 | /* released by caller */ | |
319 | new_profile = find_attach(ns, &ns->base.profiles, | |
320 | name); | |
321 | break; | |
322 | case AA_X_TABLE: | |
323 | /* released by caller */ | |
324 | new_profile = x_table_lookup(profile, xindex); | |
325 | break; | |
326 | } | |
327 | ||
328 | /* released by caller */ | |
329 | return new_profile; | |
330 | } | |
331 | ||
332 | /** | |
333 | * apparmor_bprm_set_creds - set the new creds on the bprm struct | |
334 | * @bprm: binprm for the exec (NOT NULL) | |
335 | * | |
336 | * Returns: %0 or error on failure | |
337 | */ | |
338 | int apparmor_bprm_set_creds(struct linux_binprm *bprm) | |
339 | { | |
55a26ebf | 340 | struct aa_task_ctx *ctx; |
898127c3 | 341 | struct aa_profile *profile, *new_profile = NULL; |
98849dff | 342 | struct aa_ns *ns; |
898127c3 JJ |
343 | char *buffer = NULL; |
344 | unsigned int state; | |
345 | struct file_perms perms = {}; | |
346 | struct path_cond cond = { | |
496ad9aa AV |
347 | file_inode(bprm->file)->i_uid, |
348 | file_inode(bprm->file)->i_mode | |
898127c3 | 349 | }; |
f7da2de0 | 350 | const char *name = NULL, *info = NULL; |
b1d9e6b0 | 351 | int error = 0; |
898127c3 JJ |
352 | |
353 | if (bprm->cred_prepared) | |
354 | return 0; | |
355 | ||
55a26ebf JJ |
356 | ctx = cred_ctx(bprm->cred); |
357 | AA_BUG(!ctx); | |
898127c3 | 358 | |
55a26ebf | 359 | profile = aa_get_newest_profile(ctx->profile); |
4227c333 JJ |
360 | |
361 | /* buffer freed below, name is pointer into buffer */ | |
362 | get_buffers(buffer); | |
898127c3 JJ |
363 | /* |
364 | * get the namespace from the replacement profile as replacement | |
365 | * can change the namespace | |
366 | */ | |
367 | ns = profile->ns; | |
368 | state = profile->file.start; | |
369 | ||
4227c333 | 370 | error = aa_path_name(&bprm->file->f_path, profile->path_flags, buffer, |
72c8a768 | 371 | &name, &info, profile->disconnected); |
898127c3 | 372 | if (error) { |
03816507 JJ |
373 | if (unconfined(profile) || |
374 | (profile->flags & PFLAG_IX_ON_NAME_ERROR)) | |
898127c3 | 375 | error = 0; |
898127c3 JJ |
376 | name = bprm->filename; |
377 | goto audit; | |
378 | } | |
379 | ||
380 | /* Test for onexec first as onexec directives override other | |
381 | * x transitions. | |
382 | */ | |
383 | if (unconfined(profile)) { | |
384 | /* unconfined task */ | |
55a26ebf | 385 | if (ctx->onexec) |
898127c3 | 386 | /* change_profile on exec already been granted */ |
55a26ebf | 387 | new_profile = aa_get_profile(ctx->onexec); |
898127c3 JJ |
388 | else |
389 | new_profile = find_attach(ns, &ns->base.profiles, name); | |
390 | if (!new_profile) | |
391 | goto cleanup; | |
c29bceb3 JJ |
392 | /* |
393 | * NOTE: Domain transitions from unconfined are allowed | |
394 | * even when no_new_privs is set because this aways results | |
395 | * in a further reduction of permissions. | |
396 | */ | |
898127c3 JJ |
397 | goto apply; |
398 | } | |
399 | ||
400 | /* find exec permissions for name */ | |
401 | state = aa_str_perms(profile->file.dfa, state, name, &cond, &perms); | |
55a26ebf | 402 | if (ctx->onexec) { |
898127c3 JJ |
403 | struct file_perms cp; |
404 | info = "change_profile onexec"; | |
55a26ebf | 405 | new_profile = aa_get_newest_profile(ctx->onexec); |
898127c3 JJ |
406 | if (!(perms.allow & AA_MAY_ONEXEC)) |
407 | goto audit; | |
408 | ||
409 | /* test if this exec can be paired with change_profile onexec. | |
410 | * onexec permission is linked to exec with a standard pairing | |
411 | * exec\0change_profile | |
412 | */ | |
413 | state = aa_dfa_null_transition(profile->file.dfa, state); | |
55a26ebf JJ |
414 | cp = change_profile_perms(profile, ctx->onexec->ns, |
415 | ctx->onexec->base.name, | |
898127c3 JJ |
416 | AA_MAY_ONEXEC, state); |
417 | ||
418 | if (!(cp.allow & AA_MAY_ONEXEC)) | |
419 | goto audit; | |
898127c3 JJ |
420 | goto apply; |
421 | } | |
422 | ||
423 | if (perms.allow & MAY_EXEC) { | |
424 | /* exec permission determine how to transition */ | |
425 | new_profile = x_to_profile(profile, name, perms.xindex); | |
426 | if (!new_profile) { | |
427 | if (perms.xindex & AA_X_INHERIT) { | |
428 | /* (p|c|n)ix - don't change profile but do | |
429 | * use the newest version, which was picked | |
430 | * up above when getting profile | |
431 | */ | |
432 | info = "ix fallback"; | |
433 | new_profile = aa_get_profile(profile); | |
434 | goto x_clear; | |
435 | } else if (perms.xindex & AA_X_UNCONFINED) { | |
fa2ac468 | 436 | new_profile = aa_get_newest_profile(ns->unconfined); |
898127c3 JJ |
437 | info = "ux fallback"; |
438 | } else { | |
9049a792 | 439 | error = -EACCES; |
898127c3 | 440 | info = "profile not found"; |
17322cc3 JJ |
441 | /* remove MAY_EXEC to audit as failure */ |
442 | perms.allow &= ~MAY_EXEC; | |
898127c3 JJ |
443 | } |
444 | } | |
445 | } else if (COMPLAIN_MODE(profile)) { | |
446 | /* no exec permission - are we in learning mode */ | |
181f7c97 JJ |
447 | new_profile = aa_new_null_profile(profile, false, name, |
448 | GFP_ATOMIC); | |
898127c3 JJ |
449 | if (!new_profile) { |
450 | error = -ENOMEM; | |
451 | info = "could not create null profile"; | |
f7da2de0 | 452 | } else |
898127c3 | 453 | error = -EACCES; |
898127c3 JJ |
454 | perms.xindex |= AA_X_UNSAFE; |
455 | } else | |
456 | /* fail exec */ | |
457 | error = -EACCES; | |
458 | ||
c29bceb3 JJ |
459 | /* |
460 | * Policy has specified a domain transition, if no_new_privs then | |
461 | * fail the exec. | |
462 | */ | |
463 | if (bprm->unsafe & LSM_UNSAFE_NO_NEW_PRIVS) { | |
c29bceb3 JJ |
464 | error = -EPERM; |
465 | goto cleanup; | |
466 | } | |
467 | ||
898127c3 JJ |
468 | if (!new_profile) |
469 | goto audit; | |
470 | ||
471 | if (bprm->unsafe & LSM_UNSAFE_SHARE) { | |
472 | /* FIXME: currently don't mediate shared state */ | |
473 | ; | |
474 | } | |
475 | ||
9227dd2a | 476 | if (bprm->unsafe & LSM_UNSAFE_PTRACE) { |
51775fe7 | 477 | error = may_change_ptraced_domain(new_profile); |
f7da2de0 | 478 | if (error) |
898127c3 | 479 | goto audit; |
898127c3 JJ |
480 | } |
481 | ||
482 | /* Determine if secure exec is needed. | |
483 | * Can be at this point for the following reasons: | |
484 | * 1. unconfined switching to confined | |
485 | * 2. confined switching to different confinement | |
486 | * 3. confined switching to unconfined | |
487 | * | |
488 | * Cases 2 and 3 are marked as requiring secure exec | |
489 | * (unless policy specified "unsafe exec") | |
490 | * | |
491 | * bprm->unsafe is used to cache the AA_X_UNSAFE permission | |
492 | * to avoid having to recompute in secureexec | |
493 | */ | |
494 | if (!(perms.xindex & AA_X_UNSAFE)) { | |
495 | AA_DEBUG("scrubbing environment variables for %s profile=%s\n", | |
496 | name, new_profile->base.hname); | |
497 | bprm->unsafe |= AA_SECURE_X_NEEDED; | |
498 | } | |
499 | apply: | |
898127c3 JJ |
500 | /* when transitioning profiles clear unsafe personality bits */ |
501 | bprm->per_clear |= PER_CLEAR_ON_SETID; | |
502 | ||
503 | x_clear: | |
55a26ebf JJ |
504 | aa_put_profile(ctx->profile); |
505 | /* transfer new profile reference will be released when ctx is freed */ | |
506 | ctx->profile = new_profile; | |
f7da2de0 | 507 | new_profile = NULL; |
898127c3 JJ |
508 | |
509 | /* clear out all temporary/transitional state from the context */ | |
55a26ebf | 510 | aa_clear_task_ctx_trans(ctx); |
898127c3 JJ |
511 | |
512 | audit: | |
ef88a7ac | 513 | error = aa_audit_file(profile, &perms, OP_EXEC, MAY_EXEC, name, |
f7da2de0 JJ |
514 | new_profile ? new_profile->base.hname : NULL, |
515 | cond.uid, info, error); | |
898127c3 JJ |
516 | |
517 | cleanup: | |
f7da2de0 | 518 | aa_put_profile(new_profile); |
898127c3 | 519 | aa_put_profile(profile); |
4227c333 | 520 | put_buffers(buffer); |
898127c3 JJ |
521 | |
522 | return error; | |
523 | } | |
524 | ||
525 | /** | |
526 | * apparmor_bprm_secureexec - determine if secureexec is needed | |
527 | * @bprm: binprm for exec (NOT NULL) | |
528 | * | |
529 | * Returns: %1 if secureexec is needed else %0 | |
530 | */ | |
531 | int apparmor_bprm_secureexec(struct linux_binprm *bprm) | |
532 | { | |
898127c3 JJ |
533 | /* the decision to use secure exec is computed in set_creds |
534 | * and stored in bprm->unsafe. | |
535 | */ | |
b1d9e6b0 CS |
536 | if (bprm->unsafe & AA_SECURE_X_NEEDED) |
537 | return 1; | |
898127c3 | 538 | |
b1d9e6b0 | 539 | return 0; |
898127c3 JJ |
540 | } |
541 | ||
542 | /** | |
543 | * apparmor_bprm_committing_creds - do task cleanup on committing new creds | |
544 | * @bprm: binprm for the exec (NOT NULL) | |
545 | */ | |
546 | void apparmor_bprm_committing_creds(struct linux_binprm *bprm) | |
547 | { | |
548 | struct aa_profile *profile = __aa_current_profile(); | |
55a26ebf | 549 | struct aa_task_ctx *new_ctx = cred_ctx(bprm->cred); |
898127c3 JJ |
550 | |
551 | /* bail out if unconfined or not changing profile */ | |
55a26ebf JJ |
552 | if ((new_ctx->profile == profile) || |
553 | (unconfined(new_ctx->profile))) | |
898127c3 JJ |
554 | return; |
555 | ||
556 | current->pdeath_signal = 0; | |
557 | ||
558 | /* reset soft limits and set hard limits for the new profile */ | |
55a26ebf | 559 | __aa_transition_rlimits(profile, new_ctx->profile); |
898127c3 JJ |
560 | } |
561 | ||
562 | /** | |
563 | * apparmor_bprm_commited_cred - do cleanup after new creds committed | |
564 | * @bprm: binprm for the exec (NOT NULL) | |
565 | */ | |
566 | void apparmor_bprm_committed_creds(struct linux_binprm *bprm) | |
567 | { | |
568 | /* TODO: cleanup signals - ipc mediation */ | |
569 | return; | |
570 | } | |
571 | ||
572 | /* | |
573 | * Functions for self directed profile change | |
574 | */ | |
575 | ||
576 | /** | |
577 | * new_compound_name - create an hname with @n2 appended to @n1 | |
578 | * @n1: base of hname (NOT NULL) | |
579 | * @n2: name to append (NOT NULL) | |
580 | * | |
581 | * Returns: new name or NULL on error | |
582 | */ | |
583 | static char *new_compound_name(const char *n1, const char *n2) | |
584 | { | |
585 | char *name = kmalloc(strlen(n1) + strlen(n2) + 3, GFP_KERNEL); | |
586 | if (name) | |
587 | sprintf(name, "%s//%s", n1, n2); | |
588 | return name; | |
589 | } | |
590 | ||
591 | /** | |
592 | * aa_change_hat - change hat to/from subprofile | |
593 | * @hats: vector of hat names to try changing into (MAYBE NULL if @count == 0) | |
594 | * @count: number of hat names in @hats | |
595 | * @token: magic value to validate the hat change | |
596 | * @permtest: true if this is just a permission test | |
597 | * | |
598 | * Change to the first profile specified in @hats that exists, and store | |
599 | * the @hat_magic in the current task context. If the count == 0 and the | |
600 | * @token matches that stored in the current task context, return to the | |
601 | * top level profile. | |
602 | * | |
603 | * Returns %0 on success, error otherwise. | |
604 | */ | |
605 | int aa_change_hat(const char *hats[], int count, u64 token, bool permtest) | |
606 | { | |
607 | const struct cred *cred; | |
55a26ebf | 608 | struct aa_task_ctx *ctx; |
898127c3 JJ |
609 | struct aa_profile *profile, *previous_profile, *hat = NULL; |
610 | char *name = NULL; | |
611 | int i; | |
612 | struct file_perms perms = {}; | |
613 | const char *target = NULL, *info = NULL; | |
614 | int error = 0; | |
615 | ||
c29bceb3 JJ |
616 | /* |
617 | * Fail explicitly requested domain transitions if no_new_privs. | |
618 | * There is no exception for unconfined as change_hat is not | |
619 | * available. | |
620 | */ | |
1d4457f9 | 621 | if (task_no_new_privs(current)) |
c29bceb3 JJ |
622 | return -EPERM; |
623 | ||
898127c3 JJ |
624 | /* released below */ |
625 | cred = get_current_cred(); | |
55a26ebf | 626 | ctx = cred_ctx(cred); |
3d40658c | 627 | profile = aa_get_newest_profile(aa_cred_profile(cred)); |
55a26ebf | 628 | previous_profile = aa_get_newest_profile(ctx->previous); |
898127c3 JJ |
629 | |
630 | if (unconfined(profile)) { | |
631 | info = "unconfined"; | |
632 | error = -EPERM; | |
633 | goto audit; | |
634 | } | |
635 | ||
636 | if (count) { | |
637 | /* attempting to change into a new hat or switch to a sibling */ | |
638 | struct aa_profile *root; | |
01e2b670 JJ |
639 | if (PROFILE_IS_HAT(profile)) |
640 | root = aa_get_profile_rcu(&profile->parent); | |
641 | else | |
642 | root = aa_get_profile(profile); | |
898127c3 JJ |
643 | |
644 | /* find first matching hat */ | |
645 | for (i = 0; i < count && !hat; i++) | |
646 | /* released below */ | |
647 | hat = aa_find_child(root, hats[i]); | |
648 | if (!hat) { | |
649 | if (!COMPLAIN_MODE(root) || permtest) { | |
650 | if (list_empty(&root->base.profiles)) | |
651 | error = -ECHILD; | |
652 | else | |
653 | error = -ENOENT; | |
01e2b670 | 654 | aa_put_profile(root); |
898127c3 JJ |
655 | goto out; |
656 | } | |
657 | ||
658 | /* | |
659 | * In complain mode and failed to match any hats. | |
660 | * Audit the failure is based off of the first hat | |
661 | * supplied. This is done due how userspace | |
662 | * interacts with change_hat. | |
663 | * | |
664 | * TODO: Add logging of all failed hats | |
665 | */ | |
666 | ||
667 | /* freed below */ | |
668 | name = new_compound_name(root->base.hname, hats[0]); | |
01e2b670 | 669 | aa_put_profile(root); |
898127c3 JJ |
670 | target = name; |
671 | /* released below */ | |
181f7c97 JJ |
672 | hat = aa_new_null_profile(profile, true, hats[0], |
673 | GFP_KERNEL); | |
898127c3 JJ |
674 | if (!hat) { |
675 | info = "failed null profile create"; | |
676 | error = -ENOMEM; | |
677 | goto audit; | |
678 | } | |
679 | } else { | |
01e2b670 | 680 | aa_put_profile(root); |
898127c3 JJ |
681 | target = hat->base.hname; |
682 | if (!PROFILE_IS_HAT(hat)) { | |
683 | info = "target not hat"; | |
684 | error = -EPERM; | |
685 | goto audit; | |
686 | } | |
687 | } | |
688 | ||
51775fe7 | 689 | error = may_change_ptraced_domain(hat); |
898127c3 JJ |
690 | if (error) { |
691 | info = "ptraced"; | |
692 | error = -EPERM; | |
693 | goto audit; | |
694 | } | |
695 | ||
696 | if (!permtest) { | |
697 | error = aa_set_current_hat(hat, token); | |
698 | if (error == -EACCES) | |
699 | /* kill task in case of brute force attacks */ | |
700 | perms.kill = AA_MAY_CHANGEHAT; | |
701 | else if (name && !error) | |
702 | /* reset error for learning of new hats */ | |
703 | error = -ENOENT; | |
704 | } | |
705 | } else if (previous_profile) { | |
706 | /* Return to saved profile. Kill task if restore fails | |
707 | * to avoid brute force attacks | |
708 | */ | |
709 | target = previous_profile->base.hname; | |
710 | error = aa_restore_previous_profile(token); | |
711 | perms.kill = AA_MAY_CHANGEHAT; | |
712 | } else | |
713 | /* ignore restores when there is no saved profile */ | |
714 | goto out; | |
715 | ||
716 | audit: | |
717 | if (!permtest) | |
ef88a7ac JJ |
718 | error = aa_audit_file(profile, &perms, OP_CHANGE_HAT, |
719 | AA_MAY_CHANGEHAT, NULL, target, | |
720 | GLOBAL_ROOT_UID, info, error); | |
898127c3 JJ |
721 | |
722 | out: | |
723 | aa_put_profile(hat); | |
724 | kfree(name); | |
3d40658c JJ |
725 | aa_put_profile(profile); |
726 | aa_put_profile(previous_profile); | |
898127c3 JJ |
727 | put_cred(cred); |
728 | ||
729 | return error; | |
730 | } | |
731 | ||
732 | /** | |
733 | * aa_change_profile - perform a one-way profile transition | |
aa9a39ad | 734 | * @fqname: name of profile may include namespace (NOT NULL) |
898127c3 JJ |
735 | * @onexec: whether this transition is to take place immediately or at exec |
736 | * @permtest: true if this is just a permission test | |
737 | * | |
738 | * Change to new profile @name. Unlike with hats, there is no way | |
739 | * to change back. If @name isn't specified the current profile name is | |
740 | * used. | |
741 | * If @onexec then the transition is delayed until | |
742 | * the next exec. | |
743 | * | |
744 | * Returns %0 on success, error otherwise. | |
745 | */ | |
aa9a39ad JJ |
746 | int aa_change_profile(const char *fqname, bool onexec, |
747 | bool permtest, bool stack) | |
898127c3 JJ |
748 | { |
749 | const struct cred *cred; | |
898127c3 | 750 | struct aa_profile *profile, *target = NULL; |
898127c3 | 751 | struct file_perms perms = {}; |
aa9a39ad | 752 | const char *info = NULL, *op; |
47f6e5cc | 753 | int error = 0; |
898127c3 JJ |
754 | u32 request; |
755 | ||
aa9a39ad JJ |
756 | if (!fqname || !*fqname) { |
757 | AA_DEBUG("no profile name"); | |
898127c3 | 758 | return -EINVAL; |
aa9a39ad | 759 | } |
898127c3 JJ |
760 | |
761 | if (onexec) { | |
762 | request = AA_MAY_ONEXEC; | |
763 | op = OP_CHANGE_ONEXEC; | |
764 | } else { | |
765 | request = AA_MAY_CHANGE_PROFILE; | |
766 | op = OP_CHANGE_PROFILE; | |
767 | } | |
768 | ||
769 | cred = get_current_cred(); | |
898127c3 JJ |
770 | profile = aa_cred_profile(cred); |
771 | ||
c29bceb3 JJ |
772 | /* |
773 | * Fail explicitly requested domain transitions if no_new_privs | |
774 | * and not unconfined. | |
775 | * Domain transitions from unconfined are allowed even when | |
776 | * no_new_privs is set because this aways results in a reduction | |
777 | * of permissions. | |
778 | */ | |
1d4457f9 | 779 | if (task_no_new_privs(current) && !unconfined(profile)) { |
c29bceb3 JJ |
780 | put_cred(cred); |
781 | return -EPERM; | |
782 | } | |
783 | ||
aa9a39ad | 784 | target = aa_fqlookupn_profile(profile, fqname, strlen(fqname)); |
898127c3 JJ |
785 | if (!target) { |
786 | info = "profile not found"; | |
787 | error = -ENOENT; | |
788 | if (permtest || !COMPLAIN_MODE(profile)) | |
789 | goto audit; | |
790 | /* released below */ | |
aa9a39ad JJ |
791 | target = aa_new_null_profile(profile, false, fqname, |
792 | GFP_KERNEL); | |
898127c3 JJ |
793 | if (!target) { |
794 | info = "failed null profile create"; | |
795 | error = -ENOMEM; | |
796 | goto audit; | |
797 | } | |
798 | } | |
799 | ||
aa9a39ad JJ |
800 | perms = change_profile_perms(profile, target->ns, target->base.hname, |
801 | request, profile->file.start); | |
802 | if (!(perms.allow & request)) { | |
803 | error = -EACCES; | |
804 | goto audit; | |
805 | } | |
806 | ||
898127c3 | 807 | /* check if tracing task is allowed to trace target domain */ |
51775fe7 | 808 | error = may_change_ptraced_domain(target); |
898127c3 JJ |
809 | if (error) { |
810 | info = "ptrace prevents transition"; | |
811 | goto audit; | |
812 | } | |
813 | ||
814 | if (permtest) | |
815 | goto audit; | |
816 | ||
817 | if (onexec) | |
818 | error = aa_set_current_onexec(target); | |
819 | else | |
820 | error = aa_replace_current_profile(target); | |
821 | ||
822 | audit: | |
823 | if (!permtest) | |
aa9a39ad JJ |
824 | error = aa_audit_file(profile, &perms, op, request, NULL, |
825 | fqname, GLOBAL_ROOT_UID, info, error); | |
898127c3 | 826 | |
898127c3 JJ |
827 | aa_put_profile(target); |
828 | put_cred(cred); | |
829 | ||
830 | return error; | |
831 | } |