avc.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838
  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. *
  412. * Audit the granting or denial of permissions in accordance
  413. * with the policy. This function is typically called by
  414. * avc_has_perm() after a permission check, but can also be
  415. * called directly by callers who use avc_has_perm_noaudit()
  416. * in order to separate the permission check from the auditing.
  417. * For example, this separation is useful when the permission check must
  418. * be performed under a lock, to allow the lock to be released
  419. * before calling the auditing code.
  420. */
  421. void avc_audit(u32 ssid, u32 tsid,
  422. u16 tclass, u32 requested,
  423. struct av_decision *avd, int result, struct common_audit_data *a)
  424. {
  425. struct common_audit_data stack_data;
  426. u32 denied, audited;
  427. denied = requested & ~avd->allowed;
  428. if (denied) {
  429. audited = denied & avd->auditdeny;
  430. /*
  431. * a->selinux_audit_data.auditdeny is TRICKY! Setting a bit in
  432. * this field means that ANY denials should NOT be audited if
  433. * the policy contains an explicit dontaudit rule for that
  434. * permission. Take notice that this is unrelated to the
  435. * actual permissions that were denied. As an example lets
  436. * assume:
  437. *
  438. * denied == READ
  439. * avd.auditdeny & ACCESS == 0 (not set means explicit rule)
  440. * selinux_audit_data.auditdeny & ACCESS == 1
  441. *
  442. * We will NOT audit the denial even though the denied
  443. * permission was READ and the auditdeny checks were for
  444. * ACCESS
  445. */
  446. if (a &&
  447. a->selinux_audit_data.auditdeny &&
  448. !(a->selinux_audit_data.auditdeny & avd->auditdeny))
  449. audited = 0;
  450. } else if (result)
  451. audited = denied = requested;
  452. else
  453. audited = requested & avd->auditallow;
  454. if (!audited)
  455. return;
  456. if (!a) {
  457. a = &stack_data;
  458. COMMON_AUDIT_DATA_INIT(a, NONE);
  459. }
  460. a->selinux_audit_data.tclass = tclass;
  461. a->selinux_audit_data.requested = requested;
  462. a->selinux_audit_data.ssid = ssid;
  463. a->selinux_audit_data.tsid = tsid;
  464. a->selinux_audit_data.audited = audited;
  465. a->selinux_audit_data.denied = denied;
  466. a->lsm_pre_audit = avc_audit_pre_callback;
  467. a->lsm_post_audit = avc_audit_post_callback;
  468. common_lsm_audit(a);
  469. }
  470. /**
  471. * avc_add_callback - Register a callback for security events.
  472. * @callback: callback function
  473. * @events: security events
  474. * @ssid: source security identifier or %SECSID_WILD
  475. * @tsid: target security identifier or %SECSID_WILD
  476. * @tclass: target security class
  477. * @perms: permissions
  478. *
  479. * Register a callback function for events in the set @events
  480. * related to the SID pair (@ssid, @tsid)
  481. * and the permissions @perms, interpreting
  482. * @perms based on @tclass. Returns %0 on success or
  483. * -%ENOMEM if insufficient memory exists to add the callback.
  484. */
  485. int avc_add_callback(int (*callback)(u32 event, u32 ssid, u32 tsid,
  486. u16 tclass, u32 perms,
  487. u32 *out_retained),
  488. u32 events, u32 ssid, u32 tsid,
  489. u16 tclass, u32 perms)
  490. {
  491. struct avc_callback_node *c;
  492. int rc = 0;
  493. c = kmalloc(sizeof(*c), GFP_ATOMIC);
  494. if (!c) {
  495. rc = -ENOMEM;
  496. goto out;
  497. }
  498. c->callback = callback;
  499. c->events = events;
  500. c->ssid = ssid;
  501. c->tsid = tsid;
  502. c->perms = perms;
  503. c->next = avc_callbacks;
  504. avc_callbacks = c;
  505. out:
  506. return rc;
  507. }
  508. static inline int avc_sidcmp(u32 x, u32 y)
  509. {
  510. return (x == y || x == SECSID_WILD || y == SECSID_WILD);
  511. }
  512. /**
  513. * avc_update_node Update an AVC entry
  514. * @event : Updating event
  515. * @perms : Permission mask bits
  516. * @ssid,@tsid,@tclass : identifier of an AVC entry
  517. * @seqno : sequence number when decision was made
  518. *
  519. * if a valid AVC entry doesn't exist,this function returns -ENOENT.
  520. * if kmalloc() called internal returns NULL, this function returns -ENOMEM.
  521. * otherwise, this function updates the AVC entry. The original AVC-entry object
  522. * will release later by RCU.
  523. */
  524. static int avc_update_node(u32 event, u32 perms, u32 ssid, u32 tsid, u16 tclass,
  525. u32 seqno)
  526. {
  527. int hvalue, rc = 0;
  528. unsigned long flag;
  529. struct avc_node *pos, *node, *orig = NULL;
  530. struct hlist_head *head;
  531. struct hlist_node *next;
  532. spinlock_t *lock;
  533. node = avc_alloc_node();
  534. if (!node) {
  535. rc = -ENOMEM;
  536. goto out;
  537. }
  538. /* Lock the target slot */
  539. hvalue = avc_hash(ssid, tsid, tclass);
  540. head = &avc_cache.slots[hvalue];
  541. lock = &avc_cache.slots_lock[hvalue];
  542. spin_lock_irqsave(lock, flag);
  543. hlist_for_each_entry(pos, next, head, list) {
  544. if (ssid == pos->ae.ssid &&
  545. tsid == pos->ae.tsid &&
  546. tclass == pos->ae.tclass &&
  547. seqno == pos->ae.avd.seqno){
  548. orig = pos;
  549. break;
  550. }
  551. }
  552. if (!orig) {
  553. rc = -ENOENT;
  554. avc_node_kill(node);
  555. goto out_unlock;
  556. }
  557. /*
  558. * Copy and replace original node.
  559. */
  560. avc_node_populate(node, ssid, tsid, tclass, &orig->ae.avd);
  561. switch (event) {
  562. case AVC_CALLBACK_GRANT:
  563. node->ae.avd.allowed |= perms;
  564. break;
  565. case AVC_CALLBACK_TRY_REVOKE:
  566. case AVC_CALLBACK_REVOKE:
  567. node->ae.avd.allowed &= ~perms;
  568. break;
  569. case AVC_CALLBACK_AUDITALLOW_ENABLE:
  570. node->ae.avd.auditallow |= perms;
  571. break;
  572. case AVC_CALLBACK_AUDITALLOW_DISABLE:
  573. node->ae.avd.auditallow &= ~perms;
  574. break;
  575. case AVC_CALLBACK_AUDITDENY_ENABLE:
  576. node->ae.avd.auditdeny |= perms;
  577. break;
  578. case AVC_CALLBACK_AUDITDENY_DISABLE:
  579. node->ae.avd.auditdeny &= ~perms;
  580. break;
  581. }
  582. avc_node_replace(node, orig);
  583. out_unlock:
  584. spin_unlock_irqrestore(lock, flag);
  585. out:
  586. return rc;
  587. }
  588. /**
  589. * avc_flush - Flush the cache
  590. */
  591. static void avc_flush(void)
  592. {
  593. struct hlist_head *head;
  594. struct hlist_node *next;
  595. struct avc_node *node;
  596. spinlock_t *lock;
  597. unsigned long flag;
  598. int i;
  599. for (i = 0; i < AVC_CACHE_SLOTS; i++) {
  600. head = &avc_cache.slots[i];
  601. lock = &avc_cache.slots_lock[i];
  602. spin_lock_irqsave(lock, flag);
  603. /*
  604. * With preemptable RCU, the outer spinlock does not
  605. * prevent RCU grace periods from ending.
  606. */
  607. rcu_read_lock();
  608. hlist_for_each_entry(node, next, head, list)
  609. avc_node_delete(node);
  610. rcu_read_unlock();
  611. spin_unlock_irqrestore(lock, flag);
  612. }
  613. }
  614. /**
  615. * avc_ss_reset - Flush the cache and revalidate migrated permissions.
  616. * @seqno: policy sequence number
  617. */
  618. int avc_ss_reset(u32 seqno)
  619. {
  620. struct avc_callback_node *c;
  621. int rc = 0, tmprc;
  622. avc_flush();
  623. for (c = avc_callbacks; c; c = c->next) {
  624. if (c->events & AVC_CALLBACK_RESET) {
  625. tmprc = c->callback(AVC_CALLBACK_RESET,
  626. 0, 0, 0, 0, NULL);
  627. /* save the first error encountered for the return
  628. value and continue processing the callbacks */
  629. if (!rc)
  630. rc = tmprc;
  631. }
  632. }
  633. avc_latest_notif_update(seqno, 0);
  634. return rc;
  635. }
  636. /**
  637. * avc_has_perm_noaudit - Check permissions but perform no auditing.
  638. * @ssid: source security identifier
  639. * @tsid: target security identifier
  640. * @tclass: target security class
  641. * @requested: requested permissions, interpreted based on @tclass
  642. * @flags: AVC_STRICT or 0
  643. * @avd: access vector decisions
  644. *
  645. * Check the AVC to determine whether the @requested permissions are granted
  646. * for the SID pair (@ssid, @tsid), interpreting the permissions
  647. * based on @tclass, and call the security server on a cache miss to obtain
  648. * a new decision and add it to the cache. Return a copy of the decisions
  649. * in @avd. Return %0 if all @requested permissions are granted,
  650. * -%EACCES if any permissions are denied, or another -errno upon
  651. * other errors. This function is typically called by avc_has_perm(),
  652. * but may also be called directly to separate permission checking from
  653. * auditing, e.g. in cases where a lock must be held for the check but
  654. * should be released for the auditing.
  655. */
  656. int avc_has_perm_noaudit(u32 ssid, u32 tsid,
  657. u16 tclass, u32 requested,
  658. unsigned flags,
  659. struct av_decision *in_avd)
  660. {
  661. struct avc_node *node;
  662. struct av_decision avd_entry, *avd;
  663. int rc = 0;
  664. u32 denied;
  665. BUG_ON(!requested);
  666. rcu_read_lock();
  667. node = avc_lookup(ssid, tsid, tclass);
  668. if (!node) {
  669. rcu_read_unlock();
  670. if (in_avd)
  671. avd = in_avd;
  672. else
  673. avd = &avd_entry;
  674. security_compute_av(ssid, tsid, tclass, avd);
  675. rcu_read_lock();
  676. node = avc_insert(ssid, tsid, tclass, avd);
  677. } else {
  678. if (in_avd)
  679. memcpy(in_avd, &node->ae.avd, sizeof(*in_avd));
  680. avd = &node->ae.avd;
  681. }
  682. denied = requested & ~(avd->allowed);
  683. if (denied) {
  684. if (flags & AVC_STRICT)
  685. rc = -EACCES;
  686. else if (!selinux_enforcing || (avd->flags & AVD_FLAGS_PERMISSIVE))
  687. avc_update_node(AVC_CALLBACK_GRANT, requested, ssid,
  688. tsid, tclass, avd->seqno);
  689. else
  690. rc = -EACCES;
  691. }
  692. rcu_read_unlock();
  693. return rc;
  694. }
  695. /**
  696. * avc_has_perm - Check permissions and perform any appropriate auditing.
  697. * @ssid: source security identifier
  698. * @tsid: target security identifier
  699. * @tclass: target security class
  700. * @requested: requested permissions, interpreted based on @tclass
  701. * @auditdata: auxiliary audit data
  702. *
  703. * Check the AVC to determine whether the @requested permissions are granted
  704. * for the SID pair (@ssid, @tsid), interpreting the permissions
  705. * based on @tclass, and call the security server on a cache miss to obtain
  706. * a new decision and add it to the cache. Audit the granting or denial of
  707. * permissions in accordance with the policy. Return %0 if all @requested
  708. * permissions are granted, -%EACCES if any permissions are denied, or
  709. * another -errno upon other errors.
  710. */
  711. int avc_has_perm(u32 ssid, u32 tsid, u16 tclass,
  712. u32 requested, struct common_audit_data *auditdata)
  713. {
  714. struct av_decision avd;
  715. int rc;
  716. rc = avc_has_perm_noaudit(ssid, tsid, tclass, requested, 0, &avd);
  717. avc_audit(ssid, tsid, tclass, requested, &avd, rc, auditdata);
  718. return rc;
  719. }
  720. u32 avc_policy_seqno(void)
  721. {
  722. return avc_cache.latest_notif;
  723. }
  724. void avc_disable(void)
  725. {
  726. /*
  727. * If you are looking at this because you have realized that we are
  728. * not destroying the avc_node_cachep it might be easy to fix, but
  729. * I don't know the memory barrier semantics well enough to know. It's
  730. * possible that some other task dereferenced security_ops when
  731. * it still pointed to selinux operations. If that is the case it's
  732. * possible that it is about to use the avc and is about to need the
  733. * avc_node_cachep. I know I could wrap the security.c security_ops call
  734. * in an rcu_lock, but seriously, it's not worth it. Instead I just flush
  735. * the cache and get that memory back.
  736. */
  737. if (avc_node_cachep) {
  738. avc_flush();
  739. /* kmem_cache_destroy(avc_node_cachep); */
  740. }
  741. }