avc.c 32 KB

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