avc.c 31 KB

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