avc.c 22 KB

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