1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Implementation of the kernel access vector cache (AVC).
4 *
5 * Authors: Stephen Smalley, <stephen.smalley.work@gmail.com>
6 * James Morris <jmorris@redhat.com>
7 *
8 * Update: KaiGai, Kohei <kaigai@ak.jp.nec.com>
9 * Replaced the avc_lock spinlock by RCU.
10 *
11 * Copyright (C) 2003 Red Hat, Inc., James Morris <jmorris@redhat.com>
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 <linux/percpu.h>
22 #include <linux/list.h>
23 #include <net/sock.h>
24 #include <linux/un.h>
25 #include <net/af_unix.h>
26 #include <linux/ip.h>
27 #include <linux/audit.h>
28 #include <linux/ipv6.h>
29 #include <net/ipv6.h>
30 #include "avc.h"
31 #include "avc_ss.h"
32 #include "classmap.h"
33
34 #define CREATE_TRACE_POINTS
35 #include <trace/events/avc.h>
36
37 #define AVC_CACHE_SLOTS 512
38 #define AVC_DEF_CACHE_THRESHOLD 512
39 #define AVC_CACHE_RECLAIM 16
40
41 #ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
42 #define avc_cache_stats_incr(field) this_cpu_inc(avc_cache_stats.field)
43 #else
44 #define avc_cache_stats_incr(field) do {} while (0)
45 #endif
46
47 #undef CREATE_TRACE_POINTS
48 #include <trace/hooks/avc.h>
49
50 struct avc_entry {
51 u32 ssid;
52 u32 tsid;
53 u16 tclass;
54 struct av_decision avd;
55 struct avc_xperms_node *xp_node;
56 };
57
58 struct avc_node {
59 struct avc_entry ae;
60 struct hlist_node list; /* anchored in avc_cache->slots[i] */
61 struct rcu_head rhead;
62 };
63
64 struct avc_xperms_decision_node {
65 struct extended_perms_decision xpd;
66 struct list_head xpd_list; /* list of extended_perms_decision */
67 };
68
69 struct avc_xperms_node {
70 struct extended_perms xp;
71 struct list_head xpd_head; /* list head of extended_perms_decision */
72 };
73
74 struct avc_cache {
75 struct hlist_head slots[AVC_CACHE_SLOTS]; /* head for avc_node->list */
76 spinlock_t slots_lock[AVC_CACHE_SLOTS]; /* lock for writes */
77 atomic_t lru_hint; /* LRU hint for reclaim scan */
78 atomic_t active_nodes;
79 u32 latest_notif; /* latest revocation notification */
80 };
81
82 struct avc_callback_node {
83 int (*callback) (u32 event);
84 u32 events;
85 struct avc_callback_node *next;
86 };
87
88 #ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
89 DEFINE_PER_CPU(struct avc_cache_stats, avc_cache_stats) = { 0 };
90 #endif
91
92 struct selinux_avc {
93 unsigned int avc_cache_threshold;
94 struct avc_cache avc_cache;
95 };
96
97 static struct selinux_avc selinux_avc;
98
selinux_avc_init(void)99 void selinux_avc_init(void)
100 {
101 int i;
102
103 selinux_avc.avc_cache_threshold = AVC_DEF_CACHE_THRESHOLD;
104 for (i = 0; i < AVC_CACHE_SLOTS; i++) {
105 INIT_HLIST_HEAD(&selinux_avc.avc_cache.slots[i]);
106 spin_lock_init(&selinux_avc.avc_cache.slots_lock[i]);
107 }
108 atomic_set(&selinux_avc.avc_cache.active_nodes, 0);
109 atomic_set(&selinux_avc.avc_cache.lru_hint, 0);
110 }
111
avc_get_cache_threshold(void)112 unsigned int avc_get_cache_threshold(void)
113 {
114 return selinux_avc.avc_cache_threshold;
115 }
116
avc_set_cache_threshold(unsigned int cache_threshold)117 void avc_set_cache_threshold(unsigned int cache_threshold)
118 {
119 selinux_avc.avc_cache_threshold = cache_threshold;
120 }
121
122 static struct avc_callback_node *avc_callbacks __ro_after_init;
123 static struct kmem_cache *avc_node_cachep __ro_after_init;
124 static struct kmem_cache *avc_xperms_data_cachep __ro_after_init;
125 static struct kmem_cache *avc_xperms_decision_cachep __ro_after_init;
126 static struct kmem_cache *avc_xperms_cachep __ro_after_init;
127
avc_hash(u32 ssid,u32 tsid,u16 tclass)128 static inline u32 avc_hash(u32 ssid, u32 tsid, u16 tclass)
129 {
130 return (ssid ^ (tsid<<2) ^ (tclass<<4)) & (AVC_CACHE_SLOTS - 1);
131 }
132
133 /**
134 * avc_init - Initialize the AVC.
135 *
136 * Initialize the access vector cache.
137 */
avc_init(void)138 void __init avc_init(void)
139 {
140 avc_node_cachep = kmem_cache_create("avc_node", sizeof(struct avc_node),
141 0, SLAB_PANIC, NULL);
142 avc_xperms_cachep = kmem_cache_create("avc_xperms_node",
143 sizeof(struct avc_xperms_node),
144 0, SLAB_PANIC, NULL);
145 avc_xperms_decision_cachep = kmem_cache_create(
146 "avc_xperms_decision_node",
147 sizeof(struct avc_xperms_decision_node),
148 0, SLAB_PANIC, NULL);
149 avc_xperms_data_cachep = kmem_cache_create("avc_xperms_data",
150 sizeof(struct extended_perms_data),
151 0, SLAB_PANIC, NULL);
152 }
153
avc_get_hash_stats(char * page)154 int avc_get_hash_stats(char *page)
155 {
156 int i, chain_len, max_chain_len, slots_used;
157 struct avc_node *node;
158 struct hlist_head *head;
159
160 rcu_read_lock();
161
162 slots_used = 0;
163 max_chain_len = 0;
164 for (i = 0; i < AVC_CACHE_SLOTS; i++) {
165 head = &selinux_avc.avc_cache.slots[i];
166 if (!hlist_empty(head)) {
167 slots_used++;
168 chain_len = 0;
169 hlist_for_each_entry_rcu(node, head, list)
170 chain_len++;
171 if (chain_len > max_chain_len)
172 max_chain_len = chain_len;
173 }
174 }
175
176 rcu_read_unlock();
177
178 return scnprintf(page, PAGE_SIZE, "entries: %d\nbuckets used: %d/%d\n"
179 "longest chain: %d\n",
180 atomic_read(&selinux_avc.avc_cache.active_nodes),
181 slots_used, AVC_CACHE_SLOTS, max_chain_len);
182 }
183
184 /*
185 * using a linked list for extended_perms_decision lookup because the list is
186 * always small. i.e. less than 5, typically 1
187 */
avc_xperms_decision_lookup(u8 driver,struct avc_xperms_node * xp_node)188 static struct extended_perms_decision *avc_xperms_decision_lookup(u8 driver,
189 struct avc_xperms_node *xp_node)
190 {
191 struct avc_xperms_decision_node *xpd_node;
192
193 list_for_each_entry(xpd_node, &xp_node->xpd_head, xpd_list) {
194 if (xpd_node->xpd.driver == driver)
195 return &xpd_node->xpd;
196 }
197 return NULL;
198 }
199
200 static inline unsigned int
avc_xperms_has_perm(struct extended_perms_decision * xpd,u8 perm,u8 which)201 avc_xperms_has_perm(struct extended_perms_decision *xpd,
202 u8 perm, u8 which)
203 {
204 unsigned int rc = 0;
205
206 if ((which == XPERMS_ALLOWED) &&
207 (xpd->used & XPERMS_ALLOWED))
208 rc = security_xperm_test(xpd->allowed->p, perm);
209 else if ((which == XPERMS_AUDITALLOW) &&
210 (xpd->used & XPERMS_AUDITALLOW))
211 rc = security_xperm_test(xpd->auditallow->p, perm);
212 else if ((which == XPERMS_DONTAUDIT) &&
213 (xpd->used & XPERMS_DONTAUDIT))
214 rc = security_xperm_test(xpd->dontaudit->p, perm);
215 return rc;
216 }
217
avc_xperms_allow_perm(struct avc_xperms_node * xp_node,u8 driver,u8 perm)218 static void avc_xperms_allow_perm(struct avc_xperms_node *xp_node,
219 u8 driver, u8 perm)
220 {
221 struct extended_perms_decision *xpd;
222 security_xperm_set(xp_node->xp.drivers.p, driver);
223 xpd = avc_xperms_decision_lookup(driver, xp_node);
224 if (xpd && xpd->allowed)
225 security_xperm_set(xpd->allowed->p, perm);
226 }
227
avc_xperms_decision_free(struct avc_xperms_decision_node * xpd_node)228 static void avc_xperms_decision_free(struct avc_xperms_decision_node *xpd_node)
229 {
230 struct extended_perms_decision *xpd;
231
232 xpd = &xpd_node->xpd;
233 if (xpd->allowed)
234 kmem_cache_free(avc_xperms_data_cachep, xpd->allowed);
235 if (xpd->auditallow)
236 kmem_cache_free(avc_xperms_data_cachep, xpd->auditallow);
237 if (xpd->dontaudit)
238 kmem_cache_free(avc_xperms_data_cachep, xpd->dontaudit);
239 kmem_cache_free(avc_xperms_decision_cachep, xpd_node);
240 }
241
avc_xperms_free(struct avc_xperms_node * xp_node)242 static void avc_xperms_free(struct avc_xperms_node *xp_node)
243 {
244 struct avc_xperms_decision_node *xpd_node, *tmp;
245
246 if (!xp_node)
247 return;
248
249 list_for_each_entry_safe(xpd_node, tmp, &xp_node->xpd_head, xpd_list) {
250 list_del(&xpd_node->xpd_list);
251 avc_xperms_decision_free(xpd_node);
252 }
253 kmem_cache_free(avc_xperms_cachep, xp_node);
254 }
255
avc_copy_xperms_decision(struct extended_perms_decision * dest,struct extended_perms_decision * src)256 static void avc_copy_xperms_decision(struct extended_perms_decision *dest,
257 struct extended_perms_decision *src)
258 {
259 dest->driver = src->driver;
260 dest->used = src->used;
261 if (dest->used & XPERMS_ALLOWED)
262 memcpy(dest->allowed->p, src->allowed->p,
263 sizeof(src->allowed->p));
264 if (dest->used & XPERMS_AUDITALLOW)
265 memcpy(dest->auditallow->p, src->auditallow->p,
266 sizeof(src->auditallow->p));
267 if (dest->used & XPERMS_DONTAUDIT)
268 memcpy(dest->dontaudit->p, src->dontaudit->p,
269 sizeof(src->dontaudit->p));
270 }
271
272 /*
273 * similar to avc_copy_xperms_decision, but only copy decision
274 * information relevant to this perm
275 */
avc_quick_copy_xperms_decision(u8 perm,struct extended_perms_decision * dest,struct extended_perms_decision * src)276 static inline void avc_quick_copy_xperms_decision(u8 perm,
277 struct extended_perms_decision *dest,
278 struct extended_perms_decision *src)
279 {
280 /*
281 * compute index of the u32 of the 256 bits (8 u32s) that contain this
282 * command permission
283 */
284 u8 i = perm >> 5;
285
286 dest->used = src->used;
287 if (dest->used & XPERMS_ALLOWED)
288 dest->allowed->p[i] = src->allowed->p[i];
289 if (dest->used & XPERMS_AUDITALLOW)
290 dest->auditallow->p[i] = src->auditallow->p[i];
291 if (dest->used & XPERMS_DONTAUDIT)
292 dest->dontaudit->p[i] = src->dontaudit->p[i];
293 }
294
295 static struct avc_xperms_decision_node
avc_xperms_decision_alloc(u8 which)296 *avc_xperms_decision_alloc(u8 which)
297 {
298 struct avc_xperms_decision_node *xpd_node;
299 struct extended_perms_decision *xpd;
300
301 xpd_node = kmem_cache_zalloc(avc_xperms_decision_cachep,
302 GFP_NOWAIT | __GFP_NOWARN);
303 if (!xpd_node)
304 return NULL;
305
306 xpd = &xpd_node->xpd;
307 if (which & XPERMS_ALLOWED) {
308 xpd->allowed = kmem_cache_zalloc(avc_xperms_data_cachep,
309 GFP_NOWAIT | __GFP_NOWARN);
310 if (!xpd->allowed)
311 goto error;
312 }
313 if (which & XPERMS_AUDITALLOW) {
314 xpd->auditallow = kmem_cache_zalloc(avc_xperms_data_cachep,
315 GFP_NOWAIT | __GFP_NOWARN);
316 if (!xpd->auditallow)
317 goto error;
318 }
319 if (which & XPERMS_DONTAUDIT) {
320 xpd->dontaudit = kmem_cache_zalloc(avc_xperms_data_cachep,
321 GFP_NOWAIT | __GFP_NOWARN);
322 if (!xpd->dontaudit)
323 goto error;
324 }
325 return xpd_node;
326 error:
327 avc_xperms_decision_free(xpd_node);
328 return NULL;
329 }
330
avc_add_xperms_decision(struct avc_node * node,struct extended_perms_decision * src)331 static int avc_add_xperms_decision(struct avc_node *node,
332 struct extended_perms_decision *src)
333 {
334 struct avc_xperms_decision_node *dest_xpd;
335
336 dest_xpd = avc_xperms_decision_alloc(src->used);
337 if (!dest_xpd)
338 return -ENOMEM;
339 avc_copy_xperms_decision(&dest_xpd->xpd, src);
340 list_add(&dest_xpd->xpd_list, &node->ae.xp_node->xpd_head);
341 node->ae.xp_node->xp.len++;
342 return 0;
343 }
344
avc_xperms_alloc(void)345 static struct avc_xperms_node *avc_xperms_alloc(void)
346 {
347 struct avc_xperms_node *xp_node;
348
349 xp_node = kmem_cache_zalloc(avc_xperms_cachep, GFP_NOWAIT | __GFP_NOWARN);
350 if (!xp_node)
351 return xp_node;
352 INIT_LIST_HEAD(&xp_node->xpd_head);
353 return xp_node;
354 }
355
avc_xperms_populate(struct avc_node * node,struct avc_xperms_node * src)356 static int avc_xperms_populate(struct avc_node *node,
357 struct avc_xperms_node *src)
358 {
359 struct avc_xperms_node *dest;
360 struct avc_xperms_decision_node *dest_xpd;
361 struct avc_xperms_decision_node *src_xpd;
362
363 if (src->xp.len == 0)
364 return 0;
365 dest = avc_xperms_alloc();
366 if (!dest)
367 return -ENOMEM;
368
369 memcpy(dest->xp.drivers.p, src->xp.drivers.p, sizeof(dest->xp.drivers.p));
370 dest->xp.len = src->xp.len;
371
372 /* for each source xpd allocate a destination xpd and copy */
373 list_for_each_entry(src_xpd, &src->xpd_head, xpd_list) {
374 dest_xpd = avc_xperms_decision_alloc(src_xpd->xpd.used);
375 if (!dest_xpd)
376 goto error;
377 avc_copy_xperms_decision(&dest_xpd->xpd, &src_xpd->xpd);
378 list_add(&dest_xpd->xpd_list, &dest->xpd_head);
379 }
380 node->ae.xp_node = dest;
381 return 0;
382 error:
383 avc_xperms_free(dest);
384 return -ENOMEM;
385
386 }
387
avc_xperms_audit_required(u32 requested,struct av_decision * avd,struct extended_perms_decision * xpd,u8 perm,int result,u32 * deniedp)388 static inline u32 avc_xperms_audit_required(u32 requested,
389 struct av_decision *avd,
390 struct extended_perms_decision *xpd,
391 u8 perm,
392 int result,
393 u32 *deniedp)
394 {
395 u32 denied, audited;
396
397 denied = requested & ~avd->allowed;
398 if (unlikely(denied)) {
399 audited = denied & avd->auditdeny;
400 if (audited && xpd) {
401 if (avc_xperms_has_perm(xpd, perm, XPERMS_DONTAUDIT))
402 audited &= ~requested;
403 }
404 } else if (result) {
405 audited = denied = requested;
406 } else {
407 audited = requested & avd->auditallow;
408 if (audited && xpd) {
409 if (!avc_xperms_has_perm(xpd, perm, XPERMS_AUDITALLOW))
410 audited &= ~requested;
411 }
412 }
413
414 *deniedp = denied;
415 return audited;
416 }
417
avc_xperms_audit(u32 ssid,u32 tsid,u16 tclass,u32 requested,struct av_decision * avd,struct extended_perms_decision * xpd,u8 perm,int result,struct common_audit_data * ad)418 static inline int avc_xperms_audit(u32 ssid, u32 tsid, u16 tclass,
419 u32 requested, struct av_decision *avd,
420 struct extended_perms_decision *xpd,
421 u8 perm, int result,
422 struct common_audit_data *ad)
423 {
424 u32 audited, denied;
425
426 audited = avc_xperms_audit_required(
427 requested, avd, xpd, perm, result, &denied);
428 if (likely(!audited))
429 return 0;
430 return slow_avc_audit(ssid, tsid, tclass, requested,
431 audited, denied, result, ad);
432 }
433
avc_node_free(struct rcu_head * rhead)434 static void avc_node_free(struct rcu_head *rhead)
435 {
436 struct avc_node *node = container_of(rhead, struct avc_node, rhead);
437 avc_xperms_free(node->ae.xp_node);
438 kmem_cache_free(avc_node_cachep, node);
439 avc_cache_stats_incr(frees);
440 }
441
avc_node_delete(struct avc_node * node)442 static void avc_node_delete(struct avc_node *node)
443 {
444 trace_android_rvh_selinux_avc_node_delete(node);
445 hlist_del_rcu(&node->list);
446 call_rcu(&node->rhead, avc_node_free);
447 atomic_dec(&selinux_avc.avc_cache.active_nodes);
448 }
449
avc_node_kill(struct avc_node * node)450 static void avc_node_kill(struct avc_node *node)
451 {
452 avc_xperms_free(node->ae.xp_node);
453 kmem_cache_free(avc_node_cachep, node);
454 avc_cache_stats_incr(frees);
455 atomic_dec(&selinux_avc.avc_cache.active_nodes);
456 }
457
avc_node_replace(struct avc_node * new,struct avc_node * old)458 static void avc_node_replace(struct avc_node *new, struct avc_node *old)
459 {
460 trace_android_rvh_selinux_avc_node_replace(old, new);
461 hlist_replace_rcu(&old->list, &new->list);
462 call_rcu(&old->rhead, avc_node_free);
463 atomic_dec(&selinux_avc.avc_cache.active_nodes);
464 }
465
avc_reclaim_node(void)466 static inline int avc_reclaim_node(void)
467 {
468 struct avc_node *node;
469 int hvalue, try, ecx;
470 unsigned long flags;
471 struct hlist_head *head;
472 spinlock_t *lock;
473
474 for (try = 0, ecx = 0; try < AVC_CACHE_SLOTS; try++) {
475 hvalue = atomic_inc_return(&selinux_avc.avc_cache.lru_hint) &
476 (AVC_CACHE_SLOTS - 1);
477 head = &selinux_avc.avc_cache.slots[hvalue];
478 lock = &selinux_avc.avc_cache.slots_lock[hvalue];
479
480 if (!spin_trylock_irqsave(lock, flags))
481 continue;
482
483 rcu_read_lock();
484 hlist_for_each_entry(node, head, list) {
485 avc_node_delete(node);
486 avc_cache_stats_incr(reclaims);
487 ecx++;
488 if (ecx >= AVC_CACHE_RECLAIM) {
489 rcu_read_unlock();
490 spin_unlock_irqrestore(lock, flags);
491 goto out;
492 }
493 }
494 rcu_read_unlock();
495 spin_unlock_irqrestore(lock, flags);
496 }
497 out:
498 return ecx;
499 }
500
avc_alloc_node(void)501 static struct avc_node *avc_alloc_node(void)
502 {
503 struct avc_node *node;
504
505 node = kmem_cache_zalloc(avc_node_cachep, GFP_NOWAIT | __GFP_NOWARN);
506 if (!node)
507 goto out;
508
509 INIT_HLIST_NODE(&node->list);
510 avc_cache_stats_incr(allocations);
511
512 if (atomic_inc_return(&selinux_avc.avc_cache.active_nodes) >
513 selinux_avc.avc_cache_threshold)
514 avc_reclaim_node();
515
516 out:
517 return node;
518 }
519
avc_node_populate(struct avc_node * node,u32 ssid,u32 tsid,u16 tclass,struct av_decision * avd)520 static void avc_node_populate(struct avc_node *node, u32 ssid, u32 tsid, u16 tclass, struct av_decision *avd)
521 {
522 node->ae.ssid = ssid;
523 node->ae.tsid = tsid;
524 node->ae.tclass = tclass;
525 memcpy(&node->ae.avd, avd, sizeof(node->ae.avd));
526 }
527
avc_search_node(u32 ssid,u32 tsid,u16 tclass)528 static inline struct avc_node *avc_search_node(u32 ssid, u32 tsid, u16 tclass)
529 {
530 struct avc_node *node, *ret = NULL;
531 u32 hvalue;
532 struct hlist_head *head;
533
534 hvalue = avc_hash(ssid, tsid, tclass);
535 head = &selinux_avc.avc_cache.slots[hvalue];
536 hlist_for_each_entry_rcu(node, head, list) {
537 if (ssid == node->ae.ssid &&
538 tclass == node->ae.tclass &&
539 tsid == node->ae.tsid) {
540 ret = node;
541 break;
542 }
543 }
544
545 return ret;
546 }
547
548 /**
549 * avc_lookup - Look up an AVC entry.
550 * @ssid: source security identifier
551 * @tsid: target security identifier
552 * @tclass: target security class
553 *
554 * Look up an AVC entry that is valid for the
555 * (@ssid, @tsid), interpreting the permissions
556 * based on @tclass. If a valid AVC entry exists,
557 * then this function returns the avc_node.
558 * Otherwise, this function returns NULL.
559 */
avc_lookup(u32 ssid,u32 tsid,u16 tclass)560 static struct avc_node *avc_lookup(u32 ssid, u32 tsid, u16 tclass)
561 {
562 struct avc_node *node;
563
564 avc_cache_stats_incr(lookups);
565 node = avc_search_node(ssid, tsid, tclass);
566
567 if (node) {
568 trace_android_rvh_selinux_avc_lookup(node, ssid, tsid, tclass);
569 return node;
570 }
571
572 avc_cache_stats_incr(misses);
573 return NULL;
574 }
575
avc_latest_notif_update(u32 seqno,int is_insert)576 static int avc_latest_notif_update(u32 seqno, int is_insert)
577 {
578 int ret = 0;
579 static DEFINE_SPINLOCK(notif_lock);
580 unsigned long flag;
581
582 spin_lock_irqsave(¬if_lock, flag);
583 if (is_insert) {
584 if (seqno < selinux_avc.avc_cache.latest_notif) {
585 pr_warn("SELinux: avc: seqno %d < latest_notif %d\n",
586 seqno, selinux_avc.avc_cache.latest_notif);
587 ret = -EAGAIN;
588 }
589 } else {
590 if (seqno > selinux_avc.avc_cache.latest_notif)
591 selinux_avc.avc_cache.latest_notif = seqno;
592 }
593 spin_unlock_irqrestore(¬if_lock, flag);
594
595 return ret;
596 }
597
598 /**
599 * avc_insert - Insert an AVC entry.
600 * @ssid: source security identifier
601 * @tsid: target security identifier
602 * @tclass: target security class
603 * @avd: resulting av decision
604 * @xp_node: resulting extended permissions
605 *
606 * Insert an AVC entry for the SID pair
607 * (@ssid, @tsid) and class @tclass.
608 * The access vectors and the sequence number are
609 * normally provided by the security server in
610 * response to a security_compute_av() call. If the
611 * sequence number @avd->seqno is not less than the latest
612 * revocation notification, then the function copies
613 * the access vectors into a cache entry.
614 */
avc_insert(u32 ssid,u32 tsid,u16 tclass,struct av_decision * avd,struct avc_xperms_node * xp_node)615 static void avc_insert(u32 ssid, u32 tsid, u16 tclass,
616 struct av_decision *avd, struct avc_xperms_node *xp_node)
617 {
618 struct avc_node *pos, *node = NULL;
619 u32 hvalue;
620 unsigned long flag;
621 spinlock_t *lock;
622 struct hlist_head *head;
623
624 if (avc_latest_notif_update(avd->seqno, 1))
625 return;
626
627 node = avc_alloc_node();
628 if (!node)
629 return;
630
631 avc_node_populate(node, ssid, tsid, tclass, avd);
632 if (avc_xperms_populate(node, xp_node)) {
633 avc_node_kill(node);
634 return;
635 }
636
637 hvalue = avc_hash(ssid, tsid, tclass);
638 head = &selinux_avc.avc_cache.slots[hvalue];
639 lock = &selinux_avc.avc_cache.slots_lock[hvalue];
640 spin_lock_irqsave(lock, flag);
641 hlist_for_each_entry(pos, head, list) {
642 if (pos->ae.ssid == ssid &&
643 pos->ae.tsid == tsid &&
644 pos->ae.tclass == tclass) {
645 avc_node_replace(node, pos);
646 goto found;
647 }
648 }
649 hlist_add_head_rcu(&node->list, head);
650 trace_android_rvh_selinux_avc_insert(node);
651 found:
652 spin_unlock_irqrestore(lock, flag);
653 }
654
655 /**
656 * avc_audit_pre_callback - SELinux specific information
657 * will be called by generic audit code
658 * @ab: the audit buffer
659 * @a: audit_data
660 */
avc_audit_pre_callback(struct audit_buffer * ab,void * a)661 static void avc_audit_pre_callback(struct audit_buffer *ab, void *a)
662 {
663 struct common_audit_data *ad = a;
664 struct selinux_audit_data *sad = ad->selinux_audit_data;
665 u32 av = sad->audited, perm;
666 const char *const *perms;
667 u32 i;
668
669 audit_log_format(ab, "avc: %s ", sad->denied ? "denied" : "granted");
670
671 if (av == 0) {
672 audit_log_format(ab, " null");
673 return;
674 }
675
676 perms = secclass_map[sad->tclass-1].perms;
677
678 audit_log_format(ab, " {");
679 i = 0;
680 perm = 1;
681 while (i < (sizeof(av) * 8)) {
682 if ((perm & av) && perms[i]) {
683 audit_log_format(ab, " %s", perms[i]);
684 av &= ~perm;
685 }
686 i++;
687 perm <<= 1;
688 }
689
690 if (av)
691 audit_log_format(ab, " 0x%x", av);
692
693 audit_log_format(ab, " } for ");
694 }
695
696 /**
697 * avc_audit_post_callback - SELinux specific information
698 * will be called by generic audit code
699 * @ab: the audit buffer
700 * @a: audit_data
701 */
avc_audit_post_callback(struct audit_buffer * ab,void * a)702 static void avc_audit_post_callback(struct audit_buffer *ab, void *a)
703 {
704 struct common_audit_data *ad = a;
705 struct selinux_audit_data *sad = ad->selinux_audit_data;
706 char *scontext = NULL;
707 char *tcontext = NULL;
708 const char *tclass = NULL;
709 u32 scontext_len;
710 u32 tcontext_len;
711 int rc;
712
713 rc = security_sid_to_context(sad->ssid, &scontext,
714 &scontext_len);
715 if (rc)
716 audit_log_format(ab, " ssid=%d", sad->ssid);
717 else
718 audit_log_format(ab, " scontext=%s", scontext);
719
720 rc = security_sid_to_context(sad->tsid, &tcontext,
721 &tcontext_len);
722 if (rc)
723 audit_log_format(ab, " tsid=%d", sad->tsid);
724 else
725 audit_log_format(ab, " tcontext=%s", tcontext);
726
727 tclass = secclass_map[sad->tclass-1].name;
728 audit_log_format(ab, " tclass=%s", tclass);
729
730 if (sad->denied)
731 audit_log_format(ab, " permissive=%u", sad->result ? 0 : 1);
732
733 trace_selinux_audited(sad, scontext, tcontext, tclass);
734 kfree(tcontext);
735 kfree(scontext);
736
737 /* in case of invalid context report also the actual context string */
738 rc = security_sid_to_context_inval(sad->ssid, &scontext,
739 &scontext_len);
740 if (!rc && scontext) {
741 if (scontext_len && scontext[scontext_len - 1] == '\0')
742 scontext_len--;
743 audit_log_format(ab, " srawcon=");
744 audit_log_n_untrustedstring(ab, scontext, scontext_len);
745 kfree(scontext);
746 }
747
748 rc = security_sid_to_context_inval(sad->tsid, &scontext,
749 &scontext_len);
750 if (!rc && scontext) {
751 if (scontext_len && scontext[scontext_len - 1] == '\0')
752 scontext_len--;
753 audit_log_format(ab, " trawcon=");
754 audit_log_n_untrustedstring(ab, scontext, scontext_len);
755 kfree(scontext);
756 }
757 }
758
759 /*
760 * This is the slow part of avc audit with big stack footprint.
761 * Note that it is non-blocking and can be called from under
762 * rcu_read_lock().
763 */
slow_avc_audit(u32 ssid,u32 tsid,u16 tclass,u32 requested,u32 audited,u32 denied,int result,struct common_audit_data * a)764 noinline int slow_avc_audit(u32 ssid, u32 tsid, u16 tclass,
765 u32 requested, u32 audited, u32 denied, int result,
766 struct common_audit_data *a)
767 {
768 struct common_audit_data stack_data;
769 struct selinux_audit_data sad;
770
771 if (WARN_ON(!tclass || tclass >= ARRAY_SIZE(secclass_map)))
772 return -EINVAL;
773
774 if (!a) {
775 a = &stack_data;
776 a->type = LSM_AUDIT_DATA_NONE;
777 }
778
779 sad.tclass = tclass;
780 sad.requested = requested;
781 sad.ssid = ssid;
782 sad.tsid = tsid;
783 sad.audited = audited;
784 sad.denied = denied;
785 sad.result = result;
786
787 a->selinux_audit_data = &sad;
788
789 common_lsm_audit(a, avc_audit_pre_callback, avc_audit_post_callback);
790 return 0;
791 }
792
793 /**
794 * avc_add_callback - Register a callback for security events.
795 * @callback: callback function
796 * @events: security events
797 *
798 * Register a callback function for events in the set @events.
799 * Returns %0 on success or -%ENOMEM if insufficient memory
800 * exists to add the callback.
801 */
avc_add_callback(int (* callback)(u32 event),u32 events)802 int __init avc_add_callback(int (*callback)(u32 event), u32 events)
803 {
804 struct avc_callback_node *c;
805 int rc = 0;
806
807 c = kmalloc(sizeof(*c), GFP_KERNEL);
808 if (!c) {
809 rc = -ENOMEM;
810 goto out;
811 }
812
813 c->callback = callback;
814 c->events = events;
815 c->next = avc_callbacks;
816 avc_callbacks = c;
817 out:
818 return rc;
819 }
820
821 /**
822 * avc_update_node - Update an AVC entry
823 * @event : Updating event
824 * @perms : Permission mask bits
825 * @driver: xperm driver information
826 * @xperm: xperm permissions
827 * @ssid: AVC entry source sid
828 * @tsid: AVC entry target sid
829 * @tclass : AVC entry target object class
830 * @seqno : sequence number when decision was made
831 * @xpd: extended_perms_decision to be added to the node
832 * @flags: the AVC_* flags, e.g. AVC_EXTENDED_PERMS, or 0.
833 *
834 * if a valid AVC entry doesn't exist,this function returns -ENOENT.
835 * if kmalloc() called internal returns NULL, this function returns -ENOMEM.
836 * otherwise, this function updates the AVC entry. The original AVC-entry object
837 * will release later by RCU.
838 */
avc_update_node(u32 event,u32 perms,u8 driver,u8 xperm,u32 ssid,u32 tsid,u16 tclass,u32 seqno,struct extended_perms_decision * xpd,u32 flags)839 static int avc_update_node(u32 event, u32 perms, u8 driver, u8 xperm, u32 ssid,
840 u32 tsid, u16 tclass, u32 seqno,
841 struct extended_perms_decision *xpd,
842 u32 flags)
843 {
844 u32 hvalue;
845 int rc = 0;
846 unsigned long flag;
847 struct avc_node *pos, *node, *orig = NULL;
848 struct hlist_head *head;
849 spinlock_t *lock;
850
851 node = avc_alloc_node();
852 if (!node) {
853 rc = -ENOMEM;
854 goto out;
855 }
856
857 /* Lock the target slot */
858 hvalue = avc_hash(ssid, tsid, tclass);
859
860 head = &selinux_avc.avc_cache.slots[hvalue];
861 lock = &selinux_avc.avc_cache.slots_lock[hvalue];
862
863 spin_lock_irqsave(lock, flag);
864
865 hlist_for_each_entry(pos, head, list) {
866 if (ssid == pos->ae.ssid &&
867 tsid == pos->ae.tsid &&
868 tclass == pos->ae.tclass &&
869 seqno == pos->ae.avd.seqno){
870 orig = pos;
871 break;
872 }
873 }
874
875 if (!orig) {
876 rc = -ENOENT;
877 avc_node_kill(node);
878 goto out_unlock;
879 }
880
881 /*
882 * Copy and replace original node.
883 */
884
885 avc_node_populate(node, ssid, tsid, tclass, &orig->ae.avd);
886
887 if (orig->ae.xp_node) {
888 rc = avc_xperms_populate(node, orig->ae.xp_node);
889 if (rc) {
890 avc_node_kill(node);
891 goto out_unlock;
892 }
893 }
894
895 switch (event) {
896 case AVC_CALLBACK_GRANT:
897 node->ae.avd.allowed |= perms;
898 if (node->ae.xp_node && (flags & AVC_EXTENDED_PERMS))
899 avc_xperms_allow_perm(node->ae.xp_node, driver, xperm);
900 break;
901 case AVC_CALLBACK_TRY_REVOKE:
902 case AVC_CALLBACK_REVOKE:
903 node->ae.avd.allowed &= ~perms;
904 break;
905 case AVC_CALLBACK_AUDITALLOW_ENABLE:
906 node->ae.avd.auditallow |= perms;
907 break;
908 case AVC_CALLBACK_AUDITALLOW_DISABLE:
909 node->ae.avd.auditallow &= ~perms;
910 break;
911 case AVC_CALLBACK_AUDITDENY_ENABLE:
912 node->ae.avd.auditdeny |= perms;
913 break;
914 case AVC_CALLBACK_AUDITDENY_DISABLE:
915 node->ae.avd.auditdeny &= ~perms;
916 break;
917 case AVC_CALLBACK_ADD_XPERMS:
918 rc = avc_add_xperms_decision(node, xpd);
919 if (rc) {
920 avc_node_kill(node);
921 goto out_unlock;
922 }
923 break;
924 }
925 avc_node_replace(node, orig);
926 out_unlock:
927 spin_unlock_irqrestore(lock, flag);
928 out:
929 return rc;
930 }
931
932 /**
933 * avc_flush - Flush the cache
934 */
avc_flush(void)935 static void avc_flush(void)
936 {
937 struct hlist_head *head;
938 struct avc_node *node;
939 spinlock_t *lock;
940 unsigned long flag;
941 int i;
942
943 for (i = 0; i < AVC_CACHE_SLOTS; i++) {
944 head = &selinux_avc.avc_cache.slots[i];
945 lock = &selinux_avc.avc_cache.slots_lock[i];
946
947 spin_lock_irqsave(lock, flag);
948 /*
949 * With preemptable RCU, the outer spinlock does not
950 * prevent RCU grace periods from ending.
951 */
952 rcu_read_lock();
953 hlist_for_each_entry(node, head, list)
954 avc_node_delete(node);
955 rcu_read_unlock();
956 spin_unlock_irqrestore(lock, flag);
957 }
958 }
959
960 /**
961 * avc_ss_reset - Flush the cache and revalidate migrated permissions.
962 * @seqno: policy sequence number
963 */
avc_ss_reset(u32 seqno)964 int avc_ss_reset(u32 seqno)
965 {
966 struct avc_callback_node *c;
967 int rc = 0, tmprc;
968
969 avc_flush();
970
971 for (c = avc_callbacks; c; c = c->next) {
972 if (c->events & AVC_CALLBACK_RESET) {
973 tmprc = c->callback(AVC_CALLBACK_RESET);
974 /* save the first error encountered for the return
975 value and continue processing the callbacks */
976 if (!rc)
977 rc = tmprc;
978 }
979 }
980
981 avc_latest_notif_update(seqno, 0);
982 return rc;
983 }
984
985 /**
986 * avc_compute_av - Add an entry to the AVC based on the security policy
987 * @ssid: subject
988 * @tsid: object/target
989 * @tclass: object class
990 * @avd: access vector decision
991 * @xp_node: AVC extended permissions node
992 *
993 * Slow-path helper function for avc_has_perm_noaudit, when the avc_node lookup
994 * fails. Don't inline this, since it's the slow-path and just results in a
995 * bigger stack frame.
996 */
avc_compute_av(u32 ssid,u32 tsid,u16 tclass,struct av_decision * avd,struct avc_xperms_node * xp_node)997 static noinline void avc_compute_av(u32 ssid, u32 tsid, u16 tclass,
998 struct av_decision *avd,
999 struct avc_xperms_node *xp_node)
1000 {
1001 INIT_LIST_HEAD(&xp_node->xpd_head);
1002 security_compute_av(ssid, tsid, tclass, avd, &xp_node->xp);
1003 avc_insert(ssid, tsid, tclass, avd, xp_node);
1004 }
1005
avc_denied(u32 ssid,u32 tsid,u16 tclass,u32 requested,u8 driver,u8 xperm,unsigned int flags,struct av_decision * avd)1006 static noinline int avc_denied(u32 ssid, u32 tsid,
1007 u16 tclass, u32 requested,
1008 u8 driver, u8 xperm, unsigned int flags,
1009 struct av_decision *avd)
1010 {
1011 if (flags & AVC_STRICT)
1012 return -EACCES;
1013
1014 if (enforcing_enabled() &&
1015 !(avd->flags & AVD_FLAGS_PERMISSIVE))
1016 return -EACCES;
1017
1018 avc_update_node(AVC_CALLBACK_GRANT, requested, driver,
1019 xperm, ssid, tsid, tclass, avd->seqno, NULL, flags);
1020 return 0;
1021 }
1022
1023 /*
1024 * The avc extended permissions logic adds an additional 256 bits of
1025 * permissions to an avc node when extended permissions for that node are
1026 * specified in the avtab. If the additional 256 permissions is not adequate,
1027 * as-is the case with ioctls, then multiple may be chained together and the
1028 * driver field is used to specify which set contains the permission.
1029 */
avc_has_extended_perms(u32 ssid,u32 tsid,u16 tclass,u32 requested,u8 driver,u8 xperm,struct common_audit_data * ad)1030 int avc_has_extended_perms(u32 ssid, u32 tsid, u16 tclass, u32 requested,
1031 u8 driver, u8 xperm, struct common_audit_data *ad)
1032 {
1033 struct avc_node *node;
1034 struct av_decision avd;
1035 u32 denied;
1036 struct extended_perms_decision local_xpd;
1037 struct extended_perms_decision *xpd = NULL;
1038 struct extended_perms_data allowed;
1039 struct extended_perms_data auditallow;
1040 struct extended_perms_data dontaudit;
1041 struct avc_xperms_node local_xp_node;
1042 struct avc_xperms_node *xp_node;
1043 int rc = 0, rc2;
1044
1045 xp_node = &local_xp_node;
1046 if (WARN_ON(!requested))
1047 return -EACCES;
1048
1049 rcu_read_lock();
1050
1051 node = avc_lookup(ssid, tsid, tclass);
1052 if (unlikely(!node)) {
1053 avc_compute_av(ssid, tsid, tclass, &avd, xp_node);
1054 } else {
1055 memcpy(&avd, &node->ae.avd, sizeof(avd));
1056 xp_node = node->ae.xp_node;
1057 }
1058 /* if extended permissions are not defined, only consider av_decision */
1059 if (!xp_node || !xp_node->xp.len)
1060 goto decision;
1061
1062 local_xpd.allowed = &allowed;
1063 local_xpd.auditallow = &auditallow;
1064 local_xpd.dontaudit = &dontaudit;
1065
1066 xpd = avc_xperms_decision_lookup(driver, xp_node);
1067 if (unlikely(!xpd)) {
1068 /*
1069 * Compute the extended_perms_decision only if the driver
1070 * is flagged
1071 */
1072 if (!security_xperm_test(xp_node->xp.drivers.p, driver)) {
1073 avd.allowed &= ~requested;
1074 goto decision;
1075 }
1076 rcu_read_unlock();
1077 security_compute_xperms_decision(ssid, tsid, tclass,
1078 driver, &local_xpd);
1079 rcu_read_lock();
1080 avc_update_node(AVC_CALLBACK_ADD_XPERMS, requested,
1081 driver, xperm, ssid, tsid, tclass, avd.seqno,
1082 &local_xpd, 0);
1083 } else {
1084 avc_quick_copy_xperms_decision(xperm, &local_xpd, xpd);
1085 }
1086 xpd = &local_xpd;
1087
1088 if (!avc_xperms_has_perm(xpd, xperm, XPERMS_ALLOWED))
1089 avd.allowed &= ~requested;
1090
1091 decision:
1092 denied = requested & ~(avd.allowed);
1093 if (unlikely(denied))
1094 rc = avc_denied(ssid, tsid, tclass, requested,
1095 driver, xperm, AVC_EXTENDED_PERMS, &avd);
1096
1097 rcu_read_unlock();
1098
1099 rc2 = avc_xperms_audit(ssid, tsid, tclass, requested,
1100 &avd, xpd, xperm, rc, ad);
1101 if (rc2)
1102 return rc2;
1103 return rc;
1104 }
1105
1106 /**
1107 * avc_perm_nonode - Add an entry to the AVC
1108 * @ssid: subject
1109 * @tsid: object/target
1110 * @tclass: object class
1111 * @requested: requested permissions
1112 * @flags: AVC flags
1113 * @avd: access vector decision
1114 *
1115 * This is the "we have no node" part of avc_has_perm_noaudit(), which is
1116 * unlikely and needs extra stack space for the new node that we generate, so
1117 * don't inline it.
1118 */
avc_perm_nonode(u32 ssid,u32 tsid,u16 tclass,u32 requested,unsigned int flags,struct av_decision * avd)1119 static noinline int avc_perm_nonode(u32 ssid, u32 tsid, u16 tclass,
1120 u32 requested, unsigned int flags,
1121 struct av_decision *avd)
1122 {
1123 u32 denied;
1124 struct avc_xperms_node xp_node;
1125
1126 avc_compute_av(ssid, tsid, tclass, avd, &xp_node);
1127 denied = requested & ~(avd->allowed);
1128 if (unlikely(denied))
1129 return avc_denied(ssid, tsid, tclass, requested, 0, 0,
1130 flags, avd);
1131 return 0;
1132 }
1133
1134 /**
1135 * avc_has_perm_noaudit - Check permissions but perform no auditing.
1136 * @ssid: source security identifier
1137 * @tsid: target security identifier
1138 * @tclass: target security class
1139 * @requested: requested permissions, interpreted based on @tclass
1140 * @flags: AVC_STRICT or 0
1141 * @avd: access vector decisions
1142 *
1143 * Check the AVC to determine whether the @requested permissions are granted
1144 * for the SID pair (@ssid, @tsid), interpreting the permissions
1145 * based on @tclass, and call the security server on a cache miss to obtain
1146 * a new decision and add it to the cache. Return a copy of the decisions
1147 * in @avd. Return %0 if all @requested permissions are granted,
1148 * -%EACCES if any permissions are denied, or another -errno upon
1149 * other errors. This function is typically called by avc_has_perm(),
1150 * but may also be called directly to separate permission checking from
1151 * auditing, e.g. in cases where a lock must be held for the check but
1152 * should be released for the auditing.
1153 */
avc_has_perm_noaudit(u32 ssid,u32 tsid,u16 tclass,u32 requested,unsigned int flags,struct av_decision * avd)1154 inline int avc_has_perm_noaudit(u32 ssid, u32 tsid,
1155 u16 tclass, u32 requested,
1156 unsigned int flags,
1157 struct av_decision *avd)
1158 {
1159 u32 denied;
1160 struct avc_node *node;
1161
1162 if (WARN_ON(!requested))
1163 return -EACCES;
1164
1165 rcu_read_lock();
1166 node = avc_lookup(ssid, tsid, tclass);
1167 if (unlikely(!node)) {
1168 rcu_read_unlock();
1169 return avc_perm_nonode(ssid, tsid, tclass, requested,
1170 flags, avd);
1171 }
1172 denied = requested & ~node->ae.avd.allowed;
1173 memcpy(avd, &node->ae.avd, sizeof(*avd));
1174 rcu_read_unlock();
1175
1176 if (unlikely(denied))
1177 return avc_denied(ssid, tsid, tclass, requested, 0, 0,
1178 flags, avd);
1179 return 0;
1180 }
1181
1182 /**
1183 * avc_has_perm - Check permissions and perform any appropriate auditing.
1184 * @ssid: source security identifier
1185 * @tsid: target security identifier
1186 * @tclass: target security class
1187 * @requested: requested permissions, interpreted based on @tclass
1188 * @auditdata: auxiliary audit data
1189 *
1190 * Check the AVC to determine whether the @requested permissions are granted
1191 * for the SID pair (@ssid, @tsid), interpreting the permissions
1192 * based on @tclass, and call the security server on a cache miss to obtain
1193 * a new decision and add it to the cache. Audit the granting or denial of
1194 * permissions in accordance with the policy. Return %0 if all @requested
1195 * permissions are granted, -%EACCES if any permissions are denied, or
1196 * another -errno upon other errors.
1197 */
avc_has_perm(u32 ssid,u32 tsid,u16 tclass,u32 requested,struct common_audit_data * auditdata)1198 int avc_has_perm(u32 ssid, u32 tsid, u16 tclass,
1199 u32 requested, struct common_audit_data *auditdata)
1200 {
1201 struct av_decision avd;
1202 int rc, rc2;
1203
1204 rc = avc_has_perm_noaudit(ssid, tsid, tclass, requested, 0,
1205 &avd);
1206
1207 rc2 = avc_audit(ssid, tsid, tclass, requested, &avd, rc,
1208 auditdata);
1209 if (rc2)
1210 return rc2;
1211 return rc;
1212 }
1213
avc_policy_seqno(void)1214 u32 avc_policy_seqno(void)
1215 {
1216 return selinux_avc.avc_cache.latest_notif;
1217 }
1218