| 1 | // SPDX-License-Identifier: GPL-2.0-only |
| 2 | /* |
| 3 | * AppArmor security module |
| 4 | * |
| 5 | * This file contains AppArmor policy attachment and domain transitions |
| 6 | * |
| 7 | * Copyright (C) 2002-2008 Novell/SUSE |
| 8 | * Copyright 2009-2010 Canonical Ltd. |
| 9 | */ |
| 10 | |
| 11 | #include <linux/errno.h> |
| 12 | #include <linux/fdtable.h> |
| 13 | #include <linux/fs.h> |
| 14 | #include <linux/file.h> |
| 15 | #include <linux/mount.h> |
| 16 | #include <linux/syscalls.h> |
| 17 | #include <linux/personality.h> |
| 18 | #include <linux/xattr.h> |
| 19 | #include <linux/user_namespace.h> |
| 20 | |
| 21 | #include "include/audit.h" |
| 22 | #include "include/apparmorfs.h" |
| 23 | #include "include/cred.h" |
| 24 | #include "include/domain.h" |
| 25 | #include "include/file.h" |
| 26 | #include "include/ipc.h" |
| 27 | #include "include/match.h" |
| 28 | #include "include/path.h" |
| 29 | #include "include/policy.h" |
| 30 | #include "include/policy_ns.h" |
| 31 | |
| 32 | /** |
| 33 | * aa_free_domain_entries - free entries in a domain table |
| 34 | * @domain: the domain table to free (MAYBE NULL) |
| 35 | */ |
| 36 | void aa_free_domain_entries(struct aa_domain *domain) |
| 37 | { |
| 38 | int i; |
| 39 | if (domain) { |
| 40 | if (!domain->table) |
| 41 | return; |
| 42 | |
| 43 | for (i = 0; i < domain->size; i++) |
| 44 | kfree_sensitive(domain->table[i]); |
| 45 | kfree_sensitive(domain->table); |
| 46 | domain->table = NULL; |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * may_change_ptraced_domain - check if can change profile on ptraced task |
| 52 | * @to_label: profile to change to (NOT NULL) |
| 53 | * @info: message if there is an error |
| 54 | * |
| 55 | * Check if current is ptraced and if so if the tracing task is allowed |
| 56 | * to trace the new domain |
| 57 | * |
| 58 | * Returns: %0 or error if change not allowed |
| 59 | */ |
| 60 | static int may_change_ptraced_domain(struct aa_label *to_label, |
| 61 | const char **info) |
| 62 | { |
| 63 | struct task_struct *tracer; |
| 64 | struct aa_label *tracerl = NULL; |
| 65 | int error = 0; |
| 66 | |
| 67 | rcu_read_lock(); |
| 68 | tracer = ptrace_parent(current); |
| 69 | if (tracer) |
| 70 | /* released below */ |
| 71 | tracerl = aa_get_task_label(tracer); |
| 72 | |
| 73 | /* not ptraced */ |
| 74 | if (!tracer || unconfined(tracerl)) |
| 75 | goto out; |
| 76 | |
| 77 | error = aa_may_ptrace(tracerl, to_label, PTRACE_MODE_ATTACH); |
| 78 | |
| 79 | out: |
| 80 | rcu_read_unlock(); |
| 81 | aa_put_label(tracerl); |
| 82 | |
| 83 | if (error) |
| 84 | *info = "ptrace prevents transition"; |
| 85 | return error; |
| 86 | } |
| 87 | |
| 88 | /**** TODO: dedup to aa_label_match - needs perm and dfa, merging |
| 89 | * specifically this is an exact copy of aa_label_match except |
| 90 | * aa_compute_perms is replaced with aa_compute_fperms |
| 91 | * and policy.dfa with file.dfa |
| 92 | ****/ |
| 93 | /* match a profile and its associated ns component if needed |
| 94 | * Assumes visibility test has already been done. |
| 95 | * If a subns profile is not to be matched should be prescreened with |
| 96 | * visibility test. |
| 97 | */ |
| 98 | static inline unsigned int match_component(struct aa_profile *profile, |
| 99 | struct aa_profile *tp, |
| 100 | bool stack, unsigned int state) |
| 101 | { |
| 102 | const char *ns_name; |
| 103 | |
| 104 | if (stack) |
| 105 | state = aa_dfa_match(profile->file.dfa, state, "&"); |
| 106 | if (profile->ns == tp->ns) |
| 107 | return aa_dfa_match(profile->file.dfa, state, tp->base.hname); |
| 108 | |
| 109 | /* try matching with namespace name and then profile */ |
| 110 | ns_name = aa_ns_name(profile->ns, tp->ns, true); |
| 111 | state = aa_dfa_match_len(profile->file.dfa, state, ":", 1); |
| 112 | state = aa_dfa_match(profile->file.dfa, state, ns_name); |
| 113 | state = aa_dfa_match_len(profile->file.dfa, state, ":", 1); |
| 114 | return aa_dfa_match(profile->file.dfa, state, tp->base.hname); |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * label_compound_match - find perms for full compound label |
| 119 | * @profile: profile to find perms for |
| 120 | * @label: label to check access permissions for |
| 121 | * @stack: whether this is a stacking request |
| 122 | * @state: state to start match in |
| 123 | * @subns: whether to do permission checks on components in a subns |
| 124 | * @request: permissions to request |
| 125 | * @perms: perms struct to set |
| 126 | * |
| 127 | * Returns: 0 on success else ERROR |
| 128 | * |
| 129 | * For the label A//&B//&C this does the perm match for A//&B//&C |
| 130 | * @perms should be preinitialized with allperms OR a previous permission |
| 131 | * check to be stacked. |
| 132 | */ |
| 133 | static int label_compound_match(struct aa_profile *profile, |
| 134 | struct aa_label *label, bool stack, |
| 135 | unsigned int state, bool subns, u32 request, |
| 136 | struct aa_perms *perms) |
| 137 | { |
| 138 | struct aa_profile *tp; |
| 139 | struct label_it i; |
| 140 | struct path_cond cond = { }; |
| 141 | |
| 142 | /* find first subcomponent that is visible */ |
| 143 | label_for_each(i, label, tp) { |
| 144 | if (!aa_ns_visible(profile->ns, tp->ns, subns)) |
| 145 | continue; |
| 146 | state = match_component(profile, tp, stack, state); |
| 147 | if (!state) |
| 148 | goto fail; |
| 149 | goto next; |
| 150 | } |
| 151 | |
| 152 | /* no component visible */ |
| 153 | *perms = allperms; |
| 154 | return 0; |
| 155 | |
| 156 | next: |
| 157 | label_for_each_cont(i, label, tp) { |
| 158 | if (!aa_ns_visible(profile->ns, tp->ns, subns)) |
| 159 | continue; |
| 160 | state = aa_dfa_match(profile->file.dfa, state, "//&"); |
| 161 | state = match_component(profile, tp, false, state); |
| 162 | if (!state) |
| 163 | goto fail; |
| 164 | } |
| 165 | *perms = *(aa_lookup_fperms(&(profile->file), state, &cond)); |
| 166 | aa_apply_modes_to_perms(profile, perms); |
| 167 | if ((perms->allow & request) != request) |
| 168 | return -EACCES; |
| 169 | |
| 170 | return 0; |
| 171 | |
| 172 | fail: |
| 173 | *perms = nullperms; |
| 174 | return -EACCES; |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * label_components_match - find perms for all subcomponents of a label |
| 179 | * @profile: profile to find perms for |
| 180 | * @label: label to check access permissions for |
| 181 | * @stack: whether this is a stacking request |
| 182 | * @start: state to start match in |
| 183 | * @subns: whether to do permission checks on components in a subns |
| 184 | * @request: permissions to request |
| 185 | * @perms: an initialized perms struct to add accumulation to |
| 186 | * |
| 187 | * Returns: 0 on success else ERROR |
| 188 | * |
| 189 | * For the label A//&B//&C this does the perm match for each of A and B and C |
| 190 | * @perms should be preinitialized with allperms OR a previous permission |
| 191 | * check to be stacked. |
| 192 | */ |
| 193 | static int label_components_match(struct aa_profile *profile, |
| 194 | struct aa_label *label, bool stack, |
| 195 | unsigned int start, bool subns, u32 request, |
| 196 | struct aa_perms *perms) |
| 197 | { |
| 198 | struct aa_profile *tp; |
| 199 | struct label_it i; |
| 200 | struct aa_perms tmp; |
| 201 | struct path_cond cond = { }; |
| 202 | unsigned int state = 0; |
| 203 | |
| 204 | /* find first subcomponent to test */ |
| 205 | label_for_each(i, label, tp) { |
| 206 | if (!aa_ns_visible(profile->ns, tp->ns, subns)) |
| 207 | continue; |
| 208 | state = match_component(profile, tp, stack, start); |
| 209 | if (!state) |
| 210 | goto fail; |
| 211 | goto next; |
| 212 | } |
| 213 | |
| 214 | /* no subcomponents visible - no change in perms */ |
| 215 | return 0; |
| 216 | |
| 217 | next: |
| 218 | tmp = *(aa_lookup_fperms(&(profile->file), state, &cond)); |
| 219 | aa_apply_modes_to_perms(profile, &tmp); |
| 220 | aa_perms_accum(perms, &tmp); |
| 221 | label_for_each_cont(i, label, tp) { |
| 222 | if (!aa_ns_visible(profile->ns, tp->ns, subns)) |
| 223 | continue; |
| 224 | state = match_component(profile, tp, stack, start); |
| 225 | if (!state) |
| 226 | goto fail; |
| 227 | tmp = *(aa_lookup_fperms(&(profile->file), state, &cond)); |
| 228 | aa_apply_modes_to_perms(profile, &tmp); |
| 229 | aa_perms_accum(perms, &tmp); |
| 230 | } |
| 231 | |
| 232 | if ((perms->allow & request) != request) |
| 233 | return -EACCES; |
| 234 | |
| 235 | return 0; |
| 236 | |
| 237 | fail: |
| 238 | *perms = nullperms; |
| 239 | return -EACCES; |
| 240 | } |
| 241 | |
| 242 | /** |
| 243 | * label_match - do a multi-component label match |
| 244 | * @profile: profile to match against (NOT NULL) |
| 245 | * @label: label to match (NOT NULL) |
| 246 | * @stack: whether this is a stacking request |
| 247 | * @state: state to start in |
| 248 | * @subns: whether to match subns components |
| 249 | * @request: permission request |
| 250 | * @perms: Returns computed perms (NOT NULL) |
| 251 | * |
| 252 | * Returns: the state the match finished in, may be the none matching state |
| 253 | */ |
| 254 | static int label_match(struct aa_profile *profile, struct aa_label *label, |
| 255 | bool stack, unsigned int state, bool subns, u32 request, |
| 256 | struct aa_perms *perms) |
| 257 | { |
| 258 | int error; |
| 259 | |
| 260 | *perms = nullperms; |
| 261 | error = label_compound_match(profile, label, stack, state, subns, |
| 262 | request, perms); |
| 263 | if (!error) |
| 264 | return error; |
| 265 | |
| 266 | *perms = allperms; |
| 267 | return label_components_match(profile, label, stack, state, subns, |
| 268 | request, perms); |
| 269 | } |
| 270 | |
| 271 | /******* end TODO: dedup *****/ |
| 272 | |
| 273 | /** |
| 274 | * change_profile_perms - find permissions for change_profile |
| 275 | * @profile: the current profile (NOT NULL) |
| 276 | * @target: label to transition to (NOT NULL) |
| 277 | * @stack: whether this is a stacking request |
| 278 | * @request: requested perms |
| 279 | * @start: state to start matching in |
| 280 | * |
| 281 | * |
| 282 | * Returns: permission set |
| 283 | * |
| 284 | * currently only matches full label A//&B//&C or individual components A, B, C |
| 285 | * not arbitrary combinations. Eg. A//&B, C |
| 286 | */ |
| 287 | static int change_profile_perms(struct aa_profile *profile, |
| 288 | struct aa_label *target, bool stack, |
| 289 | u32 request, unsigned int start, |
| 290 | struct aa_perms *perms) |
| 291 | { |
| 292 | if (profile_unconfined(profile)) { |
| 293 | perms->allow = AA_MAY_CHANGE_PROFILE | AA_MAY_ONEXEC; |
| 294 | perms->audit = perms->quiet = perms->kill = 0; |
| 295 | return 0; |
| 296 | } |
| 297 | |
| 298 | /* TODO: add profile in ns screening */ |
| 299 | return label_match(profile, target, stack, start, true, request, perms); |
| 300 | } |
| 301 | |
| 302 | /** |
| 303 | * aa_xattrs_match - check whether a file matches the xattrs defined in profile |
| 304 | * @bprm: binprm struct for the process to validate |
| 305 | * @profile: profile to match against (NOT NULL) |
| 306 | * @state: state to start match in |
| 307 | * |
| 308 | * Returns: number of extended attributes that matched, or < 0 on error |
| 309 | */ |
| 310 | static int aa_xattrs_match(const struct linux_binprm *bprm, |
| 311 | struct aa_profile *profile, unsigned int state) |
| 312 | { |
| 313 | int i; |
| 314 | ssize_t size; |
| 315 | struct dentry *d; |
| 316 | char *value = NULL; |
| 317 | int value_size = 0, ret = profile->xattr_count; |
| 318 | |
| 319 | if (!bprm || !profile->xattr_count) |
| 320 | return 0; |
| 321 | might_sleep(); |
| 322 | |
| 323 | /* transition from exec match to xattr set */ |
| 324 | state = aa_dfa_outofband_transition(profile->xmatch.dfa, state); |
| 325 | d = bprm->file->f_path.dentry; |
| 326 | |
| 327 | for (i = 0; i < profile->xattr_count; i++) { |
| 328 | size = vfs_getxattr_alloc(&init_user_ns, d, profile->xattrs[i], |
| 329 | &value, value_size, GFP_KERNEL); |
| 330 | if (size >= 0) { |
| 331 | u32 index, perm; |
| 332 | |
| 333 | /* |
| 334 | * Check the xattr presence before value. This ensure |
| 335 | * that not present xattr can be distinguished from a 0 |
| 336 | * length value or rule that matches any value |
| 337 | */ |
| 338 | state = aa_dfa_null_transition(profile->xmatch.dfa, |
| 339 | state); |
| 340 | /* Check xattr value */ |
| 341 | state = aa_dfa_match_len(profile->xmatch.dfa, state, |
| 342 | value, size); |
| 343 | index = ACCEPT_TABLE(profile->xmatch.dfa)[state]; |
| 344 | perm = profile->xmatch.perms[index].allow; |
| 345 | if (!(perm & MAY_EXEC)) { |
| 346 | ret = -EINVAL; |
| 347 | goto out; |
| 348 | } |
| 349 | } |
| 350 | /* transition to next element */ |
| 351 | state = aa_dfa_outofband_transition(profile->xmatch.dfa, state); |
| 352 | if (size < 0) { |
| 353 | /* |
| 354 | * No xattr match, so verify if transition to |
| 355 | * next element was valid. IFF so the xattr |
| 356 | * was optional. |
| 357 | */ |
| 358 | if (!state) { |
| 359 | ret = -EINVAL; |
| 360 | goto out; |
| 361 | } |
| 362 | /* don't count missing optional xattr as matched */ |
| 363 | ret--; |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | out: |
| 368 | kfree(value); |
| 369 | return ret; |
| 370 | } |
| 371 | |
| 372 | /** |
| 373 | * find_attach - do attachment search for unconfined processes |
| 374 | * @bprm - binprm structure of transitioning task |
| 375 | * @ns: the current namespace (NOT NULL) |
| 376 | * @head - profile list to walk (NOT NULL) |
| 377 | * @name - to match against (NOT NULL) |
| 378 | * @info - info message if there was an error (NOT NULL) |
| 379 | * |
| 380 | * Do a linear search on the profiles in the list. There is a matching |
| 381 | * preference where an exact match is preferred over a name which uses |
| 382 | * expressions to match, and matching expressions with the greatest |
| 383 | * xmatch_len are preferred. |
| 384 | * |
| 385 | * Requires: @head not be shared or have appropriate locks held |
| 386 | * |
| 387 | * Returns: label or NULL if no match found |
| 388 | */ |
| 389 | static struct aa_label *find_attach(const struct linux_binprm *bprm, |
| 390 | struct aa_ns *ns, struct list_head *head, |
| 391 | const char *name, const char **info) |
| 392 | { |
| 393 | int candidate_len = 0, candidate_xattrs = 0; |
| 394 | bool conflict = false; |
| 395 | struct aa_profile *profile, *candidate = NULL; |
| 396 | |
| 397 | AA_BUG(!name); |
| 398 | AA_BUG(!head); |
| 399 | |
| 400 | rcu_read_lock(); |
| 401 | restart: |
| 402 | list_for_each_entry_rcu(profile, head, base.list) { |
| 403 | if (profile->label.flags & FLAG_NULL && |
| 404 | &profile->label == ns_unconfined(profile->ns)) |
| 405 | continue; |
| 406 | |
| 407 | /* Find the "best" matching profile. Profiles must |
| 408 | * match the path and extended attributes (if any) |
| 409 | * associated with the file. A more specific path |
| 410 | * match will be preferred over a less specific one, |
| 411 | * and a match with more matching extended attributes |
| 412 | * will be preferred over one with fewer. If the best |
| 413 | * match has both the same level of path specificity |
| 414 | * and the same number of matching extended attributes |
| 415 | * as another profile, signal a conflict and refuse to |
| 416 | * match. |
| 417 | */ |
| 418 | if (profile->xmatch.dfa) { |
| 419 | unsigned int state, count; |
| 420 | u32 index, perm; |
| 421 | |
| 422 | state = aa_dfa_leftmatch(profile->xmatch.dfa, |
| 423 | profile->xmatch.start[AA_CLASS_XMATCH], |
| 424 | name, &count); |
| 425 | index = ACCEPT_TABLE(profile->xmatch.dfa)[state]; |
| 426 | perm = profile->xmatch.perms[index].allow; |
| 427 | /* any accepting state means a valid match. */ |
| 428 | if (perm & MAY_EXEC) { |
| 429 | int ret = 0; |
| 430 | |
| 431 | if (count < candidate_len) |
| 432 | continue; |
| 433 | |
| 434 | if (bprm && profile->xattr_count) { |
| 435 | long rev = READ_ONCE(ns->revision); |
| 436 | |
| 437 | if (!aa_get_profile_not0(profile)) |
| 438 | goto restart; |
| 439 | rcu_read_unlock(); |
| 440 | ret = aa_xattrs_match(bprm, profile, |
| 441 | state); |
| 442 | rcu_read_lock(); |
| 443 | aa_put_profile(profile); |
| 444 | if (rev != |
| 445 | READ_ONCE(ns->revision)) |
| 446 | /* policy changed */ |
| 447 | goto restart; |
| 448 | /* |
| 449 | * Fail matching if the xattrs don't |
| 450 | * match |
| 451 | */ |
| 452 | if (ret < 0) |
| 453 | continue; |
| 454 | } |
| 455 | /* |
| 456 | * TODO: allow for more flexible best match |
| 457 | * |
| 458 | * The new match isn't more specific |
| 459 | * than the current best match |
| 460 | */ |
| 461 | if (count == candidate_len && |
| 462 | ret <= candidate_xattrs) { |
| 463 | /* Match is equivalent, so conflict */ |
| 464 | if (ret == candidate_xattrs) |
| 465 | conflict = true; |
| 466 | continue; |
| 467 | } |
| 468 | |
| 469 | /* Either the same length with more matching |
| 470 | * xattrs, or a longer match |
| 471 | */ |
| 472 | candidate = profile; |
| 473 | candidate_len = max(count, profile->xmatch_len); |
| 474 | candidate_xattrs = ret; |
| 475 | conflict = false; |
| 476 | } |
| 477 | } else if (!strcmp(profile->base.name, name)) { |
| 478 | /* |
| 479 | * old exact non-re match, without conditionals such |
| 480 | * as xattrs. no more searching required |
| 481 | */ |
| 482 | candidate = profile; |
| 483 | goto out; |
| 484 | } |
| 485 | } |
| 486 | |
| 487 | if (!candidate || conflict) { |
| 488 | if (conflict) |
| 489 | *info = "conflicting profile attachments"; |
| 490 | rcu_read_unlock(); |
| 491 | return NULL; |
| 492 | } |
| 493 | |
| 494 | out: |
| 495 | candidate = aa_get_newest_profile(candidate); |
| 496 | rcu_read_unlock(); |
| 497 | |
| 498 | return &candidate->label; |
| 499 | } |
| 500 | |
| 501 | static const char *next_name(int xtype, const char *name) |
| 502 | { |
| 503 | return NULL; |
| 504 | } |
| 505 | |
| 506 | /** |
| 507 | * x_table_lookup - lookup an x transition name via transition table |
| 508 | * @profile: current profile (NOT NULL) |
| 509 | * @xindex: index into x transition table |
| 510 | * @name: returns: name tested to find label (NOT NULL) |
| 511 | * |
| 512 | * Returns: refcounted label, or NULL on failure (MAYBE NULL) |
| 513 | */ |
| 514 | struct aa_label *x_table_lookup(struct aa_profile *profile, u32 xindex, |
| 515 | const char **name) |
| 516 | { |
| 517 | struct aa_label *label = NULL; |
| 518 | u32 xtype = xindex & AA_X_TYPE_MASK; |
| 519 | int index = xindex & AA_X_INDEX_MASK; |
| 520 | |
| 521 | AA_BUG(!name); |
| 522 | |
| 523 | /* index is guaranteed to be in range, validated at load time */ |
| 524 | /* TODO: move lookup parsing to unpack time so this is a straight |
| 525 | * index into the resultant label |
| 526 | */ |
| 527 | for (*name = profile->file.trans.table[index]; !label && *name; |
| 528 | *name = next_name(xtype, *name)) { |
| 529 | if (xindex & AA_X_CHILD) { |
| 530 | struct aa_profile *new_profile; |
| 531 | /* release by caller */ |
| 532 | new_profile = aa_find_child(profile, *name); |
| 533 | if (new_profile) |
| 534 | label = &new_profile->label; |
| 535 | continue; |
| 536 | } |
| 537 | label = aa_label_parse(&profile->label, *name, GFP_KERNEL, |
| 538 | true, false); |
| 539 | if (IS_ERR(label)) |
| 540 | label = NULL; |
| 541 | } |
| 542 | |
| 543 | /* released by caller */ |
| 544 | |
| 545 | return label; |
| 546 | } |
| 547 | |
| 548 | /** |
| 549 | * x_to_label - get target label for a given xindex |
| 550 | * @profile: current profile (NOT NULL) |
| 551 | * @bprm: binprm structure of transitioning task |
| 552 | * @name: name to lookup (NOT NULL) |
| 553 | * @xindex: index into x transition table |
| 554 | * @lookupname: returns: name used in lookup if one was specified (NOT NULL) |
| 555 | * |
| 556 | * find label for a transition index |
| 557 | * |
| 558 | * Returns: refcounted label or NULL if not found available |
| 559 | */ |
| 560 | static struct aa_label *x_to_label(struct aa_profile *profile, |
| 561 | const struct linux_binprm *bprm, |
| 562 | const char *name, u32 xindex, |
| 563 | const char **lookupname, |
| 564 | const char **info) |
| 565 | { |
| 566 | struct aa_label *new = NULL; |
| 567 | struct aa_ns *ns = profile->ns; |
| 568 | u32 xtype = xindex & AA_X_TYPE_MASK; |
| 569 | const char *stack = NULL; |
| 570 | |
| 571 | switch (xtype) { |
| 572 | case AA_X_NONE: |
| 573 | /* fail exec unless ix || ux fallback - handled by caller */ |
| 574 | *lookupname = NULL; |
| 575 | break; |
| 576 | case AA_X_TABLE: |
| 577 | /* TODO: fix when perm mapping done at unload */ |
| 578 | stack = profile->file.trans.table[xindex & AA_X_INDEX_MASK]; |
| 579 | if (*stack != '&') { |
| 580 | /* released by caller */ |
| 581 | new = x_table_lookup(profile, xindex, lookupname); |
| 582 | stack = NULL; |
| 583 | break; |
| 584 | } |
| 585 | fallthrough; /* to X_NAME */ |
| 586 | case AA_X_NAME: |
| 587 | if (xindex & AA_X_CHILD) |
| 588 | /* released by caller */ |
| 589 | new = find_attach(bprm, ns, &profile->base.profiles, |
| 590 | name, info); |
| 591 | else |
| 592 | /* released by caller */ |
| 593 | new = find_attach(bprm, ns, &ns->base.profiles, |
| 594 | name, info); |
| 595 | *lookupname = name; |
| 596 | break; |
| 597 | } |
| 598 | |
| 599 | if (!new) { |
| 600 | if (xindex & AA_X_INHERIT) { |
| 601 | /* (p|c|n)ix - don't change profile but do |
| 602 | * use the newest version |
| 603 | */ |
| 604 | *info = "ix fallback"; |
| 605 | /* no profile && no error */ |
| 606 | new = aa_get_newest_label(&profile->label); |
| 607 | } else if (xindex & AA_X_UNCONFINED) { |
| 608 | new = aa_get_newest_label(ns_unconfined(profile->ns)); |
| 609 | *info = "ux fallback"; |
| 610 | } |
| 611 | } |
| 612 | |
| 613 | if (new && stack) { |
| 614 | /* base the stack on post domain transition */ |
| 615 | struct aa_label *base = new; |
| 616 | |
| 617 | new = aa_label_parse(base, stack, GFP_KERNEL, true, false); |
| 618 | if (IS_ERR(new)) |
| 619 | new = NULL; |
| 620 | aa_put_label(base); |
| 621 | } |
| 622 | |
| 623 | /* released by caller */ |
| 624 | return new; |
| 625 | } |
| 626 | |
| 627 | static struct aa_label *profile_transition(struct aa_profile *profile, |
| 628 | const struct linux_binprm *bprm, |
| 629 | char *buffer, struct path_cond *cond, |
| 630 | bool *secure_exec) |
| 631 | { |
| 632 | struct aa_label *new = NULL; |
| 633 | const char *info = NULL, *name = NULL, *target = NULL; |
| 634 | unsigned int state = profile->file.start[AA_CLASS_FILE]; |
| 635 | struct aa_perms perms = {}; |
| 636 | bool nonewprivs = false; |
| 637 | int error = 0; |
| 638 | |
| 639 | AA_BUG(!profile); |
| 640 | AA_BUG(!bprm); |
| 641 | AA_BUG(!buffer); |
| 642 | |
| 643 | error = aa_path_name(&bprm->file->f_path, profile->path_flags, buffer, |
| 644 | &name, &info, profile->disconnected); |
| 645 | if (error) { |
| 646 | if (profile_unconfined(profile) || |
| 647 | (profile->label.flags & FLAG_IX_ON_NAME_ERROR)) { |
| 648 | AA_DEBUG("name lookup ix on error"); |
| 649 | error = 0; |
| 650 | new = aa_get_newest_label(&profile->label); |
| 651 | } |
| 652 | name = bprm->filename; |
| 653 | goto audit; |
| 654 | } |
| 655 | |
| 656 | if (profile_unconfined(profile)) { |
| 657 | new = find_attach(bprm, profile->ns, |
| 658 | &profile->ns->base.profiles, name, &info); |
| 659 | if (new) { |
| 660 | AA_DEBUG("unconfined attached to new label"); |
| 661 | return new; |
| 662 | } |
| 663 | AA_DEBUG("unconfined exec no attachment"); |
| 664 | return aa_get_newest_label(&profile->label); |
| 665 | } |
| 666 | |
| 667 | /* find exec permissions for name */ |
| 668 | state = aa_str_perms(&(profile->file), state, name, cond, &perms); |
| 669 | if (perms.allow & MAY_EXEC) { |
| 670 | /* exec permission determine how to transition */ |
| 671 | new = x_to_label(profile, bprm, name, perms.xindex, &target, |
| 672 | &info); |
| 673 | if (new && new->proxy == profile->label.proxy && info) { |
| 674 | /* hack ix fallback - improve how this is detected */ |
| 675 | goto audit; |
| 676 | } else if (!new) { |
| 677 | error = -EACCES; |
| 678 | info = "profile transition not found"; |
| 679 | /* remove MAY_EXEC to audit as failure */ |
| 680 | perms.allow &= ~MAY_EXEC; |
| 681 | } |
| 682 | } else if (COMPLAIN_MODE(profile)) { |
| 683 | /* no exec permission - learning mode */ |
| 684 | struct aa_profile *new_profile = NULL; |
| 685 | |
| 686 | new_profile = aa_new_null_profile(profile, false, name, |
| 687 | GFP_KERNEL); |
| 688 | if (!new_profile) { |
| 689 | error = -ENOMEM; |
| 690 | info = "could not create null profile"; |
| 691 | } else { |
| 692 | error = -EACCES; |
| 693 | new = &new_profile->label; |
| 694 | } |
| 695 | perms.xindex |= AA_X_UNSAFE; |
| 696 | } else |
| 697 | /* fail exec */ |
| 698 | error = -EACCES; |
| 699 | |
| 700 | if (!new) |
| 701 | goto audit; |
| 702 | |
| 703 | |
| 704 | if (!(perms.xindex & AA_X_UNSAFE)) { |
| 705 | if (DEBUG_ON) { |
| 706 | dbg_printk("apparmor: scrubbing environment variables" |
| 707 | " for %s profile=", name); |
| 708 | aa_label_printk(new, GFP_KERNEL); |
| 709 | dbg_printk("\n"); |
| 710 | } |
| 711 | *secure_exec = true; |
| 712 | } |
| 713 | |
| 714 | audit: |
| 715 | aa_audit_file(profile, &perms, OP_EXEC, MAY_EXEC, name, target, new, |
| 716 | cond->uid, info, error); |
| 717 | if (!new || nonewprivs) { |
| 718 | aa_put_label(new); |
| 719 | return ERR_PTR(error); |
| 720 | } |
| 721 | |
| 722 | return new; |
| 723 | } |
| 724 | |
| 725 | static int profile_onexec(struct aa_profile *profile, struct aa_label *onexec, |
| 726 | bool stack, const struct linux_binprm *bprm, |
| 727 | char *buffer, struct path_cond *cond, |
| 728 | bool *secure_exec) |
| 729 | { |
| 730 | unsigned int state = profile->file.start[AA_CLASS_FILE]; |
| 731 | struct aa_perms perms = {}; |
| 732 | const char *xname = NULL, *info = "change_profile onexec"; |
| 733 | int error = -EACCES; |
| 734 | |
| 735 | AA_BUG(!profile); |
| 736 | AA_BUG(!onexec); |
| 737 | AA_BUG(!bprm); |
| 738 | AA_BUG(!buffer); |
| 739 | |
| 740 | if (profile_unconfined(profile)) { |
| 741 | /* change_profile on exec already granted */ |
| 742 | /* |
| 743 | * NOTE: Domain transitions from unconfined are allowed |
| 744 | * even when no_new_privs is set because this aways results |
| 745 | * in a further reduction of permissions. |
| 746 | */ |
| 747 | return 0; |
| 748 | } |
| 749 | |
| 750 | error = aa_path_name(&bprm->file->f_path, profile->path_flags, buffer, |
| 751 | &xname, &info, profile->disconnected); |
| 752 | if (error) { |
| 753 | if (profile_unconfined(profile) || |
| 754 | (profile->label.flags & FLAG_IX_ON_NAME_ERROR)) { |
| 755 | AA_DEBUG("name lookup ix on error"); |
| 756 | error = 0; |
| 757 | } |
| 758 | xname = bprm->filename; |
| 759 | goto audit; |
| 760 | } |
| 761 | |
| 762 | /* find exec permissions for name */ |
| 763 | state = aa_str_perms(&(profile->file), state, xname, cond, &perms); |
| 764 | if (!(perms.allow & AA_MAY_ONEXEC)) { |
| 765 | info = "no change_onexec valid for executable"; |
| 766 | goto audit; |
| 767 | } |
| 768 | /* test if this exec can be paired with change_profile onexec. |
| 769 | * onexec permission is linked to exec with a standard pairing |
| 770 | * exec\0change_profile |
| 771 | */ |
| 772 | state = aa_dfa_null_transition(profile->file.dfa, state); |
| 773 | error = change_profile_perms(profile, onexec, stack, AA_MAY_ONEXEC, |
| 774 | state, &perms); |
| 775 | if (error) { |
| 776 | perms.allow &= ~AA_MAY_ONEXEC; |
| 777 | goto audit; |
| 778 | } |
| 779 | |
| 780 | if (!(perms.xindex & AA_X_UNSAFE)) { |
| 781 | if (DEBUG_ON) { |
| 782 | dbg_printk("apparmor: scrubbing environment " |
| 783 | "variables for %s label=", xname); |
| 784 | aa_label_printk(onexec, GFP_KERNEL); |
| 785 | dbg_printk("\n"); |
| 786 | } |
| 787 | *secure_exec = true; |
| 788 | } |
| 789 | |
| 790 | audit: |
| 791 | return aa_audit_file(profile, &perms, OP_EXEC, AA_MAY_ONEXEC, xname, |
| 792 | NULL, onexec, cond->uid, info, error); |
| 793 | } |
| 794 | |
| 795 | /* ensure none ns domain transitions are correctly applied with onexec */ |
| 796 | |
| 797 | static struct aa_label *handle_onexec(struct aa_label *label, |
| 798 | struct aa_label *onexec, bool stack, |
| 799 | const struct linux_binprm *bprm, |
| 800 | char *buffer, struct path_cond *cond, |
| 801 | bool *unsafe) |
| 802 | { |
| 803 | struct aa_profile *profile; |
| 804 | struct aa_label *new; |
| 805 | int error; |
| 806 | |
| 807 | AA_BUG(!label); |
| 808 | AA_BUG(!onexec); |
| 809 | AA_BUG(!bprm); |
| 810 | AA_BUG(!buffer); |
| 811 | |
| 812 | if (!stack) { |
| 813 | error = fn_for_each_in_ns(label, profile, |
| 814 | profile_onexec(profile, onexec, stack, |
| 815 | bprm, buffer, cond, unsafe)); |
| 816 | if (error) |
| 817 | return ERR_PTR(error); |
| 818 | new = fn_label_build_in_ns(label, profile, GFP_KERNEL, |
| 819 | aa_get_newest_label(onexec), |
| 820 | profile_transition(profile, bprm, buffer, |
| 821 | cond, unsafe)); |
| 822 | |
| 823 | } else { |
| 824 | /* TODO: determine how much we want to loosen this */ |
| 825 | error = fn_for_each_in_ns(label, profile, |
| 826 | profile_onexec(profile, onexec, stack, bprm, |
| 827 | buffer, cond, unsafe)); |
| 828 | if (error) |
| 829 | return ERR_PTR(error); |
| 830 | new = fn_label_build_in_ns(label, profile, GFP_KERNEL, |
| 831 | aa_label_merge(&profile->label, onexec, |
| 832 | GFP_KERNEL), |
| 833 | profile_transition(profile, bprm, buffer, |
| 834 | cond, unsafe)); |
| 835 | } |
| 836 | |
| 837 | if (new) |
| 838 | return new; |
| 839 | |
| 840 | /* TODO: get rid of GLOBAL_ROOT_UID */ |
| 841 | error = fn_for_each_in_ns(label, profile, |
| 842 | aa_audit_file(profile, &nullperms, OP_CHANGE_ONEXEC, |
| 843 | AA_MAY_ONEXEC, bprm->filename, NULL, |
| 844 | onexec, GLOBAL_ROOT_UID, |
| 845 | "failed to build target label", -ENOMEM)); |
| 846 | return ERR_PTR(error); |
| 847 | } |
| 848 | |
| 849 | /** |
| 850 | * apparmor_bprm_creds_for_exec - Update the new creds on the bprm struct |
| 851 | * @bprm: binprm for the exec (NOT NULL) |
| 852 | * |
| 853 | * Returns: %0 or error on failure |
| 854 | * |
| 855 | * TODO: once the other paths are done see if we can't refactor into a fn |
| 856 | */ |
| 857 | int apparmor_bprm_creds_for_exec(struct linux_binprm *bprm) |
| 858 | { |
| 859 | struct aa_task_ctx *ctx; |
| 860 | struct aa_label *label, *new = NULL; |
| 861 | struct aa_profile *profile; |
| 862 | char *buffer = NULL; |
| 863 | const char *info = NULL; |
| 864 | int error = 0; |
| 865 | bool unsafe = false; |
| 866 | kuid_t i_uid = i_uid_into_mnt(file_mnt_user_ns(bprm->file), |
| 867 | file_inode(bprm->file)); |
| 868 | struct path_cond cond = { |
| 869 | i_uid, |
| 870 | file_inode(bprm->file)->i_mode |
| 871 | }; |
| 872 | |
| 873 | ctx = task_ctx(current); |
| 874 | AA_BUG(!cred_label(bprm->cred)); |
| 875 | AA_BUG(!ctx); |
| 876 | |
| 877 | label = aa_get_newest_label(cred_label(bprm->cred)); |
| 878 | |
| 879 | /* |
| 880 | * Detect no new privs being set, and store the label it |
| 881 | * occurred under. Ideally this would happen when nnp |
| 882 | * is set but there isn't a good way to do that yet. |
| 883 | * |
| 884 | * Testing for unconfined must be done before the subset test |
| 885 | */ |
| 886 | if ((bprm->unsafe & LSM_UNSAFE_NO_NEW_PRIVS) && !unconfined(label) && |
| 887 | !ctx->nnp) |
| 888 | ctx->nnp = aa_get_label(label); |
| 889 | |
| 890 | /* buffer freed below, name is pointer into buffer */ |
| 891 | buffer = aa_get_buffer(false); |
| 892 | if (!buffer) { |
| 893 | error = -ENOMEM; |
| 894 | goto done; |
| 895 | } |
| 896 | |
| 897 | /* Test for onexec first as onexec override other x transitions. */ |
| 898 | if (ctx->onexec) |
| 899 | new = handle_onexec(label, ctx->onexec, ctx->token, |
| 900 | bprm, buffer, &cond, &unsafe); |
| 901 | else |
| 902 | new = fn_label_build(label, profile, GFP_KERNEL, |
| 903 | profile_transition(profile, bprm, buffer, |
| 904 | &cond, &unsafe)); |
| 905 | |
| 906 | AA_BUG(!new); |
| 907 | if (IS_ERR(new)) { |
| 908 | error = PTR_ERR(new); |
| 909 | goto done; |
| 910 | } else if (!new) { |
| 911 | error = -ENOMEM; |
| 912 | goto done; |
| 913 | } |
| 914 | |
| 915 | /* Policy has specified a domain transitions. If no_new_privs and |
| 916 | * confined ensure the transition is to confinement that is subset |
| 917 | * of the confinement when the task entered no new privs. |
| 918 | * |
| 919 | * NOTE: Domain transitions from unconfined and to stacked |
| 920 | * subsets are allowed even when no_new_privs is set because this |
| 921 | * aways results in a further reduction of permissions. |
| 922 | */ |
| 923 | if ((bprm->unsafe & LSM_UNSAFE_NO_NEW_PRIVS) && |
| 924 | !unconfined(label) && |
| 925 | !aa_label_is_unconfined_subset(new, ctx->nnp)) { |
| 926 | error = -EPERM; |
| 927 | info = "no new privs"; |
| 928 | goto audit; |
| 929 | } |
| 930 | |
| 931 | if (bprm->unsafe & LSM_UNSAFE_SHARE) { |
| 932 | /* FIXME: currently don't mediate shared state */ |
| 933 | ; |
| 934 | } |
| 935 | |
| 936 | if (bprm->unsafe & (LSM_UNSAFE_PTRACE)) { |
| 937 | /* TODO: test needs to be profile of label to new */ |
| 938 | error = may_change_ptraced_domain(new, &info); |
| 939 | if (error) |
| 940 | goto audit; |
| 941 | } |
| 942 | |
| 943 | if (unsafe) { |
| 944 | if (DEBUG_ON) { |
| 945 | dbg_printk("scrubbing environment variables for %s " |
| 946 | "label=", bprm->filename); |
| 947 | aa_label_printk(new, GFP_KERNEL); |
| 948 | dbg_printk("\n"); |
| 949 | } |
| 950 | bprm->secureexec = 1; |
| 951 | } |
| 952 | |
| 953 | if (label->proxy != new->proxy) { |
| 954 | /* when transitioning clear unsafe personality bits */ |
| 955 | if (DEBUG_ON) { |
| 956 | dbg_printk("apparmor: clearing unsafe personality " |
| 957 | "bits. %s label=", bprm->filename); |
| 958 | aa_label_printk(new, GFP_KERNEL); |
| 959 | dbg_printk("\n"); |
| 960 | } |
| 961 | bprm->per_clear |= PER_CLEAR_ON_SETID; |
| 962 | } |
| 963 | aa_put_label(cred_label(bprm->cred)); |
| 964 | /* transfer reference, released when cred is freed */ |
| 965 | set_cred_label(bprm->cred, new); |
| 966 | |
| 967 | done: |
| 968 | aa_put_label(label); |
| 969 | aa_put_buffer(buffer); |
| 970 | |
| 971 | return error; |
| 972 | |
| 973 | audit: |
| 974 | error = fn_for_each(label, profile, |
| 975 | aa_audit_file(profile, &nullperms, OP_EXEC, MAY_EXEC, |
| 976 | bprm->filename, NULL, new, |
| 977 | i_uid, info, error)); |
| 978 | aa_put_label(new); |
| 979 | goto done; |
| 980 | } |
| 981 | |
| 982 | /* |
| 983 | * Functions for self directed profile change |
| 984 | */ |
| 985 | |
| 986 | |
| 987 | /* helper fn for change_hat |
| 988 | * |
| 989 | * Returns: label for hat transition OR ERR_PTR. Does NOT return NULL |
| 990 | */ |
| 991 | static struct aa_label *build_change_hat(struct aa_profile *profile, |
| 992 | const char *name, bool sibling) |
| 993 | { |
| 994 | struct aa_profile *root, *hat = NULL; |
| 995 | const char *info = NULL; |
| 996 | int error = 0; |
| 997 | |
| 998 | if (sibling && PROFILE_IS_HAT(profile)) { |
| 999 | root = aa_get_profile_rcu(&profile->parent); |
| 1000 | } else if (!sibling && !PROFILE_IS_HAT(profile)) { |
| 1001 | root = aa_get_profile(profile); |
| 1002 | } else { |
| 1003 | info = "conflicting target types"; |
| 1004 | error = -EPERM; |
| 1005 | goto audit; |
| 1006 | } |
| 1007 | |
| 1008 | hat = aa_find_child(root, name); |
| 1009 | if (!hat) { |
| 1010 | error = -ENOENT; |
| 1011 | if (COMPLAIN_MODE(profile)) { |
| 1012 | hat = aa_new_null_profile(profile, true, name, |
| 1013 | GFP_KERNEL); |
| 1014 | if (!hat) { |
| 1015 | info = "failed null profile create"; |
| 1016 | error = -ENOMEM; |
| 1017 | } |
| 1018 | } |
| 1019 | } |
| 1020 | aa_put_profile(root); |
| 1021 | |
| 1022 | audit: |
| 1023 | aa_audit_file(profile, &nullperms, OP_CHANGE_HAT, AA_MAY_CHANGEHAT, |
| 1024 | name, hat ? hat->base.hname : NULL, |
| 1025 | hat ? &hat->label : NULL, GLOBAL_ROOT_UID, info, |
| 1026 | error); |
| 1027 | if (!hat || (error && error != -ENOENT)) |
| 1028 | return ERR_PTR(error); |
| 1029 | /* if hat && error - complain mode, already audited and we adjust for |
| 1030 | * complain mode allow by returning hat->label |
| 1031 | */ |
| 1032 | return &hat->label; |
| 1033 | } |
| 1034 | |
| 1035 | /* helper fn for changing into a hat |
| 1036 | * |
| 1037 | * Returns: label for hat transition or ERR_PTR. Does not return NULL |
| 1038 | */ |
| 1039 | static struct aa_label *change_hat(struct aa_label *label, const char *hats[], |
| 1040 | int count, int flags) |
| 1041 | { |
| 1042 | struct aa_profile *profile, *root, *hat = NULL; |
| 1043 | struct aa_label *new; |
| 1044 | struct label_it it; |
| 1045 | bool sibling = false; |
| 1046 | const char *name, *info = NULL; |
| 1047 | int i, error; |
| 1048 | |
| 1049 | AA_BUG(!label); |
| 1050 | AA_BUG(!hats); |
| 1051 | AA_BUG(count < 1); |
| 1052 | |
| 1053 | if (PROFILE_IS_HAT(labels_profile(label))) |
| 1054 | sibling = true; |
| 1055 | |
| 1056 | /*find first matching hat */ |
| 1057 | for (i = 0; i < count && !hat; i++) { |
| 1058 | name = hats[i]; |
| 1059 | label_for_each_in_ns(it, labels_ns(label), label, profile) { |
| 1060 | if (sibling && PROFILE_IS_HAT(profile)) { |
| 1061 | root = aa_get_profile_rcu(&profile->parent); |
| 1062 | } else if (!sibling && !PROFILE_IS_HAT(profile)) { |
| 1063 | root = aa_get_profile(profile); |
| 1064 | } else { /* conflicting change type */ |
| 1065 | info = "conflicting targets types"; |
| 1066 | error = -EPERM; |
| 1067 | goto fail; |
| 1068 | } |
| 1069 | hat = aa_find_child(root, name); |
| 1070 | aa_put_profile(root); |
| 1071 | if (!hat) { |
| 1072 | if (!COMPLAIN_MODE(profile)) |
| 1073 | goto outer_continue; |
| 1074 | /* complain mode succeed as if hat */ |
| 1075 | } else if (!PROFILE_IS_HAT(hat)) { |
| 1076 | info = "target not hat"; |
| 1077 | error = -EPERM; |
| 1078 | aa_put_profile(hat); |
| 1079 | goto fail; |
| 1080 | } |
| 1081 | aa_put_profile(hat); |
| 1082 | } |
| 1083 | /* found a hat for all profiles in ns */ |
| 1084 | goto build; |
| 1085 | outer_continue: |
| 1086 | ; |
| 1087 | } |
| 1088 | /* no hats that match, find appropriate error |
| 1089 | * |
| 1090 | * In complain mode audit of the failure is based off of the first |
| 1091 | * hat supplied. This is done due how userspace interacts with |
| 1092 | * change_hat. |
| 1093 | */ |
| 1094 | name = NULL; |
| 1095 | label_for_each_in_ns(it, labels_ns(label), label, profile) { |
| 1096 | if (!list_empty(&profile->base.profiles)) { |
| 1097 | info = "hat not found"; |
| 1098 | error = -ENOENT; |
| 1099 | goto fail; |
| 1100 | } |
| 1101 | } |
| 1102 | info = "no hats defined"; |
| 1103 | error = -ECHILD; |
| 1104 | |
| 1105 | fail: |
| 1106 | label_for_each_in_ns(it, labels_ns(label), label, profile) { |
| 1107 | /* |
| 1108 | * no target as it has failed to be found or built |
| 1109 | * |
| 1110 | * change_hat uses probing and should not log failures |
| 1111 | * related to missing hats |
| 1112 | */ |
| 1113 | /* TODO: get rid of GLOBAL_ROOT_UID */ |
| 1114 | if (count > 1 || COMPLAIN_MODE(profile)) { |
| 1115 | aa_audit_file(profile, &nullperms, OP_CHANGE_HAT, |
| 1116 | AA_MAY_CHANGEHAT, name, NULL, NULL, |
| 1117 | GLOBAL_ROOT_UID, info, error); |
| 1118 | } |
| 1119 | } |
| 1120 | return ERR_PTR(error); |
| 1121 | |
| 1122 | build: |
| 1123 | new = fn_label_build_in_ns(label, profile, GFP_KERNEL, |
| 1124 | build_change_hat(profile, name, sibling), |
| 1125 | aa_get_label(&profile->label)); |
| 1126 | if (!new) { |
| 1127 | info = "label build failed"; |
| 1128 | error = -ENOMEM; |
| 1129 | goto fail; |
| 1130 | } /* else if (IS_ERR) build_change_hat has logged error so return new */ |
| 1131 | |
| 1132 | return new; |
| 1133 | } |
| 1134 | |
| 1135 | /** |
| 1136 | * aa_change_hat - change hat to/from subprofile |
| 1137 | * @hats: vector of hat names to try changing into (MAYBE NULL if @count == 0) |
| 1138 | * @count: number of hat names in @hats |
| 1139 | * @token: magic value to validate the hat change |
| 1140 | * @flags: flags affecting behavior of the change |
| 1141 | * |
| 1142 | * Returns %0 on success, error otherwise. |
| 1143 | * |
| 1144 | * Change to the first profile specified in @hats that exists, and store |
| 1145 | * the @hat_magic in the current task context. If the count == 0 and the |
| 1146 | * @token matches that stored in the current task context, return to the |
| 1147 | * top level profile. |
| 1148 | * |
| 1149 | * change_hat only applies to profiles in the current ns, and each profile |
| 1150 | * in the ns must make the same transition otherwise change_hat will fail. |
| 1151 | */ |
| 1152 | int aa_change_hat(const char *hats[], int count, u64 token, int flags) |
| 1153 | { |
| 1154 | const struct cred *cred; |
| 1155 | struct aa_task_ctx *ctx = task_ctx(current); |
| 1156 | struct aa_label *label, *previous, *new = NULL, *target = NULL; |
| 1157 | struct aa_profile *profile; |
| 1158 | struct aa_perms perms = {}; |
| 1159 | const char *info = NULL; |
| 1160 | int error = 0; |
| 1161 | |
| 1162 | /* released below */ |
| 1163 | cred = get_current_cred(); |
| 1164 | label = aa_get_newest_cred_label(cred); |
| 1165 | previous = aa_get_newest_label(ctx->previous); |
| 1166 | |
| 1167 | /* |
| 1168 | * Detect no new privs being set, and store the label it |
| 1169 | * occurred under. Ideally this would happen when nnp |
| 1170 | * is set but there isn't a good way to do that yet. |
| 1171 | * |
| 1172 | * Testing for unconfined must be done before the subset test |
| 1173 | */ |
| 1174 | if (task_no_new_privs(current) && !unconfined(label) && !ctx->nnp) |
| 1175 | ctx->nnp = aa_get_label(label); |
| 1176 | |
| 1177 | if (unconfined(label)) { |
| 1178 | info = "unconfined can not change_hat"; |
| 1179 | error = -EPERM; |
| 1180 | goto fail; |
| 1181 | } |
| 1182 | |
| 1183 | if (count) { |
| 1184 | new = change_hat(label, hats, count, flags); |
| 1185 | AA_BUG(!new); |
| 1186 | if (IS_ERR(new)) { |
| 1187 | error = PTR_ERR(new); |
| 1188 | new = NULL; |
| 1189 | /* already audited */ |
| 1190 | goto out; |
| 1191 | } |
| 1192 | |
| 1193 | error = may_change_ptraced_domain(new, &info); |
| 1194 | if (error) |
| 1195 | goto fail; |
| 1196 | |
| 1197 | /* |
| 1198 | * no new privs prevents domain transitions that would |
| 1199 | * reduce restrictions. |
| 1200 | */ |
| 1201 | if (task_no_new_privs(current) && !unconfined(label) && |
| 1202 | !aa_label_is_unconfined_subset(new, ctx->nnp)) { |
| 1203 | /* not an apparmor denial per se, so don't log it */ |
| 1204 | AA_DEBUG("no_new_privs - change_hat denied"); |
| 1205 | error = -EPERM; |
| 1206 | goto out; |
| 1207 | } |
| 1208 | |
| 1209 | if (flags & AA_CHANGE_TEST) |
| 1210 | goto out; |
| 1211 | |
| 1212 | target = new; |
| 1213 | error = aa_set_current_hat(new, token); |
| 1214 | if (error == -EACCES) |
| 1215 | /* kill task in case of brute force attacks */ |
| 1216 | goto kill; |
| 1217 | } else if (previous && !(flags & AA_CHANGE_TEST)) { |
| 1218 | /* |
| 1219 | * no new privs prevents domain transitions that would |
| 1220 | * reduce restrictions. |
| 1221 | */ |
| 1222 | if (task_no_new_privs(current) && !unconfined(label) && |
| 1223 | !aa_label_is_unconfined_subset(previous, ctx->nnp)) { |
| 1224 | /* not an apparmor denial per se, so don't log it */ |
| 1225 | AA_DEBUG("no_new_privs - change_hat denied"); |
| 1226 | error = -EPERM; |
| 1227 | goto out; |
| 1228 | } |
| 1229 | |
| 1230 | /* Return to saved label. Kill task if restore fails |
| 1231 | * to avoid brute force attacks |
| 1232 | */ |
| 1233 | target = previous; |
| 1234 | error = aa_restore_previous_label(token); |
| 1235 | if (error) { |
| 1236 | if (error == -EACCES) |
| 1237 | goto kill; |
| 1238 | goto fail; |
| 1239 | } |
| 1240 | } /* else ignore @flags && restores when there is no saved profile */ |
| 1241 | |
| 1242 | out: |
| 1243 | aa_put_label(new); |
| 1244 | aa_put_label(previous); |
| 1245 | aa_put_label(label); |
| 1246 | put_cred(cred); |
| 1247 | |
| 1248 | return error; |
| 1249 | |
| 1250 | kill: |
| 1251 | info = "failed token match"; |
| 1252 | perms.kill = AA_MAY_CHANGEHAT; |
| 1253 | |
| 1254 | fail: |
| 1255 | fn_for_each_in_ns(label, profile, |
| 1256 | aa_audit_file(profile, &perms, OP_CHANGE_HAT, |
| 1257 | AA_MAY_CHANGEHAT, NULL, NULL, target, |
| 1258 | GLOBAL_ROOT_UID, info, error)); |
| 1259 | |
| 1260 | goto out; |
| 1261 | } |
| 1262 | |
| 1263 | |
| 1264 | static int change_profile_perms_wrapper(const char *op, const char *name, |
| 1265 | struct aa_profile *profile, |
| 1266 | struct aa_label *target, bool stack, |
| 1267 | u32 request, struct aa_perms *perms) |
| 1268 | { |
| 1269 | const char *info = NULL; |
| 1270 | int error = 0; |
| 1271 | |
| 1272 | if (!error) |
| 1273 | error = change_profile_perms(profile, target, stack, request, |
| 1274 | profile->file.start[AA_CLASS_FILE], |
| 1275 | perms); |
| 1276 | if (error) |
| 1277 | error = aa_audit_file(profile, perms, op, request, name, |
| 1278 | NULL, target, GLOBAL_ROOT_UID, info, |
| 1279 | error); |
| 1280 | |
| 1281 | return error; |
| 1282 | } |
| 1283 | |
| 1284 | /** |
| 1285 | * aa_change_profile - perform a one-way profile transition |
| 1286 | * @fqname: name of profile may include namespace (NOT NULL) |
| 1287 | * @flags: flags affecting change behavior |
| 1288 | * |
| 1289 | * Change to new profile @name. Unlike with hats, there is no way |
| 1290 | * to change back. If @name isn't specified the current profile name is |
| 1291 | * used. |
| 1292 | * If @onexec then the transition is delayed until |
| 1293 | * the next exec. |
| 1294 | * |
| 1295 | * Returns %0 on success, error otherwise. |
| 1296 | */ |
| 1297 | int aa_change_profile(const char *fqname, int flags) |
| 1298 | { |
| 1299 | struct aa_label *label, *new = NULL, *target = NULL; |
| 1300 | struct aa_profile *profile; |
| 1301 | struct aa_perms perms = {}; |
| 1302 | const char *info = NULL; |
| 1303 | const char *auditname = fqname; /* retain leading & if stack */ |
| 1304 | bool stack = flags & AA_CHANGE_STACK; |
| 1305 | struct aa_task_ctx *ctx = task_ctx(current); |
| 1306 | int error = 0; |
| 1307 | char *op; |
| 1308 | u32 request; |
| 1309 | |
| 1310 | label = aa_get_current_label(); |
| 1311 | |
| 1312 | /* |
| 1313 | * Detect no new privs being set, and store the label it |
| 1314 | * occurred under. Ideally this would happen when nnp |
| 1315 | * is set but there isn't a good way to do that yet. |
| 1316 | * |
| 1317 | * Testing for unconfined must be done before the subset test |
| 1318 | */ |
| 1319 | if (task_no_new_privs(current) && !unconfined(label) && !ctx->nnp) |
| 1320 | ctx->nnp = aa_get_label(label); |
| 1321 | |
| 1322 | if (!fqname || !*fqname) { |
| 1323 | aa_put_label(label); |
| 1324 | AA_DEBUG("no profile name"); |
| 1325 | return -EINVAL; |
| 1326 | } |
| 1327 | |
| 1328 | if (flags & AA_CHANGE_ONEXEC) { |
| 1329 | request = AA_MAY_ONEXEC; |
| 1330 | if (stack) |
| 1331 | op = OP_STACK_ONEXEC; |
| 1332 | else |
| 1333 | op = OP_CHANGE_ONEXEC; |
| 1334 | } else { |
| 1335 | request = AA_MAY_CHANGE_PROFILE; |
| 1336 | if (stack) |
| 1337 | op = OP_STACK; |
| 1338 | else |
| 1339 | op = OP_CHANGE_PROFILE; |
| 1340 | } |
| 1341 | |
| 1342 | if (*fqname == '&') { |
| 1343 | stack = true; |
| 1344 | /* don't have label_parse() do stacking */ |
| 1345 | fqname++; |
| 1346 | } |
| 1347 | target = aa_label_parse(label, fqname, GFP_KERNEL, true, false); |
| 1348 | if (IS_ERR(target)) { |
| 1349 | struct aa_profile *tprofile; |
| 1350 | |
| 1351 | info = "label not found"; |
| 1352 | error = PTR_ERR(target); |
| 1353 | target = NULL; |
| 1354 | /* |
| 1355 | * TODO: fixme using labels_profile is not right - do profile |
| 1356 | * per complain profile |
| 1357 | */ |
| 1358 | if ((flags & AA_CHANGE_TEST) || |
| 1359 | !COMPLAIN_MODE(labels_profile(label))) |
| 1360 | goto audit; |
| 1361 | /* released below */ |
| 1362 | tprofile = aa_new_null_profile(labels_profile(label), false, |
| 1363 | fqname, GFP_KERNEL); |
| 1364 | if (!tprofile) { |
| 1365 | info = "failed null profile create"; |
| 1366 | error = -ENOMEM; |
| 1367 | goto audit; |
| 1368 | } |
| 1369 | target = &tprofile->label; |
| 1370 | goto check; |
| 1371 | } |
| 1372 | |
| 1373 | /* |
| 1374 | * self directed transitions only apply to current policy ns |
| 1375 | * TODO: currently requiring perms for stacking and straight change |
| 1376 | * stacking doesn't strictly need this. Determine how much |
| 1377 | * we want to loosen this restriction for stacking |
| 1378 | * |
| 1379 | * if (!stack) { |
| 1380 | */ |
| 1381 | error = fn_for_each_in_ns(label, profile, |
| 1382 | change_profile_perms_wrapper(op, auditname, |
| 1383 | profile, target, stack, |
| 1384 | request, &perms)); |
| 1385 | if (error) |
| 1386 | /* auditing done in change_profile_perms_wrapper */ |
| 1387 | goto out; |
| 1388 | |
| 1389 | /* } */ |
| 1390 | |
| 1391 | check: |
| 1392 | /* check if tracing task is allowed to trace target domain */ |
| 1393 | error = may_change_ptraced_domain(target, &info); |
| 1394 | if (error && !fn_for_each_in_ns(label, profile, |
| 1395 | COMPLAIN_MODE(profile))) |
| 1396 | goto audit; |
| 1397 | |
| 1398 | /* TODO: add permission check to allow this |
| 1399 | * if ((flags & AA_CHANGE_ONEXEC) && !current_is_single_threaded()) { |
| 1400 | * info = "not a single threaded task"; |
| 1401 | * error = -EACCES; |
| 1402 | * goto audit; |
| 1403 | * } |
| 1404 | */ |
| 1405 | if (flags & AA_CHANGE_TEST) |
| 1406 | goto out; |
| 1407 | |
| 1408 | /* stacking is always a subset, so only check the nonstack case */ |
| 1409 | if (!stack) { |
| 1410 | new = fn_label_build_in_ns(label, profile, GFP_KERNEL, |
| 1411 | aa_get_label(target), |
| 1412 | aa_get_label(&profile->label)); |
| 1413 | /* |
| 1414 | * no new privs prevents domain transitions that would |
| 1415 | * reduce restrictions. |
| 1416 | */ |
| 1417 | if (task_no_new_privs(current) && !unconfined(label) && |
| 1418 | !aa_label_is_unconfined_subset(new, ctx->nnp)) { |
| 1419 | /* not an apparmor denial per se, so don't log it */ |
| 1420 | AA_DEBUG("no_new_privs - change_hat denied"); |
| 1421 | error = -EPERM; |
| 1422 | goto out; |
| 1423 | } |
| 1424 | } |
| 1425 | |
| 1426 | if (!(flags & AA_CHANGE_ONEXEC)) { |
| 1427 | /* only transition profiles in the current ns */ |
| 1428 | if (stack) |
| 1429 | new = aa_label_merge(label, target, GFP_KERNEL); |
| 1430 | if (IS_ERR_OR_NULL(new)) { |
| 1431 | info = "failed to build target label"; |
| 1432 | if (!new) |
| 1433 | error = -ENOMEM; |
| 1434 | else |
| 1435 | error = PTR_ERR(new); |
| 1436 | new = NULL; |
| 1437 | perms.allow = 0; |
| 1438 | goto audit; |
| 1439 | } |
| 1440 | error = aa_replace_current_label(new); |
| 1441 | } else { |
| 1442 | if (new) { |
| 1443 | aa_put_label(new); |
| 1444 | new = NULL; |
| 1445 | } |
| 1446 | |
| 1447 | /* full transition will be built in exec path */ |
| 1448 | error = aa_set_current_onexec(target, stack); |
| 1449 | } |
| 1450 | |
| 1451 | audit: |
| 1452 | error = fn_for_each_in_ns(label, profile, |
| 1453 | aa_audit_file(profile, &perms, op, request, auditname, |
| 1454 | NULL, new ? new : target, |
| 1455 | GLOBAL_ROOT_UID, info, error)); |
| 1456 | |
| 1457 | out: |
| 1458 | aa_put_label(new); |
| 1459 | aa_put_label(target); |
| 1460 | aa_put_label(label); |
| 1461 | |
| 1462 | return error; |
| 1463 | } |