avc.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951
  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. atomic_t used; /* used recently */
  84. };
  85. struct avc_node {
  86. struct avc_entry ae;
  87. struct list_head list;
  88. struct rcu_head rhead;
  89. };
  90. struct avc_cache {
  91. struct list_head slots[AVC_CACHE_SLOTS];
  92. spinlock_t slots_lock[AVC_CACHE_SLOTS]; /* lock for writes */
  93. atomic_t lru_hint; /* LRU hint for reclaim scan */
  94. atomic_t active_nodes;
  95. u32 latest_notif; /* latest revocation notification */
  96. };
  97. struct avc_callback_node {
  98. int (*callback) (u32 event, u32 ssid, u32 tsid,
  99. u16 tclass, u32 perms,
  100. u32 *out_retained);
  101. u32 events;
  102. u32 ssid;
  103. u32 tsid;
  104. u16 tclass;
  105. u32 perms;
  106. struct avc_callback_node *next;
  107. };
  108. /* Exported via selinufs */
  109. unsigned int avc_cache_threshold = AVC_DEF_CACHE_THRESHOLD;
  110. #ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
  111. DEFINE_PER_CPU(struct avc_cache_stats, avc_cache_stats) = { 0 };
  112. #endif
  113. static struct avc_cache avc_cache;
  114. static struct avc_callback_node *avc_callbacks;
  115. static struct kmem_cache *avc_node_cachep;
  116. static inline int avc_hash(u32 ssid, u32 tsid, u16 tclass)
  117. {
  118. return (ssid ^ (tsid<<2) ^ (tclass<<4)) & (AVC_CACHE_SLOTS - 1);
  119. }
  120. /**
  121. * avc_dump_av - Display an access vector in human-readable form.
  122. * @tclass: target security class
  123. * @av: access vector
  124. */
  125. void avc_dump_av(struct audit_buffer *ab, u16 tclass, u32 av)
  126. {
  127. const char **common_pts = NULL;
  128. u32 common_base = 0;
  129. int i, i2, perm;
  130. if (av == 0) {
  131. audit_log_format(ab, " null");
  132. return;
  133. }
  134. for (i = 0; i < ARRAY_SIZE(av_inherit); i++) {
  135. if (av_inherit[i].tclass == tclass) {
  136. common_pts = av_inherit[i].common_pts;
  137. common_base = av_inherit[i].common_base;
  138. break;
  139. }
  140. }
  141. audit_log_format(ab, " {");
  142. i = 0;
  143. perm = 1;
  144. while (perm < common_base) {
  145. if (perm & av) {
  146. audit_log_format(ab, " %s", common_pts[i]);
  147. av &= ~perm;
  148. }
  149. i++;
  150. perm <<= 1;
  151. }
  152. while (i < sizeof(av) * 8) {
  153. if (perm & av) {
  154. for (i2 = 0; i2 < ARRAY_SIZE(av_perm_to_string); i2++) {
  155. if ((av_perm_to_string[i2].tclass == tclass) &&
  156. (av_perm_to_string[i2].value == perm))
  157. break;
  158. }
  159. if (i2 < ARRAY_SIZE(av_perm_to_string)) {
  160. audit_log_format(ab, " %s",
  161. av_perm_to_string[i2].name);
  162. av &= ~perm;
  163. }
  164. }
  165. i++;
  166. perm <<= 1;
  167. }
  168. if (av)
  169. audit_log_format(ab, " 0x%x", av);
  170. audit_log_format(ab, " }");
  171. }
  172. /**
  173. * avc_dump_query - Display a SID pair and a class in human-readable form.
  174. * @ssid: source security identifier
  175. * @tsid: target security identifier
  176. * @tclass: target security class
  177. */
  178. static void avc_dump_query(struct audit_buffer *ab, u32 ssid, u32 tsid, u16 tclass)
  179. {
  180. int rc;
  181. char *scontext;
  182. u32 scontext_len;
  183. rc = security_sid_to_context(ssid, &scontext, &scontext_len);
  184. if (rc)
  185. audit_log_format(ab, "ssid=%d", ssid);
  186. else {
  187. audit_log_format(ab, "scontext=%s", scontext);
  188. kfree(scontext);
  189. }
  190. rc = security_sid_to_context(tsid, &scontext, &scontext_len);
  191. if (rc)
  192. audit_log_format(ab, " tsid=%d", tsid);
  193. else {
  194. audit_log_format(ab, " tcontext=%s", scontext);
  195. kfree(scontext);
  196. }
  197. BUG_ON(tclass >= ARRAY_SIZE(class_to_string) || !class_to_string[tclass]);
  198. audit_log_format(ab, " tclass=%s", class_to_string[tclass]);
  199. }
  200. /**
  201. * avc_init - Initialize the AVC.
  202. *
  203. * Initialize the access vector cache.
  204. */
  205. void __init avc_init(void)
  206. {
  207. int i;
  208. for (i = 0; i < AVC_CACHE_SLOTS; i++) {
  209. INIT_LIST_HEAD(&avc_cache.slots[i]);
  210. spin_lock_init(&avc_cache.slots_lock[i]);
  211. }
  212. atomic_set(&avc_cache.active_nodes, 0);
  213. atomic_set(&avc_cache.lru_hint, 0);
  214. avc_node_cachep = kmem_cache_create("avc_node", sizeof(struct avc_node),
  215. 0, SLAB_PANIC, NULL);
  216. audit_log(current->audit_context, GFP_KERNEL, AUDIT_KERNEL, "AVC INITIALIZED\n");
  217. }
  218. int avc_get_hash_stats(char *page)
  219. {
  220. int i, chain_len, max_chain_len, slots_used;
  221. struct avc_node *node;
  222. rcu_read_lock();
  223. slots_used = 0;
  224. max_chain_len = 0;
  225. for (i = 0; i < AVC_CACHE_SLOTS; i++) {
  226. if (!list_empty(&avc_cache.slots[i])) {
  227. slots_used++;
  228. chain_len = 0;
  229. list_for_each_entry_rcu(node, &avc_cache.slots[i], list)
  230. chain_len++;
  231. if (chain_len > max_chain_len)
  232. max_chain_len = chain_len;
  233. }
  234. }
  235. rcu_read_unlock();
  236. return scnprintf(page, PAGE_SIZE, "entries: %d\nbuckets used: %d/%d\n"
  237. "longest chain: %d\n",
  238. atomic_read(&avc_cache.active_nodes),
  239. slots_used, AVC_CACHE_SLOTS, max_chain_len);
  240. }
  241. static void avc_node_free(struct rcu_head *rhead)
  242. {
  243. struct avc_node *node = container_of(rhead, struct avc_node, rhead);
  244. kmem_cache_free(avc_node_cachep, node);
  245. avc_cache_stats_incr(frees);
  246. }
  247. static void avc_node_delete(struct avc_node *node)
  248. {
  249. list_del_rcu(&node->list);
  250. call_rcu(&node->rhead, avc_node_free);
  251. atomic_dec(&avc_cache.active_nodes);
  252. }
  253. static void avc_node_kill(struct avc_node *node)
  254. {
  255. kmem_cache_free(avc_node_cachep, node);
  256. avc_cache_stats_incr(frees);
  257. atomic_dec(&avc_cache.active_nodes);
  258. }
  259. static void avc_node_replace(struct avc_node *new, struct avc_node *old)
  260. {
  261. list_replace_rcu(&old->list, &new->list);
  262. call_rcu(&old->rhead, avc_node_free);
  263. atomic_dec(&avc_cache.active_nodes);
  264. }
  265. static inline int avc_reclaim_node(void)
  266. {
  267. struct avc_node *node;
  268. int hvalue, try, ecx;
  269. unsigned long flags;
  270. for (try = 0, ecx = 0; try < AVC_CACHE_SLOTS; try++) {
  271. hvalue = atomic_inc_return(&avc_cache.lru_hint) & (AVC_CACHE_SLOTS - 1);
  272. if (!spin_trylock_irqsave(&avc_cache.slots_lock[hvalue], flags))
  273. continue;
  274. rcu_read_lock();
  275. list_for_each_entry(node, &avc_cache.slots[hvalue], list) {
  276. if (atomic_dec_and_test(&node->ae.used)) {
  277. /* Recently Unused */
  278. avc_node_delete(node);
  279. avc_cache_stats_incr(reclaims);
  280. ecx++;
  281. if (ecx >= AVC_CACHE_RECLAIM) {
  282. rcu_read_unlock();
  283. spin_unlock_irqrestore(&avc_cache.slots_lock[hvalue], flags);
  284. goto out;
  285. }
  286. }
  287. }
  288. rcu_read_unlock();
  289. spin_unlock_irqrestore(&avc_cache.slots_lock[hvalue], flags);
  290. }
  291. out:
  292. return ecx;
  293. }
  294. static struct avc_node *avc_alloc_node(void)
  295. {
  296. struct avc_node *node;
  297. node = kmem_cache_zalloc(avc_node_cachep, GFP_ATOMIC);
  298. if (!node)
  299. goto out;
  300. INIT_RCU_HEAD(&node->rhead);
  301. INIT_LIST_HEAD(&node->list);
  302. atomic_set(&node->ae.used, 1);
  303. avc_cache_stats_incr(allocations);
  304. if (atomic_inc_return(&avc_cache.active_nodes) > avc_cache_threshold)
  305. avc_reclaim_node();
  306. out:
  307. return node;
  308. }
  309. static void avc_node_populate(struct avc_node *node, u32 ssid, u32 tsid, u16 tclass, struct avc_entry *ae)
  310. {
  311. node->ae.ssid = ssid;
  312. node->ae.tsid = tsid;
  313. node->ae.tclass = tclass;
  314. memcpy(&node->ae.avd, &ae->avd, sizeof(node->ae.avd));
  315. }
  316. static inline struct avc_node *avc_search_node(u32 ssid, u32 tsid, u16 tclass)
  317. {
  318. struct avc_node *node, *ret = NULL;
  319. int hvalue;
  320. hvalue = avc_hash(ssid, tsid, tclass);
  321. list_for_each_entry_rcu(node, &avc_cache.slots[hvalue], list) {
  322. if (ssid == node->ae.ssid &&
  323. tclass == node->ae.tclass &&
  324. tsid == node->ae.tsid) {
  325. ret = node;
  326. break;
  327. }
  328. }
  329. if (ret == NULL) {
  330. /* cache miss */
  331. goto out;
  332. }
  333. /* cache hit */
  334. if (atomic_read(&ret->ae.used) != 1)
  335. atomic_set(&ret->ae.used, 1);
  336. out:
  337. return ret;
  338. }
  339. /**
  340. * avc_lookup - Look up an AVC entry.
  341. * @ssid: source security identifier
  342. * @tsid: target security identifier
  343. * @tclass: target security class
  344. * @requested: requested permissions, interpreted based on @tclass
  345. *
  346. * Look up an AVC entry that is valid for the
  347. * @requested permissions between the SID pair
  348. * (@ssid, @tsid), interpreting the permissions
  349. * based on @tclass. If a valid AVC entry exists,
  350. * then this function return the avc_node.
  351. * Otherwise, this function returns NULL.
  352. */
  353. static struct avc_node *avc_lookup(u32 ssid, u32 tsid, u16 tclass, u32 requested)
  354. {
  355. struct avc_node *node;
  356. avc_cache_stats_incr(lookups);
  357. node = avc_search_node(ssid, tsid, tclass);
  358. if (node && ((node->ae.avd.decided & requested) == requested)) {
  359. avc_cache_stats_incr(hits);
  360. goto out;
  361. }
  362. node = NULL;
  363. avc_cache_stats_incr(misses);
  364. out:
  365. return node;
  366. }
  367. static int avc_latest_notif_update(int seqno, int is_insert)
  368. {
  369. int ret = 0;
  370. static DEFINE_SPINLOCK(notif_lock);
  371. unsigned long flag;
  372. spin_lock_irqsave(&notif_lock, flag);
  373. if (is_insert) {
  374. if (seqno < avc_cache.latest_notif) {
  375. printk(KERN_WARNING "SELinux: avc: seqno %d < latest_notif %d\n",
  376. seqno, avc_cache.latest_notif);
  377. ret = -EAGAIN;
  378. }
  379. } else {
  380. if (seqno > avc_cache.latest_notif)
  381. avc_cache.latest_notif = seqno;
  382. }
  383. spin_unlock_irqrestore(&notif_lock, flag);
  384. return ret;
  385. }
  386. /**
  387. * avc_insert - Insert an AVC entry.
  388. * @ssid: source security identifier
  389. * @tsid: target security identifier
  390. * @tclass: target security class
  391. * @ae: AVC entry
  392. *
  393. * Insert an AVC entry for the SID pair
  394. * (@ssid, @tsid) and class @tclass.
  395. * The access vectors and the sequence number are
  396. * normally provided by the security server in
  397. * response to a security_compute_av() call. If the
  398. * sequence number @ae->avd.seqno is not less than the latest
  399. * revocation notification, then the function copies
  400. * the access vectors into a cache entry, returns
  401. * avc_node inserted. Otherwise, this function returns NULL.
  402. */
  403. static struct avc_node *avc_insert(u32 ssid, u32 tsid, u16 tclass, struct avc_entry *ae)
  404. {
  405. struct avc_node *pos, *node = NULL;
  406. int hvalue;
  407. unsigned long flag;
  408. if (avc_latest_notif_update(ae->avd.seqno, 1))
  409. goto out;
  410. node = avc_alloc_node();
  411. if (node) {
  412. hvalue = avc_hash(ssid, tsid, tclass);
  413. avc_node_populate(node, ssid, tsid, tclass, ae);
  414. spin_lock_irqsave(&avc_cache.slots_lock[hvalue], flag);
  415. list_for_each_entry(pos, &avc_cache.slots[hvalue], list) {
  416. if (pos->ae.ssid == ssid &&
  417. pos->ae.tsid == tsid &&
  418. pos->ae.tclass == tclass) {
  419. avc_node_replace(node, pos);
  420. goto found;
  421. }
  422. }
  423. list_add_rcu(&node->list, &avc_cache.slots[hvalue]);
  424. found:
  425. spin_unlock_irqrestore(&avc_cache.slots_lock[hvalue], flag);
  426. }
  427. out:
  428. return node;
  429. }
  430. static inline void avc_print_ipv6_addr(struct audit_buffer *ab,
  431. struct in6_addr *addr, __be16 port,
  432. char *name1, char *name2)
  433. {
  434. if (!ipv6_addr_any(addr))
  435. audit_log_format(ab, " %s=%pI6", name1, addr);
  436. if (port)
  437. audit_log_format(ab, " %s=%d", name2, ntohs(port));
  438. }
  439. static inline void avc_print_ipv4_addr(struct audit_buffer *ab, __be32 addr,
  440. __be16 port, char *name1, char *name2)
  441. {
  442. if (addr)
  443. audit_log_format(ab, " %s=%pI4", name1, &addr);
  444. if (port)
  445. audit_log_format(ab, " %s=%d", name2, ntohs(port));
  446. }
  447. /**
  448. * avc_audit - Audit the granting or denial of permissions.
  449. * @ssid: source security identifier
  450. * @tsid: target security identifier
  451. * @tclass: target security class
  452. * @requested: requested permissions
  453. * @avd: access vector decisions
  454. * @result: result from avc_has_perm_noaudit
  455. * @a: auxiliary audit data
  456. *
  457. * Audit the granting or denial of permissions in accordance
  458. * with the policy. This function is typically called by
  459. * avc_has_perm() after a permission check, but can also be
  460. * called directly by callers who use avc_has_perm_noaudit()
  461. * in order to separate the permission check from the auditing.
  462. * For example, this separation is useful when the permission check must
  463. * be performed under a lock, to allow the lock to be released
  464. * before calling the auditing code.
  465. */
  466. void avc_audit(u32 ssid, u32 tsid,
  467. u16 tclass, u32 requested,
  468. struct av_decision *avd, int result, struct avc_audit_data *a)
  469. {
  470. struct task_struct *tsk = current;
  471. struct inode *inode = NULL;
  472. u32 denied, audited;
  473. struct audit_buffer *ab;
  474. denied = requested & ~avd->allowed;
  475. if (denied) {
  476. audited = denied;
  477. if (!(audited & avd->auditdeny))
  478. return;
  479. } else if (result) {
  480. audited = denied = requested;
  481. } else {
  482. audited = requested;
  483. if (!(audited & avd->auditallow))
  484. return;
  485. }
  486. ab = audit_log_start(current->audit_context, GFP_ATOMIC, AUDIT_AVC);
  487. if (!ab)
  488. return; /* audit_panic has been called */
  489. audit_log_format(ab, "avc: %s ", denied ? "denied" : "granted");
  490. avc_dump_av(ab, tclass, audited);
  491. audit_log_format(ab, " for ");
  492. if (a && a->tsk)
  493. tsk = a->tsk;
  494. if (tsk && tsk->pid) {
  495. audit_log_format(ab, " pid=%d comm=", tsk->pid);
  496. audit_log_untrustedstring(ab, tsk->comm);
  497. }
  498. if (a) {
  499. switch (a->type) {
  500. case AVC_AUDIT_DATA_IPC:
  501. audit_log_format(ab, " key=%d", a->u.ipc_id);
  502. break;
  503. case AVC_AUDIT_DATA_CAP:
  504. audit_log_format(ab, " capability=%d", a->u.cap);
  505. break;
  506. case AVC_AUDIT_DATA_FS:
  507. if (a->u.fs.path.dentry) {
  508. struct dentry *dentry = a->u.fs.path.dentry;
  509. if (a->u.fs.path.mnt) {
  510. audit_log_d_path(ab, "path=",
  511. &a->u.fs.path);
  512. } else {
  513. audit_log_format(ab, " name=");
  514. audit_log_untrustedstring(ab, dentry->d_name.name);
  515. }
  516. inode = dentry->d_inode;
  517. } else if (a->u.fs.inode) {
  518. struct dentry *dentry;
  519. inode = a->u.fs.inode;
  520. dentry = d_find_alias(inode);
  521. if (dentry) {
  522. audit_log_format(ab, " name=");
  523. audit_log_untrustedstring(ab, dentry->d_name.name);
  524. dput(dentry);
  525. }
  526. }
  527. if (inode)
  528. audit_log_format(ab, " dev=%s ino=%lu",
  529. inode->i_sb->s_id,
  530. inode->i_ino);
  531. break;
  532. case AVC_AUDIT_DATA_NET:
  533. if (a->u.net.sk) {
  534. struct sock *sk = a->u.net.sk;
  535. struct unix_sock *u;
  536. int len = 0;
  537. char *p = NULL;
  538. switch (sk->sk_family) {
  539. case AF_INET: {
  540. struct inet_sock *inet = inet_sk(sk);
  541. avc_print_ipv4_addr(ab, inet->rcv_saddr,
  542. inet->sport,
  543. "laddr", "lport");
  544. avc_print_ipv4_addr(ab, inet->daddr,
  545. inet->dport,
  546. "faddr", "fport");
  547. break;
  548. }
  549. case AF_INET6: {
  550. struct inet_sock *inet = inet_sk(sk);
  551. struct ipv6_pinfo *inet6 = inet6_sk(sk);
  552. avc_print_ipv6_addr(ab, &inet6->rcv_saddr,
  553. inet->sport,
  554. "laddr", "lport");
  555. avc_print_ipv6_addr(ab, &inet6->daddr,
  556. inet->dport,
  557. "faddr", "fport");
  558. break;
  559. }
  560. case AF_UNIX:
  561. u = unix_sk(sk);
  562. if (u->dentry) {
  563. struct path path = {
  564. .dentry = u->dentry,
  565. .mnt = u->mnt
  566. };
  567. audit_log_d_path(ab, "path=",
  568. &path);
  569. break;
  570. }
  571. if (!u->addr)
  572. break;
  573. len = u->addr->len-sizeof(short);
  574. p = &u->addr->name->sun_path[0];
  575. audit_log_format(ab, " path=");
  576. if (*p)
  577. audit_log_untrustedstring(ab, p);
  578. else
  579. audit_log_n_hex(ab, p, len);
  580. break;
  581. }
  582. }
  583. switch (a->u.net.family) {
  584. case AF_INET:
  585. avc_print_ipv4_addr(ab, a->u.net.v4info.saddr,
  586. a->u.net.sport,
  587. "saddr", "src");
  588. avc_print_ipv4_addr(ab, a->u.net.v4info.daddr,
  589. a->u.net.dport,
  590. "daddr", "dest");
  591. break;
  592. case AF_INET6:
  593. avc_print_ipv6_addr(ab, &a->u.net.v6info.saddr,
  594. a->u.net.sport,
  595. "saddr", "src");
  596. avc_print_ipv6_addr(ab, &a->u.net.v6info.daddr,
  597. a->u.net.dport,
  598. "daddr", "dest");
  599. break;
  600. }
  601. if (a->u.net.netif > 0) {
  602. struct net_device *dev;
  603. /* NOTE: we always use init's namespace */
  604. dev = dev_get_by_index(&init_net,
  605. a->u.net.netif);
  606. if (dev) {
  607. audit_log_format(ab, " netif=%s",
  608. dev->name);
  609. dev_put(dev);
  610. }
  611. }
  612. break;
  613. }
  614. }
  615. audit_log_format(ab, " ");
  616. avc_dump_query(ab, ssid, tsid, tclass);
  617. audit_log_end(ab);
  618. }
  619. /**
  620. * avc_add_callback - Register a callback for security events.
  621. * @callback: callback function
  622. * @events: security events
  623. * @ssid: source security identifier or %SECSID_WILD
  624. * @tsid: target security identifier or %SECSID_WILD
  625. * @tclass: target security class
  626. * @perms: permissions
  627. *
  628. * Register a callback function for events in the set @events
  629. * related to the SID pair (@ssid, @tsid) and
  630. * and the permissions @perms, interpreting
  631. * @perms based on @tclass. Returns %0 on success or
  632. * -%ENOMEM if insufficient memory exists to add the callback.
  633. */
  634. int avc_add_callback(int (*callback)(u32 event, u32 ssid, u32 tsid,
  635. u16 tclass, u32 perms,
  636. u32 *out_retained),
  637. u32 events, u32 ssid, u32 tsid,
  638. u16 tclass, u32 perms)
  639. {
  640. struct avc_callback_node *c;
  641. int rc = 0;
  642. c = kmalloc(sizeof(*c), GFP_ATOMIC);
  643. if (!c) {
  644. rc = -ENOMEM;
  645. goto out;
  646. }
  647. c->callback = callback;
  648. c->events = events;
  649. c->ssid = ssid;
  650. c->tsid = tsid;
  651. c->perms = perms;
  652. c->next = avc_callbacks;
  653. avc_callbacks = c;
  654. out:
  655. return rc;
  656. }
  657. static inline int avc_sidcmp(u32 x, u32 y)
  658. {
  659. return (x == y || x == SECSID_WILD || y == SECSID_WILD);
  660. }
  661. /**
  662. * avc_update_node Update an AVC entry
  663. * @event : Updating event
  664. * @perms : Permission mask bits
  665. * @ssid,@tsid,@tclass : identifier of an AVC entry
  666. *
  667. * if a valid AVC entry doesn't exist,this function returns -ENOENT.
  668. * if kmalloc() called internal returns NULL, this function returns -ENOMEM.
  669. * otherwise, this function update the AVC entry. The original AVC-entry object
  670. * will release later by RCU.
  671. */
  672. static int avc_update_node(u32 event, u32 perms, u32 ssid, u32 tsid, u16 tclass)
  673. {
  674. int hvalue, rc = 0;
  675. unsigned long flag;
  676. struct avc_node *pos, *node, *orig = NULL;
  677. node = avc_alloc_node();
  678. if (!node) {
  679. rc = -ENOMEM;
  680. goto out;
  681. }
  682. /* Lock the target slot */
  683. hvalue = avc_hash(ssid, tsid, tclass);
  684. spin_lock_irqsave(&avc_cache.slots_lock[hvalue], flag);
  685. list_for_each_entry(pos, &avc_cache.slots[hvalue], list) {
  686. if (ssid == pos->ae.ssid &&
  687. tsid == pos->ae.tsid &&
  688. tclass == pos->ae.tclass){
  689. orig = pos;
  690. break;
  691. }
  692. }
  693. if (!orig) {
  694. rc = -ENOENT;
  695. avc_node_kill(node);
  696. goto out_unlock;
  697. }
  698. /*
  699. * Copy and replace original node.
  700. */
  701. avc_node_populate(node, ssid, tsid, tclass, &orig->ae);
  702. switch (event) {
  703. case AVC_CALLBACK_GRANT:
  704. node->ae.avd.allowed |= perms;
  705. break;
  706. case AVC_CALLBACK_TRY_REVOKE:
  707. case AVC_CALLBACK_REVOKE:
  708. node->ae.avd.allowed &= ~perms;
  709. break;
  710. case AVC_CALLBACK_AUDITALLOW_ENABLE:
  711. node->ae.avd.auditallow |= perms;
  712. break;
  713. case AVC_CALLBACK_AUDITALLOW_DISABLE:
  714. node->ae.avd.auditallow &= ~perms;
  715. break;
  716. case AVC_CALLBACK_AUDITDENY_ENABLE:
  717. node->ae.avd.auditdeny |= perms;
  718. break;
  719. case AVC_CALLBACK_AUDITDENY_DISABLE:
  720. node->ae.avd.auditdeny &= ~perms;
  721. break;
  722. }
  723. avc_node_replace(node, orig);
  724. out_unlock:
  725. spin_unlock_irqrestore(&avc_cache.slots_lock[hvalue], flag);
  726. out:
  727. return rc;
  728. }
  729. /**
  730. * avc_ss_reset - Flush the cache and revalidate migrated permissions.
  731. * @seqno: policy sequence number
  732. */
  733. int avc_ss_reset(u32 seqno)
  734. {
  735. struct avc_callback_node *c;
  736. int i, rc = 0, tmprc;
  737. unsigned long flag;
  738. struct avc_node *node;
  739. for (i = 0; i < AVC_CACHE_SLOTS; i++) {
  740. spin_lock_irqsave(&avc_cache.slots_lock[i], flag);
  741. /*
  742. * With preemptable RCU, the outer spinlock does not
  743. * prevent RCU grace periods from ending.
  744. */
  745. rcu_read_lock();
  746. list_for_each_entry(node, &avc_cache.slots[i], list)
  747. avc_node_delete(node);
  748. rcu_read_unlock();
  749. spin_unlock_irqrestore(&avc_cache.slots_lock[i], flag);
  750. }
  751. for (c = avc_callbacks; c; c = c->next) {
  752. if (c->events & AVC_CALLBACK_RESET) {
  753. tmprc = c->callback(AVC_CALLBACK_RESET,
  754. 0, 0, 0, 0, NULL);
  755. /* save the first error encountered for the return
  756. value and continue processing the callbacks */
  757. if (!rc)
  758. rc = tmprc;
  759. }
  760. }
  761. avc_latest_notif_update(seqno, 0);
  762. return rc;
  763. }
  764. /**
  765. * avc_has_perm_noaudit - Check permissions but perform no auditing.
  766. * @ssid: source security identifier
  767. * @tsid: target security identifier
  768. * @tclass: target security class
  769. * @requested: requested permissions, interpreted based on @tclass
  770. * @flags: AVC_STRICT or 0
  771. * @avd: access vector decisions
  772. *
  773. * Check the AVC to determine whether the @requested permissions are granted
  774. * for the SID pair (@ssid, @tsid), interpreting the permissions
  775. * based on @tclass, and call the security server on a cache miss to obtain
  776. * a new decision and add it to the cache. Return a copy of the decisions
  777. * in @avd. Return %0 if all @requested permissions are granted,
  778. * -%EACCES if any permissions are denied, or another -errno upon
  779. * other errors. This function is typically called by avc_has_perm(),
  780. * but may also be called directly to separate permission checking from
  781. * auditing, e.g. in cases where a lock must be held for the check but
  782. * should be released for the auditing.
  783. */
  784. int avc_has_perm_noaudit(u32 ssid, u32 tsid,
  785. u16 tclass, u32 requested,
  786. unsigned flags,
  787. struct av_decision *avd)
  788. {
  789. struct avc_node *node;
  790. struct avc_entry entry, *p_ae;
  791. int rc = 0;
  792. u32 denied;
  793. BUG_ON(!requested);
  794. rcu_read_lock();
  795. node = avc_lookup(ssid, tsid, tclass, requested);
  796. if (!node) {
  797. rcu_read_unlock();
  798. rc = security_compute_av(ssid, tsid, tclass, requested, &entry.avd);
  799. if (rc)
  800. goto out;
  801. rcu_read_lock();
  802. node = avc_insert(ssid, tsid, tclass, &entry);
  803. }
  804. p_ae = node ? &node->ae : &entry;
  805. if (avd)
  806. memcpy(avd, &p_ae->avd, sizeof(*avd));
  807. denied = requested & ~(p_ae->avd.allowed);
  808. if (denied) {
  809. if (flags & AVC_STRICT)
  810. rc = -EACCES;
  811. else if (!selinux_enforcing || security_permissive_sid(ssid))
  812. avc_update_node(AVC_CALLBACK_GRANT, requested, ssid,
  813. tsid, tclass);
  814. else
  815. rc = -EACCES;
  816. }
  817. rcu_read_unlock();
  818. out:
  819. return rc;
  820. }
  821. /**
  822. * avc_has_perm - Check permissions and perform any appropriate auditing.
  823. * @ssid: source security identifier
  824. * @tsid: target security identifier
  825. * @tclass: target security class
  826. * @requested: requested permissions, interpreted based on @tclass
  827. * @auditdata: auxiliary audit data
  828. *
  829. * Check the AVC to determine whether the @requested permissions are granted
  830. * for the SID pair (@ssid, @tsid), interpreting the permissions
  831. * based on @tclass, and call the security server on a cache miss to obtain
  832. * a new decision and add it to the cache. Audit the granting or denial of
  833. * permissions in accordance with the policy. Return %0 if all @requested
  834. * permissions are granted, -%EACCES if any permissions are denied, or
  835. * another -errno upon other errors.
  836. */
  837. int avc_has_perm(u32 ssid, u32 tsid, u16 tclass,
  838. u32 requested, struct avc_audit_data *auditdata)
  839. {
  840. struct av_decision avd;
  841. int rc;
  842. rc = avc_has_perm_noaudit(ssid, tsid, tclass, requested, 0, &avd);
  843. avc_audit(ssid, tsid, tclass, requested, &avd, rc, auditdata);
  844. return rc;
  845. }
  846. u32 avc_policy_seqno(void)
  847. {
  848. return avc_cache.latest_notif;
  849. }