avc.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804
  1. /*
  2. * Implementation of the kernel access vector cache (AVC).
  3. *
  4. * Authors: Stephen Smalley, <sds@epoch.ncsc.mil>
  5. * James Morris <jmorris@redhat.com>
  6. *
  7. * Update: KaiGai, Kohei <kaigai@ak.jp.nec.com>
  8. * Replaced the avc_lock spinlock by RCU.
  9. *
  10. * Copyright (C) 2003 Red Hat, Inc., James Morris <jmorris@redhat.com>
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License version 2,
  14. * as published by the Free Software Foundation.
  15. */
  16. #include <linux/types.h>
  17. #include <linux/stddef.h>
  18. #include <linux/kernel.h>
  19. #include <linux/slab.h>
  20. #include <linux/fs.h>
  21. #include <linux/dcache.h>
  22. #include <linux/init.h>
  23. #include <linux/skbuff.h>
  24. #include <linux/percpu.h>
  25. #include <net/sock.h>
  26. #include <linux/un.h>
  27. #include <net/af_unix.h>
  28. #include <linux/ip.h>
  29. #include <linux/audit.h>
  30. #include <linux/ipv6.h>
  31. #include <net/ipv6.h>
  32. #include "avc.h"
  33. #include "avc_ss.h"
  34. #include "classmap.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. };
  49. struct avc_node {
  50. struct avc_entry ae;
  51. struct hlist_node list; /* anchored in avc_cache->slots[i] */
  52. struct rcu_head rhead;
  53. };
  54. struct avc_cache {
  55. struct hlist_head slots[AVC_CACHE_SLOTS]; /* head for avc_node->list */
  56. spinlock_t slots_lock[AVC_CACHE_SLOTS]; /* lock for writes */
  57. atomic_t lru_hint; /* LRU hint for reclaim scan */
  58. atomic_t active_nodes;
  59. u32 latest_notif; /* latest revocation notification */
  60. };
  61. struct avc_callback_node {
  62. int (*callback) (u32 event);
  63. u32 events;
  64. struct avc_callback_node *next;
  65. };
  66. /* Exported via selinufs */
  67. unsigned int avc_cache_threshold = AVC_DEF_CACHE_THRESHOLD;
  68. #ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
  69. DEFINE_PER_CPU(struct avc_cache_stats, avc_cache_stats) = { 0 };
  70. #endif
  71. static struct avc_cache avc_cache;
  72. static struct avc_callback_node *avc_callbacks;
  73. static struct kmem_cache *avc_node_cachep;
  74. static inline int avc_hash(u32 ssid, u32 tsid, u16 tclass)
  75. {
  76. return (ssid ^ (tsid<<2) ^ (tclass<<4)) & (AVC_CACHE_SLOTS - 1);
  77. }
  78. /**
  79. * avc_dump_av - Display an access vector in human-readable form.
  80. * @tclass: target security class
  81. * @av: access vector
  82. */
  83. static void avc_dump_av(struct audit_buffer *ab, u16 tclass, u32 av)
  84. {
  85. const char **perms;
  86. int i, perm;
  87. if (av == 0) {
  88. audit_log_format(ab, " null");
  89. return;
  90. }
  91. perms = secclass_map[tclass-1].perms;
  92. audit_log_format(ab, " {");
  93. i = 0;
  94. perm = 1;
  95. while (i < (sizeof(av) * 8)) {
  96. if ((perm & av) && perms[i]) {
  97. audit_log_format(ab, " %s", perms[i]);
  98. av &= ~perm;
  99. }
  100. i++;
  101. perm <<= 1;
  102. }
  103. if (av)
  104. audit_log_format(ab, " 0x%x", av);
  105. audit_log_format(ab, " }");
  106. }
  107. /**
  108. * avc_dump_query - Display a SID pair and a class in human-readable form.
  109. * @ssid: source security identifier
  110. * @tsid: target security identifier
  111. * @tclass: target security class
  112. */
  113. static void avc_dump_query(struct audit_buffer *ab, u32 ssid, u32 tsid, u16 tclass)
  114. {
  115. int rc;
  116. char *scontext;
  117. u32 scontext_len;
  118. rc = security_sid_to_context(ssid, &scontext, &scontext_len);
  119. if (rc)
  120. audit_log_format(ab, "ssid=%d", ssid);
  121. else {
  122. audit_log_format(ab, "scontext=%s", scontext);
  123. kfree(scontext);
  124. }
  125. rc = security_sid_to_context(tsid, &scontext, &scontext_len);
  126. if (rc)
  127. audit_log_format(ab, " tsid=%d", tsid);
  128. else {
  129. audit_log_format(ab, " tcontext=%s", scontext);
  130. kfree(scontext);
  131. }
  132. BUG_ON(tclass >= ARRAY_SIZE(secclass_map));
  133. audit_log_format(ab, " tclass=%s", secclass_map[tclass-1].name);
  134. }
  135. /**
  136. * avc_init - Initialize the AVC.
  137. *
  138. * Initialize the access vector cache.
  139. */
  140. void __init avc_init(void)
  141. {
  142. int i;
  143. for (i = 0; i < AVC_CACHE_SLOTS; i++) {
  144. INIT_HLIST_HEAD(&avc_cache.slots[i]);
  145. spin_lock_init(&avc_cache.slots_lock[i]);
  146. }
  147. atomic_set(&avc_cache.active_nodes, 0);
  148. atomic_set(&avc_cache.lru_hint, 0);
  149. avc_node_cachep = kmem_cache_create("avc_node", sizeof(struct avc_node),
  150. 0, SLAB_PANIC, NULL);
  151. audit_log(current->audit_context, GFP_KERNEL, AUDIT_KERNEL, "AVC INITIALIZED\n");
  152. }
  153. int avc_get_hash_stats(char *page)
  154. {
  155. int i, chain_len, max_chain_len, slots_used;
  156. struct avc_node *node;
  157. struct hlist_head *head;
  158. rcu_read_lock();
  159. slots_used = 0;
  160. max_chain_len = 0;
  161. for (i = 0; i < AVC_CACHE_SLOTS; i++) {
  162. head = &avc_cache.slots[i];
  163. if (!hlist_empty(head)) {
  164. struct hlist_node *next;
  165. slots_used++;
  166. chain_len = 0;
  167. hlist_for_each_entry_rcu(node, next, head, list)
  168. chain_len++;
  169. if (chain_len > max_chain_len)
  170. max_chain_len = chain_len;
  171. }
  172. }
  173. rcu_read_unlock();
  174. return scnprintf(page, PAGE_SIZE, "entries: %d\nbuckets used: %d/%d\n"
  175. "longest chain: %d\n",
  176. atomic_read(&avc_cache.active_nodes),
  177. slots_used, AVC_CACHE_SLOTS, max_chain_len);
  178. }
  179. static void avc_node_free(struct rcu_head *rhead)
  180. {
  181. struct avc_node *node = container_of(rhead, struct avc_node, rhead);
  182. kmem_cache_free(avc_node_cachep, node);
  183. avc_cache_stats_incr(frees);
  184. }
  185. static void avc_node_delete(struct avc_node *node)
  186. {
  187. hlist_del_rcu(&node->list);
  188. call_rcu(&node->rhead, avc_node_free);
  189. atomic_dec(&avc_cache.active_nodes);
  190. }
  191. static void avc_node_kill(struct avc_node *node)
  192. {
  193. kmem_cache_free(avc_node_cachep, node);
  194. avc_cache_stats_incr(frees);
  195. atomic_dec(&avc_cache.active_nodes);
  196. }
  197. static void avc_node_replace(struct avc_node *new, struct avc_node *old)
  198. {
  199. hlist_replace_rcu(&old->list, &new->list);
  200. call_rcu(&old->rhead, avc_node_free);
  201. atomic_dec(&avc_cache.active_nodes);
  202. }
  203. static inline int avc_reclaim_node(void)
  204. {
  205. struct avc_node *node;
  206. int hvalue, try, ecx;
  207. unsigned long flags;
  208. struct hlist_head *head;
  209. struct hlist_node *next;
  210. spinlock_t *lock;
  211. for (try = 0, ecx = 0; try < AVC_CACHE_SLOTS; try++) {
  212. hvalue = atomic_inc_return(&avc_cache.lru_hint) & (AVC_CACHE_SLOTS - 1);
  213. head = &avc_cache.slots[hvalue];
  214. lock = &avc_cache.slots_lock[hvalue];
  215. if (!spin_trylock_irqsave(lock, flags))
  216. continue;
  217. rcu_read_lock();
  218. hlist_for_each_entry(node, next, head, list) {
  219. avc_node_delete(node);
  220. avc_cache_stats_incr(reclaims);
  221. ecx++;
  222. if (ecx >= AVC_CACHE_RECLAIM) {
  223. rcu_read_unlock();
  224. spin_unlock_irqrestore(lock, flags);
  225. goto out;
  226. }
  227. }
  228. rcu_read_unlock();
  229. spin_unlock_irqrestore(lock, flags);
  230. }
  231. out:
  232. return ecx;
  233. }
  234. static struct avc_node *avc_alloc_node(void)
  235. {
  236. struct avc_node *node;
  237. node = kmem_cache_zalloc(avc_node_cachep, GFP_ATOMIC|__GFP_NOMEMALLOC);
  238. if (!node)
  239. goto out;
  240. INIT_HLIST_NODE(&node->list);
  241. avc_cache_stats_incr(allocations);
  242. if (atomic_inc_return(&avc_cache.active_nodes) > avc_cache_threshold)
  243. avc_reclaim_node();
  244. out:
  245. return node;
  246. }
  247. static void avc_node_populate(struct avc_node *node, u32 ssid, u32 tsid, u16 tclass, struct av_decision *avd)
  248. {
  249. node->ae.ssid = ssid;
  250. node->ae.tsid = tsid;
  251. node->ae.tclass = tclass;
  252. memcpy(&node->ae.avd, avd, sizeof(node->ae.avd));
  253. }
  254. static inline struct avc_node *avc_search_node(u32 ssid, u32 tsid, u16 tclass)
  255. {
  256. struct avc_node *node, *ret = NULL;
  257. int hvalue;
  258. struct hlist_head *head;
  259. struct hlist_node *next;
  260. hvalue = avc_hash(ssid, tsid, tclass);
  261. head = &avc_cache.slots[hvalue];
  262. hlist_for_each_entry_rcu(node, next, head, list) {
  263. if (ssid == node->ae.ssid &&
  264. tclass == node->ae.tclass &&
  265. tsid == node->ae.tsid) {
  266. ret = node;
  267. break;
  268. }
  269. }
  270. return ret;
  271. }
  272. /**
  273. * avc_lookup - Look up an AVC entry.
  274. * @ssid: source security identifier
  275. * @tsid: target security identifier
  276. * @tclass: target security class
  277. *
  278. * Look up an AVC entry that is valid for the
  279. * (@ssid, @tsid), interpreting the permissions
  280. * based on @tclass. If a valid AVC entry exists,
  281. * then this function returns the avc_node.
  282. * Otherwise, this function returns NULL.
  283. */
  284. static struct avc_node *avc_lookup(u32 ssid, u32 tsid, u16 tclass)
  285. {
  286. struct avc_node *node;
  287. avc_cache_stats_incr(lookups);
  288. node = avc_search_node(ssid, tsid, tclass);
  289. if (node)
  290. return node;
  291. avc_cache_stats_incr(misses);
  292. return NULL;
  293. }
  294. static int avc_latest_notif_update(int seqno, int is_insert)
  295. {
  296. int ret = 0;
  297. static DEFINE_SPINLOCK(notif_lock);
  298. unsigned long flag;
  299. spin_lock_irqsave(&notif_lock, flag);
  300. if (is_insert) {
  301. if (seqno < avc_cache.latest_notif) {
  302. printk(KERN_WARNING "SELinux: avc: seqno %d < latest_notif %d\n",
  303. seqno, avc_cache.latest_notif);
  304. ret = -EAGAIN;
  305. }
  306. } else {
  307. if (seqno > avc_cache.latest_notif)
  308. avc_cache.latest_notif = seqno;
  309. }
  310. spin_unlock_irqrestore(&notif_lock, flag);
  311. return ret;
  312. }
  313. /**
  314. * avc_insert - Insert an AVC entry.
  315. * @ssid: source security identifier
  316. * @tsid: target security identifier
  317. * @tclass: target security class
  318. * @avd: resulting av decision
  319. *
  320. * Insert an AVC entry for the SID pair
  321. * (@ssid, @tsid) and class @tclass.
  322. * The access vectors and the sequence number are
  323. * normally provided by the security server in
  324. * response to a security_compute_av() call. If the
  325. * sequence number @avd->seqno is not less than the latest
  326. * revocation notification, then the function copies
  327. * the access vectors into a cache entry, returns
  328. * avc_node inserted. Otherwise, this function returns NULL.
  329. */
  330. static struct avc_node *avc_insert(u32 ssid, u32 tsid, u16 tclass, struct av_decision *avd)
  331. {
  332. struct avc_node *pos, *node = NULL;
  333. int hvalue;
  334. unsigned long flag;
  335. if (avc_latest_notif_update(avd->seqno, 1))
  336. goto out;
  337. node = avc_alloc_node();
  338. if (node) {
  339. struct hlist_head *head;
  340. struct hlist_node *next;
  341. spinlock_t *lock;
  342. hvalue = avc_hash(ssid, tsid, tclass);
  343. avc_node_populate(node, ssid, tsid, tclass, avd);
  344. head = &avc_cache.slots[hvalue];
  345. lock = &avc_cache.slots_lock[hvalue];
  346. spin_lock_irqsave(lock, flag);
  347. hlist_for_each_entry(pos, next, head, list) {
  348. if (pos->ae.ssid == ssid &&
  349. pos->ae.tsid == tsid &&
  350. pos->ae.tclass == tclass) {
  351. avc_node_replace(node, pos);
  352. goto found;
  353. }
  354. }
  355. hlist_add_head_rcu(&node->list, head);
  356. found:
  357. spin_unlock_irqrestore(lock, flag);
  358. }
  359. out:
  360. return node;
  361. }
  362. /**
  363. * avc_audit_pre_callback - SELinux specific information
  364. * will be called by generic audit code
  365. * @ab: the audit buffer
  366. * @a: audit_data
  367. */
  368. static void avc_audit_pre_callback(struct audit_buffer *ab, void *a)
  369. {
  370. struct common_audit_data *ad = a;
  371. audit_log_format(ab, "avc: %s ",
  372. ad->selinux_audit_data->denied ? "denied" : "granted");
  373. avc_dump_av(ab, ad->selinux_audit_data->tclass,
  374. ad->selinux_audit_data->audited);
  375. audit_log_format(ab, " for ");
  376. }
  377. /**
  378. * avc_audit_post_callback - SELinux specific information
  379. * will be called by generic audit code
  380. * @ab: the audit buffer
  381. * @a: audit_data
  382. */
  383. static void avc_audit_post_callback(struct audit_buffer *ab, void *a)
  384. {
  385. struct common_audit_data *ad = a;
  386. audit_log_format(ab, " ");
  387. avc_dump_query(ab, ad->selinux_audit_data->ssid,
  388. ad->selinux_audit_data->tsid,
  389. ad->selinux_audit_data->tclass);
  390. }
  391. /* This is the slow part of avc audit with big stack footprint */
  392. noinline int slow_avc_audit(u32 ssid, u32 tsid, u16 tclass,
  393. u32 requested, u32 audited, u32 denied,
  394. struct common_audit_data *a,
  395. unsigned flags)
  396. {
  397. struct common_audit_data stack_data;
  398. struct selinux_audit_data sad;
  399. if (!a) {
  400. a = &stack_data;
  401. a->type = LSM_AUDIT_DATA_NONE;
  402. }
  403. /*
  404. * When in a RCU walk do the audit on the RCU retry. This is because
  405. * the collection of the dname in an inode audit message is not RCU
  406. * safe. Note this may drop some audits when the situation changes
  407. * during retry. However this is logically just as if the operation
  408. * happened a little later.
  409. */
  410. if ((a->type == LSM_AUDIT_DATA_INODE) &&
  411. (flags & MAY_NOT_BLOCK))
  412. return -ECHILD;
  413. sad.tclass = tclass;
  414. sad.requested = requested;
  415. sad.ssid = ssid;
  416. sad.tsid = tsid;
  417. sad.audited = audited;
  418. sad.denied = denied;
  419. a->selinux_audit_data = &sad;
  420. common_lsm_audit(a, avc_audit_pre_callback, avc_audit_post_callback);
  421. return 0;
  422. }
  423. /**
  424. * avc_add_callback - Register a callback for security events.
  425. * @callback: callback function
  426. * @events: security events
  427. *
  428. * Register a callback function for events in the set @events.
  429. * Returns %0 on success or -%ENOMEM if insufficient memory
  430. * exists to add the callback.
  431. */
  432. int __init avc_add_callback(int (*callback)(u32 event), u32 events)
  433. {
  434. struct avc_callback_node *c;
  435. int rc = 0;
  436. c = kmalloc(sizeof(*c), GFP_KERNEL);
  437. if (!c) {
  438. rc = -ENOMEM;
  439. goto out;
  440. }
  441. c->callback = callback;
  442. c->events = events;
  443. c->next = avc_callbacks;
  444. avc_callbacks = c;
  445. out:
  446. return rc;
  447. }
  448. static inline int avc_sidcmp(u32 x, u32 y)
  449. {
  450. return (x == y || x == SECSID_WILD || y == SECSID_WILD);
  451. }
  452. /**
  453. * avc_update_node Update an AVC entry
  454. * @event : Updating event
  455. * @perms : Permission mask bits
  456. * @ssid,@tsid,@tclass : identifier of an AVC entry
  457. * @seqno : sequence number when decision was made
  458. *
  459. * if a valid AVC entry doesn't exist,this function returns -ENOENT.
  460. * if kmalloc() called internal returns NULL, this function returns -ENOMEM.
  461. * otherwise, this function updates the AVC entry. The original AVC-entry object
  462. * will release later by RCU.
  463. */
  464. static int avc_update_node(u32 event, u32 perms, u32 ssid, u32 tsid, u16 tclass,
  465. u32 seqno)
  466. {
  467. int hvalue, rc = 0;
  468. unsigned long flag;
  469. struct avc_node *pos, *node, *orig = NULL;
  470. struct hlist_head *head;
  471. struct hlist_node *next;
  472. spinlock_t *lock;
  473. node = avc_alloc_node();
  474. if (!node) {
  475. rc = -ENOMEM;
  476. goto out;
  477. }
  478. /* Lock the target slot */
  479. hvalue = avc_hash(ssid, tsid, tclass);
  480. head = &avc_cache.slots[hvalue];
  481. lock = &avc_cache.slots_lock[hvalue];
  482. spin_lock_irqsave(lock, flag);
  483. hlist_for_each_entry(pos, next, head, list) {
  484. if (ssid == pos->ae.ssid &&
  485. tsid == pos->ae.tsid &&
  486. tclass == pos->ae.tclass &&
  487. seqno == pos->ae.avd.seqno){
  488. orig = pos;
  489. break;
  490. }
  491. }
  492. if (!orig) {
  493. rc = -ENOENT;
  494. avc_node_kill(node);
  495. goto out_unlock;
  496. }
  497. /*
  498. * Copy and replace original node.
  499. */
  500. avc_node_populate(node, ssid, tsid, tclass, &orig->ae.avd);
  501. switch (event) {
  502. case AVC_CALLBACK_GRANT:
  503. node->ae.avd.allowed |= perms;
  504. break;
  505. case AVC_CALLBACK_TRY_REVOKE:
  506. case AVC_CALLBACK_REVOKE:
  507. node->ae.avd.allowed &= ~perms;
  508. break;
  509. case AVC_CALLBACK_AUDITALLOW_ENABLE:
  510. node->ae.avd.auditallow |= perms;
  511. break;
  512. case AVC_CALLBACK_AUDITALLOW_DISABLE:
  513. node->ae.avd.auditallow &= ~perms;
  514. break;
  515. case AVC_CALLBACK_AUDITDENY_ENABLE:
  516. node->ae.avd.auditdeny |= perms;
  517. break;
  518. case AVC_CALLBACK_AUDITDENY_DISABLE:
  519. node->ae.avd.auditdeny &= ~perms;
  520. break;
  521. }
  522. avc_node_replace(node, orig);
  523. out_unlock:
  524. spin_unlock_irqrestore(lock, flag);
  525. out:
  526. return rc;
  527. }
  528. /**
  529. * avc_flush - Flush the cache
  530. */
  531. static void avc_flush(void)
  532. {
  533. struct hlist_head *head;
  534. struct hlist_node *next;
  535. struct avc_node *node;
  536. spinlock_t *lock;
  537. unsigned long flag;
  538. int i;
  539. for (i = 0; i < AVC_CACHE_SLOTS; i++) {
  540. head = &avc_cache.slots[i];
  541. lock = &avc_cache.slots_lock[i];
  542. spin_lock_irqsave(lock, flag);
  543. /*
  544. * With preemptable RCU, the outer spinlock does not
  545. * prevent RCU grace periods from ending.
  546. */
  547. rcu_read_lock();
  548. hlist_for_each_entry(node, next, head, list)
  549. avc_node_delete(node);
  550. rcu_read_unlock();
  551. spin_unlock_irqrestore(lock, flag);
  552. }
  553. }
  554. /**
  555. * avc_ss_reset - Flush the cache and revalidate migrated permissions.
  556. * @seqno: policy sequence number
  557. */
  558. int avc_ss_reset(u32 seqno)
  559. {
  560. struct avc_callback_node *c;
  561. int rc = 0, tmprc;
  562. avc_flush();
  563. for (c = avc_callbacks; c; c = c->next) {
  564. if (c->events & AVC_CALLBACK_RESET) {
  565. tmprc = c->callback(AVC_CALLBACK_RESET);
  566. /* save the first error encountered for the return
  567. value and continue processing the callbacks */
  568. if (!rc)
  569. rc = tmprc;
  570. }
  571. }
  572. avc_latest_notif_update(seqno, 0);
  573. return rc;
  574. }
  575. /*
  576. * Slow-path helper function for avc_has_perm_noaudit,
  577. * when the avc_node lookup fails. We get called with
  578. * the RCU read lock held, and need to return with it
  579. * still held, but drop if for the security compute.
  580. *
  581. * Don't inline this, since it's the slow-path and just
  582. * results in a bigger stack frame.
  583. */
  584. static noinline struct avc_node *avc_compute_av(u32 ssid, u32 tsid,
  585. u16 tclass, struct av_decision *avd)
  586. {
  587. rcu_read_unlock();
  588. security_compute_av(ssid, tsid, tclass, avd);
  589. rcu_read_lock();
  590. return avc_insert(ssid, tsid, tclass, avd);
  591. }
  592. static noinline int avc_denied(u32 ssid, u32 tsid,
  593. u16 tclass, u32 requested,
  594. unsigned flags,
  595. struct av_decision *avd)
  596. {
  597. if (flags & AVC_STRICT)
  598. return -EACCES;
  599. if (selinux_enforcing && !(avd->flags & AVD_FLAGS_PERMISSIVE))
  600. return -EACCES;
  601. avc_update_node(AVC_CALLBACK_GRANT, requested, ssid,
  602. tsid, tclass, avd->seqno);
  603. return 0;
  604. }
  605. /**
  606. * avc_has_perm_noaudit - Check permissions but perform no auditing.
  607. * @ssid: source security identifier
  608. * @tsid: target security identifier
  609. * @tclass: target security class
  610. * @requested: requested permissions, interpreted based on @tclass
  611. * @flags: AVC_STRICT or 0
  612. * @avd: access vector decisions
  613. *
  614. * Check the AVC to determine whether the @requested permissions are granted
  615. * for the SID pair (@ssid, @tsid), interpreting the permissions
  616. * based on @tclass, and call the security server on a cache miss to obtain
  617. * a new decision and add it to the cache. Return a copy of the decisions
  618. * in @avd. Return %0 if all @requested permissions are granted,
  619. * -%EACCES if any permissions are denied, or another -errno upon
  620. * other errors. This function is typically called by avc_has_perm(),
  621. * but may also be called directly to separate permission checking from
  622. * auditing, e.g. in cases where a lock must be held for the check but
  623. * should be released for the auditing.
  624. */
  625. inline int avc_has_perm_noaudit(u32 ssid, u32 tsid,
  626. u16 tclass, u32 requested,
  627. unsigned flags,
  628. struct av_decision *avd)
  629. {
  630. struct avc_node *node;
  631. int rc = 0;
  632. u32 denied;
  633. BUG_ON(!requested);
  634. rcu_read_lock();
  635. node = avc_lookup(ssid, tsid, tclass);
  636. if (unlikely(!node)) {
  637. node = avc_compute_av(ssid, tsid, tclass, avd);
  638. } else {
  639. memcpy(avd, &node->ae.avd, sizeof(*avd));
  640. avd = &node->ae.avd;
  641. }
  642. denied = requested & ~(avd->allowed);
  643. if (unlikely(denied))
  644. rc = avc_denied(ssid, tsid, tclass, requested, flags, avd);
  645. rcu_read_unlock();
  646. return rc;
  647. }
  648. /**
  649. * avc_has_perm - Check permissions and perform any appropriate auditing.
  650. * @ssid: source security identifier
  651. * @tsid: target security identifier
  652. * @tclass: target security class
  653. * @requested: requested permissions, interpreted based on @tclass
  654. * @auditdata: auxiliary audit data
  655. * @flags: VFS walk flags
  656. *
  657. * Check the AVC to determine whether the @requested permissions are granted
  658. * for the SID pair (@ssid, @tsid), interpreting the permissions
  659. * based on @tclass, and call the security server on a cache miss to obtain
  660. * a new decision and add it to the cache. Audit the granting or denial of
  661. * permissions in accordance with the policy. Return %0 if all @requested
  662. * permissions are granted, -%EACCES if any permissions are denied, or
  663. * another -errno upon other errors.
  664. */
  665. int avc_has_perm_flags(u32 ssid, u32 tsid, u16 tclass,
  666. u32 requested, struct common_audit_data *auditdata,
  667. unsigned flags)
  668. {
  669. struct av_decision avd;
  670. int rc, rc2;
  671. rc = avc_has_perm_noaudit(ssid, tsid, tclass, requested, 0, &avd);
  672. rc2 = avc_audit(ssid, tsid, tclass, requested, &avd, rc, auditdata,
  673. flags);
  674. if (rc2)
  675. return rc2;
  676. return rc;
  677. }
  678. u32 avc_policy_seqno(void)
  679. {
  680. return avc_cache.latest_notif;
  681. }
  682. void avc_disable(void)
  683. {
  684. /*
  685. * If you are looking at this because you have realized that we are
  686. * not destroying the avc_node_cachep it might be easy to fix, but
  687. * I don't know the memory barrier semantics well enough to know. It's
  688. * possible that some other task dereferenced security_ops when
  689. * it still pointed to selinux operations. If that is the case it's
  690. * possible that it is about to use the avc and is about to need the
  691. * avc_node_cachep. I know I could wrap the security.c security_ops call
  692. * in an rcu_lock, but seriously, it's not worth it. Instead I just flush
  693. * the cache and get that memory back.
  694. */
  695. if (avc_node_cachep) {
  696. avc_flush();
  697. /* kmem_cache_destroy(avc_node_cachep); */
  698. }
  699. }