avc.c 23 KB

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