1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Implementation of the kernel access vector cache (AVC).
4 *
5 * Authors: Stephen Smalley, <sds@tycho.nsa.gov>
6 * James Morris <jmorris@redhat.com>
7 *
8 * Update: KaiGai, Kohei <kaigai@ak.jp.nec.com>
9 * Replaced the avc_lock spinlock by RCU.
10 *
11 * Copyright (C) 2003 Red Hat, Inc., James Morris <jmorris@redhat.com>
12 */
13 #include <linux/types.h>
14 #include <linux/stddef.h>
15 #include <linux/kernel.h>
16 #include <linux/slab.h>
17 #include <linux/fs.h>
18 #include <linux/dcache.h>
19 #include <linux/init.h>
20 #include <linux/skbuff.h>
21 #include <linux/percpu.h>
22 #include <linux/list.h>
23 #include <net/sock.h>
24 #include <linux/un.h>
25 #include <net/af_unix.h>
26 #include <linux/ip.h>
27 #include <linux/audit.h>
28 #include <linux/ipv6.h>
29 #include <net/ipv6.h>
30 #include "avc.h"
31 #include "avc_ss.h"
32 #include "classmap.h"
33
34 #define CREATE_TRACE_POINTS
35 #include <trace/events/avc.h>
36
37 #define AVC_CACHE_SLOTS 512
38 #define AVC_DEF_CACHE_THRESHOLD 512
39 #define AVC_CACHE_RECLAIM 16
40
41 #ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
42 #define avc_cache_stats_incr(field) this_cpu_inc(avc_cache_stats.field)
43 #else
44 #define avc_cache_stats_incr(field) do {} while (0)
45 #endif
46
47 #undef CREATE_TRACE_POINTS
48 #include <trace/hooks/avc.h>
49
50 struct avc_entry {
51 u32 ssid;
52 u32 tsid;
53 u16 tclass;
54 struct av_decision avd;
55 struct avc_xperms_node *xp_node;
56 };
57
58 struct avc_node {
59 struct avc_entry ae;
60 struct hlist_node list; /* anchored in avc_cache->slots[i] */
61 struct rcu_head rhead;
62 };
63
64 struct avc_xperms_decision_node {
65 struct extended_perms_decision xpd;
66 struct list_head xpd_list; /* list of extended_perms_decision */
67 };
68
69 struct avc_xperms_node {
70 struct extended_perms xp;
71 struct list_head xpd_head; /* list head of extended_perms_decision */
72 };
73
74 struct avc_cache {
75 struct hlist_head slots[AVC_CACHE_SLOTS]; /* head for avc_node->list */
76 spinlock_t slots_lock[AVC_CACHE_SLOTS]; /* lock for writes */
77 atomic_t lru_hint; /* LRU hint for reclaim scan */
78 atomic_t active_nodes;
79 u32 latest_notif; /* latest revocation notification */
80 };
81
82 struct avc_callback_node {
83 int (*callback) (u32 event);
84 u32 events;
85 struct avc_callback_node *next;
86 };
87
88 #ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
89 DEFINE_PER_CPU(struct avc_cache_stats, avc_cache_stats) = { 0 };
90 #endif
91
92 struct selinux_avc {
93 unsigned int avc_cache_threshold;
94 struct avc_cache avc_cache;
95 };
96
97 static struct selinux_avc selinux_avc;
98
selinux_avc_init(struct selinux_avc ** avc)99 void selinux_avc_init(struct selinux_avc **avc)
100 {
101 int i;
102
103 selinux_avc.avc_cache_threshold = AVC_DEF_CACHE_THRESHOLD;
104 for (i = 0; i < AVC_CACHE_SLOTS; i++) {
105 INIT_HLIST_HEAD(&selinux_avc.avc_cache.slots[i]);
106 spin_lock_init(&selinux_avc.avc_cache.slots_lock[i]);
107 }
108 atomic_set(&selinux_avc.avc_cache.active_nodes, 0);
109 atomic_set(&selinux_avc.avc_cache.lru_hint, 0);
110 *avc = &selinux_avc;
111 }
112
avc_get_cache_threshold(struct selinux_avc * avc)113 unsigned int avc_get_cache_threshold(struct selinux_avc *avc)
114 {
115 return avc->avc_cache_threshold;
116 }
117
avc_set_cache_threshold(struct selinux_avc * avc,unsigned int cache_threshold)118 void avc_set_cache_threshold(struct selinux_avc *avc,
119 unsigned int cache_threshold)
120 {
121 avc->avc_cache_threshold = cache_threshold;
122 }
123
124 static struct avc_callback_node *avc_callbacks __ro_after_init;
125 static struct kmem_cache *avc_node_cachep __ro_after_init;
126 static struct kmem_cache *avc_xperms_data_cachep __ro_after_init;
127 static struct kmem_cache *avc_xperms_decision_cachep __ro_after_init;
128 static struct kmem_cache *avc_xperms_cachep __ro_after_init;
129
avc_hash(u32 ssid,u32 tsid,u16 tclass)130 static inline int avc_hash(u32 ssid, u32 tsid, u16 tclass)
131 {
132 return (ssid ^ (tsid<<2) ^ (tclass<<4)) & (AVC_CACHE_SLOTS - 1);
133 }
134
135 /**
136 * avc_init - Initialize the AVC.
137 *
138 * Initialize the access vector cache.
139 */
avc_init(void)140 void __init avc_init(void)
141 {
142 avc_node_cachep = kmem_cache_create("avc_node", sizeof(struct avc_node),
143 0, SLAB_PANIC, NULL);
144 avc_xperms_cachep = kmem_cache_create("avc_xperms_node",
145 sizeof(struct avc_xperms_node),
146 0, SLAB_PANIC, NULL);
147 avc_xperms_decision_cachep = kmem_cache_create(
148 "avc_xperms_decision_node",
149 sizeof(struct avc_xperms_decision_node),
150 0, SLAB_PANIC, NULL);
151 avc_xperms_data_cachep = kmem_cache_create("avc_xperms_data",
152 sizeof(struct extended_perms_data),
153 0, SLAB_PANIC, NULL);
154 }
155
avc_get_hash_stats(struct selinux_avc * avc,char * page)156 int avc_get_hash_stats(struct selinux_avc *avc, char *page)
157 {
158 int i, chain_len, max_chain_len, slots_used;
159 struct avc_node *node;
160 struct hlist_head *head;
161
162 rcu_read_lock();
163
164 slots_used = 0;
165 max_chain_len = 0;
166 for (i = 0; i < AVC_CACHE_SLOTS; i++) {
167 head = &avc->avc_cache.slots[i];
168 if (!hlist_empty(head)) {
169 slots_used++;
170 chain_len = 0;
171 hlist_for_each_entry_rcu(node, head, list)
172 chain_len++;
173 if (chain_len > max_chain_len)
174 max_chain_len = chain_len;
175 }
176 }
177
178 rcu_read_unlock();
179
180 return scnprintf(page, PAGE_SIZE, "entries: %d\nbuckets used: %d/%d\n"
181 "longest chain: %d\n",
182 atomic_read(&avc->avc_cache.active_nodes),
183 slots_used, AVC_CACHE_SLOTS, max_chain_len);
184 }
185
186 /*
187 * using a linked list for extended_perms_decision lookup because the list is
188 * always small. i.e. less than 5, typically 1
189 */
avc_xperms_decision_lookup(u8 driver,struct avc_xperms_node * xp_node)190 static struct extended_perms_decision *avc_xperms_decision_lookup(u8 driver,
191 struct avc_xperms_node *xp_node)
192 {
193 struct avc_xperms_decision_node *xpd_node;
194
195 list_for_each_entry(xpd_node, &xp_node->xpd_head, xpd_list) {
196 if (xpd_node->xpd.driver == driver)
197 return &xpd_node->xpd;
198 }
199 return NULL;
200 }
201
202 static inline unsigned int
avc_xperms_has_perm(struct extended_perms_decision * xpd,u8 perm,u8 which)203 avc_xperms_has_perm(struct extended_perms_decision *xpd,
204 u8 perm, u8 which)
205 {
206 unsigned int rc = 0;
207
208 if ((which == XPERMS_ALLOWED) &&
209 (xpd->used & XPERMS_ALLOWED))
210 rc = security_xperm_test(xpd->allowed->p, perm);
211 else if ((which == XPERMS_AUDITALLOW) &&
212 (xpd->used & XPERMS_AUDITALLOW))
213 rc = security_xperm_test(xpd->auditallow->p, perm);
214 else if ((which == XPERMS_DONTAUDIT) &&
215 (xpd->used & XPERMS_DONTAUDIT))
216 rc = security_xperm_test(xpd->dontaudit->p, perm);
217 return rc;
218 }
219
avc_xperms_allow_perm(struct avc_xperms_node * xp_node,u8 driver,u8 perm)220 static void avc_xperms_allow_perm(struct avc_xperms_node *xp_node,
221 u8 driver, u8 perm)
222 {
223 struct extended_perms_decision *xpd;
224 security_xperm_set(xp_node->xp.drivers.p, driver);
225 xpd = avc_xperms_decision_lookup(driver, xp_node);
226 if (xpd && xpd->allowed)
227 security_xperm_set(xpd->allowed->p, perm);
228 }
229
avc_xperms_decision_free(struct avc_xperms_decision_node * xpd_node)230 static void avc_xperms_decision_free(struct avc_xperms_decision_node *xpd_node)
231 {
232 struct extended_perms_decision *xpd;
233
234 xpd = &xpd_node->xpd;
235 if (xpd->allowed)
236 kmem_cache_free(avc_xperms_data_cachep, xpd->allowed);
237 if (xpd->auditallow)
238 kmem_cache_free(avc_xperms_data_cachep, xpd->auditallow);
239 if (xpd->dontaudit)
240 kmem_cache_free(avc_xperms_data_cachep, xpd->dontaudit);
241 kmem_cache_free(avc_xperms_decision_cachep, xpd_node);
242 }
243
avc_xperms_free(struct avc_xperms_node * xp_node)244 static void avc_xperms_free(struct avc_xperms_node *xp_node)
245 {
246 struct avc_xperms_decision_node *xpd_node, *tmp;
247
248 if (!xp_node)
249 return;
250
251 list_for_each_entry_safe(xpd_node, tmp, &xp_node->xpd_head, xpd_list) {
252 list_del(&xpd_node->xpd_list);
253 avc_xperms_decision_free(xpd_node);
254 }
255 kmem_cache_free(avc_xperms_cachep, xp_node);
256 }
257
avc_copy_xperms_decision(struct extended_perms_decision * dest,struct extended_perms_decision * src)258 static void avc_copy_xperms_decision(struct extended_perms_decision *dest,
259 struct extended_perms_decision *src)
260 {
261 dest->driver = src->driver;
262 dest->used = src->used;
263 if (dest->used & XPERMS_ALLOWED)
264 memcpy(dest->allowed->p, src->allowed->p,
265 sizeof(src->allowed->p));
266 if (dest->used & XPERMS_AUDITALLOW)
267 memcpy(dest->auditallow->p, src->auditallow->p,
268 sizeof(src->auditallow->p));
269 if (dest->used & XPERMS_DONTAUDIT)
270 memcpy(dest->dontaudit->p, src->dontaudit->p,
271 sizeof(src->dontaudit->p));
272 }
273
274 /*
275 * similar to avc_copy_xperms_decision, but only copy decision
276 * information relevant to this perm
277 */
avc_quick_copy_xperms_decision(u8 perm,struct extended_perms_decision * dest,struct extended_perms_decision * src)278 static inline void avc_quick_copy_xperms_decision(u8 perm,
279 struct extended_perms_decision *dest,
280 struct extended_perms_decision *src)
281 {
282 /*
283 * compute index of the u32 of the 256 bits (8 u32s) that contain this
284 * command permission
285 */
286 u8 i = perm >> 5;
287
288 dest->used = src->used;
289 if (dest->used & XPERMS_ALLOWED)
290 dest->allowed->p[i] = src->allowed->p[i];
291 if (dest->used & XPERMS_AUDITALLOW)
292 dest->auditallow->p[i] = src->auditallow->p[i];
293 if (dest->used & XPERMS_DONTAUDIT)
294 dest->dontaudit->p[i] = src->dontaudit->p[i];
295 }
296
297 static struct avc_xperms_decision_node
avc_xperms_decision_alloc(u8 which)298 *avc_xperms_decision_alloc(u8 which)
299 {
300 struct avc_xperms_decision_node *xpd_node;
301 struct extended_perms_decision *xpd;
302
303 xpd_node = kmem_cache_zalloc(avc_xperms_decision_cachep,
304 GFP_NOWAIT | __GFP_NOWARN);
305 if (!xpd_node)
306 return NULL;
307
308 xpd = &xpd_node->xpd;
309 if (which & XPERMS_ALLOWED) {
310 xpd->allowed = kmem_cache_zalloc(avc_xperms_data_cachep,
311 GFP_NOWAIT | __GFP_NOWARN);
312 if (!xpd->allowed)
313 goto error;
314 }
315 if (which & XPERMS_AUDITALLOW) {
316 xpd->auditallow = kmem_cache_zalloc(avc_xperms_data_cachep,
317 GFP_NOWAIT | __GFP_NOWARN);
318 if (!xpd->auditallow)
319 goto error;
320 }
321 if (which & XPERMS_DONTAUDIT) {
322 xpd->dontaudit = kmem_cache_zalloc(avc_xperms_data_cachep,
323 GFP_NOWAIT | __GFP_NOWARN);
324 if (!xpd->dontaudit)
325 goto error;
326 }
327 return xpd_node;
328 error:
329 avc_xperms_decision_free(xpd_node);
330 return NULL;
331 }
332
avc_add_xperms_decision(struct avc_node * node,struct extended_perms_decision * src)333 static int avc_add_xperms_decision(struct avc_node *node,
334 struct extended_perms_decision *src)
335 {
336 struct avc_xperms_decision_node *dest_xpd;
337
338 node->ae.xp_node->xp.len++;
339 dest_xpd = avc_xperms_decision_alloc(src->used);
340 if (!dest_xpd)
341 return -ENOMEM;
342 avc_copy_xperms_decision(&dest_xpd->xpd, src);
343 list_add(&dest_xpd->xpd_list, &node->ae.xp_node->xpd_head);
344 return 0;
345 }
346
avc_xperms_alloc(void)347 static struct avc_xperms_node *avc_xperms_alloc(void)
348 {
349 struct avc_xperms_node *xp_node;
350
351 xp_node = kmem_cache_zalloc(avc_xperms_cachep, GFP_NOWAIT | __GFP_NOWARN);
352 if (!xp_node)
353 return xp_node;
354 INIT_LIST_HEAD(&xp_node->xpd_head);
355 return xp_node;
356 }
357
avc_xperms_populate(struct avc_node * node,struct avc_xperms_node * src)358 static int avc_xperms_populate(struct avc_node *node,
359 struct avc_xperms_node *src)
360 {
361 struct avc_xperms_node *dest;
362 struct avc_xperms_decision_node *dest_xpd;
363 struct avc_xperms_decision_node *src_xpd;
364
365 if (src->xp.len == 0)
366 return 0;
367 dest = avc_xperms_alloc();
368 if (!dest)
369 return -ENOMEM;
370
371 memcpy(dest->xp.drivers.p, src->xp.drivers.p, sizeof(dest->xp.drivers.p));
372 dest->xp.len = src->xp.len;
373
374 /* for each source xpd allocate a destination xpd and copy */
375 list_for_each_entry(src_xpd, &src->xpd_head, xpd_list) {
376 dest_xpd = avc_xperms_decision_alloc(src_xpd->xpd.used);
377 if (!dest_xpd)
378 goto error;
379 avc_copy_xperms_decision(&dest_xpd->xpd, &src_xpd->xpd);
380 list_add(&dest_xpd->xpd_list, &dest->xpd_head);
381 }
382 node->ae.xp_node = dest;
383 return 0;
384 error:
385 avc_xperms_free(dest);
386 return -ENOMEM;
387
388 }
389
avc_xperms_audit_required(u32 requested,struct av_decision * avd,struct extended_perms_decision * xpd,u8 perm,int result,u32 * deniedp)390 static inline u32 avc_xperms_audit_required(u32 requested,
391 struct av_decision *avd,
392 struct extended_perms_decision *xpd,
393 u8 perm,
394 int result,
395 u32 *deniedp)
396 {
397 u32 denied, audited;
398
399 denied = requested & ~avd->allowed;
400 if (unlikely(denied)) {
401 audited = denied & avd->auditdeny;
402 if (audited && xpd) {
403 if (avc_xperms_has_perm(xpd, perm, XPERMS_DONTAUDIT))
404 audited &= ~requested;
405 }
406 } else if (result) {
407 audited = denied = requested;
408 } else {
409 audited = requested & avd->auditallow;
410 if (audited && xpd) {
411 if (!avc_xperms_has_perm(xpd, perm, XPERMS_AUDITALLOW))
412 audited &= ~requested;
413 }
414 }
415
416 *deniedp = denied;
417 return audited;
418 }
419
avc_xperms_audit(struct selinux_state * state,u32 ssid,u32 tsid,u16 tclass,u32 requested,struct av_decision * avd,struct extended_perms_decision * xpd,u8 perm,int result,struct common_audit_data * ad)420 static inline int avc_xperms_audit(struct selinux_state *state,
421 u32 ssid, u32 tsid, u16 tclass,
422 u32 requested, struct av_decision *avd,
423 struct extended_perms_decision *xpd,
424 u8 perm, int result,
425 struct common_audit_data *ad)
426 {
427 u32 audited, denied;
428
429 audited = avc_xperms_audit_required(
430 requested, avd, xpd, perm, result, &denied);
431 if (likely(!audited))
432 return 0;
433 return slow_avc_audit(state, ssid, tsid, tclass, requested,
434 audited, denied, result, ad);
435 }
436
avc_node_free(struct rcu_head * rhead)437 static void avc_node_free(struct rcu_head *rhead)
438 {
439 struct avc_node *node = container_of(rhead, struct avc_node, rhead);
440 avc_xperms_free(node->ae.xp_node);
441 kmem_cache_free(avc_node_cachep, node);
442 avc_cache_stats_incr(frees);
443 }
444
avc_node_delete(struct selinux_avc * avc,struct avc_node * node)445 static void avc_node_delete(struct selinux_avc *avc, struct avc_node *node)
446 {
447 trace_android_rvh_selinux_avc_node_delete(node);
448 hlist_del_rcu(&node->list);
449 call_rcu(&node->rhead, avc_node_free);
450 atomic_dec(&avc->avc_cache.active_nodes);
451 }
452
avc_node_kill(struct selinux_avc * avc,struct avc_node * node)453 static void avc_node_kill(struct selinux_avc *avc, struct avc_node *node)
454 {
455 avc_xperms_free(node->ae.xp_node);
456 kmem_cache_free(avc_node_cachep, node);
457 avc_cache_stats_incr(frees);
458 atomic_dec(&avc->avc_cache.active_nodes);
459 }
460
avc_node_replace(struct selinux_avc * avc,struct avc_node * new,struct avc_node * old)461 static void avc_node_replace(struct selinux_avc *avc,
462 struct avc_node *new, struct avc_node *old)
463 {
464 trace_android_rvh_selinux_avc_node_replace(old, new);
465 hlist_replace_rcu(&old->list, &new->list);
466 call_rcu(&old->rhead, avc_node_free);
467 atomic_dec(&avc->avc_cache.active_nodes);
468 }
469
avc_reclaim_node(struct selinux_avc * avc)470 static inline int avc_reclaim_node(struct selinux_avc *avc)
471 {
472 struct avc_node *node;
473 int hvalue, try, ecx;
474 unsigned long flags;
475 struct hlist_head *head;
476 spinlock_t *lock;
477
478 for (try = 0, ecx = 0; try < AVC_CACHE_SLOTS; try++) {
479 hvalue = atomic_inc_return(&avc->avc_cache.lru_hint) &
480 (AVC_CACHE_SLOTS - 1);
481 head = &avc->avc_cache.slots[hvalue];
482 lock = &avc->avc_cache.slots_lock[hvalue];
483
484 if (!spin_trylock_irqsave(lock, flags))
485 continue;
486
487 rcu_read_lock();
488 hlist_for_each_entry(node, head, list) {
489 avc_node_delete(avc, node);
490 avc_cache_stats_incr(reclaims);
491 ecx++;
492 if (ecx >= AVC_CACHE_RECLAIM) {
493 rcu_read_unlock();
494 spin_unlock_irqrestore(lock, flags);
495 goto out;
496 }
497 }
498 rcu_read_unlock();
499 spin_unlock_irqrestore(lock, flags);
500 }
501 out:
502 return ecx;
503 }
504
avc_alloc_node(struct selinux_avc * avc)505 static struct avc_node *avc_alloc_node(struct selinux_avc *avc)
506 {
507 struct avc_node *node;
508
509 node = kmem_cache_zalloc(avc_node_cachep, GFP_NOWAIT | __GFP_NOWARN);
510 if (!node)
511 goto out;
512
513 INIT_HLIST_NODE(&node->list);
514 avc_cache_stats_incr(allocations);
515
516 if (atomic_inc_return(&avc->avc_cache.active_nodes) >
517 avc->avc_cache_threshold)
518 avc_reclaim_node(avc);
519
520 out:
521 return node;
522 }
523
avc_node_populate(struct avc_node * node,u32 ssid,u32 tsid,u16 tclass,struct av_decision * avd)524 static void avc_node_populate(struct avc_node *node, u32 ssid, u32 tsid, u16 tclass, struct av_decision *avd)
525 {
526 node->ae.ssid = ssid;
527 node->ae.tsid = tsid;
528 node->ae.tclass = tclass;
529 memcpy(&node->ae.avd, avd, sizeof(node->ae.avd));
530 }
531
avc_search_node(struct selinux_avc * avc,u32 ssid,u32 tsid,u16 tclass)532 static inline struct avc_node *avc_search_node(struct selinux_avc *avc,
533 u32 ssid, u32 tsid, u16 tclass)
534 {
535 struct avc_node *node, *ret = NULL;
536 int hvalue;
537 struct hlist_head *head;
538
539 hvalue = avc_hash(ssid, tsid, tclass);
540 head = &avc->avc_cache.slots[hvalue];
541 hlist_for_each_entry_rcu(node, head, list) {
542 if (ssid == node->ae.ssid &&
543 tclass == node->ae.tclass &&
544 tsid == node->ae.tsid) {
545 ret = node;
546 break;
547 }
548 }
549
550 return ret;
551 }
552
553 /**
554 * avc_lookup - Look up an AVC entry.
555 * @ssid: source security identifier
556 * @tsid: target security identifier
557 * @tclass: target security class
558 *
559 * Look up an AVC entry that is valid for the
560 * (@ssid, @tsid), interpreting the permissions
561 * based on @tclass. If a valid AVC entry exists,
562 * then this function returns the avc_node.
563 * Otherwise, this function returns NULL.
564 */
avc_lookup(struct selinux_avc * avc,u32 ssid,u32 tsid,u16 tclass)565 static struct avc_node *avc_lookup(struct selinux_avc *avc,
566 u32 ssid, u32 tsid, u16 tclass)
567 {
568 struct avc_node *node;
569
570 avc_cache_stats_incr(lookups);
571 node = avc_search_node(avc, ssid, tsid, tclass);
572
573 if (node) {
574 trace_android_rvh_selinux_avc_lookup(node, ssid, tsid, tclass);
575 return node;
576 }
577
578 avc_cache_stats_incr(misses);
579 return NULL;
580 }
581
avc_latest_notif_update(struct selinux_avc * avc,int seqno,int is_insert)582 static int avc_latest_notif_update(struct selinux_avc *avc,
583 int seqno, int is_insert)
584 {
585 int ret = 0;
586 static DEFINE_SPINLOCK(notif_lock);
587 unsigned long flag;
588
589 spin_lock_irqsave(¬if_lock, flag);
590 if (is_insert) {
591 if (seqno < avc->avc_cache.latest_notif) {
592 pr_warn("SELinux: avc: seqno %d < latest_notif %d\n",
593 seqno, avc->avc_cache.latest_notif);
594 ret = -EAGAIN;
595 }
596 } else {
597 if (seqno > avc->avc_cache.latest_notif)
598 avc->avc_cache.latest_notif = seqno;
599 }
600 spin_unlock_irqrestore(¬if_lock, flag);
601
602 return ret;
603 }
604
605 /**
606 * avc_insert - Insert an AVC entry.
607 * @ssid: source security identifier
608 * @tsid: target security identifier
609 * @tclass: target security class
610 * @avd: resulting av decision
611 * @xp_node: resulting extended permissions
612 *
613 * Insert an AVC entry for the SID pair
614 * (@ssid, @tsid) and class @tclass.
615 * The access vectors and the sequence number are
616 * normally provided by the security server in
617 * response to a security_compute_av() call. If the
618 * sequence number @avd->seqno is not less than the latest
619 * revocation notification, then the function copies
620 * the access vectors into a cache entry, returns
621 * avc_node inserted. Otherwise, this function returns NULL.
622 */
avc_insert(struct selinux_avc * avc,u32 ssid,u32 tsid,u16 tclass,struct av_decision * avd,struct avc_xperms_node * xp_node)623 static struct avc_node *avc_insert(struct selinux_avc *avc,
624 u32 ssid, u32 tsid, u16 tclass,
625 struct av_decision *avd,
626 struct avc_xperms_node *xp_node)
627 {
628 struct avc_node *pos, *node = NULL;
629 int hvalue;
630 unsigned long flag;
631 spinlock_t *lock;
632 struct hlist_head *head;
633
634 if (avc_latest_notif_update(avc, avd->seqno, 1))
635 return NULL;
636
637 node = avc_alloc_node(avc);
638 if (!node)
639 return NULL;
640
641 avc_node_populate(node, ssid, tsid, tclass, avd);
642 if (avc_xperms_populate(node, xp_node)) {
643 avc_node_kill(avc, node);
644 return NULL;
645 }
646
647 hvalue = avc_hash(ssid, tsid, tclass);
648 head = &avc->avc_cache.slots[hvalue];
649 lock = &avc->avc_cache.slots_lock[hvalue];
650 spin_lock_irqsave(lock, flag);
651 hlist_for_each_entry(pos, head, list) {
652 if (pos->ae.ssid == ssid &&
653 pos->ae.tsid == tsid &&
654 pos->ae.tclass == tclass) {
655 avc_node_replace(avc, node, pos);
656 goto found;
657 }
658 }
659 hlist_add_head_rcu(&node->list, head);
660 trace_android_rvh_selinux_avc_insert(node);
661 found:
662 spin_unlock_irqrestore(lock, flag);
663 return node;
664 }
665
666 /**
667 * avc_audit_pre_callback - SELinux specific information
668 * will be called by generic audit code
669 * @ab: the audit buffer
670 * @a: audit_data
671 */
avc_audit_pre_callback(struct audit_buffer * ab,void * a)672 static void avc_audit_pre_callback(struct audit_buffer *ab, void *a)
673 {
674 struct common_audit_data *ad = a;
675 struct selinux_audit_data *sad = ad->selinux_audit_data;
676 u32 av = sad->audited;
677 const char **perms;
678 int i, perm;
679
680 audit_log_format(ab, "avc: %s ", sad->denied ? "denied" : "granted");
681
682 if (av == 0) {
683 audit_log_format(ab, " null");
684 return;
685 }
686
687 perms = secclass_map[sad->tclass-1].perms;
688
689 audit_log_format(ab, " {");
690 i = 0;
691 perm = 1;
692 while (i < (sizeof(av) * 8)) {
693 if ((perm & av) && perms[i]) {
694 audit_log_format(ab, " %s", perms[i]);
695 av &= ~perm;
696 }
697 i++;
698 perm <<= 1;
699 }
700
701 if (av)
702 audit_log_format(ab, " 0x%x", av);
703
704 audit_log_format(ab, " } for ");
705 }
706
707 /**
708 * avc_audit_post_callback - SELinux specific information
709 * will be called by generic audit code
710 * @ab: the audit buffer
711 * @a: audit_data
712 */
avc_audit_post_callback(struct audit_buffer * ab,void * a)713 static void avc_audit_post_callback(struct audit_buffer *ab, void *a)
714 {
715 struct common_audit_data *ad = a;
716 struct selinux_audit_data *sad = ad->selinux_audit_data;
717 char *scontext = NULL;
718 char *tcontext = NULL;
719 const char *tclass = NULL;
720 u32 scontext_len;
721 u32 tcontext_len;
722 int rc;
723
724 rc = security_sid_to_context(sad->state, sad->ssid, &scontext,
725 &scontext_len);
726 if (rc)
727 audit_log_format(ab, " ssid=%d", sad->ssid);
728 else
729 audit_log_format(ab, " scontext=%s", scontext);
730
731 rc = security_sid_to_context(sad->state, sad->tsid, &tcontext,
732 &tcontext_len);
733 if (rc)
734 audit_log_format(ab, " tsid=%d", sad->tsid);
735 else
736 audit_log_format(ab, " tcontext=%s", tcontext);
737
738 tclass = secclass_map[sad->tclass-1].name;
739 audit_log_format(ab, " tclass=%s", tclass);
740
741 if (sad->denied)
742 audit_log_format(ab, " permissive=%u", sad->result ? 0 : 1);
743
744 trace_selinux_audited(sad, scontext, tcontext, tclass);
745 kfree(tcontext);
746 kfree(scontext);
747
748 /* in case of invalid context report also the actual context string */
749 rc = security_sid_to_context_inval(sad->state, sad->ssid, &scontext,
750 &scontext_len);
751 if (!rc && scontext) {
752 if (scontext_len && scontext[scontext_len - 1] == '\0')
753 scontext_len--;
754 audit_log_format(ab, " srawcon=");
755 audit_log_n_untrustedstring(ab, scontext, scontext_len);
756 kfree(scontext);
757 }
758
759 rc = security_sid_to_context_inval(sad->state, sad->tsid, &scontext,
760 &scontext_len);
761 if (!rc && scontext) {
762 if (scontext_len && scontext[scontext_len - 1] == '\0')
763 scontext_len--;
764 audit_log_format(ab, " trawcon=");
765 audit_log_n_untrustedstring(ab, scontext, scontext_len);
766 kfree(scontext);
767 }
768 }
769
770 /*
771 * This is the slow part of avc audit with big stack footprint.
772 * Note that it is non-blocking and can be called from under
773 * rcu_read_lock().
774 */
slow_avc_audit(struct selinux_state * state,u32 ssid,u32 tsid,u16 tclass,u32 requested,u32 audited,u32 denied,int result,struct common_audit_data * a)775 noinline int slow_avc_audit(struct selinux_state *state,
776 u32 ssid, u32 tsid, u16 tclass,
777 u32 requested, u32 audited, u32 denied, int result,
778 struct common_audit_data *a)
779 {
780 struct common_audit_data stack_data;
781 struct selinux_audit_data sad;
782
783 if (WARN_ON(!tclass || tclass >= ARRAY_SIZE(secclass_map)))
784 return -EINVAL;
785
786 if (!a) {
787 a = &stack_data;
788 a->type = LSM_AUDIT_DATA_NONE;
789 }
790
791 sad.tclass = tclass;
792 sad.requested = requested;
793 sad.ssid = ssid;
794 sad.tsid = tsid;
795 sad.audited = audited;
796 sad.denied = denied;
797 sad.result = result;
798 sad.state = state;
799
800 a->selinux_audit_data = &sad;
801
802 common_lsm_audit(a, avc_audit_pre_callback, avc_audit_post_callback);
803 return 0;
804 }
805
806 /**
807 * avc_add_callback - Register a callback for security events.
808 * @callback: callback function
809 * @events: security events
810 *
811 * Register a callback function for events in the set @events.
812 * Returns %0 on success or -%ENOMEM if insufficient memory
813 * exists to add the callback.
814 */
avc_add_callback(int (* callback)(u32 event),u32 events)815 int __init avc_add_callback(int (*callback)(u32 event), u32 events)
816 {
817 struct avc_callback_node *c;
818 int rc = 0;
819
820 c = kmalloc(sizeof(*c), GFP_KERNEL);
821 if (!c) {
822 rc = -ENOMEM;
823 goto out;
824 }
825
826 c->callback = callback;
827 c->events = events;
828 c->next = avc_callbacks;
829 avc_callbacks = c;
830 out:
831 return rc;
832 }
833
834 /**
835 * avc_update_node - Update an AVC entry
836 * @event : Updating event
837 * @perms : Permission mask bits
838 * @ssid,@tsid,@tclass : identifier of an AVC entry
839 * @seqno : sequence number when decision was made
840 * @xpd: extended_perms_decision to be added to the node
841 * @flags: the AVC_* flags, e.g. AVC_EXTENDED_PERMS, or 0.
842 *
843 * if a valid AVC entry doesn't exist,this function returns -ENOENT.
844 * if kmalloc() called internal returns NULL, this function returns -ENOMEM.
845 * otherwise, this function updates the AVC entry. The original AVC-entry object
846 * will release later by RCU.
847 */
avc_update_node(struct selinux_avc * avc,u32 event,u32 perms,u8 driver,u8 xperm,u32 ssid,u32 tsid,u16 tclass,u32 seqno,struct extended_perms_decision * xpd,u32 flags)848 static int avc_update_node(struct selinux_avc *avc,
849 u32 event, u32 perms, u8 driver, u8 xperm, u32 ssid,
850 u32 tsid, u16 tclass, u32 seqno,
851 struct extended_perms_decision *xpd,
852 u32 flags)
853 {
854 int hvalue, rc = 0;
855 unsigned long flag;
856 struct avc_node *pos, *node, *orig = NULL;
857 struct hlist_head *head;
858 spinlock_t *lock;
859
860 node = avc_alloc_node(avc);
861 if (!node) {
862 rc = -ENOMEM;
863 goto out;
864 }
865
866 /* Lock the target slot */
867 hvalue = avc_hash(ssid, tsid, tclass);
868
869 head = &avc->avc_cache.slots[hvalue];
870 lock = &avc->avc_cache.slots_lock[hvalue];
871
872 spin_lock_irqsave(lock, flag);
873
874 hlist_for_each_entry(pos, head, list) {
875 if (ssid == pos->ae.ssid &&
876 tsid == pos->ae.tsid &&
877 tclass == pos->ae.tclass &&
878 seqno == pos->ae.avd.seqno){
879 orig = pos;
880 break;
881 }
882 }
883
884 if (!orig) {
885 rc = -ENOENT;
886 avc_node_kill(avc, node);
887 goto out_unlock;
888 }
889
890 /*
891 * Copy and replace original node.
892 */
893
894 avc_node_populate(node, ssid, tsid, tclass, &orig->ae.avd);
895
896 if (orig->ae.xp_node) {
897 rc = avc_xperms_populate(node, orig->ae.xp_node);
898 if (rc) {
899 avc_node_kill(avc, node);
900 goto out_unlock;
901 }
902 }
903
904 switch (event) {
905 case AVC_CALLBACK_GRANT:
906 node->ae.avd.allowed |= perms;
907 if (node->ae.xp_node && (flags & AVC_EXTENDED_PERMS))
908 avc_xperms_allow_perm(node->ae.xp_node, driver, xperm);
909 break;
910 case AVC_CALLBACK_TRY_REVOKE:
911 case AVC_CALLBACK_REVOKE:
912 node->ae.avd.allowed &= ~perms;
913 break;
914 case AVC_CALLBACK_AUDITALLOW_ENABLE:
915 node->ae.avd.auditallow |= perms;
916 break;
917 case AVC_CALLBACK_AUDITALLOW_DISABLE:
918 node->ae.avd.auditallow &= ~perms;
919 break;
920 case AVC_CALLBACK_AUDITDENY_ENABLE:
921 node->ae.avd.auditdeny |= perms;
922 break;
923 case AVC_CALLBACK_AUDITDENY_DISABLE:
924 node->ae.avd.auditdeny &= ~perms;
925 break;
926 case AVC_CALLBACK_ADD_XPERMS:
927 avc_add_xperms_decision(node, xpd);
928 break;
929 }
930 avc_node_replace(avc, node, orig);
931 out_unlock:
932 spin_unlock_irqrestore(lock, flag);
933 out:
934 return rc;
935 }
936
937 /**
938 * avc_flush - Flush the cache
939 */
avc_flush(struct selinux_avc * avc)940 static void avc_flush(struct selinux_avc *avc)
941 {
942 struct hlist_head *head;
943 struct avc_node *node;
944 spinlock_t *lock;
945 unsigned long flag;
946 int i;
947
948 for (i = 0; i < AVC_CACHE_SLOTS; i++) {
949 head = &avc->avc_cache.slots[i];
950 lock = &avc->avc_cache.slots_lock[i];
951
952 spin_lock_irqsave(lock, flag);
953 /*
954 * With preemptable RCU, the outer spinlock does not
955 * prevent RCU grace periods from ending.
956 */
957 rcu_read_lock();
958 hlist_for_each_entry(node, head, list)
959 avc_node_delete(avc, node);
960 rcu_read_unlock();
961 spin_unlock_irqrestore(lock, flag);
962 }
963 }
964
965 /**
966 * avc_ss_reset - Flush the cache and revalidate migrated permissions.
967 * @seqno: policy sequence number
968 */
avc_ss_reset(struct selinux_avc * avc,u32 seqno)969 int avc_ss_reset(struct selinux_avc *avc, u32 seqno)
970 {
971 struct avc_callback_node *c;
972 int rc = 0, tmprc;
973
974 avc_flush(avc);
975
976 for (c = avc_callbacks; c; c = c->next) {
977 if (c->events & AVC_CALLBACK_RESET) {
978 tmprc = c->callback(AVC_CALLBACK_RESET);
979 /* save the first error encountered for the return
980 value and continue processing the callbacks */
981 if (!rc)
982 rc = tmprc;
983 }
984 }
985
986 avc_latest_notif_update(avc, seqno, 0);
987 return rc;
988 }
989
990 /*
991 * Slow-path helper function for avc_has_perm_noaudit,
992 * when the avc_node lookup fails. We get called with
993 * the RCU read lock held, and need to return with it
994 * still held, but drop if for the security compute.
995 *
996 * Don't inline this, since it's the slow-path and just
997 * results in a bigger stack frame.
998 */
999 static noinline
avc_compute_av(struct selinux_state * state,u32 ssid,u32 tsid,u16 tclass,struct av_decision * avd,struct avc_xperms_node * xp_node)1000 struct avc_node *avc_compute_av(struct selinux_state *state,
1001 u32 ssid, u32 tsid,
1002 u16 tclass, struct av_decision *avd,
1003 struct avc_xperms_node *xp_node)
1004 {
1005 rcu_read_unlock();
1006 INIT_LIST_HEAD(&xp_node->xpd_head);
1007 security_compute_av(state, ssid, tsid, tclass, avd, &xp_node->xp);
1008 rcu_read_lock();
1009 return avc_insert(state->avc, ssid, tsid, tclass, avd, xp_node);
1010 }
1011
avc_denied(struct selinux_state * state,u32 ssid,u32 tsid,u16 tclass,u32 requested,u8 driver,u8 xperm,unsigned int flags,struct av_decision * avd)1012 static noinline int avc_denied(struct selinux_state *state,
1013 u32 ssid, u32 tsid,
1014 u16 tclass, u32 requested,
1015 u8 driver, u8 xperm, unsigned int flags,
1016 struct av_decision *avd)
1017 {
1018 if (flags & AVC_STRICT)
1019 return -EACCES;
1020
1021 if (enforcing_enabled(state) &&
1022 !(avd->flags & AVD_FLAGS_PERMISSIVE))
1023 return -EACCES;
1024
1025 avc_update_node(state->avc, AVC_CALLBACK_GRANT, requested, driver,
1026 xperm, ssid, tsid, tclass, avd->seqno, NULL, flags);
1027 return 0;
1028 }
1029
1030 /*
1031 * The avc extended permissions logic adds an additional 256 bits of
1032 * permissions to an avc node when extended permissions for that node are
1033 * specified in the avtab. If the additional 256 permissions is not adequate,
1034 * as-is the case with ioctls, then multiple may be chained together and the
1035 * driver field is used to specify which set contains the permission.
1036 */
avc_has_extended_perms(struct selinux_state * state,u32 ssid,u32 tsid,u16 tclass,u32 requested,u8 driver,u8 xperm,struct common_audit_data * ad)1037 int avc_has_extended_perms(struct selinux_state *state,
1038 u32 ssid, u32 tsid, u16 tclass, u32 requested,
1039 u8 driver, u8 xperm, struct common_audit_data *ad)
1040 {
1041 struct avc_node *node;
1042 struct av_decision avd;
1043 u32 denied;
1044 struct extended_perms_decision local_xpd;
1045 struct extended_perms_decision *xpd = NULL;
1046 struct extended_perms_data allowed;
1047 struct extended_perms_data auditallow;
1048 struct extended_perms_data dontaudit;
1049 struct avc_xperms_node local_xp_node;
1050 struct avc_xperms_node *xp_node;
1051 int rc = 0, rc2;
1052
1053 xp_node = &local_xp_node;
1054 if (WARN_ON(!requested))
1055 return -EACCES;
1056
1057 rcu_read_lock();
1058
1059 node = avc_lookup(state->avc, ssid, tsid, tclass);
1060 if (unlikely(!node)) {
1061 node = avc_compute_av(state, ssid, tsid, tclass, &avd, xp_node);
1062 } else {
1063 memcpy(&avd, &node->ae.avd, sizeof(avd));
1064 xp_node = node->ae.xp_node;
1065 }
1066 /* if extended permissions are not defined, only consider av_decision */
1067 if (!xp_node || !xp_node->xp.len)
1068 goto decision;
1069
1070 local_xpd.allowed = &allowed;
1071 local_xpd.auditallow = &auditallow;
1072 local_xpd.dontaudit = &dontaudit;
1073
1074 xpd = avc_xperms_decision_lookup(driver, xp_node);
1075 if (unlikely(!xpd)) {
1076 /*
1077 * Compute the extended_perms_decision only if the driver
1078 * is flagged
1079 */
1080 if (!security_xperm_test(xp_node->xp.drivers.p, driver)) {
1081 avd.allowed &= ~requested;
1082 goto decision;
1083 }
1084 rcu_read_unlock();
1085 security_compute_xperms_decision(state, ssid, tsid, tclass,
1086 driver, &local_xpd);
1087 rcu_read_lock();
1088 avc_update_node(state->avc, AVC_CALLBACK_ADD_XPERMS, requested,
1089 driver, xperm, ssid, tsid, tclass, avd.seqno,
1090 &local_xpd, 0);
1091 } else {
1092 avc_quick_copy_xperms_decision(xperm, &local_xpd, xpd);
1093 }
1094 xpd = &local_xpd;
1095
1096 if (!avc_xperms_has_perm(xpd, xperm, XPERMS_ALLOWED))
1097 avd.allowed &= ~requested;
1098
1099 decision:
1100 denied = requested & ~(avd.allowed);
1101 if (unlikely(denied))
1102 rc = avc_denied(state, ssid, tsid, tclass, requested,
1103 driver, xperm, AVC_EXTENDED_PERMS, &avd);
1104
1105 rcu_read_unlock();
1106
1107 rc2 = avc_xperms_audit(state, ssid, tsid, tclass, requested,
1108 &avd, xpd, xperm, rc, ad);
1109 if (rc2)
1110 return rc2;
1111 return rc;
1112 }
1113
1114 /**
1115 * avc_has_perm_noaudit - Check permissions but perform no auditing.
1116 * @ssid: source security identifier
1117 * @tsid: target security identifier
1118 * @tclass: target security class
1119 * @requested: requested permissions, interpreted based on @tclass
1120 * @flags: AVC_STRICT or 0
1121 * @avd: access vector decisions
1122 *
1123 * Check the AVC to determine whether the @requested permissions are granted
1124 * for the SID pair (@ssid, @tsid), interpreting the permissions
1125 * based on @tclass, and call the security server on a cache miss to obtain
1126 * a new decision and add it to the cache. Return a copy of the decisions
1127 * in @avd. Return %0 if all @requested permissions are granted,
1128 * -%EACCES if any permissions are denied, or another -errno upon
1129 * other errors. This function is typically called by avc_has_perm(),
1130 * but may also be called directly to separate permission checking from
1131 * auditing, e.g. in cases where a lock must be held for the check but
1132 * should be released for the auditing.
1133 */
avc_has_perm_noaudit(struct selinux_state * state,u32 ssid,u32 tsid,u16 tclass,u32 requested,unsigned int flags,struct av_decision * avd)1134 inline int avc_has_perm_noaudit(struct selinux_state *state,
1135 u32 ssid, u32 tsid,
1136 u16 tclass, u32 requested,
1137 unsigned int flags,
1138 struct av_decision *avd)
1139 {
1140 struct avc_node *node;
1141 struct avc_xperms_node xp_node;
1142 int rc = 0;
1143 u32 denied;
1144
1145 if (WARN_ON(!requested))
1146 return -EACCES;
1147
1148 rcu_read_lock();
1149
1150 node = avc_lookup(state->avc, ssid, tsid, tclass);
1151 if (unlikely(!node))
1152 node = avc_compute_av(state, ssid, tsid, tclass, avd, &xp_node);
1153 else
1154 memcpy(avd, &node->ae.avd, sizeof(*avd));
1155
1156 denied = requested & ~(avd->allowed);
1157 if (unlikely(denied))
1158 rc = avc_denied(state, ssid, tsid, tclass, requested, 0, 0,
1159 flags, avd);
1160
1161 rcu_read_unlock();
1162 return rc;
1163 }
1164
1165 /**
1166 * avc_has_perm - Check permissions and perform any appropriate auditing.
1167 * @ssid: source security identifier
1168 * @tsid: target security identifier
1169 * @tclass: target security class
1170 * @requested: requested permissions, interpreted based on @tclass
1171 * @auditdata: auxiliary audit data
1172 *
1173 * Check the AVC to determine whether the @requested permissions are granted
1174 * for the SID pair (@ssid, @tsid), interpreting the permissions
1175 * based on @tclass, and call the security server on a cache miss to obtain
1176 * a new decision and add it to the cache. Audit the granting or denial of
1177 * permissions in accordance with the policy. Return %0 if all @requested
1178 * permissions are granted, -%EACCES if any permissions are denied, or
1179 * another -errno upon other errors.
1180 */
avc_has_perm(struct selinux_state * state,u32 ssid,u32 tsid,u16 tclass,u32 requested,struct common_audit_data * auditdata)1181 int avc_has_perm(struct selinux_state *state, u32 ssid, u32 tsid, u16 tclass,
1182 u32 requested, struct common_audit_data *auditdata)
1183 {
1184 struct av_decision avd;
1185 int rc, rc2;
1186
1187 rc = avc_has_perm_noaudit(state, ssid, tsid, tclass, requested, 0,
1188 &avd);
1189
1190 rc2 = avc_audit(state, ssid, tsid, tclass, requested, &avd, rc,
1191 auditdata);
1192 if (rc2)
1193 return rc2;
1194 return rc;
1195 }
1196
avc_policy_seqno(struct selinux_state * state)1197 u32 avc_policy_seqno(struct selinux_state *state)
1198 {
1199 return state->avc->avc_cache.latest_notif;
1200 }
1201
avc_disable(void)1202 void avc_disable(void)
1203 {
1204 /*
1205 * If you are looking at this because you have realized that we are
1206 * not destroying the avc_node_cachep it might be easy to fix, but
1207 * I don't know the memory barrier semantics well enough to know. It's
1208 * possible that some other task dereferenced security_ops when
1209 * it still pointed to selinux operations. If that is the case it's
1210 * possible that it is about to use the avc and is about to need the
1211 * avc_node_cachep. I know I could wrap the security.c security_ops call
1212 * in an rcu_lock, but seriously, it's not worth it. Instead I just flush
1213 * the cache and get that memory back.
1214 */
1215 if (avc_node_cachep) {
1216 avc_flush(selinux_state.avc);
1217 /* kmem_cache_destroy(avc_node_cachep); */
1218 }
1219 }
1220