This source file includes following definitions.
- sel_ib_pkey_hashfn
- sel_ib_pkey_find
- sel_ib_pkey_insert
- sel_ib_pkey_sid_slow
- sel_ib_pkey_sid
- sel_ib_pkey_flush
- sel_ib_pkey_init
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 #include <linux/types.h>
22 #include <linux/rcupdate.h>
23 #include <linux/list.h>
24 #include <linux/spinlock.h>
25
26 #include "ibpkey.h"
27 #include "objsec.h"
28
29 #define SEL_PKEY_HASH_SIZE 256
30 #define SEL_PKEY_HASH_BKT_LIMIT 16
31
32 struct sel_ib_pkey_bkt {
33 int size;
34 struct list_head list;
35 };
36
37 struct sel_ib_pkey {
38 struct pkey_security_struct psec;
39 struct list_head list;
40 struct rcu_head rcu;
41 };
42
43 static LIST_HEAD(sel_ib_pkey_list);
44 static DEFINE_SPINLOCK(sel_ib_pkey_lock);
45 static struct sel_ib_pkey_bkt sel_ib_pkey_hash[SEL_PKEY_HASH_SIZE];
46
47
48
49
50
51
52
53
54
55
56 static unsigned int sel_ib_pkey_hashfn(u16 pkey)
57 {
58 return (pkey & (SEL_PKEY_HASH_SIZE - 1));
59 }
60
61
62
63
64
65
66
67
68
69
70
71 static struct sel_ib_pkey *sel_ib_pkey_find(u64 subnet_prefix, u16 pkey_num)
72 {
73 unsigned int idx;
74 struct sel_ib_pkey *pkey;
75
76 idx = sel_ib_pkey_hashfn(pkey_num);
77 list_for_each_entry_rcu(pkey, &sel_ib_pkey_hash[idx].list, list) {
78 if (pkey->psec.pkey == pkey_num &&
79 pkey->psec.subnet_prefix == subnet_prefix)
80 return pkey;
81 }
82
83 return NULL;
84 }
85
86
87
88
89
90
91
92
93
94 static void sel_ib_pkey_insert(struct sel_ib_pkey *pkey)
95 {
96 unsigned int idx;
97
98
99
100
101 idx = sel_ib_pkey_hashfn(pkey->psec.pkey);
102 list_add_rcu(&pkey->list, &sel_ib_pkey_hash[idx].list);
103 if (sel_ib_pkey_hash[idx].size == SEL_PKEY_HASH_BKT_LIMIT) {
104 struct sel_ib_pkey *tail;
105
106 tail = list_entry(
107 rcu_dereference_protected(
108 sel_ib_pkey_hash[idx].list.prev,
109 lockdep_is_held(&sel_ib_pkey_lock)),
110 struct sel_ib_pkey, list);
111 list_del_rcu(&tail->list);
112 kfree_rcu(tail, rcu);
113 } else {
114 sel_ib_pkey_hash[idx].size++;
115 }
116 }
117
118
119
120
121
122
123
124
125
126
127
128
129
130 static int sel_ib_pkey_sid_slow(u64 subnet_prefix, u16 pkey_num, u32 *sid)
131 {
132 int ret;
133 struct sel_ib_pkey *pkey;
134 struct sel_ib_pkey *new = NULL;
135 unsigned long flags;
136
137 spin_lock_irqsave(&sel_ib_pkey_lock, flags);
138 pkey = sel_ib_pkey_find(subnet_prefix, pkey_num);
139 if (pkey) {
140 *sid = pkey->psec.sid;
141 spin_unlock_irqrestore(&sel_ib_pkey_lock, flags);
142 return 0;
143 }
144
145 ret = security_ib_pkey_sid(&selinux_state, subnet_prefix, pkey_num,
146 sid);
147 if (ret)
148 goto out;
149
150
151
152
153 new = kzalloc(sizeof(*new), GFP_ATOMIC);
154 if (!new)
155 goto out;
156
157 new->psec.subnet_prefix = subnet_prefix;
158 new->psec.pkey = pkey_num;
159 new->psec.sid = *sid;
160 sel_ib_pkey_insert(new);
161
162 out:
163 spin_unlock_irqrestore(&sel_ib_pkey_lock, flags);
164 return ret;
165 }
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180 int sel_ib_pkey_sid(u64 subnet_prefix, u16 pkey_num, u32 *sid)
181 {
182 struct sel_ib_pkey *pkey;
183
184 rcu_read_lock();
185 pkey = sel_ib_pkey_find(subnet_prefix, pkey_num);
186 if (pkey) {
187 *sid = pkey->psec.sid;
188 rcu_read_unlock();
189 return 0;
190 }
191 rcu_read_unlock();
192
193 return sel_ib_pkey_sid_slow(subnet_prefix, pkey_num, sid);
194 }
195
196
197
198
199
200
201
202
203 void sel_ib_pkey_flush(void)
204 {
205 unsigned int idx;
206 struct sel_ib_pkey *pkey, *pkey_tmp;
207 unsigned long flags;
208
209 spin_lock_irqsave(&sel_ib_pkey_lock, flags);
210 for (idx = 0; idx < SEL_PKEY_HASH_SIZE; idx++) {
211 list_for_each_entry_safe(pkey, pkey_tmp,
212 &sel_ib_pkey_hash[idx].list, list) {
213 list_del_rcu(&pkey->list);
214 kfree_rcu(pkey, rcu);
215 }
216 sel_ib_pkey_hash[idx].size = 0;
217 }
218 spin_unlock_irqrestore(&sel_ib_pkey_lock, flags);
219 }
220
221 static __init int sel_ib_pkey_init(void)
222 {
223 int iter;
224
225 if (!selinux_enabled)
226 return 0;
227
228 for (iter = 0; iter < SEL_PKEY_HASH_SIZE; iter++) {
229 INIT_LIST_HEAD(&sel_ib_pkey_hash[iter].list);
230 sel_ib_pkey_hash[iter].size = 0;
231 }
232
233 return 0;
234 }
235
236 subsys_initcall(sel_ib_pkey_init);