xfrm.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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/module.h>
  29. #include <linux/kernel.h>
  30. #include <linux/init.h>
  31. #include <linux/security.h>
  32. #include <linux/types.h>
  33. #include <linux/netfilter.h>
  34. #include <linux/netfilter_ipv4.h>
  35. #include <linux/netfilter_ipv6.h>
  36. #include <linux/ip.h>
  37. #include <linux/tcp.h>
  38. #include <linux/skbuff.h>
  39. #include <linux/xfrm.h>
  40. #include <net/xfrm.h>
  41. #include <net/checksum.h>
  42. #include <net/udp.h>
  43. #include <asm/semaphore.h>
  44. #include "avc.h"
  45. #include "objsec.h"
  46. #include "xfrm.h"
  47. /*
  48. * Returns true if an LSM/SELinux context
  49. */
  50. static inline int selinux_authorizable_ctx(struct xfrm_sec_ctx *ctx)
  51. {
  52. return (ctx &&
  53. (ctx->ctx_doi == XFRM_SC_DOI_LSM) &&
  54. (ctx->ctx_alg == XFRM_SC_ALG_SELINUX));
  55. }
  56. /*
  57. * Returns true if the xfrm contains a security blob for SELinux
  58. */
  59. static inline int selinux_authorizable_xfrm(struct xfrm_state *x)
  60. {
  61. return selinux_authorizable_ctx(x->security);
  62. }
  63. /*
  64. * LSM hook implementation that authorizes that a socket can be used
  65. * with the corresponding xfrm_sec_ctx and direction.
  66. */
  67. int selinux_xfrm_policy_lookup(struct xfrm_policy *xp, u32 sk_sid, u8 dir)
  68. {
  69. int rc = 0;
  70. u32 sel_sid = SECINITSID_UNLABELED;
  71. struct xfrm_sec_ctx *ctx;
  72. /* Context sid is either set to label or ANY_ASSOC */
  73. if ((ctx = xp->security)) {
  74. if (!selinux_authorizable_ctx(ctx))
  75. return -EINVAL;
  76. sel_sid = ctx->ctx_sid;
  77. }
  78. rc = avc_has_perm(sk_sid, sel_sid, SECCLASS_ASSOCIATION,
  79. ((dir == FLOW_DIR_IN) ? ASSOCIATION__RECVFROM :
  80. ((dir == FLOW_DIR_OUT) ? ASSOCIATION__SENDTO :
  81. (ASSOCIATION__SENDTO | ASSOCIATION__RECVFROM))),
  82. NULL);
  83. return rc;
  84. }
  85. /*
  86. * Security blob allocation for xfrm_policy and xfrm_state
  87. * CTX does not have a meaningful value on input
  88. */
  89. static int selinux_xfrm_sec_ctx_alloc(struct xfrm_sec_ctx **ctxp, struct xfrm_user_sec_ctx *uctx)
  90. {
  91. int rc = 0;
  92. struct task_security_struct *tsec = current->security;
  93. struct xfrm_sec_ctx *ctx;
  94. BUG_ON(!uctx);
  95. BUG_ON(uctx->ctx_doi != XFRM_SC_ALG_SELINUX);
  96. if (uctx->ctx_len >= PAGE_SIZE)
  97. return -ENOMEM;
  98. *ctxp = ctx = kmalloc(sizeof(*ctx) +
  99. uctx->ctx_len,
  100. GFP_KERNEL);
  101. if (!ctx)
  102. return -ENOMEM;
  103. ctx->ctx_doi = uctx->ctx_doi;
  104. ctx->ctx_len = uctx->ctx_len;
  105. ctx->ctx_alg = uctx->ctx_alg;
  106. memcpy(ctx->ctx_str,
  107. uctx+1,
  108. ctx->ctx_len);
  109. rc = security_context_to_sid(ctx->ctx_str,
  110. ctx->ctx_len,
  111. &ctx->ctx_sid);
  112. if (rc)
  113. goto out;
  114. /*
  115. * Does the subject have permission to set security context?
  116. */
  117. rc = avc_has_perm(tsec->sid, ctx->ctx_sid,
  118. SECCLASS_ASSOCIATION,
  119. ASSOCIATION__SETCONTEXT, NULL);
  120. if (rc)
  121. goto out;
  122. return rc;
  123. out:
  124. *ctxp = NULL;
  125. kfree(ctx);
  126. return rc;
  127. }
  128. /*
  129. * LSM hook implementation that allocs and transfers uctx spec to
  130. * xfrm_policy.
  131. */
  132. int selinux_xfrm_policy_alloc(struct xfrm_policy *xp, struct xfrm_user_sec_ctx *uctx)
  133. {
  134. int err;
  135. BUG_ON(!xp);
  136. err = selinux_xfrm_sec_ctx_alloc(&xp->security, uctx);
  137. return err;
  138. }
  139. /*
  140. * LSM hook implementation that copies security data structure from old to
  141. * new for policy cloning.
  142. */
  143. int selinux_xfrm_policy_clone(struct xfrm_policy *old, struct xfrm_policy *new)
  144. {
  145. struct xfrm_sec_ctx *old_ctx, *new_ctx;
  146. old_ctx = old->security;
  147. if (old_ctx) {
  148. new_ctx = new->security = kmalloc(sizeof(*new_ctx) +
  149. old_ctx->ctx_len,
  150. GFP_KERNEL);
  151. if (!new_ctx)
  152. return -ENOMEM;
  153. memcpy(new_ctx, old_ctx, sizeof(*new_ctx));
  154. memcpy(new_ctx->ctx_str, old_ctx->ctx_str, new_ctx->ctx_len);
  155. }
  156. return 0;
  157. }
  158. /*
  159. * LSM hook implementation that frees xfrm_policy security information.
  160. */
  161. void selinux_xfrm_policy_free(struct xfrm_policy *xp)
  162. {
  163. struct xfrm_sec_ctx *ctx = xp->security;
  164. if (ctx)
  165. kfree(ctx);
  166. }
  167. /*
  168. * LSM hook implementation that authorizes deletion of labeled policies.
  169. */
  170. int selinux_xfrm_policy_delete(struct xfrm_policy *xp)
  171. {
  172. struct task_security_struct *tsec = current->security;
  173. struct xfrm_sec_ctx *ctx = xp->security;
  174. int rc = 0;
  175. if (ctx)
  176. rc = avc_has_perm(tsec->sid, ctx->ctx_sid,
  177. SECCLASS_ASSOCIATION,
  178. ASSOCIATION__SETCONTEXT, NULL);
  179. return rc;
  180. }
  181. /*
  182. * LSM hook implementation that allocs and transfers sec_ctx spec to
  183. * xfrm_state.
  184. */
  185. int selinux_xfrm_state_alloc(struct xfrm_state *x, struct xfrm_user_sec_ctx *uctx)
  186. {
  187. int err;
  188. BUG_ON(!x);
  189. err = selinux_xfrm_sec_ctx_alloc(&x->security, uctx);
  190. return err;
  191. }
  192. /*
  193. * LSM hook implementation that frees xfrm_state security information.
  194. */
  195. void selinux_xfrm_state_free(struct xfrm_state *x)
  196. {
  197. struct xfrm_sec_ctx *ctx = x->security;
  198. if (ctx)
  199. kfree(ctx);
  200. }
  201. /*
  202. * SELinux internal function to retrieve the context of a connected
  203. * (sk->sk_state == TCP_ESTABLISHED) TCP socket based on its security
  204. * association used to connect to the remote socket.
  205. *
  206. * Retrieve via getsockopt SO_PEERSEC.
  207. */
  208. u32 selinux_socket_getpeer_stream(struct sock *sk)
  209. {
  210. struct dst_entry *dst, *dst_test;
  211. u32 peer_sid = SECSID_NULL;
  212. if (sk->sk_state != TCP_ESTABLISHED)
  213. goto out;
  214. dst = sk_dst_get(sk);
  215. if (!dst)
  216. goto out;
  217. for (dst_test = dst; dst_test != 0;
  218. dst_test = dst_test->child) {
  219. struct xfrm_state *x = dst_test->xfrm;
  220. if (x && selinux_authorizable_xfrm(x)) {
  221. struct xfrm_sec_ctx *ctx = x->security;
  222. peer_sid = ctx->ctx_sid;
  223. break;
  224. }
  225. }
  226. dst_release(dst);
  227. out:
  228. return peer_sid;
  229. }
  230. /*
  231. * SELinux internal function to retrieve the context of a UDP packet
  232. * based on its security association used to connect to the remote socket.
  233. *
  234. * Retrieve via setsockopt IP_PASSSEC and recvmsg with control message
  235. * type SCM_SECURITY.
  236. */
  237. u32 selinux_socket_getpeer_dgram(struct sk_buff *skb)
  238. {
  239. struct sec_path *sp;
  240. if (skb == NULL)
  241. return SECSID_NULL;
  242. if (skb->sk->sk_protocol != IPPROTO_UDP)
  243. return SECSID_NULL;
  244. sp = skb->sp;
  245. if (sp) {
  246. int i;
  247. for (i = sp->len-1; i >= 0; i--) {
  248. struct xfrm_state *x = sp->xvec[i];
  249. if (selinux_authorizable_xfrm(x)) {
  250. struct xfrm_sec_ctx *ctx = x->security;
  251. return ctx->ctx_sid;
  252. }
  253. }
  254. }
  255. return SECSID_NULL;
  256. }
  257. /*
  258. * LSM hook implementation that authorizes deletion of labeled SAs.
  259. */
  260. int selinux_xfrm_state_delete(struct xfrm_state *x)
  261. {
  262. struct task_security_struct *tsec = current->security;
  263. struct xfrm_sec_ctx *ctx = x->security;
  264. int rc = 0;
  265. if (ctx)
  266. rc = avc_has_perm(tsec->sid, ctx->ctx_sid,
  267. SECCLASS_ASSOCIATION,
  268. ASSOCIATION__SETCONTEXT, NULL);
  269. return rc;
  270. }
  271. /*
  272. * LSM hook that controls access to unlabelled packets. If
  273. * a xfrm_state is authorizable (defined by macro) then it was
  274. * already authorized by the IPSec process. If not, then
  275. * we need to check for unlabelled access since this may not have
  276. * gone thru the IPSec process.
  277. */
  278. int selinux_xfrm_sock_rcv_skb(u32 isec_sid, struct sk_buff *skb)
  279. {
  280. int i, rc = 0;
  281. struct sec_path *sp;
  282. sp = skb->sp;
  283. if (sp) {
  284. /*
  285. * __xfrm_policy_check does not approve unless xfrm_policy_ok
  286. * says that spi's match for policy and the socket.
  287. *
  288. * Only need to verify the existence of an authorizable sp.
  289. */
  290. for (i = 0; i < sp->len; i++) {
  291. struct xfrm_state *x = sp->xvec[i];
  292. if (x && selinux_authorizable_xfrm(x))
  293. goto accept;
  294. }
  295. }
  296. /* check SELinux sock for unlabelled access */
  297. rc = avc_has_perm(isec_sid, SECINITSID_UNLABELED, SECCLASS_ASSOCIATION,
  298. ASSOCIATION__RECVFROM, NULL);
  299. if (rc)
  300. goto drop;
  301. accept:
  302. return 0;
  303. drop:
  304. return rc;
  305. }
  306. /*
  307. * POSTROUTE_LAST hook's XFRM processing:
  308. * If we have no security association, then we need to determine
  309. * whether the socket is allowed to send to an unlabelled destination.
  310. * If we do have a authorizable security association, then it has already been
  311. * checked in xfrm_policy_lookup hook.
  312. */
  313. int selinux_xfrm_postroute_last(u32 isec_sid, struct sk_buff *skb)
  314. {
  315. struct dst_entry *dst;
  316. int rc = 0;
  317. dst = skb->dst;
  318. if (dst) {
  319. struct dst_entry *dst_test;
  320. for (dst_test = dst; dst_test != 0;
  321. dst_test = dst_test->child) {
  322. struct xfrm_state *x = dst_test->xfrm;
  323. if (x && selinux_authorizable_xfrm(x))
  324. goto out;
  325. }
  326. }
  327. rc = avc_has_perm(isec_sid, SECINITSID_UNLABELED, SECCLASS_ASSOCIATION,
  328. ASSOCIATION__SENDTO, NULL);
  329. out:
  330. return rc;
  331. }