blob: 3662b0f15ec5d59930aaaf03555eab57be377bde [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Implementation of the kernel access vector cache (AVC).
3 *
4 * Authors: Stephen Smalley, <sds@epoch.ncsc.mil>
Eric Paris95fff332008-04-17 14:42:10 -04005 * James Morris <jmorris@redhat.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
7 * Update: KaiGai, Kohei <kaigai@ak.jp.nec.com>
Eric Paris95fff332008-04-17 14:42:10 -04008 * Replaced the avc_lock spinlock by RCU.
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 *
10 * Copyright (C) 2003 Red Hat, Inc., James Morris <jmorris@redhat.com>
11 *
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,
Eric Paris95fff332008-04-17 14:42:10 -040014 * as published by the Free Software Foundation.
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 */
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"
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -040034#include "classmap.h"
Chad Sellers5c458992006-11-06 12:38:16 -050035
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#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
Eric Paris95fff332008-04-17 14:42:10 -040041#define avc_cache_stats_incr(field) \
Linus Torvalds1da177e2005-04-16 15:20:36 -070042do { \
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;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055};
56
57struct avc_node {
58 struct avc_entry ae;
Eric Paris26036652009-02-12 14:51:04 -050059 struct hlist_node list; /* anchored in avc_cache->slots[i] */
Eric Paris95fff332008-04-17 14:42:10 -040060 struct rcu_head rhead;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061};
62
63struct avc_cache {
Eric Paris26036652009-02-12 14:51:04 -050064 struct hlist_head slots[AVC_CACHE_SLOTS]; /* head for avc_node->list */
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 spinlock_t slots_lock[AVC_CACHE_SLOTS]; /* lock for writes */
66 atomic_t lru_hint; /* LRU hint for reclaim scan */
67 atomic_t active_nodes;
68 u32 latest_notif; /* latest revocation notification */
69};
70
71struct avc_callback_node {
72 int (*callback) (u32 event, u32 ssid, u32 tsid,
Eric Paris95fff332008-04-17 14:42:10 -040073 u16 tclass, u32 perms,
74 u32 *out_retained);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 u32 events;
76 u32 ssid;
77 u32 tsid;
78 u16 tclass;
79 u32 perms;
80 struct avc_callback_node *next;
81};
82
83/* Exported via selinufs */
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;
Christoph Lametere18b8902006-12-06 20:33:20 -080092static struct kmem_cache *avc_node_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
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 * avc_dump_av - Display an access vector in human-readable form.
101 * @tclass: target security class
102 * @av: access vector
103 */
KaiGai Kohei44c2d9b2009-06-18 17:26:13 +0900104static void avc_dump_av(struct audit_buffer *ab, u16 tclass, u32 av)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105{
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -0400106 const char **perms;
107 int i, perm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
109 if (av == 0) {
110 audit_log_format(ab, " null");
111 return;
112 }
113
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -0400114 perms = secclass_map[tclass-1].perms;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
116 audit_log_format(ab, " {");
117 i = 0;
118 perm = 1;
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -0400119 while (i < (sizeof(av) * 8)) {
Eric Paris0bce9522009-11-23 16:47:23 -0500120 if ((perm & av) && perms[i]) {
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -0400121 audit_log_format(ab, " %s", perms[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 av &= ~perm;
123 }
124 i++;
125 perm <<= 1;
126 }
127
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 if (av)
129 audit_log_format(ab, " 0x%x", av);
130
131 audit_log_format(ab, " }");
132}
133
134/**
135 * avc_dump_query - Display a SID pair and a class in human-readable form.
136 * @ssid: source security identifier
137 * @tsid: target security identifier
138 * @tclass: target security class
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
Eric Paris95fff332008-04-17 14:42:10 -0400146 rc = security_sid_to_context(ssid, &scontext, &scontext_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 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 }
Stephen Smalleya764ae42007-03-26 13:36:26 -0400161
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -0400162 BUG_ON(tclass >= ARRAY_SIZE(secclass_map));
163 audit_log_format(ab, " tclass=%s", secclass_map[tclass-1].name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164}
165
166/**
167 * avc_init - Initialize the AVC.
168 *
169 * Initialize the access vector cache.
170 */
171void __init avc_init(void)
172{
173 int i;
174
175 for (i = 0; i < AVC_CACHE_SLOTS; i++) {
Eric Paris26036652009-02-12 14:51:04 -0500176 INIT_HLIST_HEAD(&avc_cache.slots[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 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),
Paul Mundt20c2df82007-07-20 10:11:58 +0900183 0, SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
David Woodhouse9ad9ad32005-06-22 15:04:33 +0100185 audit_log(current->audit_context, GFP_KERNEL, AUDIT_KERNEL, "AVC INITIALIZED\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186}
187
188int avc_get_hash_stats(char *page)
189{
190 int i, chain_len, max_chain_len, slots_used;
191 struct avc_node *node;
Eric Paris26036652009-02-12 14:51:04 -0500192 struct hlist_head *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
194 rcu_read_lock();
195
196 slots_used = 0;
197 max_chain_len = 0;
198 for (i = 0; i < AVC_CACHE_SLOTS; i++) {
Eric Parisedf3d1a2009-02-12 14:50:59 -0500199 head = &avc_cache.slots[i];
Eric Paris26036652009-02-12 14:51:04 -0500200 if (!hlist_empty(head)) {
201 struct hlist_node *next;
202
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 slots_used++;
204 chain_len = 0;
Eric Paris26036652009-02-12 14:51:04 -0500205 hlist_for_each_entry_rcu(node, next, head, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 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{
Eric Paris26036652009-02-12 14:51:04 -0500229 hlist_del_rcu(&node->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 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{
Eric Paris26036652009-02-12 14:51:04 -0500243 hlist_replace_rcu(&old->list, &new->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 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;
Eric Paris26036652009-02-12 14:51:04 -0500253 struct hlist_head *head;
254 struct hlist_node *next;
Eric Parisedf3d1a2009-02-12 14:50:59 -0500255 spinlock_t *lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
Eric Paris95fff332008-04-17 14:42:10 -0400257 for (try = 0, ecx = 0; try < AVC_CACHE_SLOTS; try++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 hvalue = atomic_inc_return(&avc_cache.lru_hint) & (AVC_CACHE_SLOTS - 1);
Eric Parisedf3d1a2009-02-12 14:50:59 -0500259 head = &avc_cache.slots[hvalue];
260 lock = &avc_cache.slots_lock[hvalue];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
Eric Parisedf3d1a2009-02-12 14:50:59 -0500262 if (!spin_trylock_irqsave(lock, flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 continue;
264
Paul E. McKenney61844252008-04-21 18:12:33 -0700265 rcu_read_lock();
Eric Paris26036652009-02-12 14:51:04 -0500266 hlist_for_each_entry(node, next, head, list) {
Eric Paris906d27d2009-02-12 14:50:43 -0500267 avc_node_delete(node);
268 avc_cache_stats_incr(reclaims);
269 ecx++;
270 if (ecx >= AVC_CACHE_RECLAIM) {
271 rcu_read_unlock();
Eric Parisedf3d1a2009-02-12 14:50:59 -0500272 spin_unlock_irqrestore(lock, flags);
Eric Paris906d27d2009-02-12 14:50:43 -0500273 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 }
275 }
Paul E. McKenney61844252008-04-21 18:12:33 -0700276 rcu_read_unlock();
Eric Parisedf3d1a2009-02-12 14:50:59 -0500277 spin_unlock_irqrestore(lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 }
279out:
280 return ecx;
281}
282
283static struct avc_node *avc_alloc_node(void)
284{
285 struct avc_node *node;
286
Robert P. J. Dayc3762222007-02-10 01:45:03 -0800287 node = kmem_cache_zalloc(avc_node_cachep, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 if (!node)
289 goto out;
290
Eric Paris26036652009-02-12 14:51:04 -0500291 INIT_HLIST_NODE(&node->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 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
Eric Paris21193dc2009-02-12 14:50:49 -0500301static void avc_node_populate(struct avc_node *node, u32 ssid, u32 tsid, u16 tclass, struct av_decision *avd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302{
303 node->ae.ssid = ssid;
304 node->ae.tsid = tsid;
305 node->ae.tclass = tclass;
Eric Paris21193dc2009-02-12 14:50:49 -0500306 memcpy(&node->ae.avd, avd, sizeof(node->ae.avd));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307}
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;
Eric Paris26036652009-02-12 14:51:04 -0500313 struct hlist_head *head;
314 struct hlist_node *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
316 hvalue = avc_hash(ssid, tsid, tclass);
Eric Parisedf3d1a2009-02-12 14:50:59 -0500317 head = &avc_cache.slots[hvalue];
Eric Paris26036652009-02-12 14:51:04 -0500318 hlist_for_each_entry_rcu(node, next, head, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 if (ssid == node->ae.ssid &&
320 tclass == node->ae.tclass &&
321 tsid == node->ae.tsid) {
322 ret = node;
323 break;
324 }
325 }
326
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 return ret;
328}
329
330/**
331 * avc_lookup - Look up an AVC entry.
332 * @ssid: source security identifier
333 * @tsid: target security identifier
334 * @tclass: target security class
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 *
336 * Look up an AVC entry that is valid for the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 * (@ssid, @tsid), interpreting the permissions
338 * based on @tclass. If a valid AVC entry exists,
Justin P. Mattock6382dc32010-01-14 23:03:18 -0800339 * then this function returns the avc_node.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 * Otherwise, this function returns NULL.
341 */
Eric Parisf1c63812009-02-12 14:50:54 -0500342static struct avc_node *avc_lookup(u32 ssid, u32 tsid, u16 tclass)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343{
344 struct avc_node *node;
345
346 avc_cache_stats_incr(lookups);
347 node = avc_search_node(ssid, tsid, tclass);
348
Eric Parisf1c63812009-02-12 14:50:54 -0500349 if (node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 avc_cache_stats_incr(hits);
Eric Parisf1c63812009-02-12 14:50:54 -0500351 else
352 avc_cache_stats_incr(misses);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 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(&notif_lock, flag);
364 if (is_insert) {
365 if (seqno < avc_cache.latest_notif) {
Eric Paris744ba352008-04-17 11:52:44 -0400366 printk(KERN_WARNING "SELinux: avc: seqno %d < latest_notif %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 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(&notif_lock, flag);
375
376 return ret;
377}
378
379/**
380 * avc_insert - Insert an AVC entry.
381 * @ssid: source security identifier
382 * @tsid: target security identifier
383 * @tclass: target security class
Eric Paris21193dc2009-02-12 14:50:49 -0500384 * @avd: resulting av decision
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 *
386 * Insert an AVC entry for the SID pair
387 * (@ssid, @tsid) and class @tclass.
388 * The access vectors and the sequence number are
389 * normally provided by the security server in
390 * response to a security_compute_av() call. If the
Eric Paris21193dc2009-02-12 14:50:49 -0500391 * sequence number @avd->seqno is not less than the latest
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 * revocation notification, then the function copies
393 * the access vectors into a cache entry, returns
394 * avc_node inserted. Otherwise, this function returns NULL.
395 */
Eric Paris21193dc2009-02-12 14:50:49 -0500396static struct avc_node *avc_insert(u32 ssid, u32 tsid, u16 tclass, struct av_decision *avd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397{
398 struct avc_node *pos, *node = NULL;
399 int hvalue;
400 unsigned long flag;
401
Eric Paris21193dc2009-02-12 14:50:49 -0500402 if (avc_latest_notif_update(avd->seqno, 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 goto out;
404
405 node = avc_alloc_node();
406 if (node) {
Eric Paris26036652009-02-12 14:51:04 -0500407 struct hlist_head *head;
408 struct hlist_node *next;
Eric Parisedf3d1a2009-02-12 14:50:59 -0500409 spinlock_t *lock;
410
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 hvalue = avc_hash(ssid, tsid, tclass);
Eric Paris21193dc2009-02-12 14:50:49 -0500412 avc_node_populate(node, ssid, tsid, tclass, avd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
Eric Parisedf3d1a2009-02-12 14:50:59 -0500414 head = &avc_cache.slots[hvalue];
415 lock = &avc_cache.slots_lock[hvalue];
416
417 spin_lock_irqsave(lock, flag);
Eric Paris26036652009-02-12 14:51:04 -0500418 hlist_for_each_entry(pos, next, head, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 if (pos->ae.ssid == ssid &&
420 pos->ae.tsid == tsid &&
421 pos->ae.tclass == tclass) {
Eric Paris95fff332008-04-17 14:42:10 -0400422 avc_node_replace(node, pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 goto found;
424 }
425 }
Eric Paris26036652009-02-12 14:51:04 -0500426 hlist_add_head_rcu(&node->list, head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427found:
Eric Parisedf3d1a2009-02-12 14:50:59 -0500428 spin_unlock_irqrestore(lock, flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 }
430out:
431 return node;
432}
433
Thomas Liu2bf49692009-07-14 12:14:09 -0400434/**
435 * avc_audit_pre_callback - SELinux specific information
436 * will be called by generic audit code
437 * @ab: the audit buffer
438 * @a: audit_data
439 */
440static void avc_audit_pre_callback(struct audit_buffer *ab, void *a)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441{
Thomas Liu2bf49692009-07-14 12:14:09 -0400442 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 ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448}
449
Thomas Liu2bf49692009-07-14 12:14:09 -0400450/**
451 * avc_audit_post_callback - SELinux specific information
452 * will be called by generic audit code
453 * @ab: the audit buffer
454 * @a: audit_data
455 */
456static void avc_audit_post_callback(struct audit_buffer *ab, void *a)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457{
Thomas Liu2bf49692009-07-14 12:14:09 -0400458 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463}
464
465/**
466 * avc_audit - Audit the granting or denial of permissions.
467 * @ssid: source security identifier
468 * @tsid: target security identifier
469 * @tclass: target security class
470 * @requested: requested permissions
471 * @avd: access vector decisions
472 * @result: result from avc_has_perm_noaudit
473 * @a: auxiliary audit data
474 *
475 * Audit the granting or denial of permissions in accordance
476 * with the policy. This function is typically called by
477 * avc_has_perm() after a permission check, but can also be
478 * called directly by callers who use avc_has_perm_noaudit()
479 * in order to separate the permission check from the auditing.
480 * For example, this separation is useful when the permission check must
481 * be performed under a lock, to allow the lock to be released
482 * before calling the auditing code.
483 */
484void avc_audit(u32 ssid, u32 tsid,
Eric Paris95fff332008-04-17 14:42:10 -0400485 u16 tclass, u32 requested,
Thomas Liu2bf49692009-07-14 12:14:09 -0400486 struct av_decision *avd, int result, struct common_audit_data *a)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487{
Thomas Liu2bf49692009-07-14 12:14:09 -0400488 struct common_audit_data stack_data;
James Morrisbe940d62009-07-13 10:39:36 +1000489 u32 denied, audited;
James Morrisbe940d62009-07-13 10:39:36 +1000490 denied = requested & ~avd->allowed;
Stephen Smalleyb6cac5a2010-02-02 11:31:51 -0500491 if (denied)
492 audited = denied & avd->auditdeny;
493 else if (result)
James Morrisbe940d62009-07-13 10:39:36 +1000494 audited = denied = requested;
Stephen Smalleyb6cac5a2010-02-02 11:31:51 -0500495 else
496 audited = requested & avd->auditallow;
497 if (!audited)
498 return;
Thomas Liu2bf49692009-07-14 12:14:09 -0400499 if (!a) {
500 a = &stack_data;
Eric Pariscb84aa92010-04-27 17:20:38 -0400501 COMMON_AUDIT_DATA_INIT(a, NONE);
James Morrisbe940d62009-07-13 10:39:36 +1000502 }
Thomas Liu2bf49692009-07-14 12:14:09 -0400503 a->selinux_audit_data.tclass = tclass;
504 a->selinux_audit_data.requested = requested;
505 a->selinux_audit_data.ssid = ssid;
506 a->selinux_audit_data.tsid = tsid;
507 a->selinux_audit_data.audited = audited;
508 a->selinux_audit_data.denied = denied;
509 a->lsm_pre_audit = avc_audit_pre_callback;
510 a->lsm_post_audit = avc_audit_post_callback;
511 common_lsm_audit(a);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512}
513
514/**
515 * avc_add_callback - Register a callback for security events.
516 * @callback: callback function
517 * @events: security events
518 * @ssid: source security identifier or %SECSID_WILD
519 * @tsid: target security identifier or %SECSID_WILD
520 * @tclass: target security class
521 * @perms: permissions
522 *
523 * Register a callback function for events in the set @events
Justin P. Mattock6382dc32010-01-14 23:03:18 -0800524 * related to the SID pair (@ssid, @tsid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 * and the permissions @perms, interpreting
526 * @perms based on @tclass. Returns %0 on success or
527 * -%ENOMEM if insufficient memory exists to add the callback.
528 */
529int avc_add_callback(int (*callback)(u32 event, u32 ssid, u32 tsid,
Eric Paris95fff332008-04-17 14:42:10 -0400530 u16 tclass, u32 perms,
531 u32 *out_retained),
532 u32 events, u32 ssid, u32 tsid,
533 u16 tclass, u32 perms)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534{
535 struct avc_callback_node *c;
536 int rc = 0;
537
538 c = kmalloc(sizeof(*c), GFP_ATOMIC);
539 if (!c) {
540 rc = -ENOMEM;
541 goto out;
542 }
543
544 c->callback = callback;
545 c->events = events;
546 c->ssid = ssid;
547 c->tsid = tsid;
548 c->perms = perms;
549 c->next = avc_callbacks;
550 avc_callbacks = c;
551out:
552 return rc;
553}
554
555static inline int avc_sidcmp(u32 x, u32 y)
556{
557 return (x == y || x == SECSID_WILD || y == SECSID_WILD);
558}
559
560/**
561 * avc_update_node Update an AVC entry
562 * @event : Updating event
563 * @perms : Permission mask bits
564 * @ssid,@tsid,@tclass : identifier of an AVC entry
Eric Parisa5dda6832009-02-12 14:50:11 -0500565 * @seqno : sequence number when decision was made
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 *
567 * if a valid AVC entry doesn't exist,this function returns -ENOENT.
568 * if kmalloc() called internal returns NULL, this function returns -ENOMEM.
Justin P. Mattock6382dc32010-01-14 23:03:18 -0800569 * otherwise, this function updates the AVC entry. The original AVC-entry object
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 * will release later by RCU.
571 */
Eric Parisa5dda6832009-02-12 14:50:11 -0500572static int avc_update_node(u32 event, u32 perms, u32 ssid, u32 tsid, u16 tclass,
573 u32 seqno)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574{
575 int hvalue, rc = 0;
576 unsigned long flag;
577 struct avc_node *pos, *node, *orig = NULL;
Eric Paris26036652009-02-12 14:51:04 -0500578 struct hlist_head *head;
579 struct hlist_node *next;
Eric Parisedf3d1a2009-02-12 14:50:59 -0500580 spinlock_t *lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
582 node = avc_alloc_node();
583 if (!node) {
584 rc = -ENOMEM;
585 goto out;
586 }
587
588 /* Lock the target slot */
589 hvalue = avc_hash(ssid, tsid, tclass);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
Eric Parisedf3d1a2009-02-12 14:50:59 -0500591 head = &avc_cache.slots[hvalue];
592 lock = &avc_cache.slots_lock[hvalue];
593
594 spin_lock_irqsave(lock, flag);
595
Eric Paris26036652009-02-12 14:51:04 -0500596 hlist_for_each_entry(pos, next, head, list) {
Eric Paris95fff332008-04-17 14:42:10 -0400597 if (ssid == pos->ae.ssid &&
598 tsid == pos->ae.tsid &&
Eric Parisa5dda6832009-02-12 14:50:11 -0500599 tclass == pos->ae.tclass &&
600 seqno == pos->ae.avd.seqno){
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 orig = pos;
602 break;
603 }
604 }
605
606 if (!orig) {
607 rc = -ENOENT;
608 avc_node_kill(node);
609 goto out_unlock;
610 }
611
612 /*
613 * Copy and replace original node.
614 */
615
Eric Paris21193dc2009-02-12 14:50:49 -0500616 avc_node_populate(node, ssid, tsid, tclass, &orig->ae.avd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
618 switch (event) {
619 case AVC_CALLBACK_GRANT:
620 node->ae.avd.allowed |= perms;
621 break;
622 case AVC_CALLBACK_TRY_REVOKE:
623 case AVC_CALLBACK_REVOKE:
624 node->ae.avd.allowed &= ~perms;
625 break;
626 case AVC_CALLBACK_AUDITALLOW_ENABLE:
627 node->ae.avd.auditallow |= perms;
628 break;
629 case AVC_CALLBACK_AUDITALLOW_DISABLE:
630 node->ae.avd.auditallow &= ~perms;
631 break;
632 case AVC_CALLBACK_AUDITDENY_ENABLE:
633 node->ae.avd.auditdeny |= perms;
634 break;
635 case AVC_CALLBACK_AUDITDENY_DISABLE:
636 node->ae.avd.auditdeny &= ~perms;
637 break;
638 }
639 avc_node_replace(node, orig);
640out_unlock:
Eric Parisedf3d1a2009-02-12 14:50:59 -0500641 spin_unlock_irqrestore(lock, flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642out:
643 return rc;
644}
645
646/**
Eric Paris008574b2009-09-12 22:54:17 -0400647 * avc_flush - Flush the cache
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 */
Eric Paris008574b2009-09-12 22:54:17 -0400649static void avc_flush(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650{
Eric Paris26036652009-02-12 14:51:04 -0500651 struct hlist_head *head;
652 struct hlist_node *next;
Eric Paris008574b2009-09-12 22:54:17 -0400653 struct avc_node *node;
Eric Parisedf3d1a2009-02-12 14:50:59 -0500654 spinlock_t *lock;
Eric Paris008574b2009-09-12 22:54:17 -0400655 unsigned long flag;
656 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
658 for (i = 0; i < AVC_CACHE_SLOTS; i++) {
Eric Parisedf3d1a2009-02-12 14:50:59 -0500659 head = &avc_cache.slots[i];
660 lock = &avc_cache.slots_lock[i];
661
662 spin_lock_irqsave(lock, flag);
Paul E. McKenney61844252008-04-21 18:12:33 -0700663 /*
664 * With preemptable RCU, the outer spinlock does not
665 * prevent RCU grace periods from ending.
666 */
667 rcu_read_lock();
Eric Paris26036652009-02-12 14:51:04 -0500668 hlist_for_each_entry(node, next, head, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 avc_node_delete(node);
Paul E. McKenney61844252008-04-21 18:12:33 -0700670 rcu_read_unlock();
Eric Parisedf3d1a2009-02-12 14:50:59 -0500671 spin_unlock_irqrestore(lock, flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 }
Eric Paris008574b2009-09-12 22:54:17 -0400673}
674
675/**
676 * avc_ss_reset - Flush the cache and revalidate migrated permissions.
677 * @seqno: policy sequence number
678 */
679int avc_ss_reset(u32 seqno)
680{
681 struct avc_callback_node *c;
682 int rc = 0, tmprc;
683
684 avc_flush();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
686 for (c = avc_callbacks; c; c = c->next) {
687 if (c->events & AVC_CALLBACK_RESET) {
Darrel Goeddel376bd9c2006-02-24 15:44:05 -0600688 tmprc = c->callback(AVC_CALLBACK_RESET,
Eric Paris95fff332008-04-17 14:42:10 -0400689 0, 0, 0, 0, NULL);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -0600690 /* save the first error encountered for the return
691 value and continue processing the callbacks */
692 if (!rc)
693 rc = tmprc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 }
695 }
696
697 avc_latest_notif_update(seqno, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 return rc;
699}
700
701/**
702 * avc_has_perm_noaudit - Check permissions but perform no auditing.
703 * @ssid: source security identifier
704 * @tsid: target security identifier
705 * @tclass: target security class
706 * @requested: requested permissions, interpreted based on @tclass
Stephen Smalley2c3c05d2007-06-07 15:34:10 -0400707 * @flags: AVC_STRICT or 0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 * @avd: access vector decisions
709 *
710 * Check the AVC to determine whether the @requested permissions are granted
711 * for the SID pair (@ssid, @tsid), interpreting the permissions
712 * based on @tclass, and call the security server on a cache miss to obtain
713 * a new decision and add it to the cache. Return a copy of the decisions
714 * in @avd. Return %0 if all @requested permissions are granted,
715 * -%EACCES if any permissions are denied, or another -errno upon
716 * other errors. This function is typically called by avc_has_perm(),
717 * but may also be called directly to separate permission checking from
718 * auditing, e.g. in cases where a lock must be held for the check but
719 * should be released for the auditing.
720 */
721int avc_has_perm_noaudit(u32 ssid, u32 tsid,
Stephen Smalley2c3c05d2007-06-07 15:34:10 -0400722 u16 tclass, u32 requested,
723 unsigned flags,
Eric Paris21193dc2009-02-12 14:50:49 -0500724 struct av_decision *in_avd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725{
726 struct avc_node *node;
Eric Paris21193dc2009-02-12 14:50:49 -0500727 struct av_decision avd_entry, *avd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 int rc = 0;
729 u32 denied;
730
Eric Pariseda4f692008-03-11 14:19:34 -0400731 BUG_ON(!requested);
732
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 rcu_read_lock();
734
Eric Parisf1c63812009-02-12 14:50:54 -0500735 node = avc_lookup(ssid, tsid, tclass);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 if (!node) {
737 rcu_read_unlock();
Eric Paris21193dc2009-02-12 14:50:49 -0500738
739 if (in_avd)
740 avd = in_avd;
741 else
742 avd = &avd_entry;
743
Stephen Smalley19439d02010-01-14 17:28:10 -0500744 security_compute_av(ssid, tsid, tclass, avd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 rcu_read_lock();
Eric Paris21193dc2009-02-12 14:50:49 -0500746 node = avc_insert(ssid, tsid, tclass, avd);
747 } else {
748 if (in_avd)
749 memcpy(in_avd, &node->ae.avd, sizeof(*in_avd));
750 avd = &node->ae.avd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 }
752
Eric Paris21193dc2009-02-12 14:50:49 -0500753 denied = requested & ~(avd->allowed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
Eric Pariseda4f692008-03-11 14:19:34 -0400755 if (denied) {
Eric Paris64dbf072008-03-31 12:17:33 +1100756 if (flags & AVC_STRICT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 rc = -EACCES;
KaiGai Kohei8a6f83a2009-04-01 10:07:57 +0900758 else if (!selinux_enforcing || (avd->flags & AVD_FLAGS_PERMISSIVE))
Eric Paris64dbf072008-03-31 12:17:33 +1100759 avc_update_node(AVC_CALLBACK_GRANT, requested, ssid,
Eric Paris21193dc2009-02-12 14:50:49 -0500760 tsid, tclass, avd->seqno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 else
Eric Paris64dbf072008-03-31 12:17:33 +1100762 rc = -EACCES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 }
764
765 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 return rc;
767}
768
769/**
770 * avc_has_perm - Check permissions and perform any appropriate auditing.
771 * @ssid: source security identifier
772 * @tsid: target security identifier
773 * @tclass: target security class
774 * @requested: requested permissions, interpreted based on @tclass
775 * @auditdata: auxiliary audit data
776 *
777 * Check the AVC to determine whether the @requested permissions are granted
778 * for the SID pair (@ssid, @tsid), interpreting the permissions
779 * based on @tclass, and call the security server on a cache miss to obtain
780 * a new decision and add it to the cache. Audit the granting or denial of
781 * permissions in accordance with the policy. Return %0 if all @requested
782 * permissions are granted, -%EACCES if any permissions are denied, or
783 * another -errno upon other errors.
784 */
785int avc_has_perm(u32 ssid, u32 tsid, u16 tclass,
Thomas Liu2bf49692009-07-14 12:14:09 -0400786 u32 requested, struct common_audit_data *auditdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787{
788 struct av_decision avd;
789 int rc;
790
Stephen Smalley2c3c05d2007-06-07 15:34:10 -0400791 rc = avc_has_perm_noaudit(ssid, tsid, tclass, requested, 0, &avd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 avc_audit(ssid, tsid, tclass, requested, &avd, rc, auditdata);
793 return rc;
794}
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +0900795
796u32 avc_policy_seqno(void)
797{
798 return avc_cache.latest_notif;
799}
Thomas Liu89c86572009-06-24 17:58:05 -0400800
801void avc_disable(void)
802{
Eric Paris5224ee02009-09-20 21:21:10 -0400803 /*
804 * If you are looking at this because you have realized that we are
805 * not destroying the avc_node_cachep it might be easy to fix, but
806 * I don't know the memory barrier semantics well enough to know. It's
807 * possible that some other task dereferenced security_ops when
808 * it still pointed to selinux operations. If that is the case it's
809 * possible that it is about to use the avc and is about to need the
810 * avc_node_cachep. I know I could wrap the security.c security_ops call
811 * in an rcu_lock, but seriously, it's not worth it. Instead I just flush
812 * the cache and get that memory back.
813 */
814 if (avc_node_cachep) {
815 avc_flush();
816 /* kmem_cache_destroy(avc_node_cachep); */
817 }
Thomas Liu89c86572009-06-24 17:58:05 -0400818}