avc.c 21 KB

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