1 // SPDX-License-Identifier: GPL-2.0-only
5 * SELinux must keep a mapping of Infinband PKEYs to labels/SIDs. This
6 * mapping is maintained as part of the normal policy but a fast cache is
7 * needed to reduce the lookup overhead.
9 * This code is heavily based on the "netif" and "netport" concept originally
11 * James Morris <jmorris@redhat.com> and
12 * Paul Moore <paul@paul-moore.com>
13 * (see security/selinux/netif.c and security/selinux/netport.c for more
18 * (c) Mellanox Technologies, 2016
21 #include <linux/types.h>
22 #include <linux/rcupdate.h>
23 #include <linux/list.h>
24 #include <linux/spinlock.h>
29 #define SEL_PKEY_HASH_SIZE 256
30 #define SEL_PKEY_HASH_BKT_LIMIT 16
32 struct sel_ib_pkey_bkt
{
34 struct list_head list
;
38 struct pkey_security_struct psec
;
39 struct list_head list
;
43 static DEFINE_SPINLOCK(sel_ib_pkey_lock
);
44 static struct sel_ib_pkey_bkt sel_ib_pkey_hash
[SEL_PKEY_HASH_SIZE
];
47 * sel_ib_pkey_hashfn - Hashing function for the pkey table
51 * This is the hashing function for the pkey table, it returns the bucket
52 * number for the given pkey.
55 static unsigned int sel_ib_pkey_hashfn(u16 pkey
)
57 return (pkey
& (SEL_PKEY_HASH_SIZE
- 1));
61 * sel_ib_pkey_find - Search for a pkey record
62 * @subnet_prefix: subnet_prefix
66 * Search the pkey table and return the matching record. If an entry
67 * can not be found in the table return NULL.
70 static struct sel_ib_pkey
*sel_ib_pkey_find(u64 subnet_prefix
, u16 pkey_num
)
73 struct sel_ib_pkey
*pkey
;
75 idx
= sel_ib_pkey_hashfn(pkey_num
);
76 list_for_each_entry_rcu(pkey
, &sel_ib_pkey_hash
[idx
].list
, list
) {
77 if (pkey
->psec
.pkey
== pkey_num
&&
78 pkey
->psec
.subnet_prefix
== subnet_prefix
)
86 * sel_ib_pkey_insert - Insert a new pkey into the table
87 * @pkey: the new pkey record
90 * Add a new pkey record to the hash table.
93 static void sel_ib_pkey_insert(struct sel_ib_pkey
*pkey
)
97 /* we need to impose a limit on the growth of the hash table so check
98 * this bucket to make sure it is within the specified bounds
100 idx
= sel_ib_pkey_hashfn(pkey
->psec
.pkey
);
101 list_add_rcu(&pkey
->list
, &sel_ib_pkey_hash
[idx
].list
);
102 if (sel_ib_pkey_hash
[idx
].size
== SEL_PKEY_HASH_BKT_LIMIT
) {
103 struct sel_ib_pkey
*tail
;
106 rcu_dereference_protected(
107 list_tail_rcu(&sel_ib_pkey_hash
[idx
].list
),
108 lockdep_is_held(&sel_ib_pkey_lock
)),
109 struct sel_ib_pkey
, list
);
110 list_del_rcu(&tail
->list
);
111 kfree_rcu(tail
, rcu
);
113 sel_ib_pkey_hash
[idx
].size
++;
118 * sel_ib_pkey_sid_slow - Lookup the SID of a pkey using the policy
119 * @subnet_prefix: subnet prefix
120 * @pkey_num: pkey number
124 * This function determines the SID of a pkey by querying the security
125 * policy. The result is added to the pkey table to speedup future
126 * queries. Returns zero on success, negative values on failure.
129 static int sel_ib_pkey_sid_slow(u64 subnet_prefix
, u16 pkey_num
, u32
*sid
)
132 struct sel_ib_pkey
*pkey
;
133 struct sel_ib_pkey
*new = NULL
;
136 spin_lock_irqsave(&sel_ib_pkey_lock
, flags
);
137 pkey
= sel_ib_pkey_find(subnet_prefix
, pkey_num
);
139 *sid
= pkey
->psec
.sid
;
140 spin_unlock_irqrestore(&sel_ib_pkey_lock
, flags
);
144 ret
= security_ib_pkey_sid(subnet_prefix
, pkey_num
,
149 /* If this memory allocation fails still return 0. The SID
150 * is valid, it just won't be added to the cache.
152 new = kzalloc(sizeof(*new), GFP_ATOMIC
);
158 new->psec
.subnet_prefix
= subnet_prefix
;
159 new->psec
.pkey
= pkey_num
;
160 new->psec
.sid
= *sid
;
161 sel_ib_pkey_insert(new);
164 spin_unlock_irqrestore(&sel_ib_pkey_lock
, flags
);
169 * sel_ib_pkey_sid - Lookup the SID of a PKEY
170 * @subnet_prefix: subnet_prefix
171 * @pkey_num: pkey number
175 * This function determines the SID of a PKEY using the fastest method
176 * possible. First the pkey table is queried, but if an entry can't be found
177 * then the policy is queried and the result is added to the table to speedup
178 * future queries. Returns zero on success, negative values on failure.
181 int sel_ib_pkey_sid(u64 subnet_prefix
, u16 pkey_num
, u32
*sid
)
183 struct sel_ib_pkey
*pkey
;
186 pkey
= sel_ib_pkey_find(subnet_prefix
, pkey_num
);
188 *sid
= pkey
->psec
.sid
;
194 return sel_ib_pkey_sid_slow(subnet_prefix
, pkey_num
, sid
);
198 * sel_ib_pkey_flush - Flush the entire pkey table
201 * Remove all entries from the pkey table
204 void sel_ib_pkey_flush(void)
207 struct sel_ib_pkey
*pkey
, *pkey_tmp
;
210 spin_lock_irqsave(&sel_ib_pkey_lock
, flags
);
211 for (idx
= 0; idx
< SEL_PKEY_HASH_SIZE
; idx
++) {
212 list_for_each_entry_safe(pkey
, pkey_tmp
,
213 &sel_ib_pkey_hash
[idx
].list
, list
) {
214 list_del_rcu(&pkey
->list
);
215 kfree_rcu(pkey
, rcu
);
217 sel_ib_pkey_hash
[idx
].size
= 0;
219 spin_unlock_irqrestore(&sel_ib_pkey_lock
, flags
);
222 static __init
int sel_ib_pkey_init(void)
226 if (!selinux_enabled_boot
)
229 for (iter
= 0; iter
< SEL_PKEY_HASH_SIZE
; iter
++) {
230 INIT_LIST_HEAD(&sel_ib_pkey_hash
[iter
].list
);
231 sel_ib_pkey_hash
[iter
].size
= 0;
237 subsys_initcall(sel_ib_pkey_init
);