John Johansen | c75afcd | 2010-07-29 14:47:59 -0700 | [diff] [blame] | 1 | /* |
| 2 | * AppArmor security module |
| 3 | * |
| 4 | * This file contains AppArmor functions used to manipulate object security |
| 5 | * contexts. |
| 6 | * |
| 7 | * Copyright (C) 1998-2008 Novell/SUSE |
| 8 | * Copyright 2009-2010 Canonical Ltd. |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or |
| 11 | * modify it under the terms of the GNU General Public License as |
| 12 | * published by the Free Software Foundation, version 2 of the |
| 13 | * License. |
| 14 | * |
| 15 | * |
John Johansen | 55a26eb | 2017-01-16 00:43:00 -0800 | [diff] [blame] | 16 | * AppArmor sets confinement on every task, via the the aa_task_ctx and |
John Johansen | 637f688 | 2017-06-09 08:14:28 -0700 | [diff] [blame^] | 17 | * the aa_task_ctx.label, both of which are required and are not allowed |
John Johansen | 55a26eb | 2017-01-16 00:43:00 -0800 | [diff] [blame] | 18 | * to be NULL. The aa_task_ctx is not reference counted and is unique |
John Johansen | 637f688 | 2017-06-09 08:14:28 -0700 | [diff] [blame^] | 19 | * to each cred (which is reference count). The label pointed to by |
John Johansen | 55a26eb | 2017-01-16 00:43:00 -0800 | [diff] [blame] | 20 | * the task_ctx is reference counted. |
John Johansen | c75afcd | 2010-07-29 14:47:59 -0700 | [diff] [blame] | 21 | * |
| 22 | * TODO |
| 23 | * If a task uses change_hat it currently does not return to the old |
| 24 | * cred or task context but instead creates a new one. Ideally the task |
| 25 | * should return to the previous cred if it has not been modified. |
| 26 | * |
| 27 | */ |
| 28 | |
| 29 | #include "include/context.h" |
| 30 | #include "include/policy.h" |
| 31 | |
| 32 | /** |
John Johansen | 55a26eb | 2017-01-16 00:43:00 -0800 | [diff] [blame] | 33 | * aa_alloc_task_context - allocate a new task_ctx |
John Johansen | c75afcd | 2010-07-29 14:47:59 -0700 | [diff] [blame] | 34 | * @flags: gfp flags for allocation |
| 35 | * |
| 36 | * Returns: allocated buffer or NULL on failure |
| 37 | */ |
John Johansen | 55a26eb | 2017-01-16 00:43:00 -0800 | [diff] [blame] | 38 | struct aa_task_ctx *aa_alloc_task_context(gfp_t flags) |
John Johansen | c75afcd | 2010-07-29 14:47:59 -0700 | [diff] [blame] | 39 | { |
John Johansen | 55a26eb | 2017-01-16 00:43:00 -0800 | [diff] [blame] | 40 | return kzalloc(sizeof(struct aa_task_ctx), flags); |
John Johansen | c75afcd | 2010-07-29 14:47:59 -0700 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | /** |
John Johansen | 55a26eb | 2017-01-16 00:43:00 -0800 | [diff] [blame] | 44 | * aa_free_task_context - free a task_ctx |
| 45 | * @ctx: task_ctx to free (MAYBE NULL) |
John Johansen | c75afcd | 2010-07-29 14:47:59 -0700 | [diff] [blame] | 46 | */ |
John Johansen | 55a26eb | 2017-01-16 00:43:00 -0800 | [diff] [blame] | 47 | void aa_free_task_context(struct aa_task_ctx *ctx) |
John Johansen | c75afcd | 2010-07-29 14:47:59 -0700 | [diff] [blame] | 48 | { |
John Johansen | 55a26eb | 2017-01-16 00:43:00 -0800 | [diff] [blame] | 49 | if (ctx) { |
John Johansen | 637f688 | 2017-06-09 08:14:28 -0700 | [diff] [blame^] | 50 | aa_put_label(ctx->label); |
| 51 | aa_put_label(ctx->previous); |
| 52 | aa_put_label(ctx->onexec); |
John Johansen | c75afcd | 2010-07-29 14:47:59 -0700 | [diff] [blame] | 53 | |
John Johansen | 55a26eb | 2017-01-16 00:43:00 -0800 | [diff] [blame] | 54 | kzfree(ctx); |
John Johansen | c75afcd | 2010-07-29 14:47:59 -0700 | [diff] [blame] | 55 | } |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * aa_dup_task_context - duplicate a task context, incrementing reference counts |
| 60 | * @new: a blank task context (NOT NULL) |
| 61 | * @old: the task context to copy (NOT NULL) |
| 62 | */ |
John Johansen | 55a26eb | 2017-01-16 00:43:00 -0800 | [diff] [blame] | 63 | void aa_dup_task_context(struct aa_task_ctx *new, const struct aa_task_ctx *old) |
John Johansen | c75afcd | 2010-07-29 14:47:59 -0700 | [diff] [blame] | 64 | { |
| 65 | *new = *old; |
John Johansen | 637f688 | 2017-06-09 08:14:28 -0700 | [diff] [blame^] | 66 | aa_get_label(new->label); |
| 67 | aa_get_label(new->previous); |
| 68 | aa_get_label(new->onexec); |
John Johansen | c75afcd | 2010-07-29 14:47:59 -0700 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | /** |
John Johansen | 637f688 | 2017-06-09 08:14:28 -0700 | [diff] [blame^] | 72 | * aa_get_task_label - Get another task's label |
John Johansen | 3cfcc19 | 2013-02-18 16:03:34 -0800 | [diff] [blame] | 73 | * @task: task to query (NOT NULL) |
| 74 | * |
John Johansen | 637f688 | 2017-06-09 08:14:28 -0700 | [diff] [blame^] | 75 | * Returns: counted reference to @task's label |
John Johansen | 3cfcc19 | 2013-02-18 16:03:34 -0800 | [diff] [blame] | 76 | */ |
John Johansen | 637f688 | 2017-06-09 08:14:28 -0700 | [diff] [blame^] | 77 | struct aa_label *aa_get_task_label(struct task_struct *task) |
John Johansen | 3cfcc19 | 2013-02-18 16:03:34 -0800 | [diff] [blame] | 78 | { |
John Johansen | 637f688 | 2017-06-09 08:14:28 -0700 | [diff] [blame^] | 79 | struct aa_label *p; |
John Johansen | 3cfcc19 | 2013-02-18 16:03:34 -0800 | [diff] [blame] | 80 | |
| 81 | rcu_read_lock(); |
John Johansen | 637f688 | 2017-06-09 08:14:28 -0700 | [diff] [blame^] | 82 | p = aa_get_newest_label(__aa_task_raw_label(task)); |
John Johansen | 3cfcc19 | 2013-02-18 16:03:34 -0800 | [diff] [blame] | 83 | rcu_read_unlock(); |
| 84 | |
| 85 | return p; |
| 86 | } |
| 87 | |
| 88 | /** |
John Johansen | 637f688 | 2017-06-09 08:14:28 -0700 | [diff] [blame^] | 89 | * aa_replace_current_label - replace the current tasks label |
| 90 | * @label: new label (NOT NULL) |
John Johansen | c75afcd | 2010-07-29 14:47:59 -0700 | [diff] [blame] | 91 | * |
| 92 | * Returns: 0 or error on failure |
| 93 | */ |
John Johansen | 637f688 | 2017-06-09 08:14:28 -0700 | [diff] [blame^] | 94 | int aa_replace_current_label(struct aa_label *label) |
John Johansen | c75afcd | 2010-07-29 14:47:59 -0700 | [diff] [blame] | 95 | { |
John Johansen | 55a26eb | 2017-01-16 00:43:00 -0800 | [diff] [blame] | 96 | struct aa_task_ctx *ctx = current_ctx(); |
John Johansen | c75afcd | 2010-07-29 14:47:59 -0700 | [diff] [blame] | 97 | struct cred *new; |
John Johansen | 637f688 | 2017-06-09 08:14:28 -0700 | [diff] [blame^] | 98 | AA_BUG(!label); |
John Johansen | c75afcd | 2010-07-29 14:47:59 -0700 | [diff] [blame] | 99 | |
John Johansen | 637f688 | 2017-06-09 08:14:28 -0700 | [diff] [blame^] | 100 | if (ctx->label == label) |
John Johansen | c75afcd | 2010-07-29 14:47:59 -0700 | [diff] [blame] | 101 | return 0; |
| 102 | |
John Johansen | a20aa95 | 2017-01-16 00:42:59 -0800 | [diff] [blame] | 103 | if (current_cred() != current_real_cred()) |
| 104 | return -EBUSY; |
| 105 | |
John Johansen | c75afcd | 2010-07-29 14:47:59 -0700 | [diff] [blame] | 106 | new = prepare_creds(); |
| 107 | if (!new) |
| 108 | return -ENOMEM; |
| 109 | |
John Johansen | 55a26eb | 2017-01-16 00:43:00 -0800 | [diff] [blame] | 110 | ctx = cred_ctx(new); |
John Johansen | 637f688 | 2017-06-09 08:14:28 -0700 | [diff] [blame^] | 111 | if (unconfined(label) || (labels_ns(ctx->label) != labels_ns(label))) |
| 112 | /* if switching to unconfined or a different label namespace |
John Johansen | c75afcd | 2010-07-29 14:47:59 -0700 | [diff] [blame] | 113 | * clear out context state |
| 114 | */ |
John Johansen | 55a26eb | 2017-01-16 00:43:00 -0800 | [diff] [blame] | 115 | aa_clear_task_ctx_trans(ctx); |
John Johansen | 7a2871b | 2013-02-18 16:05:34 -0800 | [diff] [blame] | 116 | |
John Johansen | 55a26eb | 2017-01-16 00:43:00 -0800 | [diff] [blame] | 117 | /* |
| 118 | * be careful switching ctx->profile, when racing replacement it |
| 119 | * is possible that ctx->profile->proxy->profile is the reference |
John Johansen | 77b071b | 2013-07-10 21:07:43 -0700 | [diff] [blame] | 120 | * keeping @profile valid, so make sure to get its reference before |
John Johansen | 55a26eb | 2017-01-16 00:43:00 -0800 | [diff] [blame] | 121 | * dropping the reference on ctx->profile |
| 122 | */ |
John Johansen | 637f688 | 2017-06-09 08:14:28 -0700 | [diff] [blame^] | 123 | aa_get_label(label); |
| 124 | aa_put_label(ctx->label); |
| 125 | ctx->label = label; |
John Johansen | c75afcd | 2010-07-29 14:47:59 -0700 | [diff] [blame] | 126 | |
| 127 | commit_creds(new); |
| 128 | return 0; |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * aa_set_current_onexec - set the tasks change_profile to happen onexec |
John Johansen | 637f688 | 2017-06-09 08:14:28 -0700 | [diff] [blame^] | 133 | * @label: system label to set at exec (MAYBE NULL to clear value) |
| 134 | * @stack: whether stacking should be done |
John Johansen | c75afcd | 2010-07-29 14:47:59 -0700 | [diff] [blame] | 135 | * Returns: 0 or error on failure |
| 136 | */ |
John Johansen | 637f688 | 2017-06-09 08:14:28 -0700 | [diff] [blame^] | 137 | int aa_set_current_onexec(struct aa_label *label, bool stack) |
John Johansen | c75afcd | 2010-07-29 14:47:59 -0700 | [diff] [blame] | 138 | { |
John Johansen | 55a26eb | 2017-01-16 00:43:00 -0800 | [diff] [blame] | 139 | struct aa_task_ctx *ctx; |
John Johansen | c75afcd | 2010-07-29 14:47:59 -0700 | [diff] [blame] | 140 | struct cred *new = prepare_creds(); |
| 141 | if (!new) |
| 142 | return -ENOMEM; |
| 143 | |
John Johansen | 55a26eb | 2017-01-16 00:43:00 -0800 | [diff] [blame] | 144 | ctx = cred_ctx(new); |
John Johansen | 637f688 | 2017-06-09 08:14:28 -0700 | [diff] [blame^] | 145 | aa_get_label(label); |
| 146 | aa_clear_task_ctx_trans(ctx); |
| 147 | ctx->onexec = label; |
| 148 | ctx->token = stack; |
John Johansen | c75afcd | 2010-07-29 14:47:59 -0700 | [diff] [blame] | 149 | |
| 150 | commit_creds(new); |
| 151 | return 0; |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * aa_set_current_hat - set the current tasks hat |
John Johansen | 637f688 | 2017-06-09 08:14:28 -0700 | [diff] [blame^] | 156 | * @label: label to set as the current hat (NOT NULL) |
John Johansen | c75afcd | 2010-07-29 14:47:59 -0700 | [diff] [blame] | 157 | * @token: token value that must be specified to change from the hat |
| 158 | * |
| 159 | * Do switch of tasks hat. If the task is currently in a hat |
| 160 | * validate the token to match. |
| 161 | * |
| 162 | * Returns: 0 or error on failure |
| 163 | */ |
John Johansen | 637f688 | 2017-06-09 08:14:28 -0700 | [diff] [blame^] | 164 | int aa_set_current_hat(struct aa_label *label, u64 token) |
John Johansen | c75afcd | 2010-07-29 14:47:59 -0700 | [diff] [blame] | 165 | { |
John Johansen | 55a26eb | 2017-01-16 00:43:00 -0800 | [diff] [blame] | 166 | struct aa_task_ctx *ctx; |
John Johansen | c75afcd | 2010-07-29 14:47:59 -0700 | [diff] [blame] | 167 | struct cred *new = prepare_creds(); |
| 168 | if (!new) |
| 169 | return -ENOMEM; |
John Johansen | 637f688 | 2017-06-09 08:14:28 -0700 | [diff] [blame^] | 170 | AA_BUG(!label); |
John Johansen | c75afcd | 2010-07-29 14:47:59 -0700 | [diff] [blame] | 171 | |
John Johansen | 55a26eb | 2017-01-16 00:43:00 -0800 | [diff] [blame] | 172 | ctx = cred_ctx(new); |
| 173 | if (!ctx->previous) { |
John Johansen | c75afcd | 2010-07-29 14:47:59 -0700 | [diff] [blame] | 174 | /* transfer refcount */ |
John Johansen | 637f688 | 2017-06-09 08:14:28 -0700 | [diff] [blame^] | 175 | ctx->previous = ctx->label; |
John Johansen | 55a26eb | 2017-01-16 00:43:00 -0800 | [diff] [blame] | 176 | ctx->token = token; |
| 177 | } else if (ctx->token == token) { |
John Johansen | 637f688 | 2017-06-09 08:14:28 -0700 | [diff] [blame^] | 178 | aa_put_label(ctx->label); |
John Johansen | c75afcd | 2010-07-29 14:47:59 -0700 | [diff] [blame] | 179 | } else { |
John Johansen | 55a26eb | 2017-01-16 00:43:00 -0800 | [diff] [blame] | 180 | /* previous_profile && ctx->token != token */ |
John Johansen | c75afcd | 2010-07-29 14:47:59 -0700 | [diff] [blame] | 181 | abort_creds(new); |
| 182 | return -EACCES; |
| 183 | } |
John Johansen | 637f688 | 2017-06-09 08:14:28 -0700 | [diff] [blame^] | 184 | ctx->label = aa_get_newest_label(label); |
John Johansen | c75afcd | 2010-07-29 14:47:59 -0700 | [diff] [blame] | 185 | /* clear exec on switching context */ |
John Johansen | 637f688 | 2017-06-09 08:14:28 -0700 | [diff] [blame^] | 186 | aa_put_label(ctx->onexec); |
John Johansen | 55a26eb | 2017-01-16 00:43:00 -0800 | [diff] [blame] | 187 | ctx->onexec = NULL; |
John Johansen | c75afcd | 2010-07-29 14:47:59 -0700 | [diff] [blame] | 188 | |
| 189 | commit_creds(new); |
| 190 | return 0; |
| 191 | } |
| 192 | |
| 193 | /** |
John Johansen | 637f688 | 2017-06-09 08:14:28 -0700 | [diff] [blame^] | 194 | * aa_restore_previous_label - exit from hat context restoring previous label |
John Johansen | c75afcd | 2010-07-29 14:47:59 -0700 | [diff] [blame] | 195 | * @token: the token that must be matched to exit hat context |
| 196 | * |
John Johansen | 637f688 | 2017-06-09 08:14:28 -0700 | [diff] [blame^] | 197 | * Attempt to return out of a hat to the previous label. The token |
John Johansen | c75afcd | 2010-07-29 14:47:59 -0700 | [diff] [blame] | 198 | * must match the stored token value. |
| 199 | * |
| 200 | * Returns: 0 or error of failure |
| 201 | */ |
John Johansen | 637f688 | 2017-06-09 08:14:28 -0700 | [diff] [blame^] | 202 | int aa_restore_previous_label(u64 token) |
John Johansen | c75afcd | 2010-07-29 14:47:59 -0700 | [diff] [blame] | 203 | { |
John Johansen | 55a26eb | 2017-01-16 00:43:00 -0800 | [diff] [blame] | 204 | struct aa_task_ctx *ctx; |
John Johansen | c75afcd | 2010-07-29 14:47:59 -0700 | [diff] [blame] | 205 | struct cred *new = prepare_creds(); |
| 206 | if (!new) |
| 207 | return -ENOMEM; |
| 208 | |
John Johansen | 55a26eb | 2017-01-16 00:43:00 -0800 | [diff] [blame] | 209 | ctx = cred_ctx(new); |
| 210 | if (ctx->token != token) { |
John Johansen | c75afcd | 2010-07-29 14:47:59 -0700 | [diff] [blame] | 211 | abort_creds(new); |
| 212 | return -EACCES; |
| 213 | } |
John Johansen | 637f688 | 2017-06-09 08:14:28 -0700 | [diff] [blame^] | 214 | /* ignore restores when there is no saved label */ |
John Johansen | 55a26eb | 2017-01-16 00:43:00 -0800 | [diff] [blame] | 215 | if (!ctx->previous) { |
John Johansen | c75afcd | 2010-07-29 14:47:59 -0700 | [diff] [blame] | 216 | abort_creds(new); |
| 217 | return 0; |
| 218 | } |
| 219 | |
John Johansen | 637f688 | 2017-06-09 08:14:28 -0700 | [diff] [blame^] | 220 | aa_put_label(ctx->label); |
| 221 | ctx->label = aa_get_newest_label(ctx->previous); |
| 222 | AA_BUG(!ctx->label); |
John Johansen | 7a2871b | 2013-02-18 16:05:34 -0800 | [diff] [blame] | 223 | /* clear exec && prev information when restoring to previous context */ |
John Johansen | 55a26eb | 2017-01-16 00:43:00 -0800 | [diff] [blame] | 224 | aa_clear_task_ctx_trans(ctx); |
John Johansen | c75afcd | 2010-07-29 14:47:59 -0700 | [diff] [blame] | 225 | |
| 226 | commit_creds(new); |
| 227 | return 0; |
| 228 | } |