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