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;
125 static struct kmem_cache *avc_node_cachep;
126 static struct kmem_cache *avc_xperms_data_cachep;
127 static struct kmem_cache *avc_xperms_decision_cachep;
128 static struct kmem_cache *avc_xperms_cachep;
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_vh_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_vh_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_vh_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_vh_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 /* This is the slow part of avc audit with big stack footprint */
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)771 noinline int slow_avc_audit(struct selinux_state *state,
772 u32 ssid, u32 tsid, u16 tclass,
773 u32 requested, u32 audited, u32 denied, int result,
774 struct common_audit_data *a)
775 {
776 struct common_audit_data stack_data;
777 struct selinux_audit_data sad;
778
779 if (WARN_ON(!tclass || tclass >= ARRAY_SIZE(secclass_map)))
780 return -EINVAL;
781
782 if (!a) {
783 a = &stack_data;
784 a->type = LSM_AUDIT_DATA_NONE;
785 }
786
787 sad.tclass = tclass;
788 sad.requested = requested;
789 sad.ssid = ssid;
790 sad.tsid = tsid;
791 sad.audited = audited;
792 sad.denied = denied;
793 sad.result = result;
794 sad.state = state;
795
796 a->selinux_audit_data = &sad;
797
798 common_lsm_audit(a, avc_audit_pre_callback, avc_audit_post_callback);
799 return 0;
800 }
801
802 /**
803 * avc_add_callback - Register a callback for security events.
804 * @callback: callback function
805 * @events: security events
806 *
807 * Register a callback function for events in the set @events.
808 * Returns %0 on success or -%ENOMEM if insufficient memory
809 * exists to add the callback.
810 */
avc_add_callback(int (* callback)(u32 event),u32 events)811 int __init avc_add_callback(int (*callback)(u32 event), u32 events)
812 {
813 struct avc_callback_node *c;
814 int rc = 0;
815
816 c = kmalloc(sizeof(*c), GFP_KERNEL);
817 if (!c) {
818 rc = -ENOMEM;
819 goto out;
820 }
821
822 c->callback = callback;
823 c->events = events;
824 c->next = avc_callbacks;
825 avc_callbacks = c;
826 out:
827 return rc;
828 }
829
830 /**
831 * avc_update_node Update an AVC entry
832 * @event : Updating event
833 * @perms : Permission mask bits
834 * @ssid,@tsid,@tclass : identifier of an AVC entry
835 * @seqno : sequence number when decision was made
836 * @xpd: extended_perms_decision to be added to the node
837 * @flags: the AVC_* flags, e.g. AVC_NONBLOCKING, AVC_EXTENDED_PERMS, or 0.
838 *
839 * if a valid AVC entry doesn't exist,this function returns -ENOENT.
840 * if kmalloc() called internal returns NULL, this function returns -ENOMEM.
841 * otherwise, this function updates the AVC entry. The original AVC-entry object
842 * will release later by RCU.
843 */
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)844 static int avc_update_node(struct selinux_avc *avc,
845 u32 event, u32 perms, u8 driver, u8 xperm, u32 ssid,
846 u32 tsid, u16 tclass, u32 seqno,
847 struct extended_perms_decision *xpd,
848 u32 flags)
849 {
850 int hvalue, rc = 0;
851 unsigned long flag;
852 struct avc_node *pos, *node, *orig = NULL;
853 struct hlist_head *head;
854 spinlock_t *lock;
855
856 /*
857 * If we are in a non-blocking code path, e.g. VFS RCU walk,
858 * then we must not add permissions to a cache entry
859 * because we will not audit the denial. Otherwise,
860 * during the subsequent blocking retry (e.g. VFS ref walk), we
861 * will find the permissions already granted in the cache entry
862 * and won't audit anything at all, leading to silent denials in
863 * permissive mode that only appear when in enforcing mode.
864 *
865 * See the corresponding handling of MAY_NOT_BLOCK in avc_audit()
866 * and selinux_inode_permission().
867 */
868 if (flags & AVC_NONBLOCKING)
869 return 0;
870
871 node = avc_alloc_node(avc);
872 if (!node) {
873 rc = -ENOMEM;
874 goto out;
875 }
876
877 /* Lock the target slot */
878 hvalue = avc_hash(ssid, tsid, tclass);
879
880 head = &avc->avc_cache.slots[hvalue];
881 lock = &avc->avc_cache.slots_lock[hvalue];
882
883 spin_lock_irqsave(lock, flag);
884
885 hlist_for_each_entry(pos, head, list) {
886 if (ssid == pos->ae.ssid &&
887 tsid == pos->ae.tsid &&
888 tclass == pos->ae.tclass &&
889 seqno == pos->ae.avd.seqno){
890 orig = pos;
891 break;
892 }
893 }
894
895 if (!orig) {
896 rc = -ENOENT;
897 avc_node_kill(avc, node);
898 goto out_unlock;
899 }
900
901 /*
902 * Copy and replace original node.
903 */
904
905 avc_node_populate(node, ssid, tsid, tclass, &orig->ae.avd);
906
907 if (orig->ae.xp_node) {
908 rc = avc_xperms_populate(node, orig->ae.xp_node);
909 if (rc) {
910 avc_node_kill(avc, node);
911 goto out_unlock;
912 }
913 }
914
915 switch (event) {
916 case AVC_CALLBACK_GRANT:
917 node->ae.avd.allowed |= perms;
918 if (node->ae.xp_node && (flags & AVC_EXTENDED_PERMS))
919 avc_xperms_allow_perm(node->ae.xp_node, driver, xperm);
920 break;
921 case AVC_CALLBACK_TRY_REVOKE:
922 case AVC_CALLBACK_REVOKE:
923 node->ae.avd.allowed &= ~perms;
924 break;
925 case AVC_CALLBACK_AUDITALLOW_ENABLE:
926 node->ae.avd.auditallow |= perms;
927 break;
928 case AVC_CALLBACK_AUDITALLOW_DISABLE:
929 node->ae.avd.auditallow &= ~perms;
930 break;
931 case AVC_CALLBACK_AUDITDENY_ENABLE:
932 node->ae.avd.auditdeny |= perms;
933 break;
934 case AVC_CALLBACK_AUDITDENY_DISABLE:
935 node->ae.avd.auditdeny &= ~perms;
936 break;
937 case AVC_CALLBACK_ADD_XPERMS:
938 avc_add_xperms_decision(node, xpd);
939 break;
940 }
941 avc_node_replace(avc, node, orig);
942 out_unlock:
943 spin_unlock_irqrestore(lock, flag);
944 out:
945 return rc;
946 }
947
948 /**
949 * avc_flush - Flush the cache
950 */
avc_flush(struct selinux_avc * avc)951 static void avc_flush(struct selinux_avc *avc)
952 {
953 struct hlist_head *head;
954 struct avc_node *node;
955 spinlock_t *lock;
956 unsigned long flag;
957 int i;
958
959 for (i = 0; i < AVC_CACHE_SLOTS; i++) {
960 head = &avc->avc_cache.slots[i];
961 lock = &avc->avc_cache.slots_lock[i];
962
963 spin_lock_irqsave(lock, flag);
964 /*
965 * With preemptable RCU, the outer spinlock does not
966 * prevent RCU grace periods from ending.
967 */
968 rcu_read_lock();
969 hlist_for_each_entry(node, head, list)
970 avc_node_delete(avc, node);
971 rcu_read_unlock();
972 spin_unlock_irqrestore(lock, flag);
973 }
974 }
975
976 /**
977 * avc_ss_reset - Flush the cache and revalidate migrated permissions.
978 * @seqno: policy sequence number
979 */
avc_ss_reset(struct selinux_avc * avc,u32 seqno)980 int avc_ss_reset(struct selinux_avc *avc, u32 seqno)
981 {
982 struct avc_callback_node *c;
983 int rc = 0, tmprc;
984
985 avc_flush(avc);
986
987 for (c = avc_callbacks; c; c = c->next) {
988 if (c->events & AVC_CALLBACK_RESET) {
989 tmprc = c->callback(AVC_CALLBACK_RESET);
990 /* save the first error encountered for the return
991 value and continue processing the callbacks */
992 if (!rc)
993 rc = tmprc;
994 }
995 }
996
997 avc_latest_notif_update(avc, seqno, 0);
998 return rc;
999 }
1000
1001 /*
1002 * Slow-path helper function for avc_has_perm_noaudit,
1003 * when the avc_node lookup fails. We get called with
1004 * the RCU read lock held, and need to return with it
1005 * still held, but drop if for the security compute.
1006 *
1007 * Don't inline this, since it's the slow-path and just
1008 * results in a bigger stack frame.
1009 */
1010 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)1011 struct avc_node *avc_compute_av(struct selinux_state *state,
1012 u32 ssid, u32 tsid,
1013 u16 tclass, struct av_decision *avd,
1014 struct avc_xperms_node *xp_node)
1015 {
1016 rcu_read_unlock();
1017 INIT_LIST_HEAD(&xp_node->xpd_head);
1018 security_compute_av(state, ssid, tsid, tclass, avd, &xp_node->xp);
1019 rcu_read_lock();
1020 return avc_insert(state->avc, ssid, tsid, tclass, avd, xp_node);
1021 }
1022
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)1023 static noinline int avc_denied(struct selinux_state *state,
1024 u32 ssid, u32 tsid,
1025 u16 tclass, u32 requested,
1026 u8 driver, u8 xperm, unsigned int flags,
1027 struct av_decision *avd)
1028 {
1029 if (flags & AVC_STRICT)
1030 return -EACCES;
1031
1032 if (enforcing_enabled(state) &&
1033 !(avd->flags & AVD_FLAGS_PERMISSIVE))
1034 return -EACCES;
1035
1036 avc_update_node(state->avc, AVC_CALLBACK_GRANT, requested, driver,
1037 xperm, ssid, tsid, tclass, avd->seqno, NULL, flags);
1038 return 0;
1039 }
1040
1041 /*
1042 * The avc extended permissions logic adds an additional 256 bits of
1043 * permissions to an avc node when extended permissions for that node are
1044 * specified in the avtab. If the additional 256 permissions is not adequate,
1045 * as-is the case with ioctls, then multiple may be chained together and the
1046 * driver field is used to specify which set contains the permission.
1047 */
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)1048 int avc_has_extended_perms(struct selinux_state *state,
1049 u32 ssid, u32 tsid, u16 tclass, u32 requested,
1050 u8 driver, u8 xperm, struct common_audit_data *ad)
1051 {
1052 struct avc_node *node;
1053 struct av_decision avd;
1054 u32 denied;
1055 struct extended_perms_decision local_xpd;
1056 struct extended_perms_decision *xpd = NULL;
1057 struct extended_perms_data allowed;
1058 struct extended_perms_data auditallow;
1059 struct extended_perms_data dontaudit;
1060 struct avc_xperms_node local_xp_node;
1061 struct avc_xperms_node *xp_node;
1062 int rc = 0, rc2;
1063
1064 xp_node = &local_xp_node;
1065 if (WARN_ON(!requested))
1066 return -EACCES;
1067
1068 rcu_read_lock();
1069
1070 node = avc_lookup(state->avc, ssid, tsid, tclass);
1071 if (unlikely(!node)) {
1072 node = avc_compute_av(state, ssid, tsid, tclass, &avd, xp_node);
1073 } else {
1074 memcpy(&avd, &node->ae.avd, sizeof(avd));
1075 xp_node = node->ae.xp_node;
1076 }
1077 /* if extended permissions are not defined, only consider av_decision */
1078 if (!xp_node || !xp_node->xp.len)
1079 goto decision;
1080
1081 local_xpd.allowed = &allowed;
1082 local_xpd.auditallow = &auditallow;
1083 local_xpd.dontaudit = &dontaudit;
1084
1085 xpd = avc_xperms_decision_lookup(driver, xp_node);
1086 if (unlikely(!xpd)) {
1087 /*
1088 * Compute the extended_perms_decision only if the driver
1089 * is flagged
1090 */
1091 if (!security_xperm_test(xp_node->xp.drivers.p, driver)) {
1092 avd.allowed &= ~requested;
1093 goto decision;
1094 }
1095 rcu_read_unlock();
1096 security_compute_xperms_decision(state, ssid, tsid, tclass,
1097 driver, &local_xpd);
1098 rcu_read_lock();
1099 avc_update_node(state->avc, AVC_CALLBACK_ADD_XPERMS, requested,
1100 driver, xperm, ssid, tsid, tclass, avd.seqno,
1101 &local_xpd, 0);
1102 } else {
1103 avc_quick_copy_xperms_decision(xperm, &local_xpd, xpd);
1104 }
1105 xpd = &local_xpd;
1106
1107 if (!avc_xperms_has_perm(xpd, xperm, XPERMS_ALLOWED))
1108 avd.allowed &= ~requested;
1109
1110 decision:
1111 denied = requested & ~(avd.allowed);
1112 if (unlikely(denied))
1113 rc = avc_denied(state, ssid, tsid, tclass, requested,
1114 driver, xperm, AVC_EXTENDED_PERMS, &avd);
1115
1116 rcu_read_unlock();
1117
1118 rc2 = avc_xperms_audit(state, ssid, tsid, tclass, requested,
1119 &avd, xpd, xperm, rc, ad);
1120 if (rc2)
1121 return rc2;
1122 return rc;
1123 }
1124
1125 /**
1126 * avc_has_perm_noaudit - Check permissions but perform no auditing.
1127 * @ssid: source security identifier
1128 * @tsid: target security identifier
1129 * @tclass: target security class
1130 * @requested: requested permissions, interpreted based on @tclass
1131 * @flags: AVC_STRICT, AVC_NONBLOCKING, or 0
1132 * @avd: access vector decisions
1133 *
1134 * Check the AVC to determine whether the @requested permissions are granted
1135 * for the SID pair (@ssid, @tsid), interpreting the permissions
1136 * based on @tclass, and call the security server on a cache miss to obtain
1137 * a new decision and add it to the cache. Return a copy of the decisions
1138 * in @avd. Return %0 if all @requested permissions are granted,
1139 * -%EACCES if any permissions are denied, or another -errno upon
1140 * other errors. This function is typically called by avc_has_perm(),
1141 * but may also be called directly to separate permission checking from
1142 * auditing, e.g. in cases where a lock must be held for the check but
1143 * should be released for the auditing.
1144 */
avc_has_perm_noaudit(struct selinux_state * state,u32 ssid,u32 tsid,u16 tclass,u32 requested,unsigned int flags,struct av_decision * avd)1145 inline int avc_has_perm_noaudit(struct selinux_state *state,
1146 u32 ssid, u32 tsid,
1147 u16 tclass, u32 requested,
1148 unsigned int flags,
1149 struct av_decision *avd)
1150 {
1151 struct avc_node *node;
1152 struct avc_xperms_node xp_node;
1153 int rc = 0;
1154 u32 denied;
1155
1156 if (WARN_ON(!requested))
1157 return -EACCES;
1158
1159 rcu_read_lock();
1160
1161 node = avc_lookup(state->avc, ssid, tsid, tclass);
1162 if (unlikely(!node))
1163 node = avc_compute_av(state, ssid, tsid, tclass, avd, &xp_node);
1164 else
1165 memcpy(avd, &node->ae.avd, sizeof(*avd));
1166
1167 denied = requested & ~(avd->allowed);
1168 if (unlikely(denied))
1169 rc = avc_denied(state, ssid, tsid, tclass, requested, 0, 0,
1170 flags, avd);
1171
1172 rcu_read_unlock();
1173 return rc;
1174 }
1175
1176 /**
1177 * avc_has_perm - Check permissions and perform any appropriate auditing.
1178 * @ssid: source security identifier
1179 * @tsid: target security identifier
1180 * @tclass: target security class
1181 * @requested: requested permissions, interpreted based on @tclass
1182 * @auditdata: auxiliary audit data
1183 *
1184 * Check the AVC to determine whether the @requested permissions are granted
1185 * for the SID pair (@ssid, @tsid), interpreting the permissions
1186 * based on @tclass, and call the security server on a cache miss to obtain
1187 * a new decision and add it to the cache. Audit the granting or denial of
1188 * permissions in accordance with the policy. Return %0 if all @requested
1189 * permissions are granted, -%EACCES if any permissions are denied, or
1190 * another -errno upon other errors.
1191 */
avc_has_perm(struct selinux_state * state,u32 ssid,u32 tsid,u16 tclass,u32 requested,struct common_audit_data * auditdata)1192 int avc_has_perm(struct selinux_state *state, u32 ssid, u32 tsid, u16 tclass,
1193 u32 requested, struct common_audit_data *auditdata)
1194 {
1195 struct av_decision avd;
1196 int rc, rc2;
1197
1198 rc = avc_has_perm_noaudit(state, ssid, tsid, tclass, requested, 0,
1199 &avd);
1200
1201 rc2 = avc_audit(state, ssid, tsid, tclass, requested, &avd, rc,
1202 auditdata, 0);
1203 if (rc2)
1204 return rc2;
1205 return rc;
1206 }
1207
avc_has_perm_flags(struct selinux_state * state,u32 ssid,u32 tsid,u16 tclass,u32 requested,struct common_audit_data * auditdata,int flags)1208 int avc_has_perm_flags(struct selinux_state *state,
1209 u32 ssid, u32 tsid, u16 tclass, u32 requested,
1210 struct common_audit_data *auditdata,
1211 int flags)
1212 {
1213 struct av_decision avd;
1214 int rc, rc2;
1215
1216 rc = avc_has_perm_noaudit(state, ssid, tsid, tclass, requested,
1217 (flags & MAY_NOT_BLOCK) ? AVC_NONBLOCKING : 0,
1218 &avd);
1219
1220 rc2 = avc_audit(state, ssid, tsid, tclass, requested, &avd, rc,
1221 auditdata, flags);
1222 if (rc2)
1223 return rc2;
1224 return rc;
1225 }
1226
avc_policy_seqno(struct selinux_state * state)1227 u32 avc_policy_seqno(struct selinux_state *state)
1228 {
1229 return state->avc->avc_cache.latest_notif;
1230 }
1231
avc_disable(void)1232 void avc_disable(void)
1233 {
1234 /*
1235 * If you are looking at this because you have realized that we are
1236 * not destroying the avc_node_cachep it might be easy to fix, but
1237 * I don't know the memory barrier semantics well enough to know. It's
1238 * possible that some other task dereferenced security_ops when
1239 * it still pointed to selinux operations. If that is the case it's
1240 * possible that it is about to use the avc and is about to need the
1241 * avc_node_cachep. I know I could wrap the security.c security_ops call
1242 * in an rcu_lock, but seriously, it's not worth it. Instead I just flush
1243 * the cache and get that memory back.
1244 */
1245 if (avc_node_cachep) {
1246 avc_flush(selinux_state.avc);
1247 /* kmem_cache_destroy(avc_node_cachep); */
1248 }
1249 }
1250