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