avc.c 32 KB

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