xfrm.c 10 KB

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