xfrm.c 11 KB

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