xfrm.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  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. static int selinux_xfrm_skb_sid_ingress(struct sk_buff *skb,
  176. u32 *sid, int ckall)
  177. {
  178. struct sec_path *sp = skb->sp;
  179. *sid = SECSID_NULL;
  180. if (sp) {
  181. int i, sid_set = 0;
  182. for (i = sp->len-1; i >= 0; i--) {
  183. struct xfrm_state *x = sp->xvec[i];
  184. if (selinux_authorizable_xfrm(x)) {
  185. struct xfrm_sec_ctx *ctx = x->security;
  186. if (!sid_set) {
  187. *sid = ctx->ctx_sid;
  188. sid_set = 1;
  189. if (!ckall)
  190. break;
  191. } else if (*sid != ctx->ctx_sid)
  192. return -EINVAL;
  193. }
  194. }
  195. }
  196. return 0;
  197. }
  198. static u32 selinux_xfrm_skb_sid_egress(struct sk_buff *skb)
  199. {
  200. struct dst_entry *dst = skb_dst(skb);
  201. struct xfrm_state *x;
  202. if (dst == NULL)
  203. return SECSID_NULL;
  204. x = dst->xfrm;
  205. if (x == NULL || !selinux_authorizable_xfrm(x))
  206. return SECSID_NULL;
  207. return x->security->ctx_sid;
  208. }
  209. /*
  210. * LSM hook implementation that checks and/or returns the xfrm sid for the
  211. * incoming packet.
  212. */
  213. int selinux_xfrm_decode_session(struct sk_buff *skb, u32 *sid, int ckall)
  214. {
  215. if (skb == NULL) {
  216. *sid = SECSID_NULL;
  217. return 0;
  218. }
  219. return selinux_xfrm_skb_sid_ingress(skb, sid, ckall);
  220. }
  221. int selinux_xfrm_skb_sid(struct sk_buff *skb, u32 *sid)
  222. {
  223. int rc;
  224. rc = selinux_xfrm_skb_sid_ingress(skb, sid, 0);
  225. if (rc == 0 && *sid == SECSID_NULL)
  226. *sid = selinux_xfrm_skb_sid_egress(skb);
  227. return rc;
  228. }
  229. /*
  230. * LSM hook implementation that allocs and transfers uctx spec to
  231. * xfrm_policy.
  232. */
  233. int selinux_xfrm_policy_alloc(struct xfrm_sec_ctx **ctxp,
  234. struct xfrm_user_sec_ctx *uctx)
  235. {
  236. return selinux_xfrm_alloc_user(ctxp, uctx);
  237. }
  238. /*
  239. * LSM hook implementation that copies security data structure from old to
  240. * new for policy cloning.
  241. */
  242. int selinux_xfrm_policy_clone(struct xfrm_sec_ctx *old_ctx,
  243. struct xfrm_sec_ctx **new_ctxp)
  244. {
  245. struct xfrm_sec_ctx *new_ctx;
  246. if (old_ctx) {
  247. new_ctx = kmalloc(sizeof(*old_ctx) + old_ctx->ctx_len,
  248. GFP_ATOMIC);
  249. if (!new_ctx)
  250. return -ENOMEM;
  251. memcpy(new_ctx, old_ctx, sizeof(*new_ctx));
  252. memcpy(new_ctx->ctx_str, old_ctx->ctx_str, new_ctx->ctx_len);
  253. *new_ctxp = new_ctx;
  254. }
  255. return 0;
  256. }
  257. /*
  258. * LSM hook implementation that frees xfrm_sec_ctx security information.
  259. */
  260. void selinux_xfrm_policy_free(struct xfrm_sec_ctx *ctx)
  261. {
  262. kfree(ctx);
  263. }
  264. /*
  265. * LSM hook implementation that authorizes deletion of labeled policies.
  266. */
  267. int selinux_xfrm_policy_delete(struct xfrm_sec_ctx *ctx)
  268. {
  269. const struct task_security_struct *tsec = current_security();
  270. int rc = 0;
  271. if (ctx) {
  272. rc = avc_has_perm(tsec->sid, ctx->ctx_sid,
  273. SECCLASS_ASSOCIATION,
  274. ASSOCIATION__SETCONTEXT, NULL);
  275. if (rc == 0)
  276. atomic_dec(&selinux_xfrm_refcount);
  277. }
  278. return rc;
  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);
  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. return -ENOMEM;
  310. ctx->ctx_doi = XFRM_SC_DOI_LSM;
  311. ctx->ctx_alg = XFRM_SC_ALG_SELINUX;
  312. ctx->ctx_sid = secid;
  313. ctx->ctx_len = str_len;
  314. memcpy(ctx->ctx_str, ctx_str, str_len);
  315. kfree(ctx_str);
  316. x->security = ctx;
  317. atomic_inc(&selinux_xfrm_refcount);
  318. return 0;
  319. }
  320. /*
  321. * LSM hook implementation that frees xfrm_state security information.
  322. */
  323. void selinux_xfrm_state_free(struct xfrm_state *x)
  324. {
  325. struct xfrm_sec_ctx *ctx = x->security;
  326. kfree(ctx);
  327. }
  328. /*
  329. * LSM hook implementation that authorizes deletion of labeled SAs.
  330. */
  331. int selinux_xfrm_state_delete(struct xfrm_state *x)
  332. {
  333. const struct task_security_struct *tsec = current_security();
  334. struct xfrm_sec_ctx *ctx = x->security;
  335. int rc = 0;
  336. if (ctx) {
  337. rc = avc_has_perm(tsec->sid, ctx->ctx_sid,
  338. SECCLASS_ASSOCIATION,
  339. ASSOCIATION__SETCONTEXT, NULL);
  340. if (rc == 0)
  341. atomic_dec(&selinux_xfrm_refcount);
  342. }
  343. return rc;
  344. }
  345. /*
  346. * LSM hook that controls access to unlabelled packets. If
  347. * a xfrm_state is authorizable (defined by macro) then it was
  348. * already authorized by the IPSec process. If not, then
  349. * we need to check for unlabelled access since this may not have
  350. * gone thru the IPSec process.
  351. */
  352. int selinux_xfrm_sock_rcv_skb(u32 isec_sid, struct sk_buff *skb,
  353. struct common_audit_data *ad)
  354. {
  355. int i, rc = 0;
  356. struct sec_path *sp;
  357. u32 sel_sid = SECINITSID_UNLABELED;
  358. sp = skb->sp;
  359. if (sp) {
  360. for (i = 0; i < sp->len; i++) {
  361. struct xfrm_state *x = sp->xvec[i];
  362. if (x && selinux_authorizable_xfrm(x)) {
  363. struct xfrm_sec_ctx *ctx = x->security;
  364. sel_sid = ctx->ctx_sid;
  365. break;
  366. }
  367. }
  368. }
  369. /*
  370. * This check even when there's no association involved is
  371. * intended, according to Trent Jaeger, to make sure a
  372. * process can't engage in non-ipsec communication unless
  373. * explicitly allowed by policy.
  374. */
  375. rc = avc_has_perm(isec_sid, sel_sid, SECCLASS_ASSOCIATION,
  376. ASSOCIATION__RECVFROM, ad);
  377. return rc;
  378. }
  379. /*
  380. * POSTROUTE_LAST hook's XFRM processing:
  381. * If we have no security association, then we need to determine
  382. * whether the socket is allowed to send to an unlabelled destination.
  383. * If we do have a authorizable security association, then it has already been
  384. * checked in the selinux_xfrm_state_pol_flow_match hook above.
  385. */
  386. int selinux_xfrm_postroute_last(u32 isec_sid, struct sk_buff *skb,
  387. struct common_audit_data *ad, u8 proto)
  388. {
  389. struct dst_entry *dst;
  390. int rc = 0;
  391. dst = skb_dst(skb);
  392. if (dst) {
  393. struct dst_entry *dst_test;
  394. for (dst_test = dst; dst_test != NULL;
  395. dst_test = dst_test->child) {
  396. struct xfrm_state *x = dst_test->xfrm;
  397. if (x && selinux_authorizable_xfrm(x))
  398. goto out;
  399. }
  400. }
  401. switch (proto) {
  402. case IPPROTO_AH:
  403. case IPPROTO_ESP:
  404. case IPPROTO_COMP:
  405. /*
  406. * We should have already seen this packet once before
  407. * it underwent xfrm(s). No need to subject it to the
  408. * unlabeled check.
  409. */
  410. goto out;
  411. default:
  412. break;
  413. }
  414. /*
  415. * This check even when there's no association involved is
  416. * intended, according to Trent Jaeger, to make sure a
  417. * process can't engage in non-ipsec communication unless
  418. * explicitly allowed by policy.
  419. */
  420. rc = avc_has_perm(isec_sid, SECINITSID_UNLABELED, SECCLASS_ASSOCIATION,
  421. ASSOCIATION__SENDTO, ad);
  422. out:
  423. return rc;
  424. }