xfrm.c 11 KB

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