xfrm.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  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/atomic.h>
  48. #include "avc.h"
  49. #include "objsec.h"
  50. #include "xfrm.h"
  51. /* Labeled XFRM instance counter */
  52. atomic_t selinux_xfrm_refcount = ATOMIC_INIT(0);
  53. /*
  54. * Returns true if an LSM/SELinux context
  55. */
  56. static inline int selinux_authorizable_ctx(struct xfrm_sec_ctx *ctx)
  57. {
  58. return (ctx &&
  59. (ctx->ctx_doi == XFRM_SC_DOI_LSM) &&
  60. (ctx->ctx_alg == XFRM_SC_ALG_SELINUX));
  61. }
  62. /*
  63. * Returns true if the xfrm contains a security blob for SELinux
  64. */
  65. static inline int selinux_authorizable_xfrm(struct xfrm_state *x)
  66. {
  67. return selinux_authorizable_ctx(x->security);
  68. }
  69. /*
  70. * LSM hook implementation that authorizes that a flow can use
  71. * a xfrm policy rule.
  72. */
  73. int selinux_xfrm_policy_lookup(struct xfrm_sec_ctx *ctx, u32 fl_secid, u8 dir)
  74. {
  75. int rc;
  76. u32 sel_sid;
  77. /* Context sid is either set to label or ANY_ASSOC */
  78. if (ctx) {
  79. if (!selinux_authorizable_ctx(ctx))
  80. return -EINVAL;
  81. sel_sid = ctx->ctx_sid;
  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. return -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. } else if (*sid != ctx->ctx_sid)
  157. return -EINVAL;
  158. }
  159. }
  160. }
  161. return 0;
  162. }
  163. /*
  164. * Security blob allocation for xfrm_policy and xfrm_state
  165. * CTX does not have a meaningful value on input
  166. */
  167. static int selinux_xfrm_sec_ctx_alloc(struct xfrm_sec_ctx **ctxp,
  168. struct xfrm_user_sec_ctx *uctx, u32 sid)
  169. {
  170. int rc = 0;
  171. struct task_security_struct *tsec = current->cred->security;
  172. struct xfrm_sec_ctx *ctx = NULL;
  173. char *ctx_str = NULL;
  174. u32 str_len;
  175. BUG_ON(uctx && sid);
  176. if (!uctx)
  177. goto not_from_user;
  178. if (uctx->ctx_doi != XFRM_SC_ALG_SELINUX)
  179. return -EINVAL;
  180. str_len = uctx->ctx_len;
  181. if (str_len >= PAGE_SIZE)
  182. return -ENOMEM;
  183. *ctxp = ctx = kmalloc(sizeof(*ctx) +
  184. str_len + 1,
  185. GFP_KERNEL);
  186. if (!ctx)
  187. return -ENOMEM;
  188. ctx->ctx_doi = uctx->ctx_doi;
  189. ctx->ctx_len = str_len;
  190. ctx->ctx_alg = uctx->ctx_alg;
  191. memcpy(ctx->ctx_str,
  192. uctx+1,
  193. str_len);
  194. ctx->ctx_str[str_len] = 0;
  195. rc = security_context_to_sid(ctx->ctx_str,
  196. str_len,
  197. &ctx->ctx_sid);
  198. if (rc)
  199. goto out;
  200. /*
  201. * Does the subject have permission to set security context?
  202. */
  203. rc = avc_has_perm(tsec->sid, ctx->ctx_sid,
  204. SECCLASS_ASSOCIATION,
  205. ASSOCIATION__SETCONTEXT, NULL);
  206. if (rc)
  207. goto out;
  208. return rc;
  209. not_from_user:
  210. rc = security_sid_to_context(sid, &ctx_str, &str_len);
  211. if (rc)
  212. goto out;
  213. *ctxp = ctx = kmalloc(sizeof(*ctx) +
  214. str_len,
  215. GFP_ATOMIC);
  216. if (!ctx) {
  217. rc = -ENOMEM;
  218. goto out;
  219. }
  220. ctx->ctx_doi = XFRM_SC_DOI_LSM;
  221. ctx->ctx_alg = XFRM_SC_ALG_SELINUX;
  222. ctx->ctx_sid = sid;
  223. ctx->ctx_len = str_len;
  224. memcpy(ctx->ctx_str,
  225. ctx_str,
  226. str_len);
  227. goto out2;
  228. out:
  229. *ctxp = NULL;
  230. kfree(ctx);
  231. out2:
  232. kfree(ctx_str);
  233. return rc;
  234. }
  235. /*
  236. * LSM hook implementation that allocs and transfers uctx spec to
  237. * xfrm_policy.
  238. */
  239. int selinux_xfrm_policy_alloc(struct xfrm_sec_ctx **ctxp,
  240. struct xfrm_user_sec_ctx *uctx)
  241. {
  242. int err;
  243. BUG_ON(!uctx);
  244. err = selinux_xfrm_sec_ctx_alloc(ctxp, uctx, 0);
  245. if (err == 0)
  246. atomic_inc(&selinux_xfrm_refcount);
  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_sec_ctx *old_ctx,
  254. struct xfrm_sec_ctx **new_ctxp)
  255. {
  256. struct xfrm_sec_ctx *new_ctx;
  257. if (old_ctx) {
  258. new_ctx = kmalloc(sizeof(*old_ctx) + old_ctx->ctx_len,
  259. GFP_KERNEL);
  260. if (!new_ctx)
  261. return -ENOMEM;
  262. memcpy(new_ctx, old_ctx, sizeof(*new_ctx));
  263. memcpy(new_ctx->ctx_str, old_ctx->ctx_str, new_ctx->ctx_len);
  264. *new_ctxp = new_ctx;
  265. }
  266. return 0;
  267. }
  268. /*
  269. * LSM hook implementation that frees xfrm_sec_ctx security information.
  270. */
  271. void selinux_xfrm_policy_free(struct xfrm_sec_ctx *ctx)
  272. {
  273. kfree(ctx);
  274. }
  275. /*
  276. * LSM hook implementation that authorizes deletion of labeled policies.
  277. */
  278. int selinux_xfrm_policy_delete(struct xfrm_sec_ctx *ctx)
  279. {
  280. struct task_security_struct *tsec = current->cred->security;
  281. int rc = 0;
  282. if (ctx) {
  283. rc = avc_has_perm(tsec->sid, ctx->ctx_sid,
  284. SECCLASS_ASSOCIATION,
  285. ASSOCIATION__SETCONTEXT, NULL);
  286. if (rc == 0)
  287. atomic_dec(&selinux_xfrm_refcount);
  288. }
  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. if (err == 0)
  302. atomic_inc(&selinux_xfrm_refcount);
  303. return err;
  304. }
  305. /*
  306. * LSM hook implementation that frees xfrm_state security information.
  307. */
  308. void selinux_xfrm_state_free(struct xfrm_state *x)
  309. {
  310. struct xfrm_sec_ctx *ctx = x->security;
  311. kfree(ctx);
  312. }
  313. /*
  314. * LSM hook implementation that authorizes deletion of labeled SAs.
  315. */
  316. int selinux_xfrm_state_delete(struct xfrm_state *x)
  317. {
  318. struct task_security_struct *tsec = current->cred->security;
  319. struct xfrm_sec_ctx *ctx = x->security;
  320. int rc = 0;
  321. if (ctx) {
  322. rc = avc_has_perm(tsec->sid, ctx->ctx_sid,
  323. SECCLASS_ASSOCIATION,
  324. ASSOCIATION__SETCONTEXT, NULL);
  325. if (rc == 0)
  326. atomic_dec(&selinux_xfrm_refcount);
  327. }
  328. return rc;
  329. }
  330. /*
  331. * LSM hook that controls access to unlabelled packets. If
  332. * a xfrm_state is authorizable (defined by macro) then it was
  333. * already authorized by the IPSec process. If not, then
  334. * we need to check for unlabelled access since this may not have
  335. * gone thru the IPSec process.
  336. */
  337. int selinux_xfrm_sock_rcv_skb(u32 isec_sid, struct sk_buff *skb,
  338. struct avc_audit_data *ad)
  339. {
  340. int i, rc = 0;
  341. struct sec_path *sp;
  342. u32 sel_sid = SECINITSID_UNLABELED;
  343. sp = skb->sp;
  344. if (sp) {
  345. for (i = 0; i < sp->len; i++) {
  346. struct xfrm_state *x = sp->xvec[i];
  347. if (x && selinux_authorizable_xfrm(x)) {
  348. struct xfrm_sec_ctx *ctx = x->security;
  349. sel_sid = ctx->ctx_sid;
  350. break;
  351. }
  352. }
  353. }
  354. /*
  355. * This check even when there's no association involved is
  356. * intended, according to Trent Jaeger, to make sure a
  357. * process can't engage in non-ipsec communication unless
  358. * explicitly allowed by policy.
  359. */
  360. rc = avc_has_perm(isec_sid, sel_sid, SECCLASS_ASSOCIATION,
  361. ASSOCIATION__RECVFROM, ad);
  362. return rc;
  363. }
  364. /*
  365. * POSTROUTE_LAST hook's XFRM processing:
  366. * If we have no security association, then we need to determine
  367. * whether the socket is allowed to send to an unlabelled destination.
  368. * If we do have a authorizable security association, then it has already been
  369. * checked in the selinux_xfrm_state_pol_flow_match hook above.
  370. */
  371. int selinux_xfrm_postroute_last(u32 isec_sid, struct sk_buff *skb,
  372. struct avc_audit_data *ad, u8 proto)
  373. {
  374. struct dst_entry *dst;
  375. int rc = 0;
  376. dst = skb->dst;
  377. if (dst) {
  378. struct dst_entry *dst_test;
  379. for (dst_test = dst; dst_test != NULL;
  380. dst_test = dst_test->child) {
  381. struct xfrm_state *x = dst_test->xfrm;
  382. if (x && selinux_authorizable_xfrm(x))
  383. goto out;
  384. }
  385. }
  386. switch (proto) {
  387. case IPPROTO_AH:
  388. case IPPROTO_ESP:
  389. case IPPROTO_COMP:
  390. /*
  391. * We should have already seen this packet once before
  392. * it underwent xfrm(s). No need to subject it to the
  393. * unlabeled check.
  394. */
  395. goto out;
  396. default:
  397. break;
  398. }
  399. /*
  400. * This check even when there's no association involved is
  401. * intended, according to Trent Jaeger, to make sure a
  402. * process can't engage in non-ipsec communication unless
  403. * explicitly allowed by policy.
  404. */
  405. rc = avc_has_perm(isec_sid, SECINITSID_UNLABELED, SECCLASS_ASSOCIATION,
  406. ASSOCIATION__SENDTO, ad);
  407. out:
  408. return rc;
  409. }