xfrm.c 11 KB

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