xfrm.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  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. * Updated: Venkat Yekkirala <vyekkirala@TrustedCS.com>
  10. *
  11. * Granular IPSec Associations for use in MLS environments.
  12. *
  13. * Copyright (C) 2005 International Business Machines Corporation
  14. * Copyright (C) 2006 Trusted Computer Solutions, Inc.
  15. *
  16. * This program is free software; you can redistribute it and/or modify
  17. * it under the terms of the GNU General Public License version 2,
  18. * as published by the Free Software Foundation.
  19. */
  20. /*
  21. * USAGE:
  22. * NOTES:
  23. * 1. Make sure to enable the following options in your kernel config:
  24. * CONFIG_SECURITY=y
  25. * CONFIG_SECURITY_NETWORK=y
  26. * CONFIG_SECURITY_NETWORK_XFRM=y
  27. * CONFIG_SECURITY_SELINUX=m/y
  28. * ISSUES:
  29. * 1. Caching packets, so they are not dropped during negotiation
  30. * 2. Emulating a reasonable SO_PEERSEC across machines
  31. * 3. Testing addition of sk_policy's with security context via setsockopt
  32. */
  33. #include <linux/kernel.h>
  34. #include <linux/init.h>
  35. #include <linux/security.h>
  36. #include <linux/types.h>
  37. #include <linux/netfilter.h>
  38. #include <linux/netfilter_ipv4.h>
  39. #include <linux/netfilter_ipv6.h>
  40. #include <linux/slab.h>
  41. #include <linux/ip.h>
  42. #include <linux/tcp.h>
  43. #include <linux/skbuff.h>
  44. #include <linux/xfrm.h>
  45. #include <net/xfrm.h>
  46. #include <net/checksum.h>
  47. #include <net/udp.h>
  48. #include <linux/atomic.h>
  49. #include "avc.h"
  50. #include "objsec.h"
  51. #include "xfrm.h"
  52. /* Labeled XFRM instance counter */
  53. atomic_t selinux_xfrm_refcount = ATOMIC_INIT(0);
  54. /*
  55. * Returns true if an LSM/SELinux context
  56. */
  57. static inline int selinux_authorizable_ctx(struct xfrm_sec_ctx *ctx)
  58. {
  59. return (ctx &&
  60. (ctx->ctx_doi == XFRM_SC_DOI_LSM) &&
  61. (ctx->ctx_alg == XFRM_SC_ALG_SELINUX));
  62. }
  63. /*
  64. * Returns true if the xfrm contains a security blob for SELinux
  65. */
  66. static inline int selinux_authorizable_xfrm(struct xfrm_state *x)
  67. {
  68. return selinux_authorizable_ctx(x->security);
  69. }
  70. /*
  71. * Allocates a xfrm_sec_state and populates it using the supplied security
  72. * xfrm_user_sec_ctx context.
  73. */
  74. static int selinux_xfrm_alloc_user(struct xfrm_sec_ctx **ctxp,
  75. struct xfrm_user_sec_ctx *uctx)
  76. {
  77. int rc;
  78. const struct task_security_struct *tsec = current_security();
  79. struct xfrm_sec_ctx *ctx = NULL;
  80. u32 str_len;
  81. if (ctxp == NULL || uctx == NULL ||
  82. uctx->ctx_doi != XFRM_SC_DOI_LSM ||
  83. uctx->ctx_alg != XFRM_SC_ALG_SELINUX)
  84. return -EINVAL;
  85. str_len = uctx->ctx_len;
  86. if (str_len >= PAGE_SIZE)
  87. return -ENOMEM;
  88. ctx = kmalloc(sizeof(*ctx) + str_len + 1, GFP_KERNEL);
  89. if (!ctx)
  90. return -ENOMEM;
  91. ctx->ctx_doi = XFRM_SC_DOI_LSM;
  92. ctx->ctx_alg = XFRM_SC_ALG_SELINUX;
  93. ctx->ctx_len = str_len;
  94. memcpy(ctx->ctx_str, &uctx[1], str_len);
  95. ctx->ctx_str[str_len] = '\0';
  96. rc = security_context_to_sid(ctx->ctx_str, str_len, &ctx->ctx_sid);
  97. if (rc)
  98. goto err;
  99. rc = avc_has_perm(tsec->sid, ctx->ctx_sid,
  100. SECCLASS_ASSOCIATION, ASSOCIATION__SETCONTEXT, NULL);
  101. if (rc)
  102. goto err;
  103. *ctxp = ctx;
  104. atomic_inc(&selinux_xfrm_refcount);
  105. return 0;
  106. err:
  107. kfree(ctx);
  108. return rc;
  109. }
  110. /*
  111. * LSM hook implementation that authorizes that a flow can use
  112. * a xfrm policy rule.
  113. */
  114. int selinux_xfrm_policy_lookup(struct xfrm_sec_ctx *ctx, u32 fl_secid, u8 dir)
  115. {
  116. int rc;
  117. u32 sel_sid;
  118. /* Context sid is either set to label or ANY_ASSOC */
  119. if (ctx) {
  120. if (!selinux_authorizable_ctx(ctx))
  121. return -EINVAL;
  122. sel_sid = ctx->ctx_sid;
  123. } else
  124. /*
  125. * All flows should be treated as polmatch'ing an
  126. * otherwise applicable "non-labeled" policy. This
  127. * would prevent inadvertent "leaks".
  128. */
  129. return 0;
  130. rc = avc_has_perm(fl_secid, sel_sid, SECCLASS_ASSOCIATION,
  131. ASSOCIATION__POLMATCH,
  132. NULL);
  133. if (rc == -EACCES)
  134. return -ESRCH;
  135. return rc;
  136. }
  137. /*
  138. * LSM hook implementation that authorizes that a state matches
  139. * the given policy, flow combo.
  140. */
  141. int selinux_xfrm_state_pol_flow_match(struct xfrm_state *x, struct xfrm_policy *xp,
  142. const struct flowi *fl)
  143. {
  144. u32 state_sid;
  145. int rc;
  146. if (!xp->security)
  147. if (x->security)
  148. /* unlabeled policy and labeled SA can't match */
  149. return 0;
  150. else
  151. /* unlabeled policy and unlabeled SA match all flows */
  152. return 1;
  153. else
  154. if (!x->security)
  155. /* unlabeled SA and labeled policy can't match */
  156. return 0;
  157. else
  158. if (!selinux_authorizable_xfrm(x))
  159. /* Not a SELinux-labeled SA */
  160. return 0;
  161. state_sid = x->security->ctx_sid;
  162. if (fl->flowi_secid != state_sid)
  163. return 0;
  164. rc = avc_has_perm(fl->flowi_secid, state_sid, SECCLASS_ASSOCIATION,
  165. ASSOCIATION__SENDTO,
  166. NULL)? 0:1;
  167. /*
  168. * We don't need a separate SA Vs. policy polmatch check
  169. * since the SA is now of the same label as the flow and
  170. * a flow Vs. policy polmatch check had already happened
  171. * in selinux_xfrm_policy_lookup() above.
  172. */
  173. return rc;
  174. }
  175. /*
  176. * LSM hook implementation that checks and/or returns the xfrm sid for the
  177. * incoming packet.
  178. */
  179. int selinux_xfrm_decode_session(struct sk_buff *skb, u32 *sid, int ckall)
  180. {
  181. struct sec_path *sp;
  182. *sid = SECSID_NULL;
  183. if (skb == NULL)
  184. return 0;
  185. sp = skb->sp;
  186. if (sp) {
  187. int i, sid_set = 0;
  188. for (i = sp->len-1; i >= 0; i--) {
  189. struct xfrm_state *x = sp->xvec[i];
  190. if (selinux_authorizable_xfrm(x)) {
  191. struct xfrm_sec_ctx *ctx = x->security;
  192. if (!sid_set) {
  193. *sid = ctx->ctx_sid;
  194. sid_set = 1;
  195. if (!ckall)
  196. break;
  197. } else if (*sid != ctx->ctx_sid)
  198. return -EINVAL;
  199. }
  200. }
  201. }
  202. return 0;
  203. }
  204. /*
  205. * LSM hook implementation that allocs and transfers uctx spec to
  206. * xfrm_policy.
  207. */
  208. int selinux_xfrm_policy_alloc(struct xfrm_sec_ctx **ctxp,
  209. struct xfrm_user_sec_ctx *uctx)
  210. {
  211. return selinux_xfrm_alloc_user(ctxp, uctx);
  212. }
  213. /*
  214. * LSM hook implementation that copies security data structure from old to
  215. * new for policy cloning.
  216. */
  217. int selinux_xfrm_policy_clone(struct xfrm_sec_ctx *old_ctx,
  218. struct xfrm_sec_ctx **new_ctxp)
  219. {
  220. struct xfrm_sec_ctx *new_ctx;
  221. if (old_ctx) {
  222. new_ctx = kmalloc(sizeof(*old_ctx) + old_ctx->ctx_len,
  223. GFP_ATOMIC);
  224. if (!new_ctx)
  225. return -ENOMEM;
  226. memcpy(new_ctx, old_ctx, sizeof(*new_ctx));
  227. memcpy(new_ctx->ctx_str, old_ctx->ctx_str, new_ctx->ctx_len);
  228. atomic_inc(&selinux_xfrm_refcount);
  229. *new_ctxp = new_ctx;
  230. }
  231. return 0;
  232. }
  233. /*
  234. * LSM hook implementation that frees xfrm_sec_ctx security information.
  235. */
  236. void selinux_xfrm_policy_free(struct xfrm_sec_ctx *ctx)
  237. {
  238. atomic_dec(&selinux_xfrm_refcount);
  239. kfree(ctx);
  240. }
  241. /*
  242. * LSM hook implementation that authorizes deletion of labeled policies.
  243. */
  244. int selinux_xfrm_policy_delete(struct xfrm_sec_ctx *ctx)
  245. {
  246. const struct task_security_struct *tsec = current_security();
  247. if (!ctx)
  248. return 0;
  249. return avc_has_perm(tsec->sid, ctx->ctx_sid,
  250. SECCLASS_ASSOCIATION, ASSOCIATION__SETCONTEXT,
  251. NULL);
  252. }
  253. /*
  254. * LSM hook implementation that allocates a xfrm_sec_state, populates it using
  255. * the supplied security context, and assigns it to the xfrm_state.
  256. */
  257. int selinux_xfrm_state_alloc(struct xfrm_state *x,
  258. struct xfrm_user_sec_ctx *uctx)
  259. {
  260. return selinux_xfrm_alloc_user(&x->security, uctx);
  261. }
  262. /*
  263. * LSM hook implementation that allocates a xfrm_sec_state and populates based
  264. * on a secid.
  265. */
  266. int selinux_xfrm_state_alloc_acquire(struct xfrm_state *x,
  267. struct xfrm_sec_ctx *polsec, u32 secid)
  268. {
  269. int rc;
  270. struct xfrm_sec_ctx *ctx;
  271. char *ctx_str = NULL;
  272. int str_len;
  273. if (!polsec)
  274. return 0;
  275. if (secid == 0)
  276. return -EINVAL;
  277. rc = security_sid_to_context(secid, &ctx_str, &str_len);
  278. if (rc)
  279. return rc;
  280. ctx = kmalloc(sizeof(*ctx) + str_len, GFP_ATOMIC);
  281. if (!ctx)
  282. return -ENOMEM;
  283. ctx->ctx_doi = XFRM_SC_DOI_LSM;
  284. ctx->ctx_alg = XFRM_SC_ALG_SELINUX;
  285. ctx->ctx_sid = secid;
  286. ctx->ctx_len = str_len;
  287. memcpy(ctx->ctx_str, ctx_str, str_len);
  288. kfree(ctx_str);
  289. x->security = ctx;
  290. atomic_inc(&selinux_xfrm_refcount);
  291. return 0;
  292. }
  293. /*
  294. * LSM hook implementation that frees xfrm_state security information.
  295. */
  296. void selinux_xfrm_state_free(struct xfrm_state *x)
  297. {
  298. atomic_dec(&selinux_xfrm_refcount);
  299. kfree(x->security);
  300. }
  301. /*
  302. * LSM hook implementation that authorizes deletion of labeled SAs.
  303. */
  304. int selinux_xfrm_state_delete(struct xfrm_state *x)
  305. {
  306. const struct task_security_struct *tsec = current_security();
  307. struct xfrm_sec_ctx *ctx = x->security;
  308. if (!ctx)
  309. return 0;
  310. return avc_has_perm(tsec->sid, ctx->ctx_sid,
  311. SECCLASS_ASSOCIATION, ASSOCIATION__SETCONTEXT,
  312. NULL);
  313. }
  314. /*
  315. * LSM hook that controls access to unlabelled packets. If
  316. * a xfrm_state is authorizable (defined by macro) then it was
  317. * already authorized by the IPSec process. If not, then
  318. * we need to check for unlabelled access since this may not have
  319. * gone thru the IPSec process.
  320. */
  321. int selinux_xfrm_sock_rcv_skb(u32 isec_sid, struct sk_buff *skb,
  322. struct common_audit_data *ad)
  323. {
  324. int i, rc = 0;
  325. struct sec_path *sp;
  326. u32 sel_sid = SECINITSID_UNLABELED;
  327. sp = skb->sp;
  328. if (sp) {
  329. for (i = 0; i < sp->len; i++) {
  330. struct xfrm_state *x = sp->xvec[i];
  331. if (x && selinux_authorizable_xfrm(x)) {
  332. struct xfrm_sec_ctx *ctx = x->security;
  333. sel_sid = ctx->ctx_sid;
  334. break;
  335. }
  336. }
  337. }
  338. /*
  339. * This check even when there's no association involved is
  340. * intended, according to Trent Jaeger, to make sure a
  341. * process can't engage in non-ipsec communication unless
  342. * explicitly allowed by policy.
  343. */
  344. rc = avc_has_perm(isec_sid, sel_sid, SECCLASS_ASSOCIATION,
  345. ASSOCIATION__RECVFROM, ad);
  346. return rc;
  347. }
  348. /*
  349. * POSTROUTE_LAST hook's XFRM processing:
  350. * If we have no security association, then we need to determine
  351. * whether the socket is allowed to send to an unlabelled destination.
  352. * If we do have a authorizable security association, then it has already been
  353. * checked in the selinux_xfrm_state_pol_flow_match hook above.
  354. */
  355. int selinux_xfrm_postroute_last(u32 isec_sid, struct sk_buff *skb,
  356. struct common_audit_data *ad, u8 proto)
  357. {
  358. struct dst_entry *dst;
  359. int rc = 0;
  360. dst = skb_dst(skb);
  361. if (dst) {
  362. struct dst_entry *dst_test;
  363. for (dst_test = dst; dst_test != NULL;
  364. dst_test = dst_test->child) {
  365. struct xfrm_state *x = dst_test->xfrm;
  366. if (x && selinux_authorizable_xfrm(x))
  367. goto out;
  368. }
  369. }
  370. switch (proto) {
  371. case IPPROTO_AH:
  372. case IPPROTO_ESP:
  373. case IPPROTO_COMP:
  374. /*
  375. * We should have already seen this packet once before
  376. * it underwent xfrm(s). No need to subject it to the
  377. * unlabeled check.
  378. */
  379. goto out;
  380. default:
  381. break;
  382. }
  383. /*
  384. * This check even when there's no association involved is
  385. * intended, according to Trent Jaeger, to make sure a
  386. * process can't engage in non-ipsec communication unless
  387. * explicitly allowed by policy.
  388. */
  389. rc = avc_has_perm(isec_sid, SECINITSID_UNLABELED, SECCLASS_ASSOCIATION,
  390. ASSOCIATION__SENDTO, ad);
  391. out:
  392. return rc;
  393. }