ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / security / selinux / avc.c
1 /*
2  * Implementation of the kernel access vector cache (AVC).
3  *
4  * Authors:  Stephen Smalley, <sds@epoch.ncsc.mil>
5  *           James Morris <jmorris@redhat.com>
6  *
7  * Copyright (C) 2003 Red Hat, Inc., James Morris <jmorris@redhat.com>
8  *
9  *      This program is free software; you can redistribute it and/or modify
10  *      it under the terms of the GNU General Public License version 2,
11  *      as published by the Free Software Foundation.
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 <net/sock.h>
22 #include <linux/un.h>
23 #include <net/af_unix.h>
24 #include <linux/ip.h>
25 #include <linux/audit.h>
26 #include <linux/ipv6.h>
27 #include <net/ipv6.h>
28 #include "avc.h"
29 #include "avc_ss.h"
30 #ifdef CONFIG_AUDIT
31 #include "class_to_string.h"
32 #endif
33 #include "common_perm_to_string.h"
34 #include "av_inherit.h"
35 #include "av_perm_to_string.h"
36 #include "objsec.h"
37
38 #define AVC_CACHE_SLOTS         512
39 #define AVC_CACHE_MAXNODES      410
40
41 struct avc_entry {
42         u32                     ssid;
43         u32                     tsid;
44         u16                     tclass;
45         struct av_decision      avd;
46         int                     used;   /* used recently */
47 };
48
49 struct avc_node {
50         struct avc_entry        ae;
51         struct avc_node         *next;
52 };
53
54 struct avc_cache {
55         struct avc_node *slots[AVC_CACHE_SLOTS];
56         u32             lru_hint;       /* LRU hint for reclaim scan */
57         u32             active_nodes;
58         u32             latest_notif;   /* latest revocation notification */
59 };
60
61 struct avc_callback_node {
62         int (*callback) (u32 event, u32 ssid, u32 tsid,
63                          u16 tclass, u32 perms,
64                          u32 *out_retained);
65         u32 events;
66         u32 ssid;
67         u32 tsid;
68         u16 tclass;
69         u32 perms;
70         struct avc_callback_node *next;
71 };
72
73 static spinlock_t avc_lock = SPIN_LOCK_UNLOCKED;
74 static struct avc_node *avc_node_freelist = NULL;
75 static struct avc_cache avc_cache;
76 static unsigned avc_cache_stats[AVC_NSTATS];
77 static struct avc_callback_node *avc_callbacks = NULL;
78
79 static inline int avc_hash(u32 ssid, u32 tsid, u16 tclass)
80 {
81         return (ssid ^ (tsid<<2) ^ (tclass<<4)) & (AVC_CACHE_SLOTS - 1);
82 }
83
84 /**
85  * avc_dump_av - Display an access vector in human-readable form.
86  * @tclass: target security class
87  * @av: access vector
88  */
89 void avc_dump_av(struct audit_buffer *ab, u16 tclass, u32 av)
90 {
91         char **common_pts = 0;
92         u32 common_base = 0;
93         int i, i2, perm;
94
95         if (av == 0) {
96                 audit_log_format(ab, " null");
97                 return;
98         }
99
100         for (i = 0; i < ARRAY_SIZE(av_inherit); i++) {
101                 if (av_inherit[i].tclass == tclass) {
102                         common_pts = av_inherit[i].common_pts;
103                         common_base = av_inherit[i].common_base;
104                         break;
105                 }
106         }
107
108         audit_log_format(ab, " {");
109         i = 0;
110         perm = 1;
111         while (perm < common_base) {
112                 if (perm & av)
113                         audit_log_format(ab, " %s", common_pts[i]);
114                 i++;
115                 perm <<= 1;
116         }
117
118         while (i < sizeof(av) * 8) {
119                 if (perm & av) {
120                         for (i2 = 0; i2 < ARRAY_SIZE(av_perm_to_string); i2++) {
121                                 if ((av_perm_to_string[i2].tclass == tclass) &&
122                                     (av_perm_to_string[i2].value == perm))
123                                         break;
124                         }
125                         if (i2 < ARRAY_SIZE(av_perm_to_string))
126                                 audit_log_format(ab, " %s",
127                                                  av_perm_to_string[i2].name);
128                 }
129                 i++;
130                 perm <<= 1;
131         }
132
133         audit_log_format(ab, " }");
134 }
135
136 /**
137  * avc_dump_query - Display a SID pair and a class in human-readable form.
138  * @ssid: source security identifier
139  * @tsid: target security identifier
140  * @tclass: target security class
141  */
142 void avc_dump_query(struct audit_buffer *ab, u32 ssid, u32 tsid, u16 tclass)
143 {
144         int rc;
145         char *scontext;
146         u32 scontext_len;
147
148         rc = security_sid_to_context(ssid, &scontext, &scontext_len);
149         if (rc)
150                 audit_log_format(ab, "ssid=%d", ssid);
151         else {
152                 audit_log_format(ab, "scontext=%s", scontext);
153                 kfree(scontext);
154         }
155
156         rc = security_sid_to_context(tsid, &scontext, &scontext_len);
157         if (rc)
158                 audit_log_format(ab, " tsid=%d", tsid);
159         else {
160                 audit_log_format(ab, " tcontext=%s", scontext);
161                 kfree(scontext);
162         }
163         audit_log_format(ab, " tclass=%s", class_to_string[tclass]);
164 }
165
166 /**
167  * avc_init - Initialize the AVC.
168  *
169  * Initialize the access vector cache.
170  */
171 void __init avc_init(void)
172 {
173         struct avc_node *new;
174         int i;
175
176         for (i = 0; i < AVC_NSTATS; i++)
177                 avc_cache_stats[i] = 0;
178
179         for (i = 0; i < AVC_CACHE_SLOTS; i++)
180                 avc_cache.slots[i] = 0;
181         avc_cache.lru_hint = 0;
182         avc_cache.active_nodes = 0;
183         avc_cache.latest_notif = 0;
184
185         for (i = 0; i < AVC_CACHE_MAXNODES; i++) {
186                 new = kmalloc(sizeof(*new), GFP_ATOMIC);
187                 if (!new) {
188                         printk(KERN_WARNING "avc:  only able to allocate "
189                                "%d entries\n", i);
190                         break;
191                 }
192                 memset(new, 0, sizeof(*new));
193                 new->next = avc_node_freelist;
194                 avc_node_freelist = new;
195         }
196
197         audit_log(current->audit_context, "AVC INITIALIZED\n");
198 }
199
200 #if 0
201 static void avc_hash_eval(char *tag)
202 {
203         int i, chain_len, max_chain_len, slots_used;
204         struct avc_node *node;
205         unsigned long flags;
206
207         spin_lock_irqsave(&avc_lock,flags);
208
209         slots_used = 0;
210         max_chain_len = 0;
211         for (i = 0; i < AVC_CACHE_SLOTS; i++) {
212                 node = avc_cache.slots[i];
213                 if (node) {
214                         slots_used++;
215                         chain_len = 0;
216                         while (node) {
217                                 chain_len++;
218                                 node = node->next;
219                         }
220                         if (chain_len > max_chain_len)
221                                 max_chain_len = chain_len;
222                 }
223         }
224
225         spin_unlock_irqrestore(&avc_lock,flags);
226
227         printk(KERN_INFO "\n");
228         printk(KERN_INFO "%s avc:  %d entries and %d/%d buckets used, longest "
229                "chain length %d\n", tag, avc_cache.active_nodes, slots_used,
230                AVC_CACHE_SLOTS, max_chain_len);
231 }
232 #else
233 static inline void avc_hash_eval(char *tag)
234 { }
235 #endif
236
237 static inline struct avc_node *avc_reclaim_node(void)
238 {
239         struct avc_node *prev, *cur;
240         int hvalue, try;
241
242         hvalue = avc_cache.lru_hint;
243         for (try = 0; try < 2; try++) {
244                 do {
245                         prev = NULL;
246                         cur = avc_cache.slots[hvalue];
247                         while (cur) {
248                                 if (!cur->ae.used)
249                                         goto found;
250
251                                 cur->ae.used = 0;
252
253                                 prev = cur;
254                                 cur = cur->next;
255                         }
256                         hvalue = (hvalue + 1) & (AVC_CACHE_SLOTS - 1);
257                 } while (hvalue != avc_cache.lru_hint);
258         }
259
260         panic("avc_reclaim_node");
261
262 found:
263         avc_cache.lru_hint = hvalue;
264
265         if (prev == NULL)
266                 avc_cache.slots[hvalue] = cur->next;
267         else
268                 prev->next = cur->next;
269
270         return cur;
271 }
272
273 static inline struct avc_node *avc_claim_node(u32 ssid,
274                                               u32 tsid, u16 tclass)
275 {
276         struct avc_node *new;
277         int hvalue;
278
279         hvalue = avc_hash(ssid, tsid, tclass);
280         if (avc_node_freelist) {
281                 new = avc_node_freelist;
282                 avc_node_freelist = avc_node_freelist->next;
283                 avc_cache.active_nodes++;
284         } else {
285                 new = avc_reclaim_node();
286                 if (!new)
287                         goto out;
288         }
289
290         new->ae.used = 1;
291         new->ae.ssid = ssid;
292         new->ae.tsid = tsid;
293         new->ae.tclass = tclass;
294         new->next = avc_cache.slots[hvalue];
295         avc_cache.slots[hvalue] = new;
296
297 out:
298         return new;
299 }
300
301 static inline struct avc_node *avc_search_node(u32 ssid, u32 tsid,
302                                                u16 tclass, int *probes)
303 {
304         struct avc_node *cur;
305         int hvalue;
306         int tprobes = 1;
307
308         hvalue = avc_hash(ssid, tsid, tclass);
309         cur = avc_cache.slots[hvalue];
310         while (cur != NULL &&
311                (ssid != cur->ae.ssid ||
312                 tclass != cur->ae.tclass ||
313                 tsid != cur->ae.tsid)) {
314                 tprobes++;
315                 cur = cur->next;
316         }
317
318         if (cur == NULL) {
319                 /* cache miss */
320                 goto out;
321         }
322
323         /* cache hit */
324         if (probes)
325                 *probes = tprobes;
326
327         cur->ae.used = 1;
328
329 out:
330         return cur;
331 }
332
333 /**
334  * avc_lookup - Look up an AVC entry.
335  * @ssid: source security identifier
336  * @tsid: target security identifier
337  * @tclass: target security class
338  * @requested: requested permissions, interpreted based on @tclass
339  * @aeref:  AVC entry reference
340  *
341  * Look up an AVC entry that is valid for the
342  * @requested permissions between the SID pair
343  * (@ssid, @tsid), interpreting the permissions
344  * based on @tclass.  If a valid AVC entry exists,
345  * then this function updates @aeref to refer to the
346  * entry and returns %0. Otherwise, this function
347  * returns -%ENOENT.
348  */
349 int avc_lookup(u32 ssid, u32 tsid, u16 tclass,
350                u32 requested, struct avc_entry_ref *aeref)
351 {
352         struct avc_node *node;
353         int probes, rc = 0;
354
355         avc_cache_stats_incr(AVC_CAV_LOOKUPS);
356         node = avc_search_node(ssid, tsid, tclass,&probes);
357
358         if (node && ((node->ae.avd.decided & requested) == requested)) {
359                 avc_cache_stats_incr(AVC_CAV_HITS);
360                 avc_cache_stats_add(AVC_CAV_PROBES,probes);
361                 aeref->ae = &node->ae;
362                 goto out;
363         }
364
365         avc_cache_stats_incr(AVC_CAV_MISSES);
366         rc = -ENOENT;
367 out:
368         return rc;
369 }
370
371 /**
372  * avc_insert - Insert an AVC entry.
373  * @ssid: source security identifier
374  * @tsid: target security identifier
375  * @tclass: target security class
376  * @ae: AVC entry
377  * @aeref:  AVC entry reference
378  *
379  * Insert an AVC entry for the SID pair
380  * (@ssid, @tsid) and class @tclass.
381  * The access vectors and the sequence number are
382  * normally provided by the security server in
383  * response to a security_compute_av() call.  If the
384  * sequence number @ae->avd.seqno is not less than the latest
385  * revocation notification, then the function copies
386  * the access vectors into a cache entry, updates
387  * @aeref to refer to the entry, and returns %0.
388  * Otherwise, this function returns -%EAGAIN.
389  */
390 int avc_insert(u32 ssid, u32 tsid, u16 tclass,
391                struct avc_entry *ae, struct avc_entry_ref *aeref)
392 {
393         struct avc_node *node;
394         int rc = 0;
395
396         if (ae->avd.seqno < avc_cache.latest_notif) {
397                 printk(KERN_WARNING "avc:  seqno %d < latest_notif %d\n",
398                        ae->avd.seqno, avc_cache.latest_notif);
399                 rc = -EAGAIN;
400                 goto out;
401         }
402
403         node = avc_claim_node(ssid, tsid, tclass);
404         if (!node) {
405                 rc = -ENOMEM;
406                 goto out;
407         }
408
409         node->ae.avd.allowed = ae->avd.allowed;
410         node->ae.avd.decided = ae->avd.decided;
411         node->ae.avd.auditallow = ae->avd.auditallow;
412         node->ae.avd.auditdeny = ae->avd.auditdeny;
413         node->ae.avd.seqno = ae->avd.seqno;
414         aeref->ae = &node->ae;
415 out:
416         return rc;
417 }
418
419 static inline void avc_print_ipv6_addr(struct audit_buffer *ab,
420                                        struct in6_addr *addr, u16 port,
421                                        char *name1, char *name2)
422 {
423         if (!ipv6_addr_any(addr))
424                 audit_log_format(ab, " %s=%04x:%04x:%04x:%04x:%04x:"
425                                  "%04x:%04x:%04x", name1, NIP6(*addr));
426         if (port)
427                 audit_log_format(ab, " %s=%d", name2, ntohs(port));
428 }
429
430 static inline void avc_print_ipv4_addr(struct audit_buffer *ab, u32 addr,
431                                        u16 port, char *name1, char *name2)
432 {
433         if (addr)
434                 audit_log_format(ab, " %s=%d.%d.%d.%d", name1, NIPQUAD(addr));
435         if (port)
436                 audit_log_format(ab, " %s=%d", name2, ntohs(port));
437 }
438
439 /**
440  * avc_audit - Audit the granting or denial of permissions.
441  * @ssid: source security identifier
442  * @tsid: target security identifier
443  * @tclass: target security class
444  * @requested: requested permissions
445  * @avd: access vector decisions
446  * @result: result from avc_has_perm_noaudit
447  * @a:  auxiliary audit data
448  *
449  * Audit the granting or denial of permissions in accordance
450  * with the policy.  This function is typically called by
451  * avc_has_perm() after a permission check, but can also be
452  * called directly by callers who use avc_has_perm_noaudit()
453  * in order to separate the permission check from the auditing.
454  * For example, this separation is useful when the permission check must
455  * be performed under a lock, to allow the lock to be released
456  * before calling the auditing code.
457  */
458 void avc_audit(u32 ssid, u32 tsid,
459                u16 tclass, u32 requested,
460                struct av_decision *avd, int result, struct avc_audit_data *a)
461 {
462         struct task_struct *tsk = current;
463         struct inode *inode = NULL;
464         u32 denied, audited;
465         struct audit_buffer *ab;
466
467         denied = requested & ~avd->allowed;
468         if (denied) {
469                 audited = denied;
470                 if (!(audited & avd->auditdeny))
471                         return;
472         } else if (result) {
473                 audited = denied = requested;
474         } else {
475                 audited = requested;
476                 if (!(audited & avd->auditallow))
477                         return;
478         }
479
480         ab = audit_log_start(current->audit_context);
481         if (!ab)
482                 return;         /* audit_panic has been called */
483         audit_log_format(ab, "avc:  %s ", denied ? "denied" : "granted");
484         avc_dump_av(ab, tclass,audited);
485         audit_log_format(ab, " for ");
486         if (a && a->tsk)
487                 tsk = a->tsk;
488         if (tsk && tsk->pid) {
489                 struct mm_struct *mm;
490                 struct vm_area_struct *vma;
491                 audit_log_format(ab, " pid=%d", tsk->pid);
492                 if (tsk == current)
493                         mm = current->mm;
494                 else
495                         mm = get_task_mm(tsk);
496                 if (mm) {
497                         if (down_read_trylock(&mm->mmap_sem)) {
498                                 vma = mm->mmap;
499                                 while (vma) {
500                                         if ((vma->vm_flags & VM_EXECUTABLE) &&
501                                             vma->vm_file) {
502                                                 audit_log_d_path(ab, "exe=",
503                                                         vma->vm_file->f_dentry,
504                                                         vma->vm_file->f_vfsmnt);
505                                                 break;
506                                         }
507                                         vma = vma->vm_next;
508                                 }
509                                 up_read(&mm->mmap_sem);
510                         }
511                         if (tsk != current)
512                                 mmput(mm);
513                 } else {
514                         audit_log_format(ab, " comm=%s", tsk->comm);
515                 }
516         }
517         if (a) {
518                 switch (a->type) {
519                 case AVC_AUDIT_DATA_IPC:
520                         audit_log_format(ab, " key=%d", a->u.ipc_id);
521                         break;
522                 case AVC_AUDIT_DATA_CAP:
523                         audit_log_format(ab, " capability=%d", a->u.cap);
524                         break;
525                 case AVC_AUDIT_DATA_FS:
526                         if (a->u.fs.dentry) {
527                                 struct dentry *dentry = a->u.fs.dentry;
528                                 if (a->u.fs.mnt) {
529                                         audit_log_d_path(ab, "path=", dentry,
530                                                         a->u.fs.mnt);
531                                 } else {
532                                         audit_log_format(ab, " name=%s",
533                                                          dentry->d_name.name);
534                                 }
535                                 inode = dentry->d_inode;
536                         } else if (a->u.fs.inode) {
537                                 struct dentry *dentry;
538                                 inode = a->u.fs.inode;
539                                 dentry = d_find_alias(inode);
540                                 if (dentry) {
541                                         audit_log_format(ab, " name=%s",
542                                                          dentry->d_name.name);
543                                         dput(dentry);
544                                 }
545                         }
546                         if (inode)
547                                 audit_log_format(ab, " dev=%s ino=%ld",
548                                                  inode->i_sb->s_id,
549                                                  inode->i_ino);
550                         break;
551                 case AVC_AUDIT_DATA_NET:
552                         if (a->u.net.sk) {
553                                 struct sock *sk = a->u.net.sk;
554                                 struct unix_sock *u;
555                                 int len = 0;
556                                 char *p = NULL;
557
558                                 switch (sk->sk_family) {
559                                 case AF_INET: {
560                                         struct inet_opt *inet = inet_sk(sk);
561
562                                         avc_print_ipv4_addr(ab, inet->rcv_saddr,
563                                                             inet->sport,
564                                                             "laddr", "lport");
565                                         avc_print_ipv4_addr(ab, inet->daddr,
566                                                             inet->dport,
567                                                             "faddr", "fport");
568                                         break;
569                                 }
570                                 case AF_INET6: {
571                                         struct inet_opt *inet = inet_sk(sk);
572                                         struct ipv6_pinfo *inet6 = inet6_sk(sk);
573
574                                         avc_print_ipv6_addr(ab, &inet6->rcv_saddr,
575                                                             inet->sport,
576                                                             "laddr", "lport");
577                                         avc_print_ipv6_addr(ab, &inet6->daddr,
578                                                             inet->dport,
579                                                             "faddr", "fport");
580                                         break;
581                                 }
582                                 case AF_UNIX:
583                                         u = unix_sk(sk);
584                                         if (u->dentry) {
585                                                 audit_log_d_path(ab, "path=",
586                                                         u->dentry, u->mnt);
587                                                 break;
588                                         }
589                                         if (!u->addr)
590                                                 break;
591                                         len = u->addr->len-sizeof(short);
592                                         p = &u->addr->name->sun_path[0];
593                                         if (*p)
594                                                 audit_log_format(ab,
595                                                         "path=%*.*s", len,
596                                                         len, p);
597                                         else
598                                                 audit_log_format(ab,
599                                                         "path=@%*.*s", len-1,
600                                                         len-1, p+1);
601                                         break;
602                                 }
603                         }
604                         
605                         switch (a->u.net.family) {
606                         case AF_INET:
607                                 avc_print_ipv4_addr(ab, a->u.net.v4info.saddr,
608                                                     a->u.net.sport,
609                                                     "saddr", "src");
610                                 avc_print_ipv4_addr(ab, a->u.net.v4info.daddr,
611                                                     a->u.net.dport,
612                                                     "daddr", "dest");
613                                 break;
614                         case AF_INET6:
615                                 avc_print_ipv6_addr(ab, &a->u.net.v6info.saddr,
616                                                     a->u.net.sport,
617                                                     "saddr", "src");
618                                 avc_print_ipv6_addr(ab, &a->u.net.v6info.daddr,
619                                                     a->u.net.dport,
620                                                     "daddr", "dest");
621                                 break;
622                         }
623                         if (a->u.net.netif)
624                                 audit_log_format(ab, " netif=%s",
625                                         a->u.net.netif);
626                         break;
627                 }
628         }
629         audit_log_format(ab, " ");
630         avc_dump_query(ab, ssid, tsid, tclass);
631         audit_log_end(ab);
632 }
633
634 /**
635  * avc_add_callback - Register a callback for security events.
636  * @callback: callback function
637  * @events: security events
638  * @ssid: source security identifier or %SECSID_WILD
639  * @tsid: target security identifier or %SECSID_WILD
640  * @tclass: target security class
641  * @perms: permissions
642  *
643  * Register a callback function for events in the set @events
644  * related to the SID pair (@ssid, @tsid) and
645  * and the permissions @perms, interpreting
646  * @perms based on @tclass.  Returns %0 on success or
647  * -%ENOMEM if insufficient memory exists to add the callback.
648  */
649 int avc_add_callback(int (*callback)(u32 event, u32 ssid, u32 tsid,
650                                      u16 tclass, u32 perms,
651                                      u32 *out_retained),
652                      u32 events, u32 ssid, u32 tsid,
653                      u16 tclass, u32 perms)
654 {
655         struct avc_callback_node *c;
656         int rc = 0;
657
658         c = kmalloc(sizeof(*c), GFP_ATOMIC);
659         if (!c) {
660                 rc = -ENOMEM;
661                 goto out;
662         }
663
664         c->callback = callback;
665         c->events = events;
666         c->ssid = ssid;
667         c->tsid = tsid;
668         c->perms = perms;
669         c->next = avc_callbacks;
670         avc_callbacks = c;
671 out:
672         return rc;
673 }
674
675 static inline int avc_sidcmp(u32 x, u32 y)
676 {
677         return (x == y || x == SECSID_WILD || y == SECSID_WILD);
678 }
679
680 static inline void avc_update_node(u32 event, struct avc_node *node, u32 perms)
681 {
682         switch (event) {
683         case AVC_CALLBACK_GRANT:
684                 node->ae.avd.allowed |= perms;
685                 break;
686         case AVC_CALLBACK_TRY_REVOKE:
687         case AVC_CALLBACK_REVOKE:
688                 node->ae.avd.allowed &= ~perms;
689                 break;
690         case AVC_CALLBACK_AUDITALLOW_ENABLE:
691                 node->ae.avd.auditallow |= perms;
692                 break;
693         case AVC_CALLBACK_AUDITALLOW_DISABLE:
694                 node->ae.avd.auditallow &= ~perms;
695                 break;
696         case AVC_CALLBACK_AUDITDENY_ENABLE:
697                 node->ae.avd.auditdeny |= perms;
698                 break;
699         case AVC_CALLBACK_AUDITDENY_DISABLE:
700                 node->ae.avd.auditdeny &= ~perms;
701                 break;
702         }
703 }
704
705 static int avc_update_cache(u32 event, u32 ssid, u32 tsid,
706                             u16 tclass, u32 perms)
707 {
708         struct avc_node *node;
709         int i;
710         unsigned long flags;
711
712         spin_lock_irqsave(&avc_lock,flags);
713
714         if (ssid == SECSID_WILD || tsid == SECSID_WILD) {
715                 /* apply to all matching nodes */
716                 for (i = 0; i < AVC_CACHE_SLOTS; i++) {
717                         for (node = avc_cache.slots[i]; node;
718                              node = node->next) {
719                                 if (avc_sidcmp(ssid, node->ae.ssid) &&
720                                     avc_sidcmp(tsid, node->ae.tsid) &&
721                                     tclass == node->ae.tclass) {
722                                         avc_update_node(event,node,perms);
723                                 }
724                         }
725                 }
726         } else {
727                 /* apply to one node */
728                 node = avc_search_node(ssid, tsid, tclass, 0);
729                 if (node) {
730                         avc_update_node(event,node,perms);
731                 }
732         }
733
734         spin_unlock_irqrestore(&avc_lock,flags);
735
736         return 0;
737 }
738
739 static int avc_control(u32 event, u32 ssid, u32 tsid,
740                        u16 tclass, u32 perms,
741                        u32 seqno, u32 *out_retained)
742 {
743         struct avc_callback_node *c;
744         u32 tretained = 0, cretained = 0;
745         int rc = 0;
746         unsigned long flags;
747
748         /*
749          * try_revoke only removes permissions from the cache
750          * state if they are not retained by the object manager.
751          * Hence, try_revoke must wait until after the callbacks have
752          * been invoked to update the cache state.
753          */
754         if (event != AVC_CALLBACK_TRY_REVOKE)
755                 avc_update_cache(event,ssid,tsid,tclass,perms);
756
757         for (c = avc_callbacks; c; c = c->next)
758         {
759                 if ((c->events & event) &&
760                     avc_sidcmp(c->ssid, ssid) &&
761                     avc_sidcmp(c->tsid, tsid) &&
762                     c->tclass == tclass &&
763                     (c->perms & perms)) {
764                         cretained = 0;
765                         rc = c->callback(event, ssid, tsid, tclass,
766                                          (c->perms & perms),
767                                          &cretained);
768                         if (rc)
769                                 goto out;
770                         tretained |= cretained;
771                 }
772         }
773
774         if (event == AVC_CALLBACK_TRY_REVOKE) {
775                 /* revoke any unretained permissions */
776                 perms &= ~tretained;
777                 avc_update_cache(event,ssid,tsid,tclass,perms);
778                 *out_retained = tretained;
779         }
780
781         spin_lock_irqsave(&avc_lock,flags);
782         if (seqno > avc_cache.latest_notif)
783                 avc_cache.latest_notif = seqno;
784         spin_unlock_irqrestore(&avc_lock,flags);
785
786 out:
787         return rc;
788 }
789
790 /**
791  * avc_ss_grant - Grant previously denied permissions.
792  * @ssid: source security identifier or %SECSID_WILD
793  * @tsid: target security identifier or %SECSID_WILD
794  * @tclass: target security class
795  * @perms: permissions to grant
796  * @seqno: policy sequence number
797  */
798 int avc_ss_grant(u32 ssid, u32 tsid, u16 tclass,
799                  u32 perms, u32 seqno)
800 {
801         return avc_control(AVC_CALLBACK_GRANT,
802                            ssid, tsid, tclass, perms, seqno, 0);
803 }
804
805 /**
806  * avc_ss_try_revoke - Try to revoke previously granted permissions.
807  * @ssid: source security identifier or %SECSID_WILD
808  * @tsid: target security identifier or %SECSID_WILD
809  * @tclass: target security class
810  * @perms: permissions to grant
811  * @seqno: policy sequence number
812  * @out_retained: subset of @perms that are retained
813  *
814  * Try to revoke previously granted permissions, but
815  * only if they are not retained as migrated permissions.
816  * Return the subset of permissions that are retained via @out_retained.
817  */
818 int avc_ss_try_revoke(u32 ssid, u32 tsid, u16 tclass,
819                       u32 perms, u32 seqno, u32 *out_retained)
820 {
821         return avc_control(AVC_CALLBACK_TRY_REVOKE,
822                            ssid, tsid, tclass, perms, seqno, out_retained);
823 }
824
825 /**
826  * avc_ss_revoke - Revoke previously granted permissions.
827  * @ssid: source security identifier or %SECSID_WILD
828  * @tsid: target security identifier or %SECSID_WILD
829  * @tclass: target security class
830  * @perms: permissions to grant
831  * @seqno: policy sequence number
832  *
833  * Revoke previously granted permissions, even if
834  * they are retained as migrated permissions.
835  */
836 int avc_ss_revoke(u32 ssid, u32 tsid, u16 tclass,
837                   u32 perms, u32 seqno)
838 {
839         return avc_control(AVC_CALLBACK_REVOKE,
840                            ssid, tsid, tclass, perms, seqno, 0);
841 }
842
843 /**
844  * avc_ss_reset - Flush the cache and revalidate migrated permissions.
845  * @seqno: policy sequence number
846  */
847 int avc_ss_reset(u32 seqno)
848 {
849         struct avc_callback_node *c;
850         int i, rc = 0;
851         struct avc_node *node, *tmp;
852         unsigned long flags;
853
854         avc_hash_eval("reset");
855
856         spin_lock_irqsave(&avc_lock,flags);
857
858         for (i = 0; i < AVC_CACHE_SLOTS; i++) {
859                 node = avc_cache.slots[i];
860                 while (node) {
861                         tmp = node;
862                         node = node->next;
863                         tmp->ae.ssid = tmp->ae.tsid = SECSID_NULL;
864                         tmp->ae.tclass = SECCLASS_NULL;
865                         tmp->ae.avd.allowed = tmp->ae.avd.decided = 0;
866                         tmp->ae.avd.auditallow = tmp->ae.avd.auditdeny = 0;
867                         tmp->ae.used = 0;
868                         tmp->next = avc_node_freelist;
869                         avc_node_freelist = tmp;
870                         avc_cache.active_nodes--;
871                 }
872                 avc_cache.slots[i] = 0;
873         }
874         avc_cache.lru_hint = 0;
875
876         spin_unlock_irqrestore(&avc_lock,flags);
877
878         for (i = 0; i < AVC_NSTATS; i++)
879                 avc_cache_stats[i] = 0;
880
881         for (c = avc_callbacks; c; c = c->next) {
882                 if (c->events & AVC_CALLBACK_RESET) {
883                         rc = c->callback(AVC_CALLBACK_RESET,
884                                          0, 0, 0, 0, 0);
885                         if (rc)
886                                 goto out;
887                 }
888         }
889
890         spin_lock_irqsave(&avc_lock,flags);
891         if (seqno > avc_cache.latest_notif)
892                 avc_cache.latest_notif = seqno;
893         spin_unlock_irqrestore(&avc_lock,flags);
894 out:
895         return rc;
896 }
897
898 /**
899  * avc_ss_set_auditallow - Enable or disable auditing of granted permissions.
900  * @ssid: source security identifier or %SECSID_WILD
901  * @tsid: target security identifier or %SECSID_WILD
902  * @tclass: target security class
903  * @perms: permissions to grant
904  * @seqno: policy sequence number
905  * @enable: enable flag.
906  */
907 int avc_ss_set_auditallow(u32 ssid, u32 tsid, u16 tclass,
908                           u32 perms, u32 seqno, u32 enable)
909 {
910         if (enable)
911                 return avc_control(AVC_CALLBACK_AUDITALLOW_ENABLE,
912                                    ssid, tsid, tclass, perms, seqno, 0);
913         else
914                 return avc_control(AVC_CALLBACK_AUDITALLOW_DISABLE,
915                                    ssid, tsid, tclass, perms, seqno, 0);
916 }
917
918 /**
919  * avc_ss_set_auditdeny - Enable or disable auditing of denied permissions.
920  * @ssid: source security identifier or %SECSID_WILD
921  * @tsid: target security identifier or %SECSID_WILD
922  * @tclass: target security class
923  * @perms: permissions to grant
924  * @seqno: policy sequence number
925  * @enable: enable flag.
926  */
927 int avc_ss_set_auditdeny(u32 ssid, u32 tsid, u16 tclass,
928                          u32 perms, u32 seqno, u32 enable)
929 {
930         if (enable)
931                 return avc_control(AVC_CALLBACK_AUDITDENY_ENABLE,
932                                    ssid, tsid, tclass, perms, seqno, 0);
933         else
934                 return avc_control(AVC_CALLBACK_AUDITDENY_DISABLE,
935                                    ssid, tsid, tclass, perms, seqno, 0);
936 }
937
938 /**
939  * avc_has_perm_noaudit - Check permissions but perform no auditing.
940  * @ssid: source security identifier
941  * @tsid: target security identifier
942  * @tclass: target security class
943  * @requested: requested permissions, interpreted based on @tclass
944  * @aeref:  AVC entry reference
945  * @avd: access vector decisions
946  *
947  * Check the AVC to determine whether the @requested permissions are granted
948  * for the SID pair (@ssid, @tsid), interpreting the permissions
949  * based on @tclass, and call the security server on a cache miss to obtain
950  * a new decision and add it to the cache.  Update @aeref to refer to an AVC
951  * entry with the resulting decisions, and return a copy of the decisions
952  * in @avd.  Return %0 if all @requested permissions are granted,
953  * -%EACCES if any permissions are denied, or another -errno upon
954  * other errors.  This function is typically called by avc_has_perm(),
955  * but may also be called directly to separate permission checking from
956  * auditing, e.g. in cases where a lock must be held for the check but
957  * should be released for the auditing.
958  */
959 int avc_has_perm_noaudit(u32 ssid, u32 tsid,
960                          u16 tclass, u32 requested,
961                          struct avc_entry_ref *aeref, struct av_decision *avd)
962 {
963         struct avc_entry *ae;
964         int rc = 0;
965         unsigned long flags;
966         struct avc_entry entry;
967         u32 denied;
968         struct avc_entry_ref ref;
969
970         if (!aeref) {
971                 avc_entry_ref_init(&ref);
972                 aeref = &ref;
973         }
974
975         spin_lock_irqsave(&avc_lock, flags);
976         avc_cache_stats_incr(AVC_ENTRY_LOOKUPS);
977         ae = aeref->ae;
978         if (ae) {
979                 if (ae->ssid == ssid &&
980                     ae->tsid == tsid &&
981                     ae->tclass == tclass &&
982                     ((ae->avd.decided & requested) == requested)) {
983                         avc_cache_stats_incr(AVC_ENTRY_HITS);
984                         ae->used = 1;
985                 } else {
986                         avc_cache_stats_incr(AVC_ENTRY_DISCARDS);
987                         ae = 0;
988                 }
989         }
990
991         if (!ae) {
992                 avc_cache_stats_incr(AVC_ENTRY_MISSES);
993                 rc = avc_lookup(ssid, tsid, tclass, requested, aeref);
994                 if (rc) {
995                         spin_unlock_irqrestore(&avc_lock,flags);
996                         rc = security_compute_av(ssid,tsid,tclass,requested,&entry.avd);
997                         if (rc)
998                                 goto out;
999                         spin_lock_irqsave(&avc_lock, flags);
1000                         rc = avc_insert(ssid,tsid,tclass,&entry,aeref);
1001                         if (rc) {
1002                                 spin_unlock_irqrestore(&avc_lock,flags);
1003                                 goto out;
1004                         }
1005                 }
1006                 ae = aeref->ae;
1007         }
1008
1009         if (avd)
1010                 memcpy(avd, &ae->avd, sizeof(*avd));
1011
1012         denied = requested & ~(ae->avd.allowed);
1013
1014         if (!requested || denied) {
1015                 if (selinux_enforcing) {
1016                         spin_unlock_irqrestore(&avc_lock,flags);
1017                         rc = -EACCES;
1018                         goto out;
1019                 } else {
1020                         ae->avd.allowed |= requested;
1021                         spin_unlock_irqrestore(&avc_lock,flags);
1022                         goto out;
1023                 }
1024         }
1025
1026         spin_unlock_irqrestore(&avc_lock,flags);
1027 out:
1028         return rc;
1029 }
1030
1031 /**
1032  * avc_has_perm - Check permissions and perform any appropriate auditing.
1033  * @ssid: source security identifier
1034  * @tsid: target security identifier
1035  * @tclass: target security class
1036  * @requested: requested permissions, interpreted based on @tclass
1037  * @aeref:  AVC entry reference
1038  * @auditdata: auxiliary audit data
1039  *
1040  * Check the AVC to determine whether the @requested permissions are granted
1041  * for the SID pair (@ssid, @tsid), interpreting the permissions
1042  * based on @tclass, and call the security server on a cache miss to obtain
1043  * a new decision and add it to the cache.  Update @aeref to refer to an AVC
1044  * entry with the resulting decisions.  Audit the granting or denial of
1045  * permissions in accordance with the policy.  Return %0 if all @requested
1046  * permissions are granted, -%EACCES if any permissions are denied, or
1047  * another -errno upon other errors.
1048  */
1049 int avc_has_perm(u32 ssid, u32 tsid, u16 tclass,
1050                  u32 requested, struct avc_entry_ref *aeref,
1051                  struct avc_audit_data *auditdata)
1052 {
1053         struct av_decision avd;
1054         int rc;
1055
1056         rc = avc_has_perm_noaudit(ssid, tsid, tclass, requested, aeref, &avd);
1057         avc_audit(ssid, tsid, tclass, requested, &avd, rc, auditdata);
1058         return rc;
1059 }