1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16#include <linux/types.h>
17#include <linux/stddef.h>
18#include <linux/kernel.h>
19#include <linux/slab.h>
20#include <linux/fs.h>
21#include <linux/dcache.h>
22#include <linux/init.h>
23#include <linux/skbuff.h>
24#include <linux/percpu.h>
25#include <linux/list.h>
26#include <net/sock.h>
27#include <linux/un.h>
28#include <net/af_unix.h>
29#include <linux/ip.h>
30#include <linux/audit.h>
31#include <linux/ipv6.h>
32#include <net/ipv6.h>
33#include "avc.h"
34#include "avc_ss.h"
35#include "classmap.h"
36
37#define CREATE_TRACE_POINTS
38#include <trace/events/avc.h>
39
40#define AVC_CACHE_SLOTS 512
41#define AVC_DEF_CACHE_THRESHOLD 512
42#define AVC_CACHE_RECLAIM 16
43
44#ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
45#define avc_cache_stats_incr(field) this_cpu_inc(avc_cache_stats.field)
46#else
47#define avc_cache_stats_incr(field) do {} while (0)
48#endif
49
50struct 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
58struct avc_node {
59 struct avc_entry ae;
60 struct hlist_node list;
61 struct rcu_head rhead;
62};
63
64struct avc_xperms_decision_node {
65 struct extended_perms_decision xpd;
66 struct list_head xpd_list;
67};
68
69struct avc_xperms_node {
70 struct extended_perms xp;
71 struct list_head xpd_head;
72};
73
74struct avc_cache {
75 struct hlist_head slots[AVC_CACHE_SLOTS];
76 spinlock_t slots_lock[AVC_CACHE_SLOTS];
77 atomic_t lru_hint;
78 atomic_t active_nodes;
79 u32 latest_notif;
80};
81
82struct 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
89DEFINE_PER_CPU(struct avc_cache_stats, avc_cache_stats) = { 0 };
90#endif
91
92struct selinux_avc {
93 unsigned int avc_cache_threshold;
94 struct avc_cache avc_cache;
95};
96
97static struct selinux_avc selinux_avc;
98
99void 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
113unsigned int avc_get_cache_threshold(struct selinux_avc *avc)
114{
115 return avc->avc_cache_threshold;
116}
117
118void avc_set_cache_threshold(struct selinux_avc *avc,
119 unsigned int cache_threshold)
120{
121 avc->avc_cache_threshold = cache_threshold;
122}
123
124static struct avc_callback_node *avc_callbacks __ro_after_init;
125static struct kmem_cache *avc_node_cachep __ro_after_init;
126static struct kmem_cache *avc_xperms_data_cachep __ro_after_init;
127static struct kmem_cache *avc_xperms_decision_cachep __ro_after_init;
128static struct kmem_cache *avc_xperms_cachep __ro_after_init;
129
130static 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
137
138
139
140void __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
156int 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
188
189
190static 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
202static inline unsigned int
203avc_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
220static 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
230static 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
244static 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
258static 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
276
277
278static inline void avc_quick_copy_xperms_decision(u8 perm,
279 struct extended_perms_decision *dest,
280 struct extended_perms_decision *src)
281{
282
283
284
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
297static struct avc_xperms_decision_node
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, GFP_NOWAIT);
304 if (!xpd_node)
305 return NULL;
306
307 xpd = &xpd_node->xpd;
308 if (which & XPERMS_ALLOWED) {
309 xpd->allowed = kmem_cache_zalloc(avc_xperms_data_cachep,
310 GFP_NOWAIT);
311 if (!xpd->allowed)
312 goto error;
313 }
314 if (which & XPERMS_AUDITALLOW) {
315 xpd->auditallow = kmem_cache_zalloc(avc_xperms_data_cachep,
316 GFP_NOWAIT);
317 if (!xpd->auditallow)
318 goto error;
319 }
320 if (which & XPERMS_DONTAUDIT) {
321 xpd->dontaudit = kmem_cache_zalloc(avc_xperms_data_cachep,
322 GFP_NOWAIT);
323 if (!xpd->dontaudit)
324 goto error;
325 }
326 return xpd_node;
327error:
328 avc_xperms_decision_free(xpd_node);
329 return NULL;
330}
331
332static int avc_add_xperms_decision(struct avc_node *node,
333 struct extended_perms_decision *src)
334{
335 struct avc_xperms_decision_node *dest_xpd;
336
337 node->ae.xp_node->xp.len++;
338 dest_xpd = avc_xperms_decision_alloc(src->used);
339 if (!dest_xpd)
340 return -ENOMEM;
341 avc_copy_xperms_decision(&dest_xpd->xpd, src);
342 list_add(&dest_xpd->xpd_list, &node->ae.xp_node->xpd_head);
343 return 0;
344}
345
346static struct avc_xperms_node *avc_xperms_alloc(void)
347{
348 struct avc_xperms_node *xp_node;
349
350 xp_node = kmem_cache_zalloc(avc_xperms_cachep, GFP_NOWAIT);
351 if (!xp_node)
352 return xp_node;
353 INIT_LIST_HEAD(&xp_node->xpd_head);
354 return xp_node;
355}
356
357static int avc_xperms_populate(struct avc_node *node,
358 struct avc_xperms_node *src)
359{
360 struct avc_xperms_node *dest;
361 struct avc_xperms_decision_node *dest_xpd;
362 struct avc_xperms_decision_node *src_xpd;
363
364 if (src->xp.len == 0)
365 return 0;
366 dest = avc_xperms_alloc();
367 if (!dest)
368 return -ENOMEM;
369
370 memcpy(dest->xp.drivers.p, src->xp.drivers.p, sizeof(dest->xp.drivers.p));
371 dest->xp.len = src->xp.len;
372
373
374 list_for_each_entry(src_xpd, &src->xpd_head, xpd_list) {
375 dest_xpd = avc_xperms_decision_alloc(src_xpd->xpd.used);
376 if (!dest_xpd)
377 goto error;
378 avc_copy_xperms_decision(&dest_xpd->xpd, &src_xpd->xpd);
379 list_add(&dest_xpd->xpd_list, &dest->xpd_head);
380 }
381 node->ae.xp_node = dest;
382 return 0;
383error:
384 avc_xperms_free(dest);
385 return -ENOMEM;
386
387}
388
389static inline u32 avc_xperms_audit_required(u32 requested,
390 struct av_decision *avd,
391 struct extended_perms_decision *xpd,
392 u8 perm,
393 int result,
394 u32 *deniedp)
395{
396 u32 denied, audited;
397
398 denied = requested & ~avd->allowed;
399 if (unlikely(denied)) {
400 audited = denied & avd->auditdeny;
401 if (audited && xpd) {
402 if (avc_xperms_has_perm(xpd, perm, XPERMS_DONTAUDIT))
403 audited &= ~requested;
404 }
405 } else if (result) {
406 audited = denied = requested;
407 } else {
408 audited = requested & avd->auditallow;
409 if (audited && xpd) {
410 if (!avc_xperms_has_perm(xpd, perm, XPERMS_AUDITALLOW))
411 audited &= ~requested;
412 }
413 }
414
415 *deniedp = denied;
416 return audited;
417}
418
419static inline int avc_xperms_audit(struct selinux_state *state,
420 u32 ssid, u32 tsid, u16 tclass,
421 u32 requested, struct av_decision *avd,
422 struct extended_perms_decision *xpd,
423 u8 perm, int result,
424 struct common_audit_data *ad)
425{
426 u32 audited, denied;
427
428 audited = avc_xperms_audit_required(
429 requested, avd, xpd, perm, result, &denied);
430 if (likely(!audited))
431 return 0;
432 return slow_avc_audit(state, ssid, tsid, tclass, requested,
433 audited, denied, result, ad);
434}
435
436static void avc_node_free(struct rcu_head *rhead)
437{
438 struct avc_node *node = container_of(rhead, struct avc_node, rhead);
439 avc_xperms_free(node->ae.xp_node);
440 kmem_cache_free(avc_node_cachep, node);
441 avc_cache_stats_incr(frees);
442}
443
444static void avc_node_delete(struct selinux_avc *avc, struct avc_node *node)
445{
446 hlist_del_rcu(&node->list);
447 call_rcu(&node->rhead, avc_node_free);
448 atomic_dec(&avc->avc_cache.active_nodes);
449}
450
451static void avc_node_kill(struct selinux_avc *avc, struct avc_node *node)
452{
453 avc_xperms_free(node->ae.xp_node);
454 kmem_cache_free(avc_node_cachep, node);
455 avc_cache_stats_incr(frees);
456 atomic_dec(&avc->avc_cache.active_nodes);
457}
458
459static void avc_node_replace(struct selinux_avc *avc,
460 struct avc_node *new, struct avc_node *old)
461{
462 hlist_replace_rcu(&old->list, &new->list);
463 call_rcu(&old->rhead, avc_node_free);
464 atomic_dec(&avc->avc_cache.active_nodes);
465}
466
467static inline int avc_reclaim_node(struct selinux_avc *avc)
468{
469 struct avc_node *node;
470 int hvalue, try, ecx;
471 unsigned long flags;
472 struct hlist_head *head;
473 spinlock_t *lock;
474
475 for (try = 0, ecx = 0; try < AVC_CACHE_SLOTS; try++) {
476 hvalue = atomic_inc_return(&avc->avc_cache.lru_hint) &
477 (AVC_CACHE_SLOTS - 1);
478 head = &avc->avc_cache.slots[hvalue];
479 lock = &avc->avc_cache.slots_lock[hvalue];
480
481 if (!spin_trylock_irqsave(lock, flags))
482 continue;
483
484 rcu_read_lock();
485 hlist_for_each_entry(node, head, list) {
486 avc_node_delete(avc, node);
487 avc_cache_stats_incr(reclaims);
488 ecx++;
489 if (ecx >= AVC_CACHE_RECLAIM) {
490 rcu_read_unlock();
491 spin_unlock_irqrestore(lock, flags);
492 goto out;
493 }
494 }
495 rcu_read_unlock();
496 spin_unlock_irqrestore(lock, flags);
497 }
498out:
499 return ecx;
500}
501
502static struct avc_node *avc_alloc_node(struct selinux_avc *avc)
503{
504 struct avc_node *node;
505
506 node = kmem_cache_zalloc(avc_node_cachep, GFP_NOWAIT);
507 if (!node)
508 goto out;
509
510 INIT_HLIST_NODE(&node->list);
511 avc_cache_stats_incr(allocations);
512
513 if (atomic_inc_return(&avc->avc_cache.active_nodes) >
514 avc->avc_cache_threshold)
515 avc_reclaim_node(avc);
516
517out:
518 return node;
519}
520
521static void avc_node_populate(struct avc_node *node, u32 ssid, u32 tsid, u16 tclass, struct av_decision *avd)
522{
523 node->ae.ssid = ssid;
524 node->ae.tsid = tsid;
525 node->ae.tclass = tclass;
526 memcpy(&node->ae.avd, avd, sizeof(node->ae.avd));
527}
528
529static inline struct avc_node *avc_search_node(struct selinux_avc *avc,
530 u32 ssid, u32 tsid, u16 tclass)
531{
532 struct avc_node *node, *ret = NULL;
533 int hvalue;
534 struct hlist_head *head;
535
536 hvalue = avc_hash(ssid, tsid, tclass);
537 head = &avc->avc_cache.slots[hvalue];
538 hlist_for_each_entry_rcu(node, head, list) {
539 if (ssid == node->ae.ssid &&
540 tclass == node->ae.tclass &&
541 tsid == node->ae.tsid) {
542 ret = node;
543 break;
544 }
545 }
546
547 return ret;
548}
549
550
551
552
553
554
555
556
557
558
559
560
561
562static struct avc_node *avc_lookup(struct selinux_avc *avc,
563 u32 ssid, u32 tsid, u16 tclass)
564{
565 struct avc_node *node;
566
567 avc_cache_stats_incr(lookups);
568 node = avc_search_node(avc, ssid, tsid, tclass);
569
570 if (node)
571 return node;
572
573 avc_cache_stats_incr(misses);
574 return NULL;
575}
576
577static int avc_latest_notif_update(struct selinux_avc *avc,
578 int seqno, int is_insert)
579{
580 int ret = 0;
581 static DEFINE_SPINLOCK(notif_lock);
582 unsigned long flag;
583
584 spin_lock_irqsave(¬if_lock, flag);
585 if (is_insert) {
586 if (seqno < avc->avc_cache.latest_notif) {
587 pr_warn("SELinux: avc: seqno %d < latest_notif %d\n",
588 seqno, avc->avc_cache.latest_notif);
589 ret = -EAGAIN;
590 }
591 } else {
592 if (seqno > avc->avc_cache.latest_notif)
593 avc->avc_cache.latest_notif = seqno;
594 }
595 spin_unlock_irqrestore(¬if_lock, flag);
596
597 return ret;
598}
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618static struct avc_node *avc_insert(struct selinux_avc *avc,
619 u32 ssid, u32 tsid, u16 tclass,
620 struct av_decision *avd,
621 struct avc_xperms_node *xp_node)
622{
623 struct avc_node *pos, *node = NULL;
624 int hvalue;
625 unsigned long flag;
626 spinlock_t *lock;
627 struct hlist_head *head;
628
629 if (avc_latest_notif_update(avc, avd->seqno, 1))
630 return NULL;
631
632 node = avc_alloc_node(avc);
633 if (!node)
634 return NULL;
635
636 avc_node_populate(node, ssid, tsid, tclass, avd);
637 if (avc_xperms_populate(node, xp_node)) {
638 avc_node_kill(avc, node);
639 return NULL;
640 }
641
642 hvalue = avc_hash(ssid, tsid, tclass);
643 head = &avc->avc_cache.slots[hvalue];
644 lock = &avc->avc_cache.slots_lock[hvalue];
645 spin_lock_irqsave(lock, flag);
646 hlist_for_each_entry(pos, head, list) {
647 if (pos->ae.ssid == ssid &&
648 pos->ae.tsid == tsid &&
649 pos->ae.tclass == tclass) {
650 avc_node_replace(avc, node, pos);
651 goto found;
652 }
653 }
654 hlist_add_head_rcu(&node->list, head);
655found:
656 spin_unlock_irqrestore(lock, flag);
657 return node;
658}
659
660
661
662
663
664
665
666static void avc_audit_pre_callback(struct audit_buffer *ab, void *a)
667{
668 struct common_audit_data *ad = a;
669 struct selinux_audit_data *sad = ad->selinux_audit_data;
670 u32 av = sad->audited;
671 const char **perms;
672 int i, perm;
673
674 audit_log_format(ab, "avc: %s ", sad->denied ? "denied" : "granted");
675
676 if (av == 0) {
677 audit_log_format(ab, " null");
678 return;
679 }
680
681 perms = secclass_map[sad->tclass-1].perms;
682
683 audit_log_format(ab, " {");
684 i = 0;
685 perm = 1;
686 while (i < (sizeof(av) * 8)) {
687 if ((perm & av) && perms[i]) {
688 audit_log_format(ab, " %s", perms[i]);
689 av &= ~perm;
690 }
691 i++;
692 perm <<= 1;
693 }
694
695 if (av)
696 audit_log_format(ab, " 0x%x", av);
697
698 audit_log_format(ab, " } for ");
699}
700
701
702
703
704
705
706
707static void avc_audit_post_callback(struct audit_buffer *ab, void *a)
708{
709 struct common_audit_data *ad = a;
710 struct selinux_audit_data *sad = ad->selinux_audit_data;
711 char *scontext = NULL;
712 char *tcontext = NULL;
713 const char *tclass = NULL;
714 u32 scontext_len;
715 u32 tcontext_len;
716 int rc;
717
718 rc = security_sid_to_context(sad->state, sad->ssid, &scontext,
719 &scontext_len);
720 if (rc)
721 audit_log_format(ab, " ssid=%d", sad->ssid);
722 else
723 audit_log_format(ab, " scontext=%s", scontext);
724
725 rc = security_sid_to_context(sad->state, sad->tsid, &tcontext,
726 &tcontext_len);
727 if (rc)
728 audit_log_format(ab, " tsid=%d", sad->tsid);
729 else
730 audit_log_format(ab, " tcontext=%s", tcontext);
731
732 tclass = secclass_map[sad->tclass-1].name;
733 audit_log_format(ab, " tclass=%s", tclass);
734
735 if (sad->denied)
736 audit_log_format(ab, " permissive=%u", sad->result ? 0 : 1);
737
738 trace_selinux_audited(sad, scontext, tcontext, tclass);
739 kfree(tcontext);
740 kfree(scontext);
741
742
743 rc = security_sid_to_context_inval(sad->state, sad->ssid, &scontext,
744 &scontext_len);
745 if (!rc && scontext) {
746 if (scontext_len && scontext[scontext_len - 1] == '\0')
747 scontext_len--;
748 audit_log_format(ab, " srawcon=");
749 audit_log_n_untrustedstring(ab, scontext, scontext_len);
750 kfree(scontext);
751 }
752
753 rc = security_sid_to_context_inval(sad->state, sad->tsid, &scontext,
754 &scontext_len);
755 if (!rc && scontext) {
756 if (scontext_len && scontext[scontext_len - 1] == '\0')
757 scontext_len--;
758 audit_log_format(ab, " trawcon=");
759 audit_log_n_untrustedstring(ab, scontext, scontext_len);
760 kfree(scontext);
761 }
762}
763
764
765noinline int slow_avc_audit(struct selinux_state *state,
766 u32 ssid, u32 tsid, u16 tclass,
767 u32 requested, u32 audited, u32 denied, int result,
768 struct common_audit_data *a)
769{
770 struct common_audit_data stack_data;
771 struct selinux_audit_data sad;
772
773 if (WARN_ON(!tclass || tclass >= ARRAY_SIZE(secclass_map)))
774 return -EINVAL;
775
776 if (!a) {
777 a = &stack_data;
778 a->type = LSM_AUDIT_DATA_NONE;
779 }
780
781 sad.tclass = tclass;
782 sad.requested = requested;
783 sad.ssid = ssid;
784 sad.tsid = tsid;
785 sad.audited = audited;
786 sad.denied = denied;
787 sad.result = result;
788 sad.state = state;
789
790 a->selinux_audit_data = &sad;
791
792 common_lsm_audit(a, avc_audit_pre_callback, avc_audit_post_callback);
793 return 0;
794}
795
796
797
798
799
800
801
802
803
804
805int __init avc_add_callback(int (*callback)(u32 event), u32 events)
806{
807 struct avc_callback_node *c;
808 int rc = 0;
809
810 c = kmalloc(sizeof(*c), GFP_KERNEL);
811 if (!c) {
812 rc = -ENOMEM;
813 goto out;
814 }
815
816 c->callback = callback;
817 c->events = events;
818 c->next = avc_callbacks;
819 avc_callbacks = c;
820out:
821 return rc;
822}
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838static int avc_update_node(struct selinux_avc *avc,
839 u32 event, u32 perms, u8 driver, u8 xperm, u32 ssid,
840 u32 tsid, u16 tclass, u32 seqno,
841 struct extended_perms_decision *xpd,
842 u32 flags)
843{
844 int hvalue, rc = 0;
845 unsigned long flag;
846 struct avc_node *pos, *node, *orig = NULL;
847 struct hlist_head *head;
848 spinlock_t *lock;
849
850
851
852
853
854
855
856
857
858
859
860
861
862 if (flags & AVC_NONBLOCKING)
863 return 0;
864
865 node = avc_alloc_node(avc);
866 if (!node) {
867 rc = -ENOMEM;
868 goto out;
869 }
870
871
872 hvalue = avc_hash(ssid, tsid, tclass);
873
874 head = &avc->avc_cache.slots[hvalue];
875 lock = &avc->avc_cache.slots_lock[hvalue];
876
877 spin_lock_irqsave(lock, flag);
878
879 hlist_for_each_entry(pos, head, list) {
880 if (ssid == pos->ae.ssid &&
881 tsid == pos->ae.tsid &&
882 tclass == pos->ae.tclass &&
883 seqno == pos->ae.avd.seqno){
884 orig = pos;
885 break;
886 }
887 }
888
889 if (!orig) {
890 rc = -ENOENT;
891 avc_node_kill(avc, node);
892 goto out_unlock;
893 }
894
895
896
897
898
899 avc_node_populate(node, ssid, tsid, tclass, &orig->ae.avd);
900
901 if (orig->ae.xp_node) {
902 rc = avc_xperms_populate(node, orig->ae.xp_node);
903 if (rc) {
904 avc_node_kill(avc, node);
905 goto out_unlock;
906 }
907 }
908
909 switch (event) {
910 case AVC_CALLBACK_GRANT:
911 node->ae.avd.allowed |= perms;
912 if (node->ae.xp_node && (flags & AVC_EXTENDED_PERMS))
913 avc_xperms_allow_perm(node->ae.xp_node, driver, xperm);
914 break;
915 case AVC_CALLBACK_TRY_REVOKE:
916 case AVC_CALLBACK_REVOKE:
917 node->ae.avd.allowed &= ~perms;
918 break;
919 case AVC_CALLBACK_AUDITALLOW_ENABLE:
920 node->ae.avd.auditallow |= perms;
921 break;
922 case AVC_CALLBACK_AUDITALLOW_DISABLE:
923 node->ae.avd.auditallow &= ~perms;
924 break;
925 case AVC_CALLBACK_AUDITDENY_ENABLE:
926 node->ae.avd.auditdeny |= perms;
927 break;
928 case AVC_CALLBACK_AUDITDENY_DISABLE:
929 node->ae.avd.auditdeny &= ~perms;
930 break;
931 case AVC_CALLBACK_ADD_XPERMS:
932 avc_add_xperms_decision(node, xpd);
933 break;
934 }
935 avc_node_replace(avc, node, orig);
936out_unlock:
937 spin_unlock_irqrestore(lock, flag);
938out:
939 return rc;
940}
941
942
943
944
945static void avc_flush(struct selinux_avc *avc)
946{
947 struct hlist_head *head;
948 struct avc_node *node;
949 spinlock_t *lock;
950 unsigned long flag;
951 int i;
952
953 for (i = 0; i < AVC_CACHE_SLOTS; i++) {
954 head = &avc->avc_cache.slots[i];
955 lock = &avc->avc_cache.slots_lock[i];
956
957 spin_lock_irqsave(lock, flag);
958
959
960
961
962 rcu_read_lock();
963 hlist_for_each_entry(node, head, list)
964 avc_node_delete(avc, node);
965 rcu_read_unlock();
966 spin_unlock_irqrestore(lock, flag);
967 }
968}
969
970
971
972
973
974int avc_ss_reset(struct selinux_avc *avc, u32 seqno)
975{
976 struct avc_callback_node *c;
977 int rc = 0, tmprc;
978
979 avc_flush(avc);
980
981 for (c = avc_callbacks; c; c = c->next) {
982 if (c->events & AVC_CALLBACK_RESET) {
983 tmprc = c->callback(AVC_CALLBACK_RESET);
984
985
986 if (!rc)
987 rc = tmprc;
988 }
989 }
990
991 avc_latest_notif_update(avc, seqno, 0);
992 return rc;
993}
994
995
996
997
998
999
1000
1001
1002
1003
1004static noinline
1005struct avc_node *avc_compute_av(struct selinux_state *state,
1006 u32 ssid, u32 tsid,
1007 u16 tclass, struct av_decision *avd,
1008 struct avc_xperms_node *xp_node)
1009{
1010 rcu_read_unlock();
1011 INIT_LIST_HEAD(&xp_node->xpd_head);
1012 security_compute_av(state, ssid, tsid, tclass, avd, &xp_node->xp);
1013 rcu_read_lock();
1014 return avc_insert(state->avc, ssid, tsid, tclass, avd, xp_node);
1015}
1016
1017static noinline int avc_denied(struct selinux_state *state,
1018 u32 ssid, u32 tsid,
1019 u16 tclass, u32 requested,
1020 u8 driver, u8 xperm, unsigned int flags,
1021 struct av_decision *avd)
1022{
1023 if (flags & AVC_STRICT)
1024 return -EACCES;
1025
1026 if (enforcing_enabled(state) &&
1027 !(avd->flags & AVD_FLAGS_PERMISSIVE))
1028 return -EACCES;
1029
1030 avc_update_node(state->avc, AVC_CALLBACK_GRANT, requested, driver,
1031 xperm, ssid, tsid, tclass, avd->seqno, NULL, flags);
1032 return 0;
1033}
1034
1035
1036
1037
1038
1039
1040
1041
1042int avc_has_extended_perms(struct selinux_state *state,
1043 u32 ssid, u32 tsid, u16 tclass, u32 requested,
1044 u8 driver, u8 xperm, struct common_audit_data *ad)
1045{
1046 struct avc_node *node;
1047 struct av_decision avd;
1048 u32 denied;
1049 struct extended_perms_decision local_xpd;
1050 struct extended_perms_decision *xpd = NULL;
1051 struct extended_perms_data allowed;
1052 struct extended_perms_data auditallow;
1053 struct extended_perms_data dontaudit;
1054 struct avc_xperms_node local_xp_node;
1055 struct avc_xperms_node *xp_node;
1056 int rc = 0, rc2;
1057
1058 xp_node = &local_xp_node;
1059 if (WARN_ON(!requested))
1060 return -EACCES;
1061
1062 rcu_read_lock();
1063
1064 node = avc_lookup(state->avc, ssid, tsid, tclass);
1065 if (unlikely(!node)) {
1066 node = avc_compute_av(state, ssid, tsid, tclass, &avd, xp_node);
1067 } else {
1068 memcpy(&avd, &node->ae.avd, sizeof(avd));
1069 xp_node = node->ae.xp_node;
1070 }
1071
1072 if (!xp_node || !xp_node->xp.len)
1073 goto decision;
1074
1075 local_xpd.allowed = &allowed;
1076 local_xpd.auditallow = &auditallow;
1077 local_xpd.dontaudit = &dontaudit;
1078
1079 xpd = avc_xperms_decision_lookup(driver, xp_node);
1080 if (unlikely(!xpd)) {
1081
1082
1083
1084
1085 if (!security_xperm_test(xp_node->xp.drivers.p, driver)) {
1086 avd.allowed &= ~requested;
1087 goto decision;
1088 }
1089 rcu_read_unlock();
1090 security_compute_xperms_decision(state, ssid, tsid, tclass,
1091 driver, &local_xpd);
1092 rcu_read_lock();
1093 avc_update_node(state->avc, AVC_CALLBACK_ADD_XPERMS, requested,
1094 driver, xperm, ssid, tsid, tclass, avd.seqno,
1095 &local_xpd, 0);
1096 } else {
1097 avc_quick_copy_xperms_decision(xperm, &local_xpd, xpd);
1098 }
1099 xpd = &local_xpd;
1100
1101 if (!avc_xperms_has_perm(xpd, xperm, XPERMS_ALLOWED))
1102 avd.allowed &= ~requested;
1103
1104decision:
1105 denied = requested & ~(avd.allowed);
1106 if (unlikely(denied))
1107 rc = avc_denied(state, ssid, tsid, tclass, requested,
1108 driver, xperm, AVC_EXTENDED_PERMS, &avd);
1109
1110 rcu_read_unlock();
1111
1112 rc2 = avc_xperms_audit(state, ssid, tsid, tclass, requested,
1113 &avd, xpd, xperm, rc, ad);
1114 if (rc2)
1115 return rc2;
1116 return rc;
1117}
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139inline int avc_has_perm_noaudit(struct selinux_state *state,
1140 u32 ssid, u32 tsid,
1141 u16 tclass, u32 requested,
1142 unsigned int flags,
1143 struct av_decision *avd)
1144{
1145 struct avc_node *node;
1146 struct avc_xperms_node xp_node;
1147 int rc = 0;
1148 u32 denied;
1149
1150 if (WARN_ON(!requested))
1151 return -EACCES;
1152
1153 rcu_read_lock();
1154
1155 node = avc_lookup(state->avc, ssid, tsid, tclass);
1156 if (unlikely(!node))
1157 node = avc_compute_av(state, ssid, tsid, tclass, avd, &xp_node);
1158 else
1159 memcpy(avd, &node->ae.avd, sizeof(*avd));
1160
1161 denied = requested & ~(avd->allowed);
1162 if (unlikely(denied))
1163 rc = avc_denied(state, ssid, tsid, tclass, requested, 0, 0,
1164 flags, avd);
1165
1166 rcu_read_unlock();
1167 return rc;
1168}
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186int avc_has_perm(struct selinux_state *state, u32 ssid, u32 tsid, u16 tclass,
1187 u32 requested, struct common_audit_data *auditdata)
1188{
1189 struct av_decision avd;
1190 int rc, rc2;
1191
1192 rc = avc_has_perm_noaudit(state, ssid, tsid, tclass, requested, 0,
1193 &avd);
1194
1195 rc2 = avc_audit(state, ssid, tsid, tclass, requested, &avd, rc,
1196 auditdata, 0);
1197 if (rc2)
1198 return rc2;
1199 return rc;
1200}
1201
1202int avc_has_perm_flags(struct selinux_state *state,
1203 u32 ssid, u32 tsid, u16 tclass, u32 requested,
1204 struct common_audit_data *auditdata,
1205 int flags)
1206{
1207 struct av_decision avd;
1208 int rc, rc2;
1209
1210 rc = avc_has_perm_noaudit(state, ssid, tsid, tclass, requested,
1211 (flags & MAY_NOT_BLOCK) ? AVC_NONBLOCKING : 0,
1212 &avd);
1213
1214 rc2 = avc_audit(state, ssid, tsid, tclass, requested, &avd, rc,
1215 auditdata, flags);
1216 if (rc2)
1217 return rc2;
1218 return rc;
1219}
1220
1221u32 avc_policy_seqno(struct selinux_state *state)
1222{
1223 return state->avc->avc_cache.latest_notif;
1224}
1225
1226void avc_disable(void)
1227{
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239 if (avc_node_cachep) {
1240 avc_flush(selinux_state.avc);
1241
1242 }
1243}
1244