xfrm.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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 context?
  117. */
  118. rc = avc_has_perm(tsec->sid, ctx->ctx_sid,
  119. SECCLASS_ASSOCIATION,
  120. ASSOCIATION__SETCONTEXT, NULL);
  121. if (rc)
  122. goto out;
  123. return rc;
  124. out:
  125. *ctxp = NULL;
  126. kfree(ctx);
  127. return rc;
  128. }
  129. /*
  130. * LSM hook implementation that allocs and transfers uctx spec to
  131. * xfrm_policy.
  132. */
  133. int selinux_xfrm_policy_alloc(struct xfrm_policy *xp, struct xfrm_user_sec_ctx *uctx)
  134. {
  135. int err;
  136. BUG_ON(!xp);
  137. err = selinux_xfrm_sec_ctx_alloc(&xp->security, uctx);
  138. return err;
  139. }
  140. /*
  141. * LSM hook implementation that copies security data structure from old to
  142. * new for policy cloning.
  143. */
  144. int selinux_xfrm_policy_clone(struct xfrm_policy *old, struct xfrm_policy *new)
  145. {
  146. struct xfrm_sec_ctx *old_ctx, *new_ctx;
  147. old_ctx = old->security;
  148. if (old_ctx) {
  149. new_ctx = new->security = kmalloc(sizeof(*new_ctx) +
  150. old_ctx->ctx_len,
  151. GFP_KERNEL);
  152. if (!new_ctx)
  153. return -ENOMEM;
  154. memcpy(new_ctx, old_ctx, sizeof(*new_ctx));
  155. memcpy(new_ctx->ctx_str, old_ctx->ctx_str, new_ctx->ctx_len);
  156. }
  157. return 0;
  158. }
  159. /*
  160. * LSM hook implementation that frees xfrm_policy security information.
  161. */
  162. void selinux_xfrm_policy_free(struct xfrm_policy *xp)
  163. {
  164. struct xfrm_sec_ctx *ctx = xp->security;
  165. if (ctx)
  166. kfree(ctx);
  167. }
  168. /*
  169. * LSM hook implementation that authorizes deletion of labeled policies.
  170. */
  171. int selinux_xfrm_policy_delete(struct xfrm_policy *xp)
  172. {
  173. struct task_security_struct *tsec = current->security;
  174. struct xfrm_sec_ctx *ctx = xp->security;
  175. int rc = 0;
  176. if (ctx)
  177. rc = avc_has_perm(tsec->sid, ctx->ctx_sid,
  178. SECCLASS_ASSOCIATION,
  179. ASSOCIATION__SETCONTEXT, NULL);
  180. return rc;
  181. }
  182. /*
  183. * LSM hook implementation that allocs and transfers sec_ctx spec to
  184. * xfrm_state.
  185. */
  186. int selinux_xfrm_state_alloc(struct xfrm_state *x, struct xfrm_user_sec_ctx *uctx)
  187. {
  188. int err;
  189. BUG_ON(!x);
  190. err = selinux_xfrm_sec_ctx_alloc(&x->security, uctx);
  191. return err;
  192. }
  193. /*
  194. * LSM hook implementation that frees xfrm_state security information.
  195. */
  196. void selinux_xfrm_state_free(struct xfrm_state *x)
  197. {
  198. struct xfrm_sec_ctx *ctx = x->security;
  199. if (ctx)
  200. kfree(ctx);
  201. }
  202. /*
  203. * SELinux internal function to retrieve the context of a connected
  204. * (sk->sk_state == TCP_ESTABLISHED) TCP socket based on its security
  205. * association used to connect to the remote socket.
  206. *
  207. * Retrieve via getsockopt SO_PEERSEC.
  208. */
  209. u32 selinux_socket_getpeer_stream(struct sock *sk)
  210. {
  211. struct dst_entry *dst, *dst_test;
  212. u32 peer_sid = SECSID_NULL;
  213. if (sk->sk_state != TCP_ESTABLISHED)
  214. goto out;
  215. dst = sk_dst_get(sk);
  216. if (!dst)
  217. goto out;
  218. for (dst_test = dst; dst_test != 0;
  219. dst_test = dst_test->child) {
  220. struct xfrm_state *x = dst_test->xfrm;
  221. if (x && selinux_authorizable_xfrm(x)) {
  222. struct xfrm_sec_ctx *ctx = x->security;
  223. peer_sid = ctx->ctx_sid;
  224. break;
  225. }
  226. }
  227. dst_release(dst);
  228. out:
  229. return peer_sid;
  230. }
  231. /*
  232. * SELinux internal function to retrieve the context of a UDP packet
  233. * based on its security association used to connect to the remote socket.
  234. *
  235. * Retrieve via setsockopt IP_PASSSEC and recvmsg with control message
  236. * type SCM_SECURITY.
  237. */
  238. u32 selinux_socket_getpeer_dgram(struct sk_buff *skb)
  239. {
  240. struct sec_path *sp;
  241. if (skb == NULL)
  242. return SECSID_NULL;
  243. if (skb->sk->sk_protocol != IPPROTO_UDP)
  244. return SECSID_NULL;
  245. sp = skb->sp;
  246. if (sp) {
  247. int i;
  248. for (i = sp->len-1; i >= 0; i--) {
  249. struct xfrm_state *x = sp->xvec[i];
  250. if (selinux_authorizable_xfrm(x)) {
  251. struct xfrm_sec_ctx *ctx = x->security;
  252. return ctx->ctx_sid;
  253. }
  254. }
  255. }
  256. return SECSID_NULL;
  257. }
  258. /*
  259. * LSM hook implementation that authorizes deletion of labeled SAs.
  260. */
  261. int selinux_xfrm_state_delete(struct xfrm_state *x)
  262. {
  263. struct task_security_struct *tsec = current->security;
  264. struct xfrm_sec_ctx *ctx = x->security;
  265. int rc = 0;
  266. if (ctx)
  267. rc = avc_has_perm(tsec->sid, ctx->ctx_sid,
  268. SECCLASS_ASSOCIATION,
  269. ASSOCIATION__SETCONTEXT, NULL);
  270. return rc;
  271. }
  272. /*
  273. * LSM hook that controls access to unlabelled packets. If
  274. * a xfrm_state is authorizable (defined by macro) then it was
  275. * already authorized by the IPSec process. If not, then
  276. * we need to check for unlabelled access since this may not have
  277. * gone thru the IPSec process.
  278. */
  279. int selinux_xfrm_sock_rcv_skb(u32 isec_sid, struct sk_buff *skb)
  280. {
  281. int i, rc = 0;
  282. struct sec_path *sp;
  283. sp = skb->sp;
  284. if (sp) {
  285. /*
  286. * __xfrm_policy_check does not approve unless xfrm_policy_ok
  287. * says that spi's match for policy and the socket.
  288. *
  289. * Only need to verify the existence of an authorizable sp.
  290. */
  291. for (i = 0; i < sp->len; i++) {
  292. struct xfrm_state *x = sp->xvec[i];
  293. if (x && selinux_authorizable_xfrm(x))
  294. goto accept;
  295. }
  296. }
  297. /* check SELinux sock for unlabelled access */
  298. rc = avc_has_perm(isec_sid, SECINITSID_UNLABELED, SECCLASS_ASSOCIATION,
  299. ASSOCIATION__RECVFROM, NULL);
  300. if (rc)
  301. goto drop;
  302. accept:
  303. return 0;
  304. drop:
  305. return rc;
  306. }
  307. /*
  308. * POSTROUTE_LAST hook's XFRM processing:
  309. * If we have no security association, then we need to determine
  310. * whether the socket is allowed to send to an unlabelled destination.
  311. * If we do have a authorizable security association, then it has already been
  312. * checked in xfrm_policy_lookup hook.
  313. */
  314. int selinux_xfrm_postroute_last(u32 isec_sid, struct sk_buff *skb)
  315. {
  316. struct dst_entry *dst;
  317. int rc = 0;
  318. dst = skb->dst;
  319. if (dst) {
  320. struct dst_entry *dst_test;
  321. for (dst_test = dst; dst_test != 0;
  322. dst_test = dst_test->child) {
  323. struct xfrm_state *x = dst_test->xfrm;
  324. if (x && selinux_authorizable_xfrm(x))
  325. goto out;
  326. }
  327. }
  328. rc = avc_has_perm(isec_sid, SECINITSID_UNLABELED, SECCLASS_ASSOCIATION,
  329. ASSOCIATION__SENDTO, NULL);
  330. out:
  331. return rc;
  332. }