avc.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856
  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. avc_cache_stats_incr(hits);
  297. else
  298. avc_cache_stats_incr(misses);
  299. return node;
  300. }
  301. static int avc_latest_notif_update(int seqno, int is_insert)
  302. {
  303. int ret = 0;
  304. static DEFINE_SPINLOCK(notif_lock);
  305. unsigned long flag;
  306. spin_lock_irqsave(&notif_lock, flag);
  307. if (is_insert) {
  308. if (seqno < avc_cache.latest_notif) {
  309. printk(KERN_WARNING "SELinux: avc: seqno %d < latest_notif %d\n",
  310. seqno, avc_cache.latest_notif);
  311. ret = -EAGAIN;
  312. }
  313. } else {
  314. if (seqno > avc_cache.latest_notif)
  315. avc_cache.latest_notif = seqno;
  316. }
  317. spin_unlock_irqrestore(&notif_lock, flag);
  318. return ret;
  319. }
  320. /**
  321. * avc_insert - Insert an AVC entry.
  322. * @ssid: source security identifier
  323. * @tsid: target security identifier
  324. * @tclass: target security class
  325. * @avd: resulting av decision
  326. *
  327. * Insert an AVC entry for the SID pair
  328. * (@ssid, @tsid) and class @tclass.
  329. * The access vectors and the sequence number are
  330. * normally provided by the security server in
  331. * response to a security_compute_av() call. If the
  332. * sequence number @avd->seqno is not less than the latest
  333. * revocation notification, then the function copies
  334. * the access vectors into a cache entry, returns
  335. * avc_node inserted. Otherwise, this function returns NULL.
  336. */
  337. static struct avc_node *avc_insert(u32 ssid, u32 tsid, u16 tclass, struct av_decision *avd)
  338. {
  339. struct avc_node *pos, *node = NULL;
  340. int hvalue;
  341. unsigned long flag;
  342. if (avc_latest_notif_update(avd->seqno, 1))
  343. goto out;
  344. node = avc_alloc_node();
  345. if (node) {
  346. struct hlist_head *head;
  347. struct hlist_node *next;
  348. spinlock_t *lock;
  349. hvalue = avc_hash(ssid, tsid, tclass);
  350. avc_node_populate(node, ssid, tsid, tclass, avd);
  351. head = &avc_cache.slots[hvalue];
  352. lock = &avc_cache.slots_lock[hvalue];
  353. spin_lock_irqsave(lock, flag);
  354. hlist_for_each_entry(pos, next, head, list) {
  355. if (pos->ae.ssid == ssid &&
  356. pos->ae.tsid == tsid &&
  357. pos->ae.tclass == tclass) {
  358. avc_node_replace(node, pos);
  359. goto found;
  360. }
  361. }
  362. hlist_add_head_rcu(&node->list, head);
  363. found:
  364. spin_unlock_irqrestore(lock, flag);
  365. }
  366. out:
  367. return node;
  368. }
  369. /**
  370. * avc_audit_pre_callback - SELinux specific information
  371. * will be called by generic audit code
  372. * @ab: the audit buffer
  373. * @a: audit_data
  374. */
  375. static void avc_audit_pre_callback(struct audit_buffer *ab, void *a)
  376. {
  377. struct common_audit_data *ad = a;
  378. audit_log_format(ab, "avc: %s ",
  379. ad->selinux_audit_data.denied ? "denied" : "granted");
  380. avc_dump_av(ab, ad->selinux_audit_data.tclass,
  381. ad->selinux_audit_data.audited);
  382. audit_log_format(ab, " for ");
  383. }
  384. /**
  385. * avc_audit_post_callback - SELinux specific information
  386. * will be called by generic audit code
  387. * @ab: the audit buffer
  388. * @a: audit_data
  389. */
  390. static void avc_audit_post_callback(struct audit_buffer *ab, void *a)
  391. {
  392. struct common_audit_data *ad = a;
  393. audit_log_format(ab, " ");
  394. avc_dump_query(ab, ad->selinux_audit_data.ssid,
  395. ad->selinux_audit_data.tsid,
  396. ad->selinux_audit_data.tclass);
  397. }
  398. /**
  399. * avc_audit - Audit the granting or denial of permissions.
  400. * @ssid: source security identifier
  401. * @tsid: target security identifier
  402. * @tclass: target security class
  403. * @requested: requested permissions
  404. * @avd: access vector decisions
  405. * @result: result from avc_has_perm_noaudit
  406. * @a: auxiliary audit data
  407. * @flags: VFS walk flags
  408. *
  409. * Audit the granting or denial of permissions in accordance
  410. * with the policy. This function is typically called by
  411. * avc_has_perm() after a permission check, but can also be
  412. * called directly by callers who use avc_has_perm_noaudit()
  413. * in order to separate the permission check from the auditing.
  414. * For example, this separation is useful when the permission check must
  415. * be performed under a lock, to allow the lock to be released
  416. * before calling the auditing code.
  417. */
  418. int avc_audit(u32 ssid, u32 tsid,
  419. u16 tclass, u32 requested,
  420. struct av_decision *avd, int result, struct common_audit_data *a,
  421. unsigned flags)
  422. {
  423. struct common_audit_data stack_data;
  424. u32 denied, audited;
  425. denied = requested & ~avd->allowed;
  426. if (denied) {
  427. audited = denied & avd->auditdeny;
  428. /*
  429. * a->selinux_audit_data.auditdeny is TRICKY! Setting a bit in
  430. * this field means that ANY denials should NOT be audited if
  431. * the policy contains an explicit dontaudit rule for that
  432. * permission. Take notice that this is unrelated to the
  433. * actual permissions that were denied. As an example lets
  434. * assume:
  435. *
  436. * denied == READ
  437. * avd.auditdeny & ACCESS == 0 (not set means explicit rule)
  438. * selinux_audit_data.auditdeny & ACCESS == 1
  439. *
  440. * We will NOT audit the denial even though the denied
  441. * permission was READ and the auditdeny checks were for
  442. * ACCESS
  443. */
  444. if (a &&
  445. a->selinux_audit_data.auditdeny &&
  446. !(a->selinux_audit_data.auditdeny & avd->auditdeny))
  447. audited = 0;
  448. } else if (result)
  449. audited = denied = requested;
  450. else
  451. audited = requested & avd->auditallow;
  452. if (!audited)
  453. return 0;
  454. if (!a) {
  455. a = &stack_data;
  456. COMMON_AUDIT_DATA_INIT(a, NONE);
  457. }
  458. /*
  459. * When in a RCU walk do the audit on the RCU retry. This is because
  460. * the collection of the dname in an inode audit message is not RCU
  461. * safe. Note this may drop some audits when the situation changes
  462. * during retry. However this is logically just as if the operation
  463. * happened a little later.
  464. */
  465. if ((a->type == LSM_AUDIT_DATA_FS) &&
  466. (flags & IPERM_FLAG_RCU))
  467. return -ECHILD;
  468. a->selinux_audit_data.tclass = tclass;
  469. a->selinux_audit_data.requested = requested;
  470. a->selinux_audit_data.ssid = ssid;
  471. a->selinux_audit_data.tsid = tsid;
  472. a->selinux_audit_data.audited = audited;
  473. a->selinux_audit_data.denied = denied;
  474. a->lsm_pre_audit = avc_audit_pre_callback;
  475. a->lsm_post_audit = avc_audit_post_callback;
  476. common_lsm_audit(a);
  477. return 0;
  478. }
  479. /**
  480. * avc_add_callback - Register a callback for security events.
  481. * @callback: callback function
  482. * @events: security events
  483. * @ssid: source security identifier or %SECSID_WILD
  484. * @tsid: target security identifier or %SECSID_WILD
  485. * @tclass: target security class
  486. * @perms: permissions
  487. *
  488. * Register a callback function for events in the set @events
  489. * related to the SID pair (@ssid, @tsid)
  490. * and the permissions @perms, interpreting
  491. * @perms based on @tclass. Returns %0 on success or
  492. * -%ENOMEM if insufficient memory exists to add the callback.
  493. */
  494. int avc_add_callback(int (*callback)(u32 event, u32 ssid, u32 tsid,
  495. u16 tclass, u32 perms,
  496. u32 *out_retained),
  497. u32 events, u32 ssid, u32 tsid,
  498. u16 tclass, u32 perms)
  499. {
  500. struct avc_callback_node *c;
  501. int rc = 0;
  502. c = kmalloc(sizeof(*c), GFP_ATOMIC);
  503. if (!c) {
  504. rc = -ENOMEM;
  505. goto out;
  506. }
  507. c->callback = callback;
  508. c->events = events;
  509. c->ssid = ssid;
  510. c->tsid = tsid;
  511. c->perms = perms;
  512. c->next = avc_callbacks;
  513. avc_callbacks = c;
  514. out:
  515. return rc;
  516. }
  517. static inline int avc_sidcmp(u32 x, u32 y)
  518. {
  519. return (x == y || x == SECSID_WILD || y == SECSID_WILD);
  520. }
  521. /**
  522. * avc_update_node Update an AVC entry
  523. * @event : Updating event
  524. * @perms : Permission mask bits
  525. * @ssid,@tsid,@tclass : identifier of an AVC entry
  526. * @seqno : sequence number when decision was made
  527. *
  528. * if a valid AVC entry doesn't exist,this function returns -ENOENT.
  529. * if kmalloc() called internal returns NULL, this function returns -ENOMEM.
  530. * otherwise, this function updates the AVC entry. The original AVC-entry object
  531. * will release later by RCU.
  532. */
  533. static int avc_update_node(u32 event, u32 perms, u32 ssid, u32 tsid, u16 tclass,
  534. u32 seqno)
  535. {
  536. int hvalue, rc = 0;
  537. unsigned long flag;
  538. struct avc_node *pos, *node, *orig = NULL;
  539. struct hlist_head *head;
  540. struct hlist_node *next;
  541. spinlock_t *lock;
  542. node = avc_alloc_node();
  543. if (!node) {
  544. rc = -ENOMEM;
  545. goto out;
  546. }
  547. /* Lock the target slot */
  548. hvalue = avc_hash(ssid, tsid, tclass);
  549. head = &avc_cache.slots[hvalue];
  550. lock = &avc_cache.slots_lock[hvalue];
  551. spin_lock_irqsave(lock, flag);
  552. hlist_for_each_entry(pos, next, head, list) {
  553. if (ssid == pos->ae.ssid &&
  554. tsid == pos->ae.tsid &&
  555. tclass == pos->ae.tclass &&
  556. seqno == pos->ae.avd.seqno){
  557. orig = pos;
  558. break;
  559. }
  560. }
  561. if (!orig) {
  562. rc = -ENOENT;
  563. avc_node_kill(node);
  564. goto out_unlock;
  565. }
  566. /*
  567. * Copy and replace original node.
  568. */
  569. avc_node_populate(node, ssid, tsid, tclass, &orig->ae.avd);
  570. switch (event) {
  571. case AVC_CALLBACK_GRANT:
  572. node->ae.avd.allowed |= perms;
  573. break;
  574. case AVC_CALLBACK_TRY_REVOKE:
  575. case AVC_CALLBACK_REVOKE:
  576. node->ae.avd.allowed &= ~perms;
  577. break;
  578. case AVC_CALLBACK_AUDITALLOW_ENABLE:
  579. node->ae.avd.auditallow |= perms;
  580. break;
  581. case AVC_CALLBACK_AUDITALLOW_DISABLE:
  582. node->ae.avd.auditallow &= ~perms;
  583. break;
  584. case AVC_CALLBACK_AUDITDENY_ENABLE:
  585. node->ae.avd.auditdeny |= perms;
  586. break;
  587. case AVC_CALLBACK_AUDITDENY_DISABLE:
  588. node->ae.avd.auditdeny &= ~perms;
  589. break;
  590. }
  591. avc_node_replace(node, orig);
  592. out_unlock:
  593. spin_unlock_irqrestore(lock, flag);
  594. out:
  595. return rc;
  596. }
  597. /**
  598. * avc_flush - Flush the cache
  599. */
  600. static void avc_flush(void)
  601. {
  602. struct hlist_head *head;
  603. struct hlist_node *next;
  604. struct avc_node *node;
  605. spinlock_t *lock;
  606. unsigned long flag;
  607. int i;
  608. for (i = 0; i < AVC_CACHE_SLOTS; i++) {
  609. head = &avc_cache.slots[i];
  610. lock = &avc_cache.slots_lock[i];
  611. spin_lock_irqsave(lock, flag);
  612. /*
  613. * With preemptable RCU, the outer spinlock does not
  614. * prevent RCU grace periods from ending.
  615. */
  616. rcu_read_lock();
  617. hlist_for_each_entry(node, next, head, list)
  618. avc_node_delete(node);
  619. rcu_read_unlock();
  620. spin_unlock_irqrestore(lock, flag);
  621. }
  622. }
  623. /**
  624. * avc_ss_reset - Flush the cache and revalidate migrated permissions.
  625. * @seqno: policy sequence number
  626. */
  627. int avc_ss_reset(u32 seqno)
  628. {
  629. struct avc_callback_node *c;
  630. int rc = 0, tmprc;
  631. avc_flush();
  632. for (c = avc_callbacks; c; c = c->next) {
  633. if (c->events & AVC_CALLBACK_RESET) {
  634. tmprc = c->callback(AVC_CALLBACK_RESET,
  635. 0, 0, 0, 0, NULL);
  636. /* save the first error encountered for the return
  637. value and continue processing the callbacks */
  638. if (!rc)
  639. rc = tmprc;
  640. }
  641. }
  642. avc_latest_notif_update(seqno, 0);
  643. return rc;
  644. }
  645. /**
  646. * avc_has_perm_noaudit - Check permissions but perform no auditing.
  647. * @ssid: source security identifier
  648. * @tsid: target security identifier
  649. * @tclass: target security class
  650. * @requested: requested permissions, interpreted based on @tclass
  651. * @flags: AVC_STRICT or 0
  652. * @avd: access vector decisions
  653. *
  654. * Check the AVC to determine whether the @requested permissions are granted
  655. * for the SID pair (@ssid, @tsid), interpreting the permissions
  656. * based on @tclass, and call the security server on a cache miss to obtain
  657. * a new decision and add it to the cache. Return a copy of the decisions
  658. * in @avd. Return %0 if all @requested permissions are granted,
  659. * -%EACCES if any permissions are denied, or another -errno upon
  660. * other errors. This function is typically called by avc_has_perm(),
  661. * but may also be called directly to separate permission checking from
  662. * auditing, e.g. in cases where a lock must be held for the check but
  663. * should be released for the auditing.
  664. */
  665. int avc_has_perm_noaudit(u32 ssid, u32 tsid,
  666. u16 tclass, u32 requested,
  667. unsigned flags,
  668. struct av_decision *in_avd)
  669. {
  670. struct avc_node *node;
  671. struct av_decision avd_entry, *avd;
  672. int rc = 0;
  673. u32 denied;
  674. BUG_ON(!requested);
  675. rcu_read_lock();
  676. node = avc_lookup(ssid, tsid, tclass);
  677. if (!node) {
  678. rcu_read_unlock();
  679. if (in_avd)
  680. avd = in_avd;
  681. else
  682. avd = &avd_entry;
  683. security_compute_av(ssid, tsid, tclass, avd);
  684. rcu_read_lock();
  685. node = avc_insert(ssid, tsid, tclass, avd);
  686. } else {
  687. if (in_avd)
  688. memcpy(in_avd, &node->ae.avd, sizeof(*in_avd));
  689. avd = &node->ae.avd;
  690. }
  691. denied = requested & ~(avd->allowed);
  692. if (denied) {
  693. if (flags & AVC_STRICT)
  694. rc = -EACCES;
  695. else if (!selinux_enforcing || (avd->flags & AVD_FLAGS_PERMISSIVE))
  696. avc_update_node(AVC_CALLBACK_GRANT, requested, ssid,
  697. tsid, tclass, avd->seqno);
  698. else
  699. rc = -EACCES;
  700. }
  701. rcu_read_unlock();
  702. return rc;
  703. }
  704. /**
  705. * avc_has_perm - Check permissions and perform any appropriate auditing.
  706. * @ssid: source security identifier
  707. * @tsid: target security identifier
  708. * @tclass: target security class
  709. * @requested: requested permissions, interpreted based on @tclass
  710. * @auditdata: auxiliary audit data
  711. * @flags: VFS walk flags
  712. *
  713. * Check the AVC to determine whether the @requested permissions are granted
  714. * for the SID pair (@ssid, @tsid), interpreting the permissions
  715. * based on @tclass, and call the security server on a cache miss to obtain
  716. * a new decision and add it to the cache. Audit the granting or denial of
  717. * permissions in accordance with the policy. Return %0 if all @requested
  718. * permissions are granted, -%EACCES if any permissions are denied, or
  719. * another -errno upon other errors.
  720. */
  721. int avc_has_perm_flags(u32 ssid, u32 tsid, u16 tclass,
  722. u32 requested, struct common_audit_data *auditdata,
  723. unsigned flags)
  724. {
  725. struct av_decision avd;
  726. int rc, rc2;
  727. rc = avc_has_perm_noaudit(ssid, tsid, tclass, requested, 0, &avd);
  728. rc2 = avc_audit(ssid, tsid, tclass, requested, &avd, rc, auditdata,
  729. flags);
  730. if (rc2)
  731. return rc2;
  732. return rc;
  733. }
  734. u32 avc_policy_seqno(void)
  735. {
  736. return avc_cache.latest_notif;
  737. }
  738. void avc_disable(void)
  739. {
  740. /*
  741. * If you are looking at this because you have realized that we are
  742. * not destroying the avc_node_cachep it might be easy to fix, but
  743. * I don't know the memory barrier semantics well enough to know. It's
  744. * possible that some other task dereferenced security_ops when
  745. * it still pointed to selinux operations. If that is the case it's
  746. * possible that it is about to use the avc and is about to need the
  747. * avc_node_cachep. I know I could wrap the security.c security_ops call
  748. * in an rcu_lock, but seriously, it's not worth it. Instead I just flush
  749. * the cache and get that memory back.
  750. */
  751. if (avc_node_cachep) {
  752. avc_flush();
  753. /* kmem_cache_destroy(avc_node_cachep); */
  754. }
  755. }