avc.c 31 KB

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