avc.c 33 KB

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