avc.c 22 KB

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