avc.c 34 KB

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