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