2 * AppArmor security module
4 * This file contains AppArmor contexts used to associate "labels" to objects.
6 * Copyright (C) 1998-2008 Novell/SUSE
7 * Copyright 2009-2010 Canonical Ltd.
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
15 #ifndef __AA_CONTEXT_H
16 #define __AA_CONTEXT_H
18 #include <linux/cred.h>
19 #include <linux/slab.h>
20 #include <linux/sched.h>
23 #include "policy_ns.h"
26 #define cred_label(X) ((X)->security)
30 * aa_cred_raw_label - obtain cred's label
31 * @cred: cred to obtain label from (NOT NULL)
33 * Returns: confining label
35 * does NOT increment reference count
37 static inline struct aa_label *aa_cred_raw_label(const struct cred *cred)
39 struct aa_label *label = cred_label(cred);
46 * aa_get_newest_cred_label - obtain the newest label on a cred
47 * @cred: cred to obtain label from (NOT NULL)
49 * Returns: newest version of confining label
51 static inline struct aa_label *aa_get_newest_cred_label(const struct cred *cred)
53 return aa_get_newest_label(aa_cred_raw_label(cred));
57 * __aa_task_raw_label - retrieve another task's label
58 * @task: task to query (NOT NULL)
60 * Returns: @task's label without incrementing its ref count
62 * If @task != current needs to be called in RCU safe critical section
64 static inline struct aa_label *__aa_task_raw_label(struct task_struct *task)
66 return aa_cred_raw_label(__task_cred(task));
70 * aa_current_raw_label - find the current tasks confining label
72 * Returns: up to date confining label or the ns unconfined label (NOT NULL)
74 * This fn will not update the tasks cred to the most up to date version
75 * of the label so it is safe to call when inside of locks.
77 static inline struct aa_label *aa_current_raw_label(void)
79 return aa_cred_raw_label(current_cred());
83 * aa_get_current_label - get the newest version of the current tasks label
85 * Returns: newest version of confining label (NOT NULL)
87 * This fn will not update the tasks cred, so it is safe inside of locks
89 * The returned reference must be put with aa_put_label()
91 static inline struct aa_label *aa_get_current_label(void)
93 struct aa_label *l = aa_current_raw_label();
95 if (label_is_stale(l))
96 return aa_get_newest_label(l);
97 return aa_get_label(l);
100 #define __end_current_label_crit_section(X) end_current_label_crit_section(X)
103 * end_label_crit_section - put a reference found with begin_current_label..
104 * @label: label reference to put
106 * Should only be used with a reference obtained with
107 * begin_current_label_crit_section and never used in situations where the
108 * task cred may be updated
110 static inline void end_current_label_crit_section(struct aa_label *label)
112 if (label != aa_current_raw_label())
117 * __begin_current_label_crit_section - current's confining label
119 * Returns: up to date confining label or the ns unconfined label (NOT NULL)
121 * safe to call inside locks
123 * The returned reference must be put with __end_current_label_crit_section()
124 * This must NOT be used if the task cred could be updated within the
125 * critical section between __begin_current_label_crit_section() ..
126 * __end_current_label_crit_section()
128 static inline struct aa_label *__begin_current_label_crit_section(void)
130 struct aa_label *label = aa_current_raw_label();
132 if (label_is_stale(label))
133 label = aa_get_newest_label(label);
139 * begin_current_label_crit_section - current's confining label and update it
141 * Returns: up to date confining label or the ns unconfined label (NOT NULL)
143 * Not safe to call inside locks
145 * The returned reference must be put with end_current_label_crit_section()
146 * This must NOT be used if the task cred could be updated within the
147 * critical section between begin_current_label_crit_section() ..
148 * end_current_label_crit_section()
150 static inline struct aa_label *begin_current_label_crit_section(void)
152 struct aa_label *label = aa_current_raw_label();
154 if (label_is_stale(label)) {
155 label = aa_get_newest_label(label);
156 if (aa_replace_current_label(label) == 0)
157 /* task cred will keep the reference */
164 static inline struct aa_ns *aa_get_current_ns(void)
166 struct aa_label *label;
169 label = __begin_current_label_crit_section();
170 ns = aa_get_ns(labels_ns(label));
171 __end_current_label_crit_section(label);
176 #endif /* __AA_CONTEXT_H */