avc.c 24 KB

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