]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - security/apparmor/include/context.h
UBUNTU: SAUCE: LSM stacking: LSM: manage credential security blobs
[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>
98738d70 21#include <linux/lsm_hooks.h>
c75afcd1 22
637f688d 23#include "label.h"
2bd8dbbf 24#include "policy_ns.h"
c75afcd1 25
98738d70 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
98738d70
CS
58static inline struct aa_task_ctx *apparmor_cred(const struct cred *cred)
59{
60 return cred->security;
61}
c75afcd1
JJ
62
63/**
637f688d
JJ
64 * aa_cred_raw_label - obtain cred's label
65 * @cred: cred to obtain label from (NOT NULL)
c75afcd1 66 *
637f688d 67 * Returns: confining label
c75afcd1
JJ
68 *
69 * does NOT increment reference count
70 */
637f688d 71static inline struct aa_label *aa_cred_raw_label(const struct cred *cred)
c75afcd1 72{
98738d70 73 struct aa_task_ctx *ctx = apparmor_cred(cred);
55a26ebf 74
637f688d
JJ
75 AA_BUG(!ctx || !ctx->label);
76 return ctx->label;
c75afcd1
JJ
77}
78
3cfcc19e 79/**
637f688d
JJ
80 * aa_get_newest_cred_label - obtain the newest label on a cred
81 * @cred: cred to obtain label from (NOT NULL)
cf797c0e 82 *
637f688d 83 * Returns: newest version of confining label
cf797c0e 84 */
637f688d 85static inline struct aa_label *aa_get_newest_cred_label(const struct cred *cred)
cf797c0e 86{
637f688d 87 return aa_get_newest_label(aa_cred_raw_label(cred));
cf797c0e
JJ
88}
89
90/**
637f688d 91 * __aa_task_raw_label - retrieve another task's label
3cfcc19e
JJ
92 * @task: task to query (NOT NULL)
93 *
637f688d 94 * Returns: @task's label without incrementing its ref count
3cfcc19e
JJ
95 *
96 * If @task != current needs to be called in RCU safe critical section
97 */
637f688d 98static inline struct aa_label *__aa_task_raw_label(struct task_struct *task)
3cfcc19e 99{
637f688d 100 return aa_cred_raw_label(__task_cred(task));
3cfcc19e
JJ
101}
102
103/**
104 * __aa_task_is_confined - determine if @task has any confinement
105 * @task: task to check confinement of (NOT NULL)
106 *
107 * If @task != current needs to be called in RCU safe critical section
108 */
109static inline bool __aa_task_is_confined(struct task_struct *task)
110{
637f688d 111 return !unconfined(__aa_task_raw_label(task));
3cfcc19e
JJ
112}
113
c75afcd1 114/**
637f688d 115 * aa_current_raw_label - find the current tasks confining label
c75afcd1 116 *
637f688d 117 * Returns: up to date confining label or the ns unconfined label (NOT NULL)
c75afcd1
JJ
118 *
119 * This fn will not update the tasks cred to the most up to date version
637f688d 120 * of the label so it is safe to call when inside of locks.
c75afcd1 121 */
637f688d 122static inline struct aa_label *aa_current_raw_label(void)
c75afcd1 123{
637f688d 124 return aa_cred_raw_label(current_cred());
c75afcd1
JJ
125}
126
127/**
637f688d 128 * aa_get_current_label - get the newest version of the current tasks label
c75afcd1 129 *
637f688d 130 * Returns: newest version of confining label (NOT NULL)
cf797c0e
JJ
131 *
132 * This fn will not update the tasks cred, so it is safe inside of locks
c75afcd1 133 *
637f688d 134 * The returned reference must be put with aa_put_label()
c75afcd1 135 */
637f688d 136static inline struct aa_label *aa_get_current_label(void)
c75afcd1 137{
637f688d 138 struct aa_label *l = aa_current_raw_label();
c75afcd1 139
637f688d
JJ
140 if (label_is_stale(l))
141 return aa_get_newest_label(l);
142 return aa_get_label(l);
cf797c0e 143}
55a26ebf 144
637f688d 145#define __end_current_label_crit_section(X) end_current_label_crit_section(X)
cf797c0e
JJ
146
147/**
637f688d
JJ
148 * end_label_crit_section - put a reference found with begin_current_label..
149 * @label: label reference to put
cf797c0e
JJ
150 *
151 * Should only be used with a reference obtained with
637f688d 152 * begin_current_label_crit_section and never used in situations where the
cf797c0e
JJ
153 * task cred may be updated
154 */
637f688d 155static inline void end_current_label_crit_section(struct aa_label *label)
cf797c0e 156{
637f688d
JJ
157 if (label != aa_current_raw_label())
158 aa_put_label(label);
cf797c0e
JJ
159}
160
161/**
637f688d 162 * __begin_current_label_crit_section - current's confining label
cf797c0e 163 *
637f688d 164 * Returns: up to date confining label or the ns unconfined label (NOT NULL)
cf797c0e
JJ
165 *
166 * safe to call inside locks
167 *
637f688d 168 * The returned reference must be put with __end_current_label_crit_section()
cf797c0e 169 * This must NOT be used if the task cred could be updated within the
637f688d
JJ
170 * critical section between __begin_current_label_crit_section() ..
171 * __end_current_label_crit_section()
cf797c0e 172 */
637f688d 173static inline struct aa_label *__begin_current_label_crit_section(void)
cf797c0e 174{
637f688d 175 struct aa_label *label = aa_current_raw_label();
cf797c0e 176
637f688d
JJ
177 if (label_is_stale(label))
178 label = aa_get_newest_label(label);
cf797c0e 179
637f688d 180 return label;
cf797c0e
JJ
181}
182
183/**
637f688d 184 * begin_current_label_crit_section - current's confining label and update it
cf797c0e 185 *
637f688d 186 * Returns: up to date confining label or the ns unconfined label (NOT NULL)
cf797c0e
JJ
187 *
188 * Not safe to call inside locks
189 *
637f688d 190 * The returned reference must be put with end_current_label_crit_section()
cf797c0e 191 * This must NOT be used if the task cred could be updated within the
637f688d
JJ
192 * critical section between begin_current_label_crit_section() ..
193 * end_current_label_crit_section()
cf797c0e 194 */
637f688d 195static inline struct aa_label *begin_current_label_crit_section(void)
cf797c0e 196{
637f688d 197 struct aa_label *label = aa_current_raw_label();
cf797c0e 198
637f688d
JJ
199 if (label_is_stale(label)) {
200 label = aa_get_newest_label(label);
201 if (aa_replace_current_label(label) == 0)
cf797c0e 202 /* task cred will keep the reference */
637f688d 203 aa_put_label(label);
77b071b3 204 }
c75afcd1 205
637f688d 206 return label;
c75afcd1
JJ
207}
208
2bd8dbbf
JJ
209static inline struct aa_ns *aa_get_current_ns(void)
210{
637f688d 211 struct aa_label *label;
cf797c0e
JJ
212 struct aa_ns *ns;
213
637f688d
JJ
214 label = __begin_current_label_crit_section();
215 ns = aa_get_ns(labels_ns(label));
216 __end_current_label_crit_section(label);
cf797c0e
JJ
217
218 return ns;
2bd8dbbf
JJ
219}
220
7a2871b5 221/**
55a26ebf
JJ
222 * aa_clear_task_ctx_trans - clear transition tracking info from the ctx
223 * @ctx: task context to clear (NOT NULL)
7a2871b5 224 */
55a26ebf 225static inline void aa_clear_task_ctx_trans(struct aa_task_ctx *ctx)
7a2871b5 226{
637f688d
JJ
227 aa_put_label(ctx->previous);
228 aa_put_label(ctx->onexec);
55a26ebf
JJ
229 ctx->previous = NULL;
230 ctx->onexec = NULL;
231 ctx->token = 0;
7a2871b5
JJ
232}
233
c75afcd1 234#endif /* __AA_CONTEXT_H */