avc.c 22 KB

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