avc.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917
  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, 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=%04x:%04x:%04x:%04x:%04x:"
  435. "%04x:%04x:%04x", name1, NIP6(*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, u32 addr,
  440. __be16 port, char *name1, char *name2)
  441. {
  442. if (addr)
  443. audit_log_format(ab, " %s=%d.%d.%d.%d", name1, NIPQUAD(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, 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.dentry) {
  508. struct dentry *dentry = a->u.fs.dentry;
  509. if (a->u.fs.mnt)
  510. audit_avc_path(dentry, a->u.fs.mnt);
  511. audit_log_format(ab, " name=");
  512. audit_log_untrustedstring(ab, dentry->d_name.name);
  513. inode = dentry->d_inode;
  514. } else if (a->u.fs.inode) {
  515. struct dentry *dentry;
  516. inode = a->u.fs.inode;
  517. dentry = d_find_alias(inode);
  518. if (dentry) {
  519. audit_log_format(ab, " name=");
  520. audit_log_untrustedstring(ab, dentry->d_name.name);
  521. dput(dentry);
  522. }
  523. }
  524. if (inode)
  525. audit_log_format(ab, " dev=%s ino=%ld",
  526. inode->i_sb->s_id,
  527. inode->i_ino);
  528. break;
  529. case AVC_AUDIT_DATA_NET:
  530. if (a->u.net.sk) {
  531. struct sock *sk = a->u.net.sk;
  532. struct unix_sock *u;
  533. int len = 0;
  534. char *p = NULL;
  535. switch (sk->sk_family) {
  536. case AF_INET: {
  537. struct inet_sock *inet = inet_sk(sk);
  538. avc_print_ipv4_addr(ab, inet->rcv_saddr,
  539. inet->sport,
  540. "laddr", "lport");
  541. avc_print_ipv4_addr(ab, inet->daddr,
  542. inet->dport,
  543. "faddr", "fport");
  544. break;
  545. }
  546. case AF_INET6: {
  547. struct inet_sock *inet = inet_sk(sk);
  548. struct ipv6_pinfo *inet6 = inet6_sk(sk);
  549. avc_print_ipv6_addr(ab, &inet6->rcv_saddr,
  550. inet->sport,
  551. "laddr", "lport");
  552. avc_print_ipv6_addr(ab, &inet6->daddr,
  553. inet->dport,
  554. "faddr", "fport");
  555. break;
  556. }
  557. case AF_UNIX:
  558. u = unix_sk(sk);
  559. if (u->dentry) {
  560. audit_avc_path(u->dentry, u->mnt);
  561. audit_log_format(ab, " name=");
  562. audit_log_untrustedstring(ab, u->dentry->d_name.name);
  563. break;
  564. }
  565. if (!u->addr)
  566. break;
  567. len = u->addr->len-sizeof(short);
  568. p = &u->addr->name->sun_path[0];
  569. audit_log_format(ab, " path=");
  570. if (*p)
  571. audit_log_untrustedstring(ab, p);
  572. else
  573. audit_log_hex(ab, p, len);
  574. break;
  575. }
  576. }
  577. switch (a->u.net.family) {
  578. case AF_INET:
  579. avc_print_ipv4_addr(ab, a->u.net.v4info.saddr,
  580. a->u.net.sport,
  581. "saddr", "src");
  582. avc_print_ipv4_addr(ab, a->u.net.v4info.daddr,
  583. a->u.net.dport,
  584. "daddr", "dest");
  585. break;
  586. case AF_INET6:
  587. avc_print_ipv6_addr(ab, &a->u.net.v6info.saddr,
  588. a->u.net.sport,
  589. "saddr", "src");
  590. avc_print_ipv6_addr(ab, &a->u.net.v6info.daddr,
  591. a->u.net.dport,
  592. "daddr", "dest");
  593. break;
  594. }
  595. if (a->u.net.netif)
  596. audit_log_format(ab, " netif=%s",
  597. a->u.net.netif);
  598. break;
  599. }
  600. }
  601. audit_log_format(ab, " ");
  602. avc_dump_query(ab, ssid, tsid, tclass);
  603. audit_log_end(ab);
  604. }
  605. /**
  606. * avc_add_callback - Register a callback for security events.
  607. * @callback: callback function
  608. * @events: security events
  609. * @ssid: source security identifier or %SECSID_WILD
  610. * @tsid: target security identifier or %SECSID_WILD
  611. * @tclass: target security class
  612. * @perms: permissions
  613. *
  614. * Register a callback function for events in the set @events
  615. * related to the SID pair (@ssid, @tsid) and
  616. * and the permissions @perms, interpreting
  617. * @perms based on @tclass. Returns %0 on success or
  618. * -%ENOMEM if insufficient memory exists to add the callback.
  619. */
  620. int avc_add_callback(int (*callback)(u32 event, u32 ssid, u32 tsid,
  621. u16 tclass, u32 perms,
  622. u32 *out_retained),
  623. u32 events, u32 ssid, u32 tsid,
  624. u16 tclass, u32 perms)
  625. {
  626. struct avc_callback_node *c;
  627. int rc = 0;
  628. c = kmalloc(sizeof(*c), GFP_ATOMIC);
  629. if (!c) {
  630. rc = -ENOMEM;
  631. goto out;
  632. }
  633. c->callback = callback;
  634. c->events = events;
  635. c->ssid = ssid;
  636. c->tsid = tsid;
  637. c->perms = perms;
  638. c->next = avc_callbacks;
  639. avc_callbacks = c;
  640. out:
  641. return rc;
  642. }
  643. static inline int avc_sidcmp(u32 x, u32 y)
  644. {
  645. return (x == y || x == SECSID_WILD || y == SECSID_WILD);
  646. }
  647. /**
  648. * avc_update_node Update an AVC entry
  649. * @event : Updating event
  650. * @perms : Permission mask bits
  651. * @ssid,@tsid,@tclass : identifier of an AVC entry
  652. *
  653. * if a valid AVC entry doesn't exist,this function returns -ENOENT.
  654. * if kmalloc() called internal returns NULL, this function returns -ENOMEM.
  655. * otherwise, this function update the AVC entry. The original AVC-entry object
  656. * will release later by RCU.
  657. */
  658. static int avc_update_node(u32 event, u32 perms, u32 ssid, u32 tsid, u16 tclass)
  659. {
  660. int hvalue, rc = 0;
  661. unsigned long flag;
  662. struct avc_node *pos, *node, *orig = NULL;
  663. node = avc_alloc_node();
  664. if (!node) {
  665. rc = -ENOMEM;
  666. goto out;
  667. }
  668. /* Lock the target slot */
  669. hvalue = avc_hash(ssid, tsid, tclass);
  670. spin_lock_irqsave(&avc_cache.slots_lock[hvalue], flag);
  671. list_for_each_entry(pos, &avc_cache.slots[hvalue], list){
  672. if ( ssid==pos->ae.ssid &&
  673. tsid==pos->ae.tsid &&
  674. tclass==pos->ae.tclass ){
  675. orig = pos;
  676. break;
  677. }
  678. }
  679. if (!orig) {
  680. rc = -ENOENT;
  681. avc_node_kill(node);
  682. goto out_unlock;
  683. }
  684. /*
  685. * Copy and replace original node.
  686. */
  687. avc_node_populate(node, ssid, tsid, tclass, &orig->ae);
  688. switch (event) {
  689. case AVC_CALLBACK_GRANT:
  690. node->ae.avd.allowed |= perms;
  691. break;
  692. case AVC_CALLBACK_TRY_REVOKE:
  693. case AVC_CALLBACK_REVOKE:
  694. node->ae.avd.allowed &= ~perms;
  695. break;
  696. case AVC_CALLBACK_AUDITALLOW_ENABLE:
  697. node->ae.avd.auditallow |= perms;
  698. break;
  699. case AVC_CALLBACK_AUDITALLOW_DISABLE:
  700. node->ae.avd.auditallow &= ~perms;
  701. break;
  702. case AVC_CALLBACK_AUDITDENY_ENABLE:
  703. node->ae.avd.auditdeny |= perms;
  704. break;
  705. case AVC_CALLBACK_AUDITDENY_DISABLE:
  706. node->ae.avd.auditdeny &= ~perms;
  707. break;
  708. }
  709. avc_node_replace(node, orig);
  710. out_unlock:
  711. spin_unlock_irqrestore(&avc_cache.slots_lock[hvalue], flag);
  712. out:
  713. return rc;
  714. }
  715. /**
  716. * avc_ss_reset - Flush the cache and revalidate migrated permissions.
  717. * @seqno: policy sequence number
  718. */
  719. int avc_ss_reset(u32 seqno)
  720. {
  721. struct avc_callback_node *c;
  722. int i, rc = 0;
  723. unsigned long flag;
  724. struct avc_node *node;
  725. for (i = 0; i < AVC_CACHE_SLOTS; i++) {
  726. spin_lock_irqsave(&avc_cache.slots_lock[i], flag);
  727. list_for_each_entry(node, &avc_cache.slots[i], list)
  728. avc_node_delete(node);
  729. spin_unlock_irqrestore(&avc_cache.slots_lock[i], flag);
  730. }
  731. for (c = avc_callbacks; c; c = c->next) {
  732. if (c->events & AVC_CALLBACK_RESET) {
  733. rc = c->callback(AVC_CALLBACK_RESET,
  734. 0, 0, 0, 0, NULL);
  735. if (rc)
  736. goto out;
  737. }
  738. }
  739. avc_latest_notif_update(seqno, 0);
  740. out:
  741. return rc;
  742. }
  743. /**
  744. * avc_has_perm_noaudit - Check permissions but perform no auditing.
  745. * @ssid: source security identifier
  746. * @tsid: target security identifier
  747. * @tclass: target security class
  748. * @requested: requested permissions, interpreted based on @tclass
  749. * @avd: access vector decisions
  750. *
  751. * Check the AVC to determine whether the @requested permissions are granted
  752. * for the SID pair (@ssid, @tsid), interpreting the permissions
  753. * based on @tclass, and call the security server on a cache miss to obtain
  754. * a new decision and add it to the cache. Return a copy of the decisions
  755. * in @avd. Return %0 if all @requested permissions are granted,
  756. * -%EACCES if any permissions are denied, or another -errno upon
  757. * other errors. This function is typically called by avc_has_perm(),
  758. * but may also be called directly to separate permission checking from
  759. * auditing, e.g. in cases where a lock must be held for the check but
  760. * should be released for the auditing.
  761. */
  762. int avc_has_perm_noaudit(u32 ssid, u32 tsid,
  763. u16 tclass, u32 requested,
  764. struct av_decision *avd)
  765. {
  766. struct avc_node *node;
  767. struct avc_entry entry, *p_ae;
  768. int rc = 0;
  769. u32 denied;
  770. rcu_read_lock();
  771. node = avc_lookup(ssid, tsid, tclass, requested);
  772. if (!node) {
  773. rcu_read_unlock();
  774. rc = security_compute_av(ssid,tsid,tclass,requested,&entry.avd);
  775. if (rc)
  776. goto out;
  777. rcu_read_lock();
  778. node = avc_insert(ssid,tsid,tclass,&entry);
  779. }
  780. p_ae = node ? &node->ae : &entry;
  781. if (avd)
  782. memcpy(avd, &p_ae->avd, sizeof(*avd));
  783. denied = requested & ~(p_ae->avd.allowed);
  784. if (!requested || denied) {
  785. if (selinux_enforcing)
  786. rc = -EACCES;
  787. else
  788. if (node)
  789. avc_update_node(AVC_CALLBACK_GRANT,requested,
  790. ssid,tsid,tclass);
  791. }
  792. rcu_read_unlock();
  793. out:
  794. return rc;
  795. }
  796. /**
  797. * avc_has_perm - Check permissions and perform any appropriate auditing.
  798. * @ssid: source security identifier
  799. * @tsid: target security identifier
  800. * @tclass: target security class
  801. * @requested: requested permissions, interpreted based on @tclass
  802. * @auditdata: auxiliary audit data
  803. *
  804. * Check the AVC to determine whether the @requested permissions are granted
  805. * for the SID pair (@ssid, @tsid), interpreting the permissions
  806. * based on @tclass, and call the security server on a cache miss to obtain
  807. * a new decision and add it to the cache. Audit the granting or denial of
  808. * permissions in accordance with the policy. Return %0 if all @requested
  809. * permissions are granted, -%EACCES if any permissions are denied, or
  810. * another -errno upon other errors.
  811. */
  812. int avc_has_perm(u32 ssid, u32 tsid, u16 tclass,
  813. u32 requested, struct avc_audit_data *auditdata)
  814. {
  815. struct av_decision avd;
  816. int rc;
  817. rc = avc_has_perm_noaudit(ssid, tsid, tclass, requested, &avd);
  818. avc_audit(ssid, tsid, tclass, requested, &avd, rc, auditdata);
  819. return rc;
  820. }