xfrm.c 11 KB

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