avc.c 23 KB

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