1 // SPDX-License-Identifier: GPL-2.0-only
3 * AppArmor security module
5 * This file contains AppArmor security identifier (secid) manipulation fns
7 * Copyright 2009-2017 Canonical Ltd.
9 * AppArmor allocates a unique secid for every label used. If a label
10 * is replaced it receives the secid of the label it is replacing.
13 #include <linux/errno.h>
14 #include <linux/err.h>
15 #include <linux/gfp.h>
16 #include <linux/idr.h>
17 #include <linux/slab.h>
18 #include <linux/spinlock.h>
20 #include "include/cred.h"
21 #include "include/lib.h"
22 #include "include/secid.h"
23 #include "include/label.h"
24 #include "include/policy_ns.h"
27 * secids - do not pin labels with a refcount. They rely on the label
28 * properly updating/freeing them
30 #define AA_FIRST_SECID 2
32 static DEFINE_IDR(aa_secids
);
33 static DEFINE_SPINLOCK(secid_lock
);
36 * TODO: allow policy to reserve a secid range?
37 * TODO: add secid pinning
38 * TODO: use secid_update in label replace
42 * aa_secid_update - update a secid mapping to a new label
43 * @secid: secid to update
44 * @label: label the secid will now map to
46 void aa_secid_update(u32 secid
, struct aa_label
*label
)
50 spin_lock_irqsave(&secid_lock
, flags
);
51 idr_replace(&aa_secids
, label
, secid
);
52 spin_unlock_irqrestore(&secid_lock
, flags
);
57 * see label for inverse aa_label_to_secid
59 struct aa_label
*aa_secid_to_label(u32 secid
)
61 struct aa_label
*label
;
64 label
= idr_find(&aa_secids
, secid
);
70 int apparmor_secid_to_secctx(u32 secid
, char **secdata
, u32
*seclen
)
72 /* TODO: cache secctx and ref count so we don't have to recreate */
73 struct aa_label
*label
= aa_secid_to_label(secid
);
82 len
= aa_label_asxprint(secdata
, root_ns
, label
,
83 FLAG_SHOW_MODE
| FLAG_VIEW_SUBNS
|
84 FLAG_HIDDEN_UNCONFINED
| FLAG_ABS_ROOT
,
87 len
= aa_label_snxprint(NULL
, 0, root_ns
, label
,
88 FLAG_SHOW_MODE
| FLAG_VIEW_SUBNS
|
89 FLAG_HIDDEN_UNCONFINED
| FLAG_ABS_ROOT
);
98 int apparmor_secctx_to_secid(const char *secdata
, u32 seclen
, u32
*secid
)
100 struct aa_label
*label
;
102 label
= aa_label_strn_parse(&root_ns
->unconfined
->label
, secdata
,
103 seclen
, GFP_KERNEL
, false, false);
105 return PTR_ERR(label
);
106 *secid
= label
->secid
;
111 void apparmor_release_secctx(char *secdata
, u32 seclen
)
117 * aa_alloc_secid - allocate a new secid for a profile
118 * @label: the label to allocate a secid for
119 * @gfp: memory allocation flags
121 * Returns: 0 with @label->secid initialized
122 * <0 returns error with @label->secid set to AA_SECID_INVALID
124 int aa_alloc_secid(struct aa_label
*label
, gfp_t gfp
)
130 spin_lock_irqsave(&secid_lock
, flags
);
131 ret
= idr_alloc(&aa_secids
, label
, AA_FIRST_SECID
, 0, GFP_ATOMIC
);
132 spin_unlock_irqrestore(&secid_lock
, flags
);
136 label
->secid
= AA_SECID_INVALID
;
140 AA_BUG(ret
== AA_SECID_INVALID
);
146 * aa_free_secid - free a secid
147 * @secid: secid to free
149 void aa_free_secid(u32 secid
)
153 spin_lock_irqsave(&secid_lock
, flags
);
154 idr_remove(&aa_secids
, secid
);
155 spin_unlock_irqrestore(&secid_lock
, flags
);
158 void aa_secids_init(void)
160 idr_init_base(&aa_secids
, AA_FIRST_SECID
);