xfrm.c 11 KB

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