]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - security/apparmor/include/context.h
UBUNTU: SAUCE: LSM stacking: LSM: General stacking
[mirror_ubuntu-bionic-kernel.git] / security / apparmor / include / context.h
CommitLineData
c75afcd1
JJ
1/*
2 * AppArmor security module
3 *
4 * This file contains AppArmor contexts used to associate "labels" to objects.
5 *
6 * Copyright (C) 1998-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#ifndef __AA_CONTEXT_H
16#define __AA_CONTEXT_H
17
18#include <linux/cred.h>
19#include <linux/slab.h>
20#include <linux/sched.h>
f1efc8c4 21#include <linux/lsm_hooks.h>
c75afcd1 22
637f688d 23#include "label.h"
2bd8dbbf 24#include "policy_ns.h"
c75afcd1 25
f1efc8c4 26#define cred_ctx(X) apparmor_cred(X)
55a26ebf 27#define current_ctx() cred_ctx(current_cred())
214beaca 28
c75afcd1 29/**
55a26ebf 30 * struct aa_task_ctx - primary label for confined tasks
637f688d
JJ
31 * @label: the current label (NOT NULL)
32 * @exec: label to transition to on next exec (MAYBE NULL)
33 * @previous: label the task may return to (MAYBE NULL)
34 * @token: magic value the task must know for returning to @previous
c75afcd1 35 *
637f688d 36 * Contains the task's current label (which could change due to
c75afcd1
JJ
37 * change_hat). Plus the hat_magic needed during change_hat.
38 *
39 * TODO: make so a task can be confined by a stack of contexts
40 */
55a26ebf 41struct aa_task_ctx {
637f688d
JJ
42 struct aa_label *label;
43 struct aa_label *onexec;
44 struct aa_label *previous;
c75afcd1
JJ
45 u64 token;
46};
47
55a26ebf
JJ
48struct aa_task_ctx *aa_alloc_task_context(gfp_t flags);
49void aa_free_task_context(struct aa_task_ctx *ctx);
50void aa_dup_task_context(struct aa_task_ctx *new,
51 const struct aa_task_ctx *old);
637f688d
JJ
52int aa_replace_current_label(struct aa_label *label);
53int aa_set_current_onexec(struct aa_label *label, bool stack);
54int aa_set_current_hat(struct aa_label *label, u64 token);
55int aa_restore_previous_label(u64 cookie);
56struct aa_label *aa_get_task_label(struct task_struct *task);
c75afcd1 57
54f4b36b
CS
58extern struct lsm_blob_sizes apparmor_blob_sizes;
59
f1efc8c4
CS
60static inline struct aa_task_ctx *apparmor_cred(const struct cred *cred)
61{
54f4b36b
CS
62#ifdef CONFIG_SECURITY_STACKING
63 return cred->security + apparmor_blob_sizes.lbs_cred;
64#else
f1efc8c4 65 return cred->security;
54f4b36b 66#endif
f1efc8c4 67}
c75afcd1
JJ
68
69/**
637f688d
JJ
70 * aa_cred_raw_label - obtain cred's label
71 * @cred: cred to obtain label from (NOT NULL)
c75afcd1 72 *
637f688d 73 * Returns: confining label
c75afcd1
JJ
74 *
75 * does NOT increment reference count
76 */
637f688d 77static inline struct aa_label *aa_cred_raw_label(const struct cred *cred)
c75afcd1 78{
f1efc8c4 79 struct aa_task_ctx *ctx = apparmor_cred(cred);
55a26ebf 80
637f688d
JJ
81 AA_BUG(!ctx || !ctx->label);
82 return ctx->label;
c75afcd1
JJ
83}
84
3cfcc19e 85/**
637f688d
JJ
86 * aa_get_newest_cred_label - obtain the newest label on a cred
87 * @cred: cred to obtain label from (NOT NULL)
cf797c0e 88 *
637f688d 89 * Returns: newest version of confining label
cf797c0e 90 */
637f688d 91static inline struct aa_label *aa_get_newest_cred_label(const struct cred *cred)
cf797c0e 92{
637f688d 93 return aa_get_newest_label(aa_cred_raw_label(cred));
cf797c0e
JJ
94}
95
ab9b110b
CS
96static inline struct aa_file_ctx *apparmor_file(const struct file *file)
97{
54f4b36b
CS
98#ifdef CONFIG_SECURITY_STACKING
99 return file->f_security + apparmor_blob_sizes.lbs_file;
100#else
ab9b110b 101 return file->f_security;
54f4b36b 102#endif
ab9b110b
CS
103}
104
cf797c0e 105/**
637f688d 106 * __aa_task_raw_label - retrieve another task's label
3cfcc19e
JJ
107 * @task: task to query (NOT NULL)
108 *
637f688d 109 * Returns: @task's label without incrementing its ref count
3cfcc19e
JJ
110 *
111 * If @task != current needs to be called in RCU safe critical section
112 */
637f688d 113static inline struct aa_label *__aa_task_raw_label(struct task_struct *task)
3cfcc19e 114{
637f688d 115 return aa_cred_raw_label(__task_cred(task));
3cfcc19e
JJ
116}
117
118/**
119 * __aa_task_is_confined - determine if @task has any confinement
120 * @task: task to check confinement of (NOT NULL)
121 *
122 * If @task != current needs to be called in RCU safe critical section
123 */
124static inline bool __aa_task_is_confined(struct task_struct *task)
125{
637f688d 126 return !unconfined(__aa_task_raw_label(task));
3cfcc19e
JJ
127}
128
c75afcd1 129/**
637f688d 130 * aa_current_raw_label - find the current tasks confining label
c75afcd1 131 *
637f688d 132 * Returns: up to date confining label or the ns unconfined label (NOT NULL)
c75afcd1
JJ
133 *
134 * This fn will not update the tasks cred to the most up to date version
637f688d 135 * of the label so it is safe to call when inside of locks.
c75afcd1 136 */
637f688d 137static inline struct aa_label *aa_current_raw_label(void)
c75afcd1 138{
637f688d 139 return aa_cred_raw_label(current_cred());
c75afcd1
JJ
140}
141
142/**
637f688d 143 * aa_get_current_label - get the newest version of the current tasks label
c75afcd1 144 *
637f688d 145 * Returns: newest version of confining label (NOT NULL)
cf797c0e
JJ
146 *
147 * This fn will not update the tasks cred, so it is safe inside of locks
c75afcd1 148 *
637f688d 149 * The returned reference must be put with aa_put_label()
c75afcd1 150 */
637f688d 151static inline struct aa_label *aa_get_current_label(void)
c75afcd1 152{
637f688d 153 struct aa_label *l = aa_current_raw_label();
c75afcd1 154
637f688d
JJ
155 if (label_is_stale(l))
156 return aa_get_newest_label(l);
157 return aa_get_label(l);
cf797c0e 158}
55a26ebf 159
637f688d 160#define __end_current_label_crit_section(X) end_current_label_crit_section(X)
cf797c0e
JJ
161
162/**
637f688d
JJ
163 * end_label_crit_section - put a reference found with begin_current_label..
164 * @label: label reference to put
cf797c0e
JJ
165 *
166 * Should only be used with a reference obtained with
637f688d 167 * begin_current_label_crit_section and never used in situations where the
cf797c0e
JJ
168 * task cred may be updated
169 */
637f688d 170static inline void end_current_label_crit_section(struct aa_label *label)
cf797c0e 171{
637f688d
JJ
172 if (label != aa_current_raw_label())
173 aa_put_label(label);
cf797c0e
JJ
174}
175
176/**
637f688d 177 * __begin_current_label_crit_section - current's confining label
cf797c0e 178 *
637f688d 179 * Returns: up to date confining label or the ns unconfined label (NOT NULL)
cf797c0e
JJ
180 *
181 * safe to call inside locks
182 *
637f688d 183 * The returned reference must be put with __end_current_label_crit_section()
cf797c0e 184 * This must NOT be used if the task cred could be updated within the
637f688d
JJ
185 * critical section between __begin_current_label_crit_section() ..
186 * __end_current_label_crit_section()
cf797c0e 187 */
637f688d 188static inline struct aa_label *__begin_current_label_crit_section(void)
cf797c0e 189{
637f688d 190 struct aa_label *label = aa_current_raw_label();
cf797c0e 191
637f688d
JJ
192 if (label_is_stale(label))
193 label = aa_get_newest_label(label);
cf797c0e 194
637f688d 195 return label;
cf797c0e
JJ
196}
197
198/**
637f688d 199 * begin_current_label_crit_section - current's confining label and update it
cf797c0e 200 *
637f688d 201 * Returns: up to date confining label or the ns unconfined label (NOT NULL)
cf797c0e
JJ
202 *
203 * Not safe to call inside locks
204 *
637f688d 205 * The returned reference must be put with end_current_label_crit_section()
cf797c0e 206 * This must NOT be used if the task cred could be updated within the
637f688d
JJ
207 * critical section between begin_current_label_crit_section() ..
208 * end_current_label_crit_section()
cf797c0e 209 */
637f688d 210static inline struct aa_label *begin_current_label_crit_section(void)
cf797c0e 211{
637f688d 212 struct aa_label *label = aa_current_raw_label();
cf797c0e 213
637f688d
JJ
214 if (label_is_stale(label)) {
215 label = aa_get_newest_label(label);
216 if (aa_replace_current_label(label) == 0)
cf797c0e 217 /* task cred will keep the reference */
637f688d 218 aa_put_label(label);
77b071b3 219 }
c75afcd1 220
637f688d 221 return label;
c75afcd1
JJ
222}
223
2bd8dbbf
JJ
224static inline struct aa_ns *aa_get_current_ns(void)
225{
637f688d 226 struct aa_label *label;
cf797c0e
JJ
227 struct aa_ns *ns;
228
637f688d
JJ
229 label = __begin_current_label_crit_section();
230 ns = aa_get_ns(labels_ns(label));
231 __end_current_label_crit_section(label);
cf797c0e
JJ
232
233 return ns;
2bd8dbbf
JJ
234}
235
7a2871b5 236/**
55a26ebf
JJ
237 * aa_clear_task_ctx_trans - clear transition tracking info from the ctx
238 * @ctx: task context to clear (NOT NULL)
7a2871b5 239 */
55a26ebf 240static inline void aa_clear_task_ctx_trans(struct aa_task_ctx *ctx)
7a2871b5 241{
637f688d
JJ
242 aa_put_label(ctx->previous);
243 aa_put_label(ctx->onexec);
55a26ebf
JJ
244 ctx->previous = NULL;
245 ctx->onexec = NULL;
246 ctx->token = 0;
7a2871b5
JJ
247}
248
c75afcd1 249#endif /* __AA_CONTEXT_H */