blob: f2f22d00db18893a917006794c5a09e341aae2b0 [file] [log] [blame]
John Johansen121d4a92017-01-16 00:42:17 -08001/*
2 * AppArmor security module
3 *
4 * This file contains AppArmor security identifier (secid) manipulation fns
5 *
John Johansenc0929212017-07-31 17:36:45 -07006 * Copyright 2009-2017 Canonical Ltd.
John Johansen121d4a92017-01-16 00:42:17 -08007 *
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
11 * License.
12 *
13 *
John Johansenc0929212017-07-31 17:36:45 -070014 * 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.
John Johansen121d4a92017-01-16 00:42:17 -080016 */
17
John Johansen121d4a92017-01-16 00:42:17 -080018#include <linux/errno.h>
19#include <linux/err.h>
John Johansenc0929212017-07-31 17:36:45 -070020#include <linux/gfp.h>
Matthew Wilcox99cc45e2018-05-22 02:32:59 -070021#include <linux/idr.h>
John Johansenc0929212017-07-31 17:36:45 -070022#include <linux/slab.h>
23#include <linux/spinlock.h>
John Johansen121d4a92017-01-16 00:42:17 -080024
John Johansenc0929212017-07-31 17:36:45 -070025#include "include/cred.h"
26#include "include/lib.h"
John Johansen121d4a92017-01-16 00:42:17 -080027#include "include/secid.h"
John Johansenc0929212017-07-31 17:36:45 -070028#include "include/label.h"
29#include "include/policy_ns.h"
John Johansen121d4a92017-01-16 00:42:17 -080030
John Johansenc0929212017-07-31 17:36:45 -070031/*
32 * secids - do not pin labels with a refcount. They rely on the label
33 * properly updating/freeing them
John Johansenc0929212017-07-31 17:36:45 -070034 */
35
John Johansena4c3f892018-06-04 19:44:59 -070036#define AA_FIRST_SECID 1
37
Matthew Wilcox99cc45e2018-05-22 02:32:59 -070038static DEFINE_IDR(aa_secids);
John Johansen121d4a92017-01-16 00:42:17 -080039static DEFINE_SPINLOCK(secid_lock);
40
John Johansenc0929212017-07-31 17:36:45 -070041/*
42 * TODO: allow policy to reserve a secid range?
43 * TODO: add secid pinning
44 * TODO: use secid_update in label replace
45 */
46
John Johansenc0929212017-07-31 17:36:45 -070047/**
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
51 */
52void aa_secid_update(u32 secid, struct aa_label *label)
53{
John Johansenc0929212017-07-31 17:36:45 -070054 unsigned long flags;
55
56 spin_lock_irqsave(&secid_lock, flags);
Matthew Wilcox99cc45e2018-05-22 02:32:59 -070057 idr_replace(&aa_secids, label, secid);
John Johansenc0929212017-07-31 17:36:45 -070058 spin_unlock_irqrestore(&secid_lock, flags);
59}
60
61/**
62 *
63 * see label for inverse aa_label_to_secid
64 */
65struct aa_label *aa_secid_to_label(u32 secid)
66{
67 struct aa_label *label;
68
69 rcu_read_lock();
Matthew Wilcox99cc45e2018-05-22 02:32:59 -070070 label = idr_find(&aa_secids, secid);
John Johansenc0929212017-07-31 17:36:45 -070071 rcu_read_unlock();
72
73 return label;
74}
75
76int apparmor_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
77{
78 /* TODO: cache secctx and ref count so we don't have to recreate */
79 struct aa_label *label = aa_secid_to_label(secid);
John Johansen52e7128e2018-05-04 01:57:47 -070080 int len;
John Johansenc0929212017-07-31 17:36:45 -070081
82 AA_BUG(!secdata);
83 AA_BUG(!seclen);
84
85 if (!label)
86 return -EINVAL;
87
88 if (secdata)
John Johansen52e7128e2018-05-04 01:57:47 -070089 len = aa_label_asxprint(secdata, root_ns, label,
90 FLAG_SHOW_MODE | FLAG_VIEW_SUBNS |
91 FLAG_HIDDEN_UNCONFINED | FLAG_ABS_ROOT,
92 GFP_ATOMIC);
John Johansenc0929212017-07-31 17:36:45 -070093 else
John Johansen52e7128e2018-05-04 01:57:47 -070094 len = aa_label_snxprint(NULL, 0, root_ns, label,
95 FLAG_SHOW_MODE | FLAG_VIEW_SUBNS |
96 FLAG_HIDDEN_UNCONFINED | FLAG_ABS_ROOT);
97 if (len < 0)
John Johansenc0929212017-07-31 17:36:45 -070098 return -ENOMEM;
99
John Johansen52e7128e2018-05-04 01:57:47 -0700100 *seclen = len;
101
John Johansenc0929212017-07-31 17:36:45 -0700102 return 0;
103}
104
John Johansenc0929212017-07-31 17:36:45 -0700105int apparmor_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
106{
107 struct aa_label *label;
108
109 label = aa_label_strn_parse(&root_ns->unconfined->label, secdata,
110 seclen, GFP_KERNEL, false, false);
111 if (IS_ERR(label))
112 return PTR_ERR(label);
113 *secid = label->secid;
114
115 return 0;
116}
117
118void apparmor_release_secctx(char *secdata, u32 seclen)
119{
120 kfree(secdata);
121}
122
John Johansen121d4a92017-01-16 00:42:17 -0800123/**
124 * aa_alloc_secid - allocate a new secid for a profile
John Johansena4c3f892018-06-04 19:44:59 -0700125 * @label: the label to allocate a secid for
126 * @gfp: memory allocation flags
127 *
128 * Returns: 0 with @label->secid initialized
129 * <0 returns error with @label->secid set to AA_SECID_INVALID
John Johansen121d4a92017-01-16 00:42:17 -0800130 */
John Johansena4c3f892018-06-04 19:44:59 -0700131int aa_alloc_secid(struct aa_label *label, gfp_t gfp)
John Johansen121d4a92017-01-16 00:42:17 -0800132{
John Johansenc0929212017-07-31 17:36:45 -0700133 unsigned long flags;
John Johansena4c3f892018-06-04 19:44:59 -0700134 int ret;
John Johansen121d4a92017-01-16 00:42:17 -0800135
Matthew Wilcox99cc45e2018-05-22 02:32:59 -0700136 idr_preload(gfp);
137 spin_lock_irqsave(&secid_lock, flags);
John Johansena4c3f892018-06-04 19:44:59 -0700138 ret = idr_alloc(&aa_secids, label, AA_FIRST_SECID, 0, GFP_ATOMIC);
Matthew Wilcox99cc45e2018-05-22 02:32:59 -0700139 spin_unlock_irqrestore(&secid_lock, flags);
140 idr_preload_end();
John Johansenc0929212017-07-31 17:36:45 -0700141
John Johansena4c3f892018-06-04 19:44:59 -0700142 if (ret < 0) {
143 label->secid = AA_SECID_INVALID;
144 return ret;
145 }
146
147 AA_BUG(ret == AA_SECID_INVALID);
148 label->secid = ret;
149 return 0;
John Johansen121d4a92017-01-16 00:42:17 -0800150}
151
152/**
153 * aa_free_secid - free a secid
154 * @secid: secid to free
155 */
156void aa_free_secid(u32 secid)
157{
John Johansenc0929212017-07-31 17:36:45 -0700158 unsigned long flags;
159
160 spin_lock_irqsave(&secid_lock, flags);
Matthew Wilcox99cc45e2018-05-22 02:32:59 -0700161 idr_remove(&aa_secids, secid);
John Johansenc0929212017-07-31 17:36:45 -0700162 spin_unlock_irqrestore(&secid_lock, flags);
John Johansen121d4a92017-01-16 00:42:17 -0800163}
John Johansena4c3f892018-06-04 19:44:59 -0700164
165void aa_secids_init(void)
166{
167 idr_init_base(&aa_secids, AA_FIRST_SECID);
168}