avc.c 31 KB

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