avc.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Implementation of the kernel access vector cache (AVC).
  4. *
  5. * Authors: Stephen Smalley, <stephen.smalley.work@gmail.com>
  6. * James Morris <jmorris@redhat.com>
  7. *
  8. * Update: KaiGai, Kohei <kaigai@ak.jp.nec.com>
  9. * Replaced the avc_lock spinlock by RCU.
  10. *
  11. * Copyright (C) 2003 Red Hat, Inc., James Morris <jmorris@redhat.com>
  12. */
  13. #include <linux/types.h>
  14. #include <linux/stddef.h>
  15. #include <linux/kernel.h>
  16. #include <linux/slab.h>
  17. #include <linux/fs.h>
  18. #include <linux/dcache.h>
  19. #include <linux/init.h>
  20. #include <linux/skbuff.h>
  21. #include <linux/percpu.h>
  22. #include <linux/list.h>
  23. #include <net/sock.h>
  24. #include <linux/un.h>
  25. #include <net/af_unix.h>
  26. #include <linux/ip.h>
  27. #include <linux/audit.h>
  28. #include <linux/ipv6.h>
  29. #include <net/ipv6.h>
  30. #include "avc.h"
  31. #include "avc_ss.h"
  32. #include "classmap.h"
  33. #define CREATE_TRACE_POINTS
  34. #include <trace/events/avc.h>
  35. #define AVC_CACHE_SLOTS 512
  36. #define AVC_DEF_CACHE_THRESHOLD 512
  37. #define AVC_CACHE_RECLAIM 16
  38. #ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
  39. #define avc_cache_stats_incr(field) this_cpu_inc(avc_cache_stats.field)
  40. #else
  41. #define avc_cache_stats_incr(field) do {} while (0)
  42. #endif
  43. struct avc_entry {
  44. u32 ssid;
  45. u32 tsid;
  46. u16 tclass;
  47. struct av_decision avd;
  48. struct avc_xperms_node *xp_node;
  49. };
  50. struct avc_node {
  51. struct avc_entry ae;
  52. struct hlist_node list; /* anchored in avc_cache->slots[i] */
  53. struct rcu_head rhead;
  54. };
  55. struct avc_xperms_decision_node {
  56. struct extended_perms_decision xpd;
  57. struct list_head xpd_list; /* list of extended_perms_decision */
  58. };
  59. struct avc_xperms_node {
  60. struct extended_perms xp;
  61. struct list_head xpd_head; /* list head of extended_perms_decision */
  62. };
  63. struct avc_cache {
  64. struct hlist_head slots[AVC_CACHE_SLOTS]; /* head for avc_node->list */
  65. spinlock_t slots_lock[AVC_CACHE_SLOTS]; /* lock for writes */
  66. atomic_t lru_hint; /* LRU hint for reclaim scan */
  67. atomic_t active_nodes;
  68. u32 latest_notif; /* latest revocation notification */
  69. };
  70. struct avc_callback_node {
  71. int (*callback) (u32 event);
  72. u32 events;
  73. struct avc_callback_node *next;
  74. };
  75. #ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
  76. DEFINE_PER_CPU(struct avc_cache_stats, avc_cache_stats) = { 0 };
  77. #endif
  78. struct selinux_avc {
  79. unsigned int avc_cache_threshold;
  80. struct avc_cache avc_cache;
  81. };
  82. static struct selinux_avc selinux_avc;
  83. void selinux_avc_init(void)
  84. {
  85. int i;
  86. selinux_avc.avc_cache_threshold = AVC_DEF_CACHE_THRESHOLD;
  87. for (i = 0; i < AVC_CACHE_SLOTS; i++) {
  88. INIT_HLIST_HEAD(&selinux_avc.avc_cache.slots[i]);
  89. spin_lock_init(&selinux_avc.avc_cache.slots_lock[i]);
  90. }
  91. atomic_set(&selinux_avc.avc_cache.active_nodes, 0);
  92. atomic_set(&selinux_avc.avc_cache.lru_hint, 0);
  93. }
  94. unsigned int avc_get_cache_threshold(void)
  95. {
  96. return selinux_avc.avc_cache_threshold;
  97. }
  98. void avc_set_cache_threshold(unsigned int cache_threshold)
  99. {
  100. selinux_avc.avc_cache_threshold = cache_threshold;
  101. }
  102. static struct avc_callback_node *avc_callbacks __ro_after_init;
  103. static struct kmem_cache *avc_node_cachep __ro_after_init;
  104. static struct kmem_cache *avc_xperms_data_cachep __ro_after_init;
  105. static struct kmem_cache *avc_xperms_decision_cachep __ro_after_init;
  106. static struct kmem_cache *avc_xperms_cachep __ro_after_init;
  107. static inline u32 avc_hash(u32 ssid, u32 tsid, u16 tclass)
  108. {
  109. return (ssid ^ (tsid<<2) ^ (tclass<<4)) & (AVC_CACHE_SLOTS - 1);
  110. }
  111. /**
  112. * avc_init - Initialize the AVC.
  113. *
  114. * Initialize the access vector cache.
  115. */
  116. void __init avc_init(void)
  117. {
  118. avc_node_cachep = KMEM_CACHE(avc_node, SLAB_PANIC);
  119. avc_xperms_cachep = KMEM_CACHE(avc_xperms_node, SLAB_PANIC);
  120. avc_xperms_decision_cachep = KMEM_CACHE(avc_xperms_decision_node, SLAB_PANIC);
  121. avc_xperms_data_cachep = KMEM_CACHE(extended_perms_data, SLAB_PANIC);
  122. }
  123. int avc_get_hash_stats(char *page)
  124. {
  125. int i, chain_len, max_chain_len, slots_used;
  126. struct avc_node *node;
  127. struct hlist_head *head;
  128. rcu_read_lock();
  129. slots_used = 0;
  130. max_chain_len = 0;
  131. for (i = 0; i < AVC_CACHE_SLOTS; i++) {
  132. head = &selinux_avc.avc_cache.slots[i];
  133. if (!hlist_empty(head)) {
  134. slots_used++;
  135. chain_len = 0;
  136. hlist_for_each_entry_rcu(node, head, list)
  137. chain_len++;
  138. if (chain_len > max_chain_len)
  139. max_chain_len = chain_len;
  140. }
  141. }
  142. rcu_read_unlock();
  143. return scnprintf(page, PAGE_SIZE, "entries: %d\nbuckets used: %d/%d\n"
  144. "longest chain: %d\n",
  145. atomic_read(&selinux_avc.avc_cache.active_nodes),
  146. slots_used, AVC_CACHE_SLOTS, max_chain_len);
  147. }
  148. /*
  149. * using a linked list for extended_perms_decision lookup because the list is
  150. * always small. i.e. less than 5, typically 1
  151. */
  152. static struct extended_perms_decision *avc_xperms_decision_lookup(u8 driver,
  153. struct avc_xperms_node *xp_node)
  154. {
  155. struct avc_xperms_decision_node *xpd_node;
  156. list_for_each_entry(xpd_node, &xp_node->xpd_head, xpd_list) {
  157. if (xpd_node->xpd.driver == driver)
  158. return &xpd_node->xpd;
  159. }
  160. return NULL;
  161. }
  162. static inline unsigned int
  163. avc_xperms_has_perm(struct extended_perms_decision *xpd,
  164. u8 perm, u8 which)
  165. {
  166. unsigned int rc = 0;
  167. if ((which == XPERMS_ALLOWED) &&
  168. (xpd->used & XPERMS_ALLOWED))
  169. rc = security_xperm_test(xpd->allowed->p, perm);
  170. else if ((which == XPERMS_AUDITALLOW) &&
  171. (xpd->used & XPERMS_AUDITALLOW))
  172. rc = security_xperm_test(xpd->auditallow->p, perm);
  173. else if ((which == XPERMS_DONTAUDIT) &&
  174. (xpd->used & XPERMS_DONTAUDIT))
  175. rc = security_xperm_test(xpd->dontaudit->p, perm);
  176. return rc;
  177. }
  178. static void avc_xperms_allow_perm(struct avc_xperms_node *xp_node,
  179. u8 driver, u8 perm)
  180. {
  181. struct extended_perms_decision *xpd;
  182. security_xperm_set(xp_node->xp.drivers.p, driver);
  183. xpd = avc_xperms_decision_lookup(driver, xp_node);
  184. if (xpd && xpd->allowed)
  185. security_xperm_set(xpd->allowed->p, perm);
  186. }
  187. static void avc_xperms_decision_free(struct avc_xperms_decision_node *xpd_node)
  188. {
  189. struct extended_perms_decision *xpd;
  190. xpd = &xpd_node->xpd;
  191. if (xpd->allowed)
  192. kmem_cache_free(avc_xperms_data_cachep, xpd->allowed);
  193. if (xpd->auditallow)
  194. kmem_cache_free(avc_xperms_data_cachep, xpd->auditallow);
  195. if (xpd->dontaudit)
  196. kmem_cache_free(avc_xperms_data_cachep, xpd->dontaudit);
  197. kmem_cache_free(avc_xperms_decision_cachep, xpd_node);
  198. }
  199. static void avc_xperms_free(struct avc_xperms_node *xp_node)
  200. {
  201. struct avc_xperms_decision_node *xpd_node, *tmp;
  202. if (!xp_node)
  203. return;
  204. list_for_each_entry_safe(xpd_node, tmp, &xp_node->xpd_head, xpd_list) {
  205. list_del(&xpd_node->xpd_list);
  206. avc_xperms_decision_free(xpd_node);
  207. }
  208. kmem_cache_free(avc_xperms_cachep, xp_node);
  209. }
  210. static void avc_copy_xperms_decision(struct extended_perms_decision *dest,
  211. struct extended_perms_decision *src)
  212. {
  213. dest->driver = src->driver;
  214. dest->used = src->used;
  215. if (dest->used & XPERMS_ALLOWED)
  216. memcpy(dest->allowed->p, src->allowed->p,
  217. sizeof(src->allowed->p));
  218. if (dest->used & XPERMS_AUDITALLOW)
  219. memcpy(dest->auditallow->p, src->auditallow->p,
  220. sizeof(src->auditallow->p));
  221. if (dest->used & XPERMS_DONTAUDIT)
  222. memcpy(dest->dontaudit->p, src->dontaudit->p,
  223. sizeof(src->dontaudit->p));
  224. }
  225. /*
  226. * similar to avc_copy_xperms_decision, but only copy decision
  227. * information relevant to this perm
  228. */
  229. static inline void avc_quick_copy_xperms_decision(u8 perm,
  230. struct extended_perms_decision *dest,
  231. struct extended_perms_decision *src)
  232. {
  233. /*
  234. * compute index of the u32 of the 256 bits (8 u32s) that contain this
  235. * command permission
  236. */
  237. u8 i = perm >> 5;
  238. dest->used = src->used;
  239. if (dest->used & XPERMS_ALLOWED)
  240. dest->allowed->p[i] = src->allowed->p[i];
  241. if (dest->used & XPERMS_AUDITALLOW)
  242. dest->auditallow->p[i] = src->auditallow->p[i];
  243. if (dest->used & XPERMS_DONTAUDIT)
  244. dest->dontaudit->p[i] = src->dontaudit->p[i];
  245. }
  246. static struct avc_xperms_decision_node
  247. *avc_xperms_decision_alloc(u8 which)
  248. {
  249. struct avc_xperms_decision_node *xpd_node;
  250. struct extended_perms_decision *xpd;
  251. xpd_node = kmem_cache_zalloc(avc_xperms_decision_cachep,
  252. GFP_NOWAIT | __GFP_NOWARN);
  253. if (!xpd_node)
  254. return NULL;
  255. xpd = &xpd_node->xpd;
  256. if (which & XPERMS_ALLOWED) {
  257. xpd->allowed = kmem_cache_zalloc(avc_xperms_data_cachep,
  258. GFP_NOWAIT | __GFP_NOWARN);
  259. if (!xpd->allowed)
  260. goto error;
  261. }
  262. if (which & XPERMS_AUDITALLOW) {
  263. xpd->auditallow = kmem_cache_zalloc(avc_xperms_data_cachep,
  264. GFP_NOWAIT | __GFP_NOWARN);
  265. if (!xpd->auditallow)
  266. goto error;
  267. }
  268. if (which & XPERMS_DONTAUDIT) {
  269. xpd->dontaudit = kmem_cache_zalloc(avc_xperms_data_cachep,
  270. GFP_NOWAIT | __GFP_NOWARN);
  271. if (!xpd->dontaudit)
  272. goto error;
  273. }
  274. return xpd_node;
  275. error:
  276. avc_xperms_decision_free(xpd_node);
  277. return NULL;
  278. }
  279. static int avc_add_xperms_decision(struct avc_node *node,
  280. struct extended_perms_decision *src)
  281. {
  282. struct avc_xperms_decision_node *dest_xpd;
  283. dest_xpd = avc_xperms_decision_alloc(src->used);
  284. if (!dest_xpd)
  285. return -ENOMEM;
  286. avc_copy_xperms_decision(&dest_xpd->xpd, src);
  287. list_add(&dest_xpd->xpd_list, &node->ae.xp_node->xpd_head);
  288. node->ae.xp_node->xp.len++;
  289. return 0;
  290. }
  291. static struct avc_xperms_node *avc_xperms_alloc(void)
  292. {
  293. struct avc_xperms_node *xp_node;
  294. xp_node = kmem_cache_zalloc(avc_xperms_cachep, GFP_NOWAIT | __GFP_NOWARN);
  295. if (!xp_node)
  296. return xp_node;
  297. INIT_LIST_HEAD(&xp_node->xpd_head);
  298. return xp_node;
  299. }
  300. static int avc_xperms_populate(struct avc_node *node,
  301. struct avc_xperms_node *src)
  302. {
  303. struct avc_xperms_node *dest;
  304. struct avc_xperms_decision_node *dest_xpd;
  305. struct avc_xperms_decision_node *src_xpd;
  306. if (src->xp.len == 0)
  307. return 0;
  308. dest = avc_xperms_alloc();
  309. if (!dest)
  310. return -ENOMEM;
  311. memcpy(dest->xp.drivers.p, src->xp.drivers.p, sizeof(dest->xp.drivers.p));
  312. dest->xp.len = src->xp.len;
  313. /* for each source xpd allocate a destination xpd and copy */
  314. list_for_each_entry(src_xpd, &src->xpd_head, xpd_list) {
  315. dest_xpd = avc_xperms_decision_alloc(src_xpd->xpd.used);
  316. if (!dest_xpd)
  317. goto error;
  318. avc_copy_xperms_decision(&dest_xpd->xpd, &src_xpd->xpd);
  319. list_add(&dest_xpd->xpd_list, &dest->xpd_head);
  320. }
  321. node->ae.xp_node = dest;
  322. return 0;
  323. error:
  324. avc_xperms_free(dest);
  325. return -ENOMEM;
  326. }
  327. static inline u32 avc_xperms_audit_required(u32 requested,
  328. struct av_decision *avd,
  329. struct extended_perms_decision *xpd,
  330. u8 perm,
  331. int result,
  332. u32 *deniedp)
  333. {
  334. u32 denied, audited;
  335. denied = requested & ~avd->allowed;
  336. if (unlikely(denied)) {
  337. audited = denied & avd->auditdeny;
  338. if (audited && xpd) {
  339. if (avc_xperms_has_perm(xpd, perm, XPERMS_DONTAUDIT))
  340. audited = 0;
  341. }
  342. } else if (result) {
  343. audited = denied = requested;
  344. } else {
  345. audited = requested & avd->auditallow;
  346. if (audited && xpd) {
  347. if (!avc_xperms_has_perm(xpd, perm, XPERMS_AUDITALLOW))
  348. audited = 0;
  349. }
  350. }
  351. *deniedp = denied;
  352. return audited;
  353. }
  354. static inline int avc_xperms_audit(u32 ssid, u32 tsid, u16 tclass,
  355. u32 requested, struct av_decision *avd,
  356. struct extended_perms_decision *xpd,
  357. u8 perm, int result,
  358. struct common_audit_data *ad)
  359. {
  360. u32 audited, denied;
  361. audited = avc_xperms_audit_required(
  362. requested, avd, xpd, perm, result, &denied);
  363. if (likely(!audited))
  364. return 0;
  365. return slow_avc_audit(ssid, tsid, tclass, requested,
  366. audited, denied, result, ad);
  367. }
  368. static void avc_node_free(struct rcu_head *rhead)
  369. {
  370. struct avc_node *node = container_of(rhead, struct avc_node, rhead);
  371. avc_xperms_free(node->ae.xp_node);
  372. kmem_cache_free(avc_node_cachep, node);
  373. avc_cache_stats_incr(frees);
  374. }
  375. static void avc_node_delete(struct avc_node *node)
  376. {
  377. hlist_del_rcu(&node->list);
  378. call_rcu(&node->rhead, avc_node_free);
  379. atomic_dec(&selinux_avc.avc_cache.active_nodes);
  380. }
  381. static void avc_node_kill(struct avc_node *node)
  382. {
  383. avc_xperms_free(node->ae.xp_node);
  384. kmem_cache_free(avc_node_cachep, node);
  385. avc_cache_stats_incr(frees);
  386. atomic_dec(&selinux_avc.avc_cache.active_nodes);
  387. }
  388. static void avc_node_replace(struct avc_node *new, struct avc_node *old)
  389. {
  390. hlist_replace_rcu(&old->list, &new->list);
  391. call_rcu(&old->rhead, avc_node_free);
  392. atomic_dec(&selinux_avc.avc_cache.active_nodes);
  393. }
  394. static inline int avc_reclaim_node(void)
  395. {
  396. struct avc_node *node;
  397. int hvalue, try, ecx;
  398. unsigned long flags;
  399. struct hlist_head *head;
  400. spinlock_t *lock;
  401. for (try = 0, ecx = 0; try < AVC_CACHE_SLOTS; try++) {
  402. hvalue = atomic_inc_return(&selinux_avc.avc_cache.lru_hint) &
  403. (AVC_CACHE_SLOTS - 1);
  404. head = &selinux_avc.avc_cache.slots[hvalue];
  405. lock = &selinux_avc.avc_cache.slots_lock[hvalue];
  406. if (!spin_trylock_irqsave(lock, flags))
  407. continue;
  408. rcu_read_lock();
  409. hlist_for_each_entry(node, head, list) {
  410. avc_node_delete(node);
  411. avc_cache_stats_incr(reclaims);
  412. ecx++;
  413. if (ecx >= AVC_CACHE_RECLAIM) {
  414. rcu_read_unlock();
  415. spin_unlock_irqrestore(lock, flags);
  416. goto out;
  417. }
  418. }
  419. rcu_read_unlock();
  420. spin_unlock_irqrestore(lock, flags);
  421. }
  422. out:
  423. return ecx;
  424. }
  425. static struct avc_node *avc_alloc_node(void)
  426. {
  427. struct avc_node *node;
  428. node = kmem_cache_zalloc(avc_node_cachep, GFP_NOWAIT | __GFP_NOWARN);
  429. if (!node)
  430. goto out;
  431. INIT_HLIST_NODE(&node->list);
  432. avc_cache_stats_incr(allocations);
  433. if (atomic_inc_return(&selinux_avc.avc_cache.active_nodes) >
  434. selinux_avc.avc_cache_threshold)
  435. avc_reclaim_node();
  436. out:
  437. return node;
  438. }
  439. static void avc_node_populate(struct avc_node *node, u32 ssid, u32 tsid, u16 tclass, struct av_decision *avd)
  440. {
  441. node->ae.ssid = ssid;
  442. node->ae.tsid = tsid;
  443. node->ae.tclass = tclass;
  444. memcpy(&node->ae.avd, avd, sizeof(node->ae.avd));
  445. }
  446. static inline struct avc_node *avc_search_node(u32 ssid, u32 tsid, u16 tclass)
  447. {
  448. struct avc_node *node, *ret = NULL;
  449. u32 hvalue;
  450. struct hlist_head *head;
  451. hvalue = avc_hash(ssid, tsid, tclass);
  452. head = &selinux_avc.avc_cache.slots[hvalue];
  453. hlist_for_each_entry_rcu(node, head, list) {
  454. if (ssid == node->ae.ssid &&
  455. tclass == node->ae.tclass &&
  456. tsid == node->ae.tsid) {
  457. ret = node;
  458. break;
  459. }
  460. }
  461. return ret;
  462. }
  463. /**
  464. * avc_lookup - Look up an AVC entry.
  465. * @ssid: source security identifier
  466. * @tsid: target security identifier
  467. * @tclass: target security class
  468. *
  469. * Look up an AVC entry that is valid for the
  470. * (@ssid, @tsid), interpreting the permissions
  471. * based on @tclass. If a valid AVC entry exists,
  472. * then this function returns the avc_node.
  473. * Otherwise, this function returns NULL.
  474. */
  475. static struct avc_node *avc_lookup(u32 ssid, u32 tsid, u16 tclass)
  476. {
  477. struct avc_node *node;
  478. avc_cache_stats_incr(lookups);
  479. node = avc_search_node(ssid, tsid, tclass);
  480. if (node)
  481. return node;
  482. avc_cache_stats_incr(misses);
  483. return NULL;
  484. }
  485. static int avc_latest_notif_update(u32 seqno, int is_insert)
  486. {
  487. int ret = 0;
  488. static DEFINE_SPINLOCK(notif_lock);
  489. unsigned long flag;
  490. spin_lock_irqsave(&notif_lock, flag);
  491. if (is_insert) {
  492. if (seqno < selinux_avc.avc_cache.latest_notif) {
  493. pr_warn("SELinux: avc: seqno %d < latest_notif %d\n",
  494. seqno, selinux_avc.avc_cache.latest_notif);
  495. ret = -EAGAIN;
  496. }
  497. } else {
  498. if (seqno > selinux_avc.avc_cache.latest_notif)
  499. selinux_avc.avc_cache.latest_notif = seqno;
  500. }
  501. spin_unlock_irqrestore(&notif_lock, flag);
  502. return ret;
  503. }
  504. /**
  505. * avc_insert - Insert an AVC entry.
  506. * @ssid: source security identifier
  507. * @tsid: target security identifier
  508. * @tclass: target security class
  509. * @avd: resulting av decision
  510. * @xp_node: resulting extended permissions
  511. *
  512. * Insert an AVC entry for the SID pair
  513. * (@ssid, @tsid) and class @tclass.
  514. * The access vectors and the sequence number are
  515. * normally provided by the security server in
  516. * response to a security_compute_av() call. If the
  517. * sequence number @avd->seqno is not less than the latest
  518. * revocation notification, then the function copies
  519. * the access vectors into a cache entry.
  520. */
  521. static void avc_insert(u32 ssid, u32 tsid, u16 tclass,
  522. struct av_decision *avd, struct avc_xperms_node *xp_node)
  523. {
  524. struct avc_node *pos, *node = NULL;
  525. u32 hvalue;
  526. unsigned long flag;
  527. spinlock_t *lock;
  528. struct hlist_head *head;
  529. if (avc_latest_notif_update(avd->seqno, 1))
  530. return;
  531. node = avc_alloc_node();
  532. if (!node)
  533. return;
  534. avc_node_populate(node, ssid, tsid, tclass, avd);
  535. if (avc_xperms_populate(node, xp_node)) {
  536. avc_node_kill(node);
  537. return;
  538. }
  539. hvalue = avc_hash(ssid, tsid, tclass);
  540. head = &selinux_avc.avc_cache.slots[hvalue];
  541. lock = &selinux_avc.avc_cache.slots_lock[hvalue];
  542. spin_lock_irqsave(lock, flag);
  543. hlist_for_each_entry(pos, head, list) {
  544. if (pos->ae.ssid == ssid &&
  545. pos->ae.tsid == tsid &&
  546. pos->ae.tclass == tclass) {
  547. avc_node_replace(node, pos);
  548. goto found;
  549. }
  550. }
  551. hlist_add_head_rcu(&node->list, head);
  552. found:
  553. spin_unlock_irqrestore(lock, flag);
  554. }
  555. /**
  556. * avc_audit_pre_callback - SELinux specific information
  557. * will be called by generic audit code
  558. * @ab: the audit buffer
  559. * @a: audit_data
  560. */
  561. static void avc_audit_pre_callback(struct audit_buffer *ab, void *a)
  562. {
  563. struct common_audit_data *ad = a;
  564. struct selinux_audit_data *sad = ad->selinux_audit_data;
  565. u32 av = sad->audited, perm;
  566. const char *const *perms;
  567. u32 i;
  568. audit_log_format(ab, "avc: %s ", sad->denied ? "denied" : "granted");
  569. if (av == 0) {
  570. audit_log_format(ab, " null");
  571. return;
  572. }
  573. perms = secclass_map[sad->tclass-1].perms;
  574. audit_log_format(ab, " {");
  575. i = 0;
  576. perm = 1;
  577. while (i < (sizeof(av) * 8)) {
  578. if ((perm & av) && perms[i]) {
  579. audit_log_format(ab, " %s", perms[i]);
  580. av &= ~perm;
  581. }
  582. i++;
  583. perm <<= 1;
  584. }
  585. if (av)
  586. audit_log_format(ab, " 0x%x", av);
  587. audit_log_format(ab, " } for ");
  588. }
  589. /**
  590. * avc_audit_post_callback - SELinux specific information
  591. * will be called by generic audit code
  592. * @ab: the audit buffer
  593. * @a: audit_data
  594. */
  595. static void avc_audit_post_callback(struct audit_buffer *ab, void *a)
  596. {
  597. struct common_audit_data *ad = a;
  598. struct selinux_audit_data *sad = ad->selinux_audit_data;
  599. char *scontext = NULL;
  600. char *tcontext = NULL;
  601. const char *tclass = NULL;
  602. u32 scontext_len;
  603. u32 tcontext_len;
  604. int rc;
  605. rc = security_sid_to_context(sad->ssid, &scontext,
  606. &scontext_len);
  607. if (rc)
  608. audit_log_format(ab, " ssid=%d", sad->ssid);
  609. else
  610. audit_log_format(ab, " scontext=%s", scontext);
  611. rc = security_sid_to_context(sad->tsid, &tcontext,
  612. &tcontext_len);
  613. if (rc)
  614. audit_log_format(ab, " tsid=%d", sad->tsid);
  615. else
  616. audit_log_format(ab, " tcontext=%s", tcontext);
  617. tclass = secclass_map[sad->tclass-1].name;
  618. audit_log_format(ab, " tclass=%s", tclass);
  619. if (sad->denied)
  620. audit_log_format(ab, " permissive=%u", sad->result ? 0 : 1);
  621. trace_selinux_audited(sad, scontext, tcontext, tclass);
  622. kfree(tcontext);
  623. kfree(scontext);
  624. /* in case of invalid context report also the actual context string */
  625. rc = security_sid_to_context_inval(sad->ssid, &scontext,
  626. &scontext_len);
  627. if (!rc && scontext) {
  628. if (scontext_len && scontext[scontext_len - 1] == '\0')
  629. scontext_len--;
  630. audit_log_format(ab, " srawcon=");
  631. audit_log_n_untrustedstring(ab, scontext, scontext_len);
  632. kfree(scontext);
  633. }
  634. rc = security_sid_to_context_inval(sad->tsid, &scontext,
  635. &scontext_len);
  636. if (!rc && scontext) {
  637. if (scontext_len && scontext[scontext_len - 1] == '\0')
  638. scontext_len--;
  639. audit_log_format(ab, " trawcon=");
  640. audit_log_n_untrustedstring(ab, scontext, scontext_len);
  641. kfree(scontext);
  642. }
  643. }
  644. /*
  645. * This is the slow part of avc audit with big stack footprint.
  646. * Note that it is non-blocking and can be called from under
  647. * rcu_read_lock().
  648. */
  649. noinline int slow_avc_audit(u32 ssid, u32 tsid, u16 tclass,
  650. u32 requested, u32 audited, u32 denied, int result,
  651. struct common_audit_data *a)
  652. {
  653. struct common_audit_data stack_data;
  654. struct selinux_audit_data sad;
  655. if (WARN_ON(!tclass || tclass >= ARRAY_SIZE(secclass_map)))
  656. return -EINVAL;
  657. if (!a) {
  658. a = &stack_data;
  659. a->type = LSM_AUDIT_DATA_NONE;
  660. }
  661. sad.tclass = tclass;
  662. sad.requested = requested;
  663. sad.ssid = ssid;
  664. sad.tsid = tsid;
  665. sad.audited = audited;
  666. sad.denied = denied;
  667. sad.result = result;
  668. a->selinux_audit_data = &sad;
  669. common_lsm_audit(a, avc_audit_pre_callback, avc_audit_post_callback);
  670. return 0;
  671. }
  672. /**
  673. * avc_add_callback - Register a callback for security events.
  674. * @callback: callback function
  675. * @events: security events
  676. *
  677. * Register a callback function for events in the set @events.
  678. * Returns %0 on success or -%ENOMEM if insufficient memory
  679. * exists to add the callback.
  680. */
  681. int __init avc_add_callback(int (*callback)(u32 event), u32 events)
  682. {
  683. struct avc_callback_node *c;
  684. int rc = 0;
  685. c = kmalloc(sizeof(*c), GFP_KERNEL);
  686. if (!c) {
  687. rc = -ENOMEM;
  688. goto out;
  689. }
  690. c->callback = callback;
  691. c->events = events;
  692. c->next = avc_callbacks;
  693. avc_callbacks = c;
  694. out:
  695. return rc;
  696. }
  697. /**
  698. * avc_update_node - Update an AVC entry
  699. * @event : Updating event
  700. * @perms : Permission mask bits
  701. * @driver: xperm driver information
  702. * @xperm: xperm permissions
  703. * @ssid: AVC entry source sid
  704. * @tsid: AVC entry target sid
  705. * @tclass : AVC entry target object class
  706. * @seqno : sequence number when decision was made
  707. * @xpd: extended_perms_decision to be added to the node
  708. * @flags: the AVC_* flags, e.g. AVC_EXTENDED_PERMS, or 0.
  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. u32 hvalue;
  721. int 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 = &selinux_avc.avc_cache.slots[hvalue];
  734. lock = &selinux_avc.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. avc_node_kill(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. rc = avc_add_xperms_decision(node, xpd);
  785. if (rc) {
  786. avc_node_kill(node);
  787. goto out_unlock;
  788. }
  789. break;
  790. }
  791. avc_node_replace(node, orig);
  792. out_unlock:
  793. spin_unlock_irqrestore(lock, flag);
  794. out:
  795. return rc;
  796. }
  797. /**
  798. * avc_flush - Flush the cache
  799. */
  800. static void avc_flush(void)
  801. {
  802. struct hlist_head *head;
  803. struct avc_node *node;
  804. spinlock_t *lock;
  805. unsigned long flag;
  806. int i;
  807. for (i = 0; i < AVC_CACHE_SLOTS; i++) {
  808. head = &selinux_avc.avc_cache.slots[i];
  809. lock = &selinux_avc.avc_cache.slots_lock[i];
  810. spin_lock_irqsave(lock, flag);
  811. /*
  812. * With preemptable RCU, the outer spinlock does not
  813. * prevent RCU grace periods from ending.
  814. */
  815. rcu_read_lock();
  816. hlist_for_each_entry(node, head, list)
  817. avc_node_delete(node);
  818. rcu_read_unlock();
  819. spin_unlock_irqrestore(lock, flag);
  820. }
  821. }
  822. /**
  823. * avc_ss_reset - Flush the cache and revalidate migrated permissions.
  824. * @seqno: policy sequence number
  825. */
  826. int avc_ss_reset(u32 seqno)
  827. {
  828. struct avc_callback_node *c;
  829. int rc = 0, tmprc;
  830. avc_flush();
  831. for (c = avc_callbacks; c; c = c->next) {
  832. if (c->events & AVC_CALLBACK_RESET) {
  833. tmprc = c->callback(AVC_CALLBACK_RESET);
  834. /* save the first error encountered for the return
  835. value and continue processing the callbacks */
  836. if (!rc)
  837. rc = tmprc;
  838. }
  839. }
  840. avc_latest_notif_update(seqno, 0);
  841. return rc;
  842. }
  843. /**
  844. * avc_compute_av - Add an entry to the AVC based on the security policy
  845. * @ssid: subject
  846. * @tsid: object/target
  847. * @tclass: object class
  848. * @avd: access vector decision
  849. * @xp_node: AVC extended permissions node
  850. *
  851. * Slow-path helper function for avc_has_perm_noaudit, when the avc_node lookup
  852. * fails. Don't inline this, since it's the slow-path and just results in a
  853. * bigger stack frame.
  854. */
  855. static noinline void avc_compute_av(u32 ssid, u32 tsid, u16 tclass,
  856. struct av_decision *avd,
  857. struct avc_xperms_node *xp_node)
  858. {
  859. INIT_LIST_HEAD(&xp_node->xpd_head);
  860. security_compute_av(ssid, tsid, tclass, avd, &xp_node->xp);
  861. avc_insert(ssid, tsid, tclass, avd, xp_node);
  862. }
  863. static noinline int avc_denied(u32 ssid, u32 tsid,
  864. u16 tclass, u32 requested,
  865. u8 driver, u8 xperm, unsigned int flags,
  866. struct av_decision *avd)
  867. {
  868. if (flags & AVC_STRICT)
  869. return -EACCES;
  870. if (enforcing_enabled() &&
  871. !(avd->flags & AVD_FLAGS_PERMISSIVE))
  872. return -EACCES;
  873. avc_update_node(AVC_CALLBACK_GRANT, requested, driver,
  874. xperm, ssid, tsid, tclass, avd->seqno, NULL, flags);
  875. return 0;
  876. }
  877. /*
  878. * The avc extended permissions logic adds an additional 256 bits of
  879. * permissions to an avc node when extended permissions for that node are
  880. * specified in the avtab. If the additional 256 permissions is not adequate,
  881. * as-is the case with ioctls, then multiple may be chained together and the
  882. * driver field is used to specify which set contains the permission.
  883. */
  884. int avc_has_extended_perms(u32 ssid, u32 tsid, u16 tclass, u32 requested,
  885. u8 driver, u8 xperm, struct common_audit_data *ad)
  886. {
  887. struct avc_node *node;
  888. struct av_decision avd;
  889. u32 denied;
  890. struct extended_perms_decision local_xpd;
  891. struct extended_perms_decision *xpd = NULL;
  892. struct extended_perms_data allowed;
  893. struct extended_perms_data auditallow;
  894. struct extended_perms_data dontaudit;
  895. struct avc_xperms_node local_xp_node;
  896. struct avc_xperms_node *xp_node;
  897. int rc = 0, rc2;
  898. xp_node = &local_xp_node;
  899. if (WARN_ON(!requested))
  900. return -EACCES;
  901. rcu_read_lock();
  902. node = avc_lookup(ssid, tsid, tclass);
  903. if (unlikely(!node)) {
  904. avc_compute_av(ssid, tsid, tclass, &avd, xp_node);
  905. } else {
  906. memcpy(&avd, &node->ae.avd, sizeof(avd));
  907. xp_node = node->ae.xp_node;
  908. }
  909. /* if extended permissions are not defined, only consider av_decision */
  910. if (!xp_node || !xp_node->xp.len)
  911. goto decision;
  912. local_xpd.allowed = &allowed;
  913. local_xpd.auditallow = &auditallow;
  914. local_xpd.dontaudit = &dontaudit;
  915. xpd = avc_xperms_decision_lookup(driver, xp_node);
  916. if (unlikely(!xpd)) {
  917. /*
  918. * Compute the extended_perms_decision only if the driver
  919. * is flagged
  920. */
  921. if (!security_xperm_test(xp_node->xp.drivers.p, driver)) {
  922. avd.allowed &= ~requested;
  923. goto decision;
  924. }
  925. rcu_read_unlock();
  926. security_compute_xperms_decision(ssid, tsid, tclass,
  927. driver, &local_xpd);
  928. rcu_read_lock();
  929. avc_update_node(AVC_CALLBACK_ADD_XPERMS, requested,
  930. driver, xperm, ssid, tsid, tclass, avd.seqno,
  931. &local_xpd, 0);
  932. } else {
  933. avc_quick_copy_xperms_decision(xperm, &local_xpd, xpd);
  934. }
  935. xpd = &local_xpd;
  936. if (!avc_xperms_has_perm(xpd, xperm, XPERMS_ALLOWED))
  937. avd.allowed &= ~requested;
  938. decision:
  939. denied = requested & ~(avd.allowed);
  940. if (unlikely(denied))
  941. rc = avc_denied(ssid, tsid, tclass, requested,
  942. driver, xperm, AVC_EXTENDED_PERMS, &avd);
  943. rcu_read_unlock();
  944. rc2 = avc_xperms_audit(ssid, tsid, tclass, requested,
  945. &avd, xpd, xperm, rc, ad);
  946. if (rc2)
  947. return rc2;
  948. return rc;
  949. }
  950. /**
  951. * avc_perm_nonode - Add an entry to the AVC
  952. * @ssid: subject
  953. * @tsid: object/target
  954. * @tclass: object class
  955. * @requested: requested permissions
  956. * @flags: AVC flags
  957. * @avd: access vector decision
  958. *
  959. * This is the "we have no node" part of avc_has_perm_noaudit(), which is
  960. * unlikely and needs extra stack space for the new node that we generate, so
  961. * don't inline it.
  962. */
  963. static noinline int avc_perm_nonode(u32 ssid, u32 tsid, u16 tclass,
  964. u32 requested, unsigned int flags,
  965. struct av_decision *avd)
  966. {
  967. u32 denied;
  968. struct avc_xperms_node xp_node;
  969. avc_compute_av(ssid, tsid, tclass, avd, &xp_node);
  970. denied = requested & ~(avd->allowed);
  971. if (unlikely(denied))
  972. return avc_denied(ssid, tsid, tclass, requested, 0, 0,
  973. flags, avd);
  974. return 0;
  975. }
  976. /**
  977. * avc_has_perm_noaudit - Check permissions but perform no auditing.
  978. * @ssid: source security identifier
  979. * @tsid: target security identifier
  980. * @tclass: target security class
  981. * @requested: requested permissions, interpreted based on @tclass
  982. * @flags: AVC_STRICT or 0
  983. * @avd: access vector decisions
  984. *
  985. * Check the AVC to determine whether the @requested permissions are granted
  986. * for the SID pair (@ssid, @tsid), interpreting the permissions
  987. * based on @tclass, and call the security server on a cache miss to obtain
  988. * a new decision and add it to the cache. Return a copy of the decisions
  989. * in @avd. Return %0 if all @requested permissions are granted,
  990. * -%EACCES if any permissions are denied, or another -errno upon
  991. * other errors. This function is typically called by avc_has_perm(),
  992. * but may also be called directly to separate permission checking from
  993. * auditing, e.g. in cases where a lock must be held for the check but
  994. * should be released for the auditing.
  995. */
  996. inline int avc_has_perm_noaudit(u32 ssid, u32 tsid,
  997. u16 tclass, u32 requested,
  998. unsigned int flags,
  999. struct av_decision *avd)
  1000. {
  1001. u32 denied;
  1002. struct avc_node *node;
  1003. if (WARN_ON(!requested))
  1004. return -EACCES;
  1005. rcu_read_lock();
  1006. node = avc_lookup(ssid, tsid, tclass);
  1007. if (unlikely(!node)) {
  1008. rcu_read_unlock();
  1009. return avc_perm_nonode(ssid, tsid, tclass, requested,
  1010. flags, avd);
  1011. }
  1012. denied = requested & ~node->ae.avd.allowed;
  1013. memcpy(avd, &node->ae.avd, sizeof(*avd));
  1014. rcu_read_unlock();
  1015. if (unlikely(denied))
  1016. return avc_denied(ssid, tsid, tclass, requested, 0, 0,
  1017. flags, avd);
  1018. return 0;
  1019. }
  1020. /**
  1021. * avc_has_perm - Check permissions and perform any appropriate auditing.
  1022. * @ssid: source security identifier
  1023. * @tsid: target security identifier
  1024. * @tclass: target security class
  1025. * @requested: requested permissions, interpreted based on @tclass
  1026. * @auditdata: auxiliary audit data
  1027. *
  1028. * Check the AVC to determine whether the @requested permissions are granted
  1029. * for the SID pair (@ssid, @tsid), interpreting the permissions
  1030. * based on @tclass, and call the security server on a cache miss to obtain
  1031. * a new decision and add it to the cache. Audit the granting or denial of
  1032. * permissions in accordance with the policy. Return %0 if all @requested
  1033. * permissions are granted, -%EACCES if any permissions are denied, or
  1034. * another -errno upon other errors.
  1035. */
  1036. int avc_has_perm(u32 ssid, u32 tsid, u16 tclass,
  1037. u32 requested, struct common_audit_data *auditdata)
  1038. {
  1039. struct av_decision avd;
  1040. int rc, rc2;
  1041. rc = avc_has_perm_noaudit(ssid, tsid, tclass, requested, 0,
  1042. &avd);
  1043. rc2 = avc_audit(ssid, tsid, tclass, requested, &avd, rc,
  1044. auditdata);
  1045. if (rc2)
  1046. return rc2;
  1047. return rc;
  1048. }
  1049. u32 avc_policy_seqno(void)
  1050. {
  1051. return selinux_avc.avc_cache.latest_notif;
  1052. }