avc.c 24 KB

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