avc.c 34 KB

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