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/idr.h>
22 #include <linux/slab.h>
23 #include <linux/spinlock.h>
25 #include "include/cred.h"
26 #include "include/lib.h"
27 #include "include/secid.h"
28 #include "include/label.h"
29 #include "include/policy_ns.h"
32 * secids - do not pin labels with a refcount. They rely on the label
33 * properly updating/freeing them
36 #define AA_FIRST_SECID 1
38 static DEFINE_IDR(aa_secids
);
39 static DEFINE_SPINLOCK(secid_lock
);
42 * TODO: allow policy to reserve a secid range?
43 * TODO: add secid pinning
44 * TODO: use secid_update in label replace
48 * aa_secid_update - update a secid mapping to a new label
49 * @secid: secid to update
50 * @label: label the secid will now map to
52 void aa_secid_update(u32 secid
, struct aa_label
*label
)
56 spin_lock_irqsave(&secid_lock
, flags
);
57 idr_replace(&aa_secids
, label
, secid
);
58 spin_unlock_irqrestore(&secid_lock
, flags
);
63 * see label for inverse aa_label_to_secid
65 struct aa_label
*aa_secid_to_label(u32 secid
)
67 struct aa_label
*label
;
70 label
= idr_find(&aa_secids
, secid
);
76 int apparmor_secid_to_secctx(u32 secid
, char **secdata
, u32
*seclen
)
78 /* TODO: cache secctx and ref count so we don't have to recreate */
79 struct aa_label
*label
= aa_secid_to_label(secid
);
88 len
= aa_label_asxprint(secdata
, root_ns
, label
,
89 FLAG_SHOW_MODE
| FLAG_VIEW_SUBNS
|
90 FLAG_HIDDEN_UNCONFINED
| FLAG_ABS_ROOT
,
93 len
= aa_label_snxprint(NULL
, 0, root_ns
, label
,
94 FLAG_SHOW_MODE
| FLAG_VIEW_SUBNS
|
95 FLAG_HIDDEN_UNCONFINED
| FLAG_ABS_ROOT
);
104 int apparmor_secctx_to_secid(const char *secdata
, u32 seclen
, u32
*secid
)
106 struct aa_label
*label
;
108 label
= aa_label_strn_parse(&root_ns
->unconfined
->label
, secdata
,
109 seclen
, GFP_KERNEL
, false, false);
111 return PTR_ERR(label
);
112 *secid
= label
->secid
;
117 void apparmor_release_secctx(char *secdata
, u32 seclen
)
123 * aa_alloc_secid - allocate a new secid for a profile
124 * @label: the label to allocate a secid for
125 * @gfp: memory allocation flags
127 * Returns: 0 with @label->secid initialized
128 * <0 returns error with @label->secid set to AA_SECID_INVALID
130 int aa_alloc_secid(struct aa_label
*label
, gfp_t gfp
)
136 spin_lock_irqsave(&secid_lock
, flags
);
137 ret
= idr_alloc(&aa_secids
, label
, AA_FIRST_SECID
, 0, GFP_ATOMIC
);
138 spin_unlock_irqrestore(&secid_lock
, flags
);
142 label
->secid
= AA_SECID_INVALID
;
146 AA_BUG(ret
== AA_SECID_INVALID
);
152 * aa_free_secid - free a secid
153 * @secid: secid to free
155 void aa_free_secid(u32 secid
)
159 spin_lock_irqsave(&secid_lock
, flags
);
160 idr_remove(&aa_secids
, secid
);
161 spin_unlock_irqrestore(&secid_lock
, flags
);
164 void aa_secids_init(void)
166 idr_init_base(&aa_secids
, AA_FIRST_SECID
);