avc.c 23 KB

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