xfrm.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /*
  2. * NSA Security-Enhanced Linux (SELinux) security module
  3. *
  4. * This file contains the SELinux XFRM hook function implementations.
  5. *
  6. * Authors: Serge Hallyn <sergeh@us.ibm.com>
  7. * Trent Jaeger <jaegert@us.ibm.com>
  8. *
  9. * Copyright (C) 2005 International Business Machines Corporation
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License version 2,
  13. * as published by the Free Software Foundation.
  14. */
  15. /*
  16. * USAGE:
  17. * NOTES:
  18. * 1. Make sure to enable the following options in your kernel config:
  19. * CONFIG_SECURITY=y
  20. * CONFIG_SECURITY_NETWORK=y
  21. * CONFIG_SECURITY_NETWORK_XFRM=y
  22. * CONFIG_SECURITY_SELINUX=m/y
  23. * ISSUES:
  24. * 1. Caching packets, so they are not dropped during negotiation
  25. * 2. Emulating a reasonable SO_PEERSEC across machines
  26. * 3. Testing addition of sk_policy's with security context via setsockopt
  27. */
  28. #include <linux/config.h>
  29. #include <linux/module.h>
  30. #include <linux/kernel.h>
  31. #include <linux/init.h>
  32. #include <linux/security.h>
  33. #include <linux/types.h>
  34. #include <linux/netfilter.h>
  35. #include <linux/netfilter_ipv4.h>
  36. #include <linux/netfilter_ipv6.h>
  37. #include <linux/ip.h>
  38. #include <linux/tcp.h>
  39. #include <linux/skbuff.h>
  40. #include <linux/xfrm.h>
  41. #include <net/xfrm.h>
  42. #include <net/checksum.h>
  43. #include <net/udp.h>
  44. #include <asm/semaphore.h>
  45. #include "avc.h"
  46. #include "objsec.h"
  47. #include "xfrm.h"
  48. /*
  49. * Returns true if an LSM/SELinux context
  50. */
  51. static inline int selinux_authorizable_ctx(struct xfrm_sec_ctx *ctx)
  52. {
  53. return (ctx &&
  54. (ctx->ctx_doi == XFRM_SC_DOI_LSM) &&
  55. (ctx->ctx_alg == XFRM_SC_ALG_SELINUX));
  56. }
  57. /*
  58. * Returns true if the xfrm contains a security blob for SELinux
  59. */
  60. static inline int selinux_authorizable_xfrm(struct xfrm_state *x)
  61. {
  62. return selinux_authorizable_ctx(x->security);
  63. }
  64. /*
  65. * LSM hook implementation that authorizes that a socket can be used
  66. * with the corresponding xfrm_sec_ctx and direction.
  67. */
  68. int selinux_xfrm_policy_lookup(struct xfrm_policy *xp, u32 sk_sid, u8 dir)
  69. {
  70. int rc = 0;
  71. u32 sel_sid = SECINITSID_UNLABELED;
  72. struct xfrm_sec_ctx *ctx;
  73. /* Context sid is either set to label or ANY_ASSOC */
  74. if ((ctx = xp->security)) {
  75. if (!selinux_authorizable_ctx(ctx))
  76. return -EINVAL;
  77. sel_sid = ctx->ctx_sid;
  78. }
  79. rc = avc_has_perm(sk_sid, sel_sid, SECCLASS_ASSOCIATION,
  80. ((dir == FLOW_DIR_IN) ? ASSOCIATION__RECVFROM :
  81. ((dir == FLOW_DIR_OUT) ? ASSOCIATION__SENDTO :
  82. (ASSOCIATION__SENDTO | ASSOCIATION__RECVFROM))),
  83. NULL);
  84. return rc;
  85. }
  86. /*
  87. * Security blob allocation for xfrm_policy and xfrm_state
  88. * CTX does not have a meaningful value on input
  89. */
  90. static int selinux_xfrm_sec_ctx_alloc(struct xfrm_sec_ctx **ctxp, struct xfrm_user_sec_ctx *uctx)
  91. {
  92. int rc = 0;
  93. struct task_security_struct *tsec = current->security;
  94. struct xfrm_sec_ctx *ctx;
  95. BUG_ON(!uctx);
  96. BUG_ON(uctx->ctx_doi != XFRM_SC_ALG_SELINUX);
  97. if (uctx->ctx_len >= PAGE_SIZE)
  98. return -ENOMEM;
  99. *ctxp = ctx = kmalloc(sizeof(*ctx) +
  100. uctx->ctx_len,
  101. GFP_KERNEL);
  102. if (!ctx)
  103. return -ENOMEM;
  104. ctx->ctx_doi = uctx->ctx_doi;
  105. ctx->ctx_len = uctx->ctx_len;
  106. ctx->ctx_alg = uctx->ctx_alg;
  107. memcpy(ctx->ctx_str,
  108. uctx+1,
  109. ctx->ctx_len);
  110. rc = security_context_to_sid(ctx->ctx_str,
  111. ctx->ctx_len,
  112. &ctx->ctx_sid);
  113. if (rc)
  114. goto out;
  115. /*
  116. * Does the subject have permission to set security or permission to
  117. * do the relabel?
  118. * Must be permitted to relabel from default socket type (process type)
  119. * to specified context
  120. */
  121. rc = avc_has_perm(tsec->sid, ctx->ctx_sid,
  122. SECCLASS_ASSOCIATION,
  123. ASSOCIATION__SETCONTEXT, NULL);
  124. if (rc)
  125. goto out;
  126. return rc;
  127. out:
  128. *ctxp = NULL;
  129. kfree(ctx);
  130. return rc;
  131. }
  132. /*
  133. * LSM hook implementation that allocs and transfers uctx spec to
  134. * xfrm_policy.
  135. */
  136. int selinux_xfrm_policy_alloc(struct xfrm_policy *xp, struct xfrm_user_sec_ctx *uctx)
  137. {
  138. int err;
  139. BUG_ON(!xp);
  140. err = selinux_xfrm_sec_ctx_alloc(&xp->security, uctx);
  141. return err;
  142. }
  143. /*
  144. * LSM hook implementation that copies security data structure from old to
  145. * new for policy cloning.
  146. */
  147. int selinux_xfrm_policy_clone(struct xfrm_policy *old, struct xfrm_policy *new)
  148. {
  149. struct xfrm_sec_ctx *old_ctx, *new_ctx;
  150. old_ctx = old->security;
  151. if (old_ctx) {
  152. new_ctx = new->security = kmalloc(sizeof(*new_ctx) +
  153. old_ctx->ctx_len,
  154. GFP_KERNEL);
  155. if (!new_ctx)
  156. return -ENOMEM;
  157. memcpy(new_ctx, old_ctx, sizeof(*new_ctx));
  158. memcpy(new_ctx->ctx_str, old_ctx->ctx_str, new_ctx->ctx_len);
  159. }
  160. return 0;
  161. }
  162. /*
  163. * LSM hook implementation that frees xfrm_policy security information.
  164. */
  165. void selinux_xfrm_policy_free(struct xfrm_policy *xp)
  166. {
  167. struct xfrm_sec_ctx *ctx = xp->security;
  168. if (ctx)
  169. kfree(ctx);
  170. }
  171. /*
  172. * LSM hook implementation that allocs and transfers sec_ctx spec to
  173. * xfrm_state.
  174. */
  175. int selinux_xfrm_state_alloc(struct xfrm_state *x, struct xfrm_user_sec_ctx *uctx)
  176. {
  177. int err;
  178. BUG_ON(!x);
  179. err = selinux_xfrm_sec_ctx_alloc(&x->security, uctx);
  180. return err;
  181. }
  182. /*
  183. * LSM hook implementation that frees xfrm_state security information.
  184. */
  185. void selinux_xfrm_state_free(struct xfrm_state *x)
  186. {
  187. struct xfrm_sec_ctx *ctx = x->security;
  188. if (ctx)
  189. kfree(ctx);
  190. }
  191. /*
  192. * LSM hook that controls access to unlabelled packets. If
  193. * a xfrm_state is authorizable (defined by macro) then it was
  194. * already authorized by the IPSec process. If not, then
  195. * we need to check for unlabelled access since this may not have
  196. * gone thru the IPSec process.
  197. */
  198. int selinux_xfrm_sock_rcv_skb(u32 isec_sid, struct sk_buff *skb)
  199. {
  200. int i, rc = 0;
  201. struct sec_path *sp;
  202. sp = skb->sp;
  203. if (sp) {
  204. /*
  205. * __xfrm_policy_check does not approve unless xfrm_policy_ok
  206. * says that spi's match for policy and the socket.
  207. *
  208. * Only need to verify the existence of an authorizable sp.
  209. */
  210. for (i = 0; i < sp->len; i++) {
  211. struct xfrm_state *x = sp->x[i].xvec;
  212. if (x && selinux_authorizable_xfrm(x))
  213. goto accept;
  214. }
  215. }
  216. /* check SELinux sock for unlabelled access */
  217. rc = avc_has_perm(isec_sid, SECINITSID_UNLABELED, SECCLASS_ASSOCIATION,
  218. ASSOCIATION__RECVFROM, NULL);
  219. if (rc)
  220. goto drop;
  221. accept:
  222. return 0;
  223. drop:
  224. return rc;
  225. }
  226. /*
  227. * POSTROUTE_LAST hook's XFRM processing:
  228. * If we have no security association, then we need to determine
  229. * whether the socket is allowed to send to an unlabelled destination.
  230. * If we do have a authorizable security association, then it has already been
  231. * checked in xfrm_policy_lookup hook.
  232. */
  233. int selinux_xfrm_postroute_last(u32 isec_sid, struct sk_buff *skb)
  234. {
  235. struct dst_entry *dst;
  236. int rc = 0;
  237. dst = skb->dst;
  238. if (dst) {
  239. struct dst_entry *dst_test;
  240. for (dst_test = dst; dst_test != 0;
  241. dst_test = dst_test->child) {
  242. struct xfrm_state *x = dst_test->xfrm;
  243. if (x && selinux_authorizable_xfrm(x))
  244. goto accept;
  245. }
  246. }
  247. rc = avc_has_perm(isec_sid, SECINITSID_UNLABELED, SECCLASS_ASSOCIATION,
  248. ASSOCIATION__SENDTO, NULL);
  249. if (rc)
  250. goto drop;
  251. accept:
  252. return NF_ACCEPT;
  253. drop:
  254. return NF_DROP;
  255. }