xfrm.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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. * SELinux internal function to retrieve the context of a connected
  193. * (sk->sk_state == TCP_ESTABLISHED) TCP socket based on its security
  194. * association used to connect to the remote socket.
  195. *
  196. * Retrieve via getsockopt SO_PEERSEC.
  197. */
  198. u32 selinux_socket_getpeer_stream(struct sock *sk)
  199. {
  200. struct dst_entry *dst, *dst_test;
  201. u32 peer_sid = SECSID_NULL;
  202. if (sk->sk_state != TCP_ESTABLISHED)
  203. goto out;
  204. dst = sk_dst_get(sk);
  205. if (!dst)
  206. goto out;
  207. for (dst_test = dst; dst_test != 0;
  208. dst_test = dst_test->child) {
  209. struct xfrm_state *x = dst_test->xfrm;
  210. if (x && selinux_authorizable_xfrm(x)) {
  211. struct xfrm_sec_ctx *ctx = x->security;
  212. peer_sid = ctx->ctx_sid;
  213. break;
  214. }
  215. }
  216. dst_release(dst);
  217. out:
  218. return peer_sid;
  219. }
  220. /*
  221. * SELinux internal function to retrieve the context of a UDP packet
  222. * based on its security association used to connect to the remote socket.
  223. *
  224. * Retrieve via setsockopt IP_PASSSEC and recvmsg with control message
  225. * type SCM_SECURITY.
  226. */
  227. u32 selinux_socket_getpeer_dgram(struct sk_buff *skb)
  228. {
  229. struct sec_path *sp;
  230. if (skb == NULL)
  231. return SECSID_NULL;
  232. if (skb->sk->sk_protocol != IPPROTO_UDP)
  233. return SECSID_NULL;
  234. sp = skb->sp;
  235. if (sp) {
  236. int i;
  237. for (i = sp->len-1; i >= 0; i--) {
  238. struct xfrm_state *x = sp->xvec[i];
  239. if (selinux_authorizable_xfrm(x)) {
  240. struct xfrm_sec_ctx *ctx = x->security;
  241. return ctx->ctx_sid;
  242. }
  243. }
  244. }
  245. return SECSID_NULL;
  246. }
  247. /*
  248. * LSM hook that controls access to unlabelled packets. If
  249. * a xfrm_state is authorizable (defined by macro) then it was
  250. * already authorized by the IPSec process. If not, then
  251. * we need to check for unlabelled access since this may not have
  252. * gone thru the IPSec process.
  253. */
  254. int selinux_xfrm_sock_rcv_skb(u32 isec_sid, struct sk_buff *skb)
  255. {
  256. int i, rc = 0;
  257. struct sec_path *sp;
  258. sp = skb->sp;
  259. if (sp) {
  260. /*
  261. * __xfrm_policy_check does not approve unless xfrm_policy_ok
  262. * says that spi's match for policy and the socket.
  263. *
  264. * Only need to verify the existence of an authorizable sp.
  265. */
  266. for (i = 0; i < sp->len; i++) {
  267. struct xfrm_state *x = sp->xvec[i];
  268. if (x && selinux_authorizable_xfrm(x))
  269. goto accept;
  270. }
  271. }
  272. /* check SELinux sock for unlabelled access */
  273. rc = avc_has_perm(isec_sid, SECINITSID_UNLABELED, SECCLASS_ASSOCIATION,
  274. ASSOCIATION__RECVFROM, NULL);
  275. if (rc)
  276. goto drop;
  277. accept:
  278. return 0;
  279. drop:
  280. return rc;
  281. }
  282. /*
  283. * POSTROUTE_LAST hook's XFRM processing:
  284. * If we have no security association, then we need to determine
  285. * whether the socket is allowed to send to an unlabelled destination.
  286. * If we do have a authorizable security association, then it has already been
  287. * checked in xfrm_policy_lookup hook.
  288. */
  289. int selinux_xfrm_postroute_last(u32 isec_sid, struct sk_buff *skb)
  290. {
  291. struct dst_entry *dst;
  292. int rc = 0;
  293. dst = skb->dst;
  294. if (dst) {
  295. struct dst_entry *dst_test;
  296. for (dst_test = dst; dst_test != 0;
  297. dst_test = dst_test->child) {
  298. struct xfrm_state *x = dst_test->xfrm;
  299. if (x && selinux_authorizable_xfrm(x))
  300. goto accept;
  301. }
  302. }
  303. rc = avc_has_perm(isec_sid, SECINITSID_UNLABELED, SECCLASS_ASSOCIATION,
  304. ASSOCIATION__SENDTO, NULL);
  305. if (rc)
  306. goto drop;
  307. accept:
  308. return NF_ACCEPT;
  309. drop:
  310. return NF_DROP;
  311. }