avc.c 31 KB

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