2 * AppArmor security module
4 * This file contains AppArmor security identifier (secid) manipulation fns
6 * Copyright 2009-2017 Canonical Ltd.
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation, version 2 of the
14 * AppArmor allocates a unique secid for every label used. If a label
15 * is replaced it receives the secid of the label it is replacing.
18 #include <linux/errno.h>
19 #include <linux/err.h>
20 #include <linux/gfp.h>
21 #include <linux/slab.h>
22 #include <linux/spinlock.h>
24 #include "include/cred.h"
25 #include "include/lib.h"
26 #include "include/secid.h"
27 #include "include/label.h"
28 #include "include/policy_ns.h"
31 * secids - do not pin labels with a refcount. They rely on the label
32 * properly updating/freeing them
34 * A singly linked free list is used to track secids that have been
35 * freed and reuse them before allocating new ones
38 #define FREE_LIST_HEAD 1
40 static RADIX_TREE(aa_secids_map, GFP_ATOMIC);
41 static DEFINE_SPINLOCK(secid_lock);
42 static u32 alloced_secid = FREE_LIST_HEAD;
43 static u32 free_list = FREE_LIST_HEAD;
44 static unsigned long free_count;
47 * TODO: allow policy to reserve a secid range?
48 * TODO: add secid pinning
49 * TODO: use secid_update in label replace
52 #define SECID_MAX U32_MAX
54 /* TODO: mark free list as exceptional */
55 static void *to_ptr(u32 secid)
58 ((((unsigned long) secid) << RADIX_TREE_EXCEPTIONAL_SHIFT));
61 static u32 to_secid(void *ptr)
63 return (u32) (((unsigned long) ptr) >> RADIX_TREE_EXCEPTIONAL_SHIFT);
67 /* TODO: tag free_list entries to mark them as different */
68 static u32 __pop(struct aa_label *label)
70 u32 secid = free_list;
74 if (free_list == FREE_LIST_HEAD)
75 return AA_SECID_INVALID;
77 slot = radix_tree_lookup_slot(&aa_secids_map, secid);
79 entry = radix_tree_deref_slot_protected(slot, &secid_lock);
80 free_list = to_secid(entry);
81 radix_tree_replace_slot(&aa_secids_map, slot, label);
87 static void __push(u32 secid)
91 slot = radix_tree_lookup_slot(&aa_secids_map, secid);
93 radix_tree_replace_slot(&aa_secids_map, slot, to_ptr(free_list));
98 static struct aa_label * __secid_update(u32 secid, struct aa_label *label)
100 struct aa_label *old;
103 slot = radix_tree_lookup_slot(&aa_secids_map, secid);
105 old = radix_tree_deref_slot_protected(slot, &secid_lock);
106 radix_tree_replace_slot(&aa_secids_map, slot, label);
112 * aa_secid_update - update a secid mapping to a new label
113 * @secid: secid to update
114 * @label: label the secid will now map to
116 void aa_secid_update(u32 secid, struct aa_label *label)
118 struct aa_label *old;
121 spin_lock_irqsave(&secid_lock, flags);
122 old = __secid_update(secid, label);
123 spin_unlock_irqrestore(&secid_lock, flags);
128 * see label for inverse aa_label_to_secid
130 struct aa_label *aa_secid_to_label(u32 secid)
132 struct aa_label *label;
135 label = radix_tree_lookup(&aa_secids_map, secid);
141 int apparmor_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
143 /* TODO: cache secctx and ref count so we don't have to recreate */
144 struct aa_label *label = aa_secid_to_label(secid);
153 *seclen = aa_label_asxprint(secdata, root_ns, label,
154 FLAG_SHOW_MODE | FLAG_VIEW_SUBNS |
155 FLAG_HIDDEN_UNCONFINED |
156 FLAG_ABS_ROOT, GFP_ATOMIC);
158 *seclen = aa_label_snxprint(NULL, 0, root_ns, label,
159 FLAG_SHOW_MODE | FLAG_VIEW_SUBNS |
160 FLAG_HIDDEN_UNCONFINED |
169 int apparmor_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
171 struct aa_label *label;
173 label = aa_label_strn_parse(&root_ns->unconfined->label, secdata,
174 seclen, GFP_KERNEL, false, false);
176 return PTR_ERR(label);
177 *secid = label->secid;
182 void apparmor_release_secctx(char *secdata, u32 seclen)
189 * aa_alloc_secid - allocate a new secid for a profile
191 u32 aa_alloc_secid(struct aa_label *label, gfp_t gfp)
196 /* racey, but at worst causes new allocation instead of reuse */
197 if (free_list == FREE_LIST_HEAD) {
202 if (gfpflags_allow_blocking(gfp) && !radix_tree_preload(gfp))
204 spin_lock_irqsave(&secid_lock, flags);
205 if (alloced_secid != SECID_MAX) {
206 secid = ++alloced_secid;
207 res = radix_tree_insert(&aa_secids_map, secid, label);
208 AA_BUG(res == -EEXIST);
210 secid = AA_SECID_INVALID;
212 spin_unlock_irqrestore(&secid_lock, flags);
214 radix_tree_preload_end();
216 spin_lock_irqsave(&secid_lock, flags);
217 /* remove entry from free list */
218 secid = __pop(label);
219 if (secid == AA_SECID_INVALID) {
220 spin_unlock_irqrestore(&secid_lock, flags);
223 spin_unlock_irqrestore(&secid_lock, flags);
230 * aa_free_secid - free a secid
231 * @secid: secid to free
233 void aa_free_secid(u32 secid)
237 spin_lock_irqsave(&secid_lock, flags);
239 spin_unlock_irqrestore(&secid_lock, flags);