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 <net/sock.h>
26#include <linux/un.h>
27#include <net/af_unix.h>
28#include <linux/ip.h>
29#include <linux/audit.h>
30#include <linux/ipv6.h>
31#include <net/ipv6.h>
32#include "avc.h"
33#include "avc_ss.h"
34#include "classmap.h"
35
36#define AVC_CACHE_SLOTS 512
37#define AVC_DEF_CACHE_THRESHOLD 512
38#define AVC_CACHE_RECLAIM 16
39
40#ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
41#define avc_cache_stats_incr(field) \
42do { \
43 per_cpu(avc_cache_stats, get_cpu()).field++; \
44 put_cpu(); \
45} while (0)
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};
56
57struct avc_node {
58 struct avc_entry ae;
59 struct hlist_node list;
60 struct rcu_head rhead;
61};
62
63struct avc_cache {
64 struct hlist_head slots[AVC_CACHE_SLOTS];
65 spinlock_t slots_lock[AVC_CACHE_SLOTS];
66 atomic_t lru_hint;
67 atomic_t active_nodes;
68 u32 latest_notif;
69};
70
71struct avc_callback_node {
72 int (*callback) (u32 event, u32 ssid, u32 tsid,
73 u16 tclass, u32 perms,
74 u32 *out_retained);
75 u32 events;
76 u32 ssid;
77 u32 tsid;
78 u16 tclass;
79 u32 perms;
80 struct avc_callback_node *next;
81};
82
83
84unsigned int avc_cache_threshold = AVC_DEF_CACHE_THRESHOLD;
85
86#ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
87DEFINE_PER_CPU(struct avc_cache_stats, avc_cache_stats) = { 0 };
88#endif
89
90static struct avc_cache avc_cache;
91static struct avc_callback_node *avc_callbacks;
92static struct kmem_cache *avc_node_cachep;
93
94static inline int avc_hash(u32 ssid, u32 tsid, u16 tclass)
95{
96 return (ssid ^ (tsid<<2) ^ (tclass<<4)) & (AVC_CACHE_SLOTS - 1);
97}
98
99
100
101
102
103
104static void avc_dump_av(struct audit_buffer *ab, u16 tclass, u32 av)
105{
106 const char **perms;
107 int i, perm;
108
109 if (av == 0) {
110 audit_log_format(ab, " null");
111 return;
112 }
113
114 perms = secclass_map[tclass-1].perms;
115
116 audit_log_format(ab, " {");
117 i = 0;
118 perm = 1;
119 while (i < (sizeof(av) * 8)) {
120 if ((perm & av) && perms[i]) {
121 audit_log_format(ab, " %s", perms[i]);
122 av &= ~perm;
123 }
124 i++;
125 perm <<= 1;
126 }
127
128 if (av)
129 audit_log_format(ab, " 0x%x", av);
130
131 audit_log_format(ab, " }");
132}
133
134
135
136
137
138
139
140static void avc_dump_query(struct audit_buffer *ab, u32 ssid, u32 tsid, u16 tclass)
141{
142 int rc;
143 char *scontext;
144 u32 scontext_len;
145
146 rc = security_sid_to_context(ssid, &scontext, &scontext_len);
147 if (rc)
148 audit_log_format(ab, "ssid=%d", ssid);
149 else {
150 audit_log_format(ab, "scontext=%s", scontext);
151 kfree(scontext);
152 }
153
154 rc = security_sid_to_context(tsid, &scontext, &scontext_len);
155 if (rc)
156 audit_log_format(ab, " tsid=%d", tsid);
157 else {
158 audit_log_format(ab, " tcontext=%s", scontext);
159 kfree(scontext);
160 }
161
162 BUG_ON(tclass >= ARRAY_SIZE(secclass_map));
163 audit_log_format(ab, " tclass=%s", secclass_map[tclass-1].name);
164}
165
166
167
168
169
170
171void __init avc_init(void)
172{
173 int i;
174
175 for (i = 0; i < AVC_CACHE_SLOTS; i++) {
176 INIT_HLIST_HEAD(&avc_cache.slots[i]);
177 spin_lock_init(&avc_cache.slots_lock[i]);
178 }
179 atomic_set(&avc_cache.active_nodes, 0);
180 atomic_set(&avc_cache.lru_hint, 0);
181
182 avc_node_cachep = kmem_cache_create("avc_node", sizeof(struct avc_node),
183 0, SLAB_PANIC, NULL);
184
185 audit_log(current->audit_context, GFP_KERNEL, AUDIT_KERNEL, "AVC INITIALIZED\n");
186}
187
188int avc_get_hash_stats(char *page)
189{
190 int i, chain_len, max_chain_len, slots_used;
191 struct avc_node *node;
192 struct hlist_head *head;
193
194 rcu_read_lock();
195
196 slots_used = 0;
197 max_chain_len = 0;
198 for (i = 0; i < AVC_CACHE_SLOTS; i++) {
199 head = &avc_cache.slots[i];
200 if (!hlist_empty(head)) {
201 struct hlist_node *next;
202
203 slots_used++;
204 chain_len = 0;
205 hlist_for_each_entry_rcu(node, next, head, list)
206 chain_len++;
207 if (chain_len > max_chain_len)
208 max_chain_len = chain_len;
209 }
210 }
211
212 rcu_read_unlock();
213
214 return scnprintf(page, PAGE_SIZE, "entries: %d\nbuckets used: %d/%d\n"
215 "longest chain: %d\n",
216 atomic_read(&avc_cache.active_nodes),
217 slots_used, AVC_CACHE_SLOTS, max_chain_len);
218}
219
220static void avc_node_free(struct rcu_head *rhead)
221{
222 struct avc_node *node = container_of(rhead, struct avc_node, rhead);
223 kmem_cache_free(avc_node_cachep, node);
224 avc_cache_stats_incr(frees);
225}
226
227static void avc_node_delete(struct avc_node *node)
228{
229 hlist_del_rcu(&node->list);
230 call_rcu(&node->rhead, avc_node_free);
231 atomic_dec(&avc_cache.active_nodes);
232}
233
234static void avc_node_kill(struct avc_node *node)
235{
236 kmem_cache_free(avc_node_cachep, node);
237 avc_cache_stats_incr(frees);
238 atomic_dec(&avc_cache.active_nodes);
239}
240
241static void avc_node_replace(struct avc_node *new, struct avc_node *old)
242{
243 hlist_replace_rcu(&old->list, &new->list);
244 call_rcu(&old->rhead, avc_node_free);
245 atomic_dec(&avc_cache.active_nodes);
246}
247
248static inline int avc_reclaim_node(void)
249{
250 struct avc_node *node;
251 int hvalue, try, ecx;
252 unsigned long flags;
253 struct hlist_head *head;
254 struct hlist_node *next;
255 spinlock_t *lock;
256
257 for (try = 0, ecx = 0; try < AVC_CACHE_SLOTS; try++) {
258 hvalue = atomic_inc_return(&avc_cache.lru_hint) & (AVC_CACHE_SLOTS - 1);
259 head = &avc_cache.slots[hvalue];
260 lock = &avc_cache.slots_lock[hvalue];
261
262 if (!spin_trylock_irqsave(lock, flags))
263 continue;
264
265 rcu_read_lock();
266 hlist_for_each_entry(node, next, head, list) {
267 avc_node_delete(node);
268 avc_cache_stats_incr(reclaims);
269 ecx++;
270 if (ecx >= AVC_CACHE_RECLAIM) {
271 rcu_read_unlock();
272 spin_unlock_irqrestore(lock, flags);
273 goto out;
274 }
275 }
276 rcu_read_unlock();
277 spin_unlock_irqrestore(lock, flags);
278 }
279out:
280 return ecx;
281}
282
283static struct avc_node *avc_alloc_node(void)
284{
285 struct avc_node *node;
286
287 node = kmem_cache_zalloc(avc_node_cachep, GFP_ATOMIC);
288 if (!node)
289 goto out;
290
291 INIT_HLIST_NODE(&node->list);
292 avc_cache_stats_incr(allocations);
293
294 if (atomic_inc_return(&avc_cache.active_nodes) > avc_cache_threshold)
295 avc_reclaim_node();
296
297out:
298 return node;
299}
300
301static void avc_node_populate(struct avc_node *node, u32 ssid, u32 tsid, u16 tclass, struct av_decision *avd)
302{
303 node->ae.ssid = ssid;
304 node->ae.tsid = tsid;
305 node->ae.tclass = tclass;
306 memcpy(&node->ae.avd, avd, sizeof(node->ae.avd));
307}
308
309static inline struct avc_node *avc_search_node(u32 ssid, u32 tsid, u16 tclass)
310{
311 struct avc_node *node, *ret = NULL;
312 int hvalue;
313 struct hlist_head *head;
314 struct hlist_node *next;
315
316 hvalue = avc_hash(ssid, tsid, tclass);
317 head = &avc_cache.slots[hvalue];
318 hlist_for_each_entry_rcu(node, next, head, list) {
319 if (ssid == node->ae.ssid &&
320 tclass == node->ae.tclass &&
321 tsid == node->ae.tsid) {
322 ret = node;
323 break;
324 }
325 }
326
327 return ret;
328}
329
330
331
332
333
334
335
336
337
338
339
340
341
342static struct avc_node *avc_lookup(u32 ssid, u32 tsid, u16 tclass)
343{
344 struct avc_node *node;
345
346 avc_cache_stats_incr(lookups);
347 node = avc_search_node(ssid, tsid, tclass);
348
349 if (node)
350 avc_cache_stats_incr(hits);
351 else
352 avc_cache_stats_incr(misses);
353
354 return node;
355}
356
357static int avc_latest_notif_update(int seqno, int is_insert)
358{
359 int ret = 0;
360 static DEFINE_SPINLOCK(notif_lock);
361 unsigned long flag;
362
363 spin_lock_irqsave(¬if_lock, flag);
364 if (is_insert) {
365 if (seqno < avc_cache.latest_notif) {
366 printk(KERN_WARNING "SELinux: avc: seqno %d < latest_notif %d\n",
367 seqno, avc_cache.latest_notif);
368 ret = -EAGAIN;
369 }
370 } else {
371 if (seqno > avc_cache.latest_notif)
372 avc_cache.latest_notif = seqno;
373 }
374 spin_unlock_irqrestore(¬if_lock, flag);
375
376 return ret;
377}
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396static struct avc_node *avc_insert(u32 ssid, u32 tsid, u16 tclass, struct av_decision *avd)
397{
398 struct avc_node *pos, *node = NULL;
399 int hvalue;
400 unsigned long flag;
401
402 if (avc_latest_notif_update(avd->seqno, 1))
403 goto out;
404
405 node = avc_alloc_node();
406 if (node) {
407 struct hlist_head *head;
408 struct hlist_node *next;
409 spinlock_t *lock;
410
411 hvalue = avc_hash(ssid, tsid, tclass);
412 avc_node_populate(node, ssid, tsid, tclass, avd);
413
414 head = &avc_cache.slots[hvalue];
415 lock = &avc_cache.slots_lock[hvalue];
416
417 spin_lock_irqsave(lock, flag);
418 hlist_for_each_entry(pos, next, head, list) {
419 if (pos->ae.ssid == ssid &&
420 pos->ae.tsid == tsid &&
421 pos->ae.tclass == tclass) {
422 avc_node_replace(node, pos);
423 goto found;
424 }
425 }
426 hlist_add_head_rcu(&node->list, head);
427found:
428 spin_unlock_irqrestore(lock, flag);
429 }
430out:
431 return node;
432}
433
434
435
436
437
438
439
440static void avc_audit_pre_callback(struct audit_buffer *ab, void *a)
441{
442 struct common_audit_data *ad = a;
443 audit_log_format(ab, "avc: %s ",
444 ad->selinux_audit_data.denied ? "denied" : "granted");
445 avc_dump_av(ab, ad->selinux_audit_data.tclass,
446 ad->selinux_audit_data.audited);
447 audit_log_format(ab, " for ");
448}
449
450
451
452
453
454
455
456static void avc_audit_post_callback(struct audit_buffer *ab, void *a)
457{
458 struct common_audit_data *ad = a;
459 audit_log_format(ab, " ");
460 avc_dump_query(ab, ad->selinux_audit_data.ssid,
461 ad->selinux_audit_data.tsid,
462 ad->selinux_audit_data.tclass);
463}
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485int avc_audit(u32 ssid, u32 tsid,
486 u16 tclass, u32 requested,
487 struct av_decision *avd, int result, struct common_audit_data *a,
488 unsigned flags)
489{
490 struct common_audit_data stack_data;
491 u32 denied, audited;
492 denied = requested & ~avd->allowed;
493 if (denied) {
494 audited = denied & avd->auditdeny;
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511 if (a &&
512 a->selinux_audit_data.auditdeny &&
513 !(a->selinux_audit_data.auditdeny & avd->auditdeny))
514 audited = 0;
515 } else if (result)
516 audited = denied = requested;
517 else
518 audited = requested & avd->auditallow;
519 if (!audited)
520 return 0;
521
522 if (!a) {
523 a = &stack_data;
524 COMMON_AUDIT_DATA_INIT(a, NONE);
525 }
526
527
528
529
530
531
532
533
534 if ((a->type == LSM_AUDIT_DATA_FS) &&
535 (flags & IPERM_FLAG_RCU))
536 return -ECHILD;
537
538 a->selinux_audit_data.tclass = tclass;
539 a->selinux_audit_data.requested = requested;
540 a->selinux_audit_data.ssid = ssid;
541 a->selinux_audit_data.tsid = tsid;
542 a->selinux_audit_data.audited = audited;
543 a->selinux_audit_data.denied = denied;
544 a->lsm_pre_audit = avc_audit_pre_callback;
545 a->lsm_post_audit = avc_audit_post_callback;
546 common_lsm_audit(a);
547 return 0;
548}
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565int avc_add_callback(int (*callback)(u32 event, u32 ssid, u32 tsid,
566 u16 tclass, u32 perms,
567 u32 *out_retained),
568 u32 events, u32 ssid, u32 tsid,
569 u16 tclass, u32 perms)
570{
571 struct avc_callback_node *c;
572 int rc = 0;
573
574 c = kmalloc(sizeof(*c), GFP_ATOMIC);
575 if (!c) {
576 rc = -ENOMEM;
577 goto out;
578 }
579
580 c->callback = callback;
581 c->events = events;
582 c->ssid = ssid;
583 c->tsid = tsid;
584 c->perms = perms;
585 c->next = avc_callbacks;
586 avc_callbacks = c;
587out:
588 return rc;
589}
590
591static inline int avc_sidcmp(u32 x, u32 y)
592{
593 return (x == y || x == SECSID_WILD || y == SECSID_WILD);
594}
595
596
597
598
599
600
601
602
603
604
605
606
607
608static int avc_update_node(u32 event, u32 perms, u32 ssid, u32 tsid, u16 tclass,
609 u32 seqno)
610{
611 int hvalue, rc = 0;
612 unsigned long flag;
613 struct avc_node *pos, *node, *orig = NULL;
614 struct hlist_head *head;
615 struct hlist_node *next;
616 spinlock_t *lock;
617
618 node = avc_alloc_node();
619 if (!node) {
620 rc = -ENOMEM;
621 goto out;
622 }
623
624
625 hvalue = avc_hash(ssid, tsid, tclass);
626
627 head = &avc_cache.slots[hvalue];
628 lock = &avc_cache.slots_lock[hvalue];
629
630 spin_lock_irqsave(lock, flag);
631
632 hlist_for_each_entry(pos, next, head, list) {
633 if (ssid == pos->ae.ssid &&
634 tsid == pos->ae.tsid &&
635 tclass == pos->ae.tclass &&
636 seqno == pos->ae.avd.seqno){
637 orig = pos;
638 break;
639 }
640 }
641
642 if (!orig) {
643 rc = -ENOENT;
644 avc_node_kill(node);
645 goto out_unlock;
646 }
647
648
649
650
651
652 avc_node_populate(node, ssid, tsid, tclass, &orig->ae.avd);
653
654 switch (event) {
655 case AVC_CALLBACK_GRANT:
656 node->ae.avd.allowed |= perms;
657 break;
658 case AVC_CALLBACK_TRY_REVOKE:
659 case AVC_CALLBACK_REVOKE:
660 node->ae.avd.allowed &= ~perms;
661 break;
662 case AVC_CALLBACK_AUDITALLOW_ENABLE:
663 node->ae.avd.auditallow |= perms;
664 break;
665 case AVC_CALLBACK_AUDITALLOW_DISABLE:
666 node->ae.avd.auditallow &= ~perms;
667 break;
668 case AVC_CALLBACK_AUDITDENY_ENABLE:
669 node->ae.avd.auditdeny |= perms;
670 break;
671 case AVC_CALLBACK_AUDITDENY_DISABLE:
672 node->ae.avd.auditdeny &= ~perms;
673 break;
674 }
675 avc_node_replace(node, orig);
676out_unlock:
677 spin_unlock_irqrestore(lock, flag);
678out:
679 return rc;
680}
681
682
683
684
685static void avc_flush(void)
686{
687 struct hlist_head *head;
688 struct hlist_node *next;
689 struct avc_node *node;
690 spinlock_t *lock;
691 unsigned long flag;
692 int i;
693
694 for (i = 0; i < AVC_CACHE_SLOTS; i++) {
695 head = &avc_cache.slots[i];
696 lock = &avc_cache.slots_lock[i];
697
698 spin_lock_irqsave(lock, flag);
699
700
701
702
703 rcu_read_lock();
704 hlist_for_each_entry(node, next, head, list)
705 avc_node_delete(node);
706 rcu_read_unlock();
707 spin_unlock_irqrestore(lock, flag);
708 }
709}
710
711
712
713
714
715int avc_ss_reset(u32 seqno)
716{
717 struct avc_callback_node *c;
718 int rc = 0, tmprc;
719
720 avc_flush();
721
722 for (c = avc_callbacks; c; c = c->next) {
723 if (c->events & AVC_CALLBACK_RESET) {
724 tmprc = c->callback(AVC_CALLBACK_RESET,
725 0, 0, 0, 0, NULL);
726
727
728 if (!rc)
729 rc = tmprc;
730 }
731 }
732
733 avc_latest_notif_update(seqno, 0);
734 return rc;
735}
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757int avc_has_perm_noaudit(u32 ssid, u32 tsid,
758 u16 tclass, u32 requested,
759 unsigned flags,
760 struct av_decision *in_avd)
761{
762 struct avc_node *node;
763 struct av_decision avd_entry, *avd;
764 int rc = 0;
765 u32 denied;
766
767 BUG_ON(!requested);
768
769 rcu_read_lock();
770
771 node = avc_lookup(ssid, tsid, tclass);
772 if (!node) {
773 rcu_read_unlock();
774
775 if (in_avd)
776 avd = in_avd;
777 else
778 avd = &avd_entry;
779
780 security_compute_av(ssid, tsid, tclass, avd);
781 rcu_read_lock();
782 node = avc_insert(ssid, tsid, tclass, avd);
783 } else {
784 if (in_avd)
785 memcpy(in_avd, &node->ae.avd, sizeof(*in_avd));
786 avd = &node->ae.avd;
787 }
788
789 denied = requested & ~(avd->allowed);
790
791 if (denied) {
792 if (flags & AVC_STRICT)
793 rc = -EACCES;
794 else if (!selinux_enforcing || (avd->flags & AVD_FLAGS_PERMISSIVE))
795 avc_update_node(AVC_CALLBACK_GRANT, requested, ssid,
796 tsid, tclass, avd->seqno);
797 else
798 rc = -EACCES;
799 }
800
801 rcu_read_unlock();
802 return rc;
803}
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822int avc_has_perm_flags(u32 ssid, u32 tsid, u16 tclass,
823 u32 requested, struct common_audit_data *auditdata,
824 unsigned flags)
825{
826 struct av_decision avd;
827 int rc, rc2;
828
829 rc = avc_has_perm_noaudit(ssid, tsid, tclass, requested, 0, &avd);
830
831 rc2 = avc_audit(ssid, tsid, tclass, requested, &avd, rc, auditdata,
832 flags);
833 if (rc2)
834 return rc2;
835 return rc;
836}
837
838u32 avc_policy_seqno(void)
839{
840 return avc_cache.latest_notif;
841}
842
843void avc_disable(void)
844{
845
846
847
848
849
850
851
852
853
854
855
856 if (avc_node_cachep) {
857 avc_flush();
858
859 }
860}
861