2 * Implementation of the kernel access vector cache (AVC).
4 * Authors: Stephen Smalley, <sds@epoch.ncsc.mil>
5 * James Morris <jmorris@redhat.com>
7 * Update: KaiGai, Kohei <kaigai@ak.jp.nec.com>
8 * Replaced the avc_lock spinlock by RCU.
10 * Copyright (C) 2003 Red Hat, Inc., James Morris <jmorris@redhat.com>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License version 2,
14 * as published by the Free Software Foundation.
16 #include <linux/types.h>
17 #include <linux/stddef.h>
18 #include <linux/kernel.h>
19 #include <linux/slab.h>
21 #include <linux/dcache.h>
22 #include <linux/init.h>
23 #include <linux/skbuff.h>
24 #include <linux/percpu.h>
27 #include <net/af_unix.h>
29 #include <linux/audit.h>
30 #include <linux/ipv6.h>
35 #include "class_to_string.h"
37 #include "common_perm_to_string.h"
38 #include "av_inherit.h"
39 #include "av_perm_to_string.h"
42 #define AVC_CACHE_SLOTS 512
43 #define AVC_DEF_CACHE_THRESHOLD 512
44 #define AVC_CACHE_RECLAIM 16
46 #ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
47 #define avc_cache_stats_incr(field) \
49 per_cpu(avc_cache_stats, get_cpu()).field++; \
53 #define avc_cache_stats_incr(field) do {} while (0)
60 struct av_decision avd;
61 atomic_t used; /* used recently */
66 struct list_head list;
67 struct rcu_head rhead;
71 struct list_head slots[AVC_CACHE_SLOTS];
72 spinlock_t slots_lock[AVC_CACHE_SLOTS]; /* lock for writes */
73 atomic_t lru_hint; /* LRU hint for reclaim scan */
74 atomic_t active_nodes;
75 u32 latest_notif; /* latest revocation notification */
78 struct avc_callback_node {
79 int (*callback) (u32 event, u32 ssid, u32 tsid,
80 u16 tclass, u32 perms,
87 struct avc_callback_node *next;
90 /* Exported via selinufs */
91 unsigned int avc_cache_threshold = AVC_DEF_CACHE_THRESHOLD;
93 #ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
94 DEFINE_PER_CPU(struct avc_cache_stats, avc_cache_stats) = { 0 };
97 static struct avc_cache avc_cache;
98 static struct avc_callback_node *avc_callbacks;
99 static kmem_cache_t *avc_node_cachep;
101 static inline int avc_hash(u32 ssid, u32 tsid, u16 tclass)
103 return (ssid ^ (tsid<<2) ^ (tclass<<4)) & (AVC_CACHE_SLOTS - 1);
107 * avc_dump_av - Display an access vector in human-readable form.
108 * @tclass: target security class
111 void avc_dump_av(struct audit_buffer *ab, u16 tclass, u32 av)
113 char **common_pts = NULL;
118 audit_log_format(ab, " null");
122 for (i = 0; i < ARRAY_SIZE(av_inherit); i++) {
123 if (av_inherit[i].tclass == tclass) {
124 common_pts = av_inherit[i].common_pts;
125 common_base = av_inherit[i].common_base;
130 audit_log_format(ab, " {");
133 while (perm < common_base) {
135 audit_log_format(ab, " %s", common_pts[i]);
140 while (i < sizeof(av) * 8) {
142 for (i2 = 0; i2 < ARRAY_SIZE(av_perm_to_string); i2++) {
143 if ((av_perm_to_string[i2].tclass == tclass) &&
144 (av_perm_to_string[i2].value == perm))
147 if (i2 < ARRAY_SIZE(av_perm_to_string))
148 audit_log_format(ab, " %s",
149 av_perm_to_string[i2].name);
155 audit_log_format(ab, " }");
159 * avc_dump_query - Display a SID pair and a class in human-readable form.
160 * @ssid: source security identifier
161 * @tsid: target security identifier
162 * @tclass: target security class
164 void avc_dump_query(struct audit_buffer *ab, u32 ssid, u32 tsid, u16 tclass)
170 rc = security_sid_to_context(ssid, &scontext, &scontext_len);
172 audit_log_format(ab, "ssid=%d", ssid);
174 audit_log_format(ab, "scontext=%s", scontext);
178 rc = security_sid_to_context(tsid, &scontext, &scontext_len);
180 audit_log_format(ab, " tsid=%d", tsid);
182 audit_log_format(ab, " tcontext=%s", scontext);
185 audit_log_format(ab, " tclass=%s", class_to_string[tclass]);
189 * avc_init - Initialize the AVC.
191 * Initialize the access vector cache.
193 void __init avc_init(void)
197 for (i = 0; i < AVC_CACHE_SLOTS; i++) {
198 INIT_LIST_HEAD(&avc_cache.slots[i]);
199 avc_cache.slots_lock[i] = SPIN_LOCK_UNLOCKED;
201 atomic_set(&avc_cache.active_nodes, 0);
202 atomic_set(&avc_cache.lru_hint, 0);
204 avc_node_cachep = kmem_cache_create("avc_node", sizeof(struct avc_node),
205 0, SLAB_PANIC, NULL, NULL);
207 audit_log(current->audit_context, "AVC INITIALIZED\n");
210 int avc_get_hash_stats(char *page)
212 int i, chain_len, max_chain_len, slots_used;
213 struct avc_node *node;
219 for (i = 0; i < AVC_CACHE_SLOTS; i++) {
220 if (!list_empty(&avc_cache.slots[i])) {
223 list_for_each_entry_rcu(node, &avc_cache.slots[i], list)
225 if (chain_len > max_chain_len)
226 max_chain_len = chain_len;
232 return scnprintf(page, PAGE_SIZE, "entries: %d\nbuckets used: %d/%d\n"
233 "longest chain: %d\n",
234 atomic_read(&avc_cache.active_nodes),
235 slots_used, AVC_CACHE_SLOTS, max_chain_len);
238 static void avc_node_free(struct rcu_head *rhead)
240 struct avc_node *node = container_of(rhead, struct avc_node, rhead);
241 kmem_cache_free(avc_node_cachep, node);
242 avc_cache_stats_incr(frees);
245 static void avc_node_delete(struct avc_node *node)
247 list_del_rcu(&node->list);
248 call_rcu(&node->rhead, avc_node_free);
249 atomic_dec(&avc_cache.active_nodes);
252 static void avc_node_kill(struct avc_node *node)
254 kmem_cache_free(avc_node_cachep, node);
255 avc_cache_stats_incr(frees);
256 atomic_dec(&avc_cache.active_nodes);
259 static void avc_node_replace(struct avc_node *new, struct avc_node *old)
261 list_replace_rcu(&old->list, &new->list);
262 call_rcu(&old->rhead, avc_node_free);
263 atomic_dec(&avc_cache.active_nodes);
266 static inline int avc_reclaim_node(void)
268 struct avc_node *node;
269 int hvalue, try, ecx;
272 for (try = 0, ecx = 0; try < AVC_CACHE_SLOTS; try++ ) {
273 hvalue = atomic_inc_return(&avc_cache.lru_hint) & (AVC_CACHE_SLOTS - 1);
275 if (!spin_trylock_irqsave(&avc_cache.slots_lock[hvalue], flags))
278 list_for_each_entry(node, &avc_cache.slots[hvalue], list) {
279 if (atomic_dec_and_test(&node->ae.used)) {
280 /* Recently Unused */
281 avc_node_delete(node);
282 avc_cache_stats_incr(reclaims);
284 if (ecx >= AVC_CACHE_RECLAIM) {
285 spin_unlock_irqrestore(&avc_cache.slots_lock[hvalue], flags);
290 spin_unlock_irqrestore(&avc_cache.slots_lock[hvalue], flags);
296 static struct avc_node *avc_alloc_node(void)
298 struct avc_node *node;
300 node = kmem_cache_alloc(avc_node_cachep, SLAB_ATOMIC);
304 memset(node, 0, sizeof(*node));
305 INIT_RCU_HEAD(&node->rhead);
306 INIT_LIST_HEAD(&node->list);
307 atomic_set(&node->ae.used, 1);
308 avc_cache_stats_incr(allocations);
310 if (atomic_inc_return(&avc_cache.active_nodes) > avc_cache_threshold)
317 static void avc_node_populate(struct avc_node *node, u32 ssid, u32 tsid, u16 tclass, struct avc_entry *ae)
319 node->ae.ssid = ssid;
320 node->ae.tsid = tsid;
321 node->ae.tclass = tclass;
322 memcpy(&node->ae.avd, &ae->avd, sizeof(node->ae.avd));
325 static inline struct avc_node *avc_search_node(u32 ssid, u32 tsid, u16 tclass)
327 struct avc_node *node, *ret = NULL;
330 hvalue = avc_hash(ssid, tsid, tclass);
331 list_for_each_entry_rcu(node, &avc_cache.slots[hvalue], list) {
332 if (ssid == node->ae.ssid &&
333 tclass == node->ae.tclass &&
334 tsid == node->ae.tsid) {
346 if (atomic_read(&ret->ae.used) != 1)
347 atomic_set(&ret->ae.used, 1);
353 * avc_lookup - Look up an AVC entry.
354 * @ssid: source security identifier
355 * @tsid: target security identifier
356 * @tclass: target security class
357 * @requested: requested permissions, interpreted based on @tclass
359 * Look up an AVC entry that is valid for the
360 * @requested permissions between the SID pair
361 * (@ssid, @tsid), interpreting the permissions
362 * based on @tclass. If a valid AVC entry exists,
363 * then this function return the avc_node.
364 * Otherwise, this function returns NULL.
366 static struct avc_node *avc_lookup(u32 ssid, u32 tsid, u16 tclass, u32 requested)
368 struct avc_node *node;
370 avc_cache_stats_incr(lookups);
371 node = avc_search_node(ssid, tsid, tclass);
373 if (node && ((node->ae.avd.decided & requested) == requested)) {
374 avc_cache_stats_incr(hits);
379 avc_cache_stats_incr(misses);
384 static int avc_latest_notif_update(int seqno, int is_insert)
387 static spinlock_t notif_lock = SPIN_LOCK_UNLOCKED;
390 spin_lock_irqsave(¬if_lock, flag);
392 if (seqno < avc_cache.latest_notif) {
393 printk(KERN_WARNING "avc: seqno %d < latest_notif %d\n",
394 seqno, avc_cache.latest_notif);
398 if (seqno > avc_cache.latest_notif)
399 avc_cache.latest_notif = seqno;
401 spin_unlock_irqrestore(¬if_lock, flag);
407 * avc_insert - Insert an AVC entry.
408 * @ssid: source security identifier
409 * @tsid: target security identifier
410 * @tclass: target security class
413 * Insert an AVC entry for the SID pair
414 * (@ssid, @tsid) and class @tclass.
415 * The access vectors and the sequence number are
416 * normally provided by the security server in
417 * response to a security_compute_av() call. If the
418 * sequence number @ae->avd.seqno is not less than the latest
419 * revocation notification, then the function copies
420 * the access vectors into a cache entry, returns
421 * avc_node inserted. Otherwise, this function returns NULL.
423 static struct avc_node *avc_insert(u32 ssid, u32 tsid, u16 tclass, struct avc_entry *ae)
425 struct avc_node *pos, *node = NULL;
429 if (avc_latest_notif_update(ae->avd.seqno, 1))
432 node = avc_alloc_node();
434 hvalue = avc_hash(ssid, tsid, tclass);
435 avc_node_populate(node, ssid, tsid, tclass, ae);
437 spin_lock_irqsave(&avc_cache.slots_lock[hvalue], flag);
438 list_for_each_entry(pos, &avc_cache.slots[hvalue], list) {
439 if (pos->ae.ssid == ssid &&
440 pos->ae.tsid == tsid &&
441 pos->ae.tclass == tclass) {
442 avc_node_replace(node, pos);
446 list_add_rcu(&node->list, &avc_cache.slots[hvalue]);
448 spin_unlock_irqrestore(&avc_cache.slots_lock[hvalue], flag);
454 static inline void avc_print_ipv6_addr(struct audit_buffer *ab,
455 struct in6_addr *addr, u16 port,
456 char *name1, char *name2)
458 if (!ipv6_addr_any(addr))
459 audit_log_format(ab, " %s=%04x:%04x:%04x:%04x:%04x:"
460 "%04x:%04x:%04x", name1, NIP6(*addr));
462 audit_log_format(ab, " %s=%d", name2, ntohs(port));
465 static inline void avc_print_ipv4_addr(struct audit_buffer *ab, u32 addr,
466 u16 port, char *name1, char *name2)
469 audit_log_format(ab, " %s=%d.%d.%d.%d", name1, NIPQUAD(addr));
471 audit_log_format(ab, " %s=%d", name2, ntohs(port));
475 * avc_audit - Audit the granting or denial of permissions.
476 * @ssid: source security identifier
477 * @tsid: target security identifier
478 * @tclass: target security class
479 * @requested: requested permissions
480 * @avd: access vector decisions
481 * @result: result from avc_has_perm_noaudit
482 * @a: auxiliary audit data
484 * Audit the granting or denial of permissions in accordance
485 * with the policy. This function is typically called by
486 * avc_has_perm() after a permission check, but can also be
487 * called directly by callers who use avc_has_perm_noaudit()
488 * in order to separate the permission check from the auditing.
489 * For example, this separation is useful when the permission check must
490 * be performed under a lock, to allow the lock to be released
491 * before calling the auditing code.
493 void avc_audit(u32 ssid, u32 tsid,
494 u16 tclass, u32 requested,
495 struct av_decision *avd, int result, struct avc_audit_data *a)
497 struct task_struct *tsk = current;
498 struct inode *inode = NULL;
500 struct audit_buffer *ab;
502 denied = requested & ~avd->allowed;
505 if (!(audited & avd->auditdeny))
508 audited = denied = requested;
511 if (!(audited & avd->auditallow))
515 ab = audit_log_start(current->audit_context);
517 return; /* audit_panic has been called */
518 audit_log_format(ab, "avc: %s ", denied ? "denied" : "granted");
519 avc_dump_av(ab, tclass,audited);
520 audit_log_format(ab, " for ");
523 if (tsk && tsk->pid) {
524 struct mm_struct *mm;
525 struct vm_area_struct *vma;
526 audit_log_format(ab, " pid=%d", tsk->pid);
530 mm = get_task_mm(tsk);
532 if (down_read_trylock(&mm->mmap_sem)) {
535 if ((vma->vm_flags & VM_EXECUTABLE) &&
537 audit_log_d_path(ab, "exe=",
538 vma->vm_file->f_dentry,
539 vma->vm_file->f_vfsmnt);
544 up_read(&mm->mmap_sem);
549 audit_log_format(ab, " comm=%s", tsk->comm);
554 case AVC_AUDIT_DATA_IPC:
555 audit_log_format(ab, " key=%d", a->u.ipc_id);
557 case AVC_AUDIT_DATA_CAP:
558 audit_log_format(ab, " capability=%d", a->u.cap);
560 case AVC_AUDIT_DATA_FS:
561 if (a->u.fs.dentry) {
562 struct dentry *dentry = a->u.fs.dentry;
564 audit_log_d_path(ab, "path=", dentry,
567 audit_log_format(ab, " name=%s",
568 dentry->d_name.name);
570 inode = dentry->d_inode;
571 } else if (a->u.fs.inode) {
572 struct dentry *dentry;
573 inode = a->u.fs.inode;
574 dentry = d_find_alias(inode);
576 audit_log_format(ab, " name=%s",
577 dentry->d_name.name);
582 audit_log_format(ab, " dev=%s ino=%ld",
586 case AVC_AUDIT_DATA_NET:
588 struct sock *sk = a->u.net.sk;
593 switch (sk->sk_family) {
595 struct inet_opt *inet = inet_sk(sk);
597 avc_print_ipv4_addr(ab, inet->rcv_saddr,
600 avc_print_ipv4_addr(ab, inet->daddr,
606 struct inet_opt *inet = inet_sk(sk);
607 struct ipv6_pinfo *inet6 = inet6_sk(sk);
609 avc_print_ipv6_addr(ab, &inet6->rcv_saddr,
612 avc_print_ipv6_addr(ab, &inet6->daddr,
620 audit_log_d_path(ab, "path=",
626 len = u->addr->len-sizeof(short);
627 p = &u->addr->name->sun_path[0];
634 "path=@%*.*s", len-1,
640 switch (a->u.net.family) {
642 avc_print_ipv4_addr(ab, a->u.net.v4info.saddr,
645 avc_print_ipv4_addr(ab, a->u.net.v4info.daddr,
650 avc_print_ipv6_addr(ab, &a->u.net.v6info.saddr,
653 avc_print_ipv6_addr(ab, &a->u.net.v6info.daddr,
659 audit_log_format(ab, " netif=%s",
664 audit_log_format(ab, " ");
665 avc_dump_query(ab, ssid, tsid, tclass);
670 * avc_add_callback - Register a callback for security events.
671 * @callback: callback function
672 * @events: security events
673 * @ssid: source security identifier or %SECSID_WILD
674 * @tsid: target security identifier or %SECSID_WILD
675 * @tclass: target security class
676 * @perms: permissions
678 * Register a callback function for events in the set @events
679 * related to the SID pair (@ssid, @tsid) and
680 * and the permissions @perms, interpreting
681 * @perms based on @tclass. Returns %0 on success or
682 * -%ENOMEM if insufficient memory exists to add the callback.
684 int avc_add_callback(int (*callback)(u32 event, u32 ssid, u32 tsid,
685 u16 tclass, u32 perms,
687 u32 events, u32 ssid, u32 tsid,
688 u16 tclass, u32 perms)
690 struct avc_callback_node *c;
693 c = kmalloc(sizeof(*c), GFP_ATOMIC);
699 c->callback = callback;
704 c->next = avc_callbacks;
710 static inline int avc_sidcmp(u32 x, u32 y)
712 return (x == y || x == SECSID_WILD || y == SECSID_WILD);
716 * avc_update_node Update an AVC entry
717 * @event : Updating event
718 * @perms : Permission mask bits
719 * @ssid,@tsid,@tclass : identifier of an AVC entry
721 * if a valid AVC entry doesn't exist,this function returns -ENOENT.
722 * if kmalloc() called internal returns NULL, this function returns -ENOMEM.
723 * otherwise, this function update the AVC entry. The original AVC-entry object
724 * will release later by RCU.
726 static int avc_update_node(u32 event, u32 perms, u32 ssid, u32 tsid, u16 tclass)
730 struct avc_node *pos, *node, *orig = NULL;
732 node = avc_alloc_node();
738 /* Lock the target slot */
739 hvalue = avc_hash(ssid, tsid, tclass);
740 spin_lock_irqsave(&avc_cache.slots_lock[hvalue], flag);
742 list_for_each_entry(pos, &avc_cache.slots[hvalue], list){
743 if ( ssid==pos->ae.ssid &&
744 tsid==pos->ae.tsid &&
745 tclass==pos->ae.tclass ){
758 * Copy and replace original node.
761 avc_node_populate(node, ssid, tsid, tclass, &orig->ae);
764 case AVC_CALLBACK_GRANT:
765 node->ae.avd.allowed |= perms;
767 case AVC_CALLBACK_TRY_REVOKE:
768 case AVC_CALLBACK_REVOKE:
769 node->ae.avd.allowed &= ~perms;
771 case AVC_CALLBACK_AUDITALLOW_ENABLE:
772 node->ae.avd.auditallow |= perms;
774 case AVC_CALLBACK_AUDITALLOW_DISABLE:
775 node->ae.avd.auditallow &= ~perms;
777 case AVC_CALLBACK_AUDITDENY_ENABLE:
778 node->ae.avd.auditdeny |= perms;
780 case AVC_CALLBACK_AUDITDENY_DISABLE:
781 node->ae.avd.auditdeny &= ~perms;
784 avc_node_replace(node, orig);
786 spin_unlock_irqrestore(&avc_cache.slots_lock[hvalue], flag);
791 static int avc_update_cache(u32 event, u32 ssid, u32 tsid,
792 u16 tclass, u32 perms)
794 struct avc_node *node;
799 if (ssid == SECSID_WILD || tsid == SECSID_WILD) {
800 /* apply to all matching nodes */
801 for (i = 0; i < AVC_CACHE_SLOTS; i++) {
802 list_for_each_entry_rcu(node, &avc_cache.slots[i], list) {
803 if (avc_sidcmp(ssid, node->ae.ssid) &&
804 avc_sidcmp(tsid, node->ae.tsid) &&
805 tclass == node->ae.tclass ) {
806 avc_update_node(event, perms, node->ae.ssid,
807 node->ae.tsid, node->ae.tclass);
812 /* apply to one node */
813 avc_update_node(event, perms, ssid, tsid, tclass);
821 static int avc_control(u32 event, u32 ssid, u32 tsid,
822 u16 tclass, u32 perms,
823 u32 seqno, u32 *out_retained)
825 struct avc_callback_node *c;
826 u32 tretained = 0, cretained = 0;
830 * try_revoke only removes permissions from the cache
831 * state if they are not retained by the object manager.
832 * Hence, try_revoke must wait until after the callbacks have
833 * been invoked to update the cache state.
835 if (event != AVC_CALLBACK_TRY_REVOKE)
836 avc_update_cache(event,ssid,tsid,tclass,perms);
838 for (c = avc_callbacks; c; c = c->next)
840 if ((c->events & event) &&
841 avc_sidcmp(c->ssid, ssid) &&
842 avc_sidcmp(c->tsid, tsid) &&
843 c->tclass == tclass &&
844 (c->perms & perms)) {
846 rc = c->callback(event, ssid, tsid, tclass,
851 tretained |= cretained;
855 if (event == AVC_CALLBACK_TRY_REVOKE) {
856 /* revoke any unretained permissions */
858 avc_update_cache(event,ssid,tsid,tclass,perms);
859 *out_retained = tretained;
862 avc_latest_notif_update(seqno, 0);
869 * avc_ss_grant - Grant previously denied permissions.
870 * @ssid: source security identifier or %SECSID_WILD
871 * @tsid: target security identifier or %SECSID_WILD
872 * @tclass: target security class
873 * @perms: permissions to grant
874 * @seqno: policy sequence number
876 int avc_ss_grant(u32 ssid, u32 tsid, u16 tclass,
877 u32 perms, u32 seqno)
879 return avc_control(AVC_CALLBACK_GRANT,
880 ssid, tsid, tclass, perms, seqno, NULL);
884 * avc_ss_try_revoke - Try to revoke previously granted permissions.
885 * @ssid: source security identifier or %SECSID_WILD
886 * @tsid: target security identifier or %SECSID_WILD
887 * @tclass: target security class
888 * @perms: permissions to grant
889 * @seqno: policy sequence number
890 * @out_retained: subset of @perms that are retained
892 * Try to revoke previously granted permissions, but
893 * only if they are not retained as migrated permissions.
894 * Return the subset of permissions that are retained via @out_retained.
896 int avc_ss_try_revoke(u32 ssid, u32 tsid, u16 tclass,
897 u32 perms, u32 seqno, u32 *out_retained)
899 return avc_control(AVC_CALLBACK_TRY_REVOKE,
900 ssid, tsid, tclass, perms, seqno, out_retained);
904 * avc_ss_revoke - Revoke previously granted permissions.
905 * @ssid: source security identifier or %SECSID_WILD
906 * @tsid: target security identifier or %SECSID_WILD
907 * @tclass: target security class
908 * @perms: permissions to grant
909 * @seqno: policy sequence number
911 * Revoke previously granted permissions, even if
912 * they are retained as migrated permissions.
914 int avc_ss_revoke(u32 ssid, u32 tsid, u16 tclass,
915 u32 perms, u32 seqno)
917 return avc_control(AVC_CALLBACK_REVOKE,
918 ssid, tsid, tclass, perms, seqno, NULL);
922 * avc_ss_reset - Flush the cache and revalidate migrated permissions.
923 * @seqno: policy sequence number
925 int avc_ss_reset(u32 seqno)
927 struct avc_callback_node *c;
930 struct avc_node *node;
932 for (i = 0; i < AVC_CACHE_SLOTS; i++) {
933 spin_lock_irqsave(&avc_cache.slots_lock[i], flag);
934 list_for_each_entry(node, &avc_cache.slots[i], list)
935 avc_node_delete(node);
936 spin_unlock_irqrestore(&avc_cache.slots_lock[i], flag);
939 for (c = avc_callbacks; c; c = c->next) {
940 if (c->events & AVC_CALLBACK_RESET) {
941 rc = c->callback(AVC_CALLBACK_RESET,
948 avc_latest_notif_update(seqno, 0);
954 * avc_ss_set_auditallow - Enable or disable auditing of granted permissions.
955 * @ssid: source security identifier or %SECSID_WILD
956 * @tsid: target security identifier or %SECSID_WILD
957 * @tclass: target security class
958 * @perms: permissions to grant
959 * @seqno: policy sequence number
960 * @enable: enable flag.
962 int avc_ss_set_auditallow(u32 ssid, u32 tsid, u16 tclass,
963 u32 perms, u32 seqno, u32 enable)
966 return avc_control(AVC_CALLBACK_AUDITALLOW_ENABLE,
967 ssid, tsid, tclass, perms, seqno, NULL);
969 return avc_control(AVC_CALLBACK_AUDITALLOW_DISABLE,
970 ssid, tsid, tclass, perms, seqno, NULL);
974 * avc_ss_set_auditdeny - Enable or disable auditing of denied permissions.
975 * @ssid: source security identifier or %SECSID_WILD
976 * @tsid: target security identifier or %SECSID_WILD
977 * @tclass: target security class
978 * @perms: permissions to grant
979 * @seqno: policy sequence number
980 * @enable: enable flag.
982 int avc_ss_set_auditdeny(u32 ssid, u32 tsid, u16 tclass,
983 u32 perms, u32 seqno, u32 enable)
986 return avc_control(AVC_CALLBACK_AUDITDENY_ENABLE,
987 ssid, tsid, tclass, perms, seqno, NULL);
989 return avc_control(AVC_CALLBACK_AUDITDENY_DISABLE,
990 ssid, tsid, tclass, perms, seqno, NULL);
994 * avc_has_perm_noaudit - Check permissions but perform no auditing.
995 * @ssid: source security identifier
996 * @tsid: target security identifier
997 * @tclass: target security class
998 * @requested: requested permissions, interpreted based on @tclass
999 * @avd: access vector decisions
1001 * Check the AVC to determine whether the @requested permissions are granted
1002 * for the SID pair (@ssid, @tsid), interpreting the permissions
1003 * based on @tclass, and call the security server on a cache miss to obtain
1004 * a new decision and add it to the cache. Return a copy of the decisions
1005 * in @avd. Return %0 if all @requested permissions are granted,
1006 * -%EACCES if any permissions are denied, or another -errno upon
1007 * other errors. This function is typically called by avc_has_perm(),
1008 * but may also be called directly to separate permission checking from
1009 * auditing, e.g. in cases where a lock must be held for the check but
1010 * should be released for the auditing.
1012 int avc_has_perm_noaudit(u32 ssid, u32 tsid,
1013 u16 tclass, u32 requested,
1014 struct av_decision *avd)
1016 struct avc_node *node;
1017 struct avc_entry entry, *p_ae;
1023 node = avc_lookup(ssid, tsid, tclass, requested);
1026 rc = security_compute_av(ssid,tsid,tclass,requested,&entry.avd);
1030 node = avc_insert(ssid,tsid,tclass,&entry);
1033 p_ae = node ? &node->ae : &entry;
1036 memcpy(avd, &p_ae->avd, sizeof(*avd));
1038 denied = requested & ~(p_ae->avd.allowed);
1040 if (!requested || denied) {
1041 if (selinux_enforcing)
1045 avc_update_node(AVC_CALLBACK_GRANT,requested,
1055 * avc_has_perm - Check permissions and perform any appropriate auditing.
1056 * @ssid: source security identifier
1057 * @tsid: target security identifier
1058 * @tclass: target security class
1059 * @requested: requested permissions, interpreted based on @tclass
1060 * @auditdata: auxiliary audit data
1062 * Check the AVC to determine whether the @requested permissions are granted
1063 * for the SID pair (@ssid, @tsid), interpreting the permissions
1064 * based on @tclass, and call the security server on a cache miss to obtain
1065 * a new decision and add it to the cache. Audit the granting or denial of
1066 * permissions in accordance with the policy. Return %0 if all @requested
1067 * permissions are granted, -%EACCES if any permissions are denied, or
1068 * another -errno upon other errors.
1070 int avc_has_perm(u32 ssid, u32 tsid, u16 tclass,
1071 u32 requested, struct avc_audit_data *auditdata)
1073 struct av_decision avd;
1076 rc = avc_has_perm_noaudit(ssid, tsid, tclass, requested, &avd);
1077 avc_audit(ssid, tsid, tclass, requested, &avd, rc, auditdata);