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