xfrm.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  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/module.h>
  34. #include <linux/kernel.h>
  35. #include <linux/init.h>
  36. #include <linux/security.h>
  37. #include <linux/types.h>
  38. #include <linux/netfilter.h>
  39. #include <linux/netfilter_ipv4.h>
  40. #include <linux/netfilter_ipv6.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 <asm/semaphore.h>
  49. #include "avc.h"
  50. #include "objsec.h"
  51. #include "xfrm.h"
  52. /*
  53. * Returns true if an LSM/SELinux context
  54. */
  55. static inline int selinux_authorizable_ctx(struct xfrm_sec_ctx *ctx)
  56. {
  57. return (ctx &&
  58. (ctx->ctx_doi == XFRM_SC_DOI_LSM) &&
  59. (ctx->ctx_alg == XFRM_SC_ALG_SELINUX));
  60. }
  61. /*
  62. * Returns true if the xfrm contains a security blob for SELinux
  63. */
  64. static inline int selinux_authorizable_xfrm(struct xfrm_state *x)
  65. {
  66. return selinux_authorizable_ctx(x->security);
  67. }
  68. /*
  69. * LSM hook implementation that authorizes that a flow can use
  70. * a xfrm policy rule.
  71. */
  72. int selinux_xfrm_policy_lookup(struct xfrm_policy *xp, u32 fl_secid, u8 dir)
  73. {
  74. int rc = 0;
  75. u32 sel_sid = SECINITSID_UNLABELED;
  76. struct xfrm_sec_ctx *ctx;
  77. /* Context sid is either set to label or ANY_ASSOC */
  78. if ((ctx = xp->security)) {
  79. if (!selinux_authorizable_ctx(ctx))
  80. return -EINVAL;
  81. sel_sid = ctx->ctx_sid;
  82. }
  83. rc = avc_has_perm(fl_secid, sel_sid, SECCLASS_ASSOCIATION,
  84. ASSOCIATION__POLMATCH,
  85. NULL);
  86. return rc;
  87. }
  88. /*
  89. * LSM hook implementation that authorizes that a state matches
  90. * the given policy, flow combo.
  91. */
  92. int selinux_xfrm_state_pol_flow_match(struct xfrm_state *x, struct xfrm_policy *xp,
  93. struct flowi *fl)
  94. {
  95. u32 state_sid;
  96. u32 pol_sid;
  97. int err;
  98. if (x->security)
  99. state_sid = x->security->ctx_sid;
  100. else
  101. state_sid = SECINITSID_UNLABELED;
  102. if (xp->security)
  103. pol_sid = xp->security->ctx_sid;
  104. else
  105. pol_sid = SECINITSID_UNLABELED;
  106. err = avc_has_perm(state_sid, pol_sid, SECCLASS_ASSOCIATION,
  107. ASSOCIATION__POLMATCH,
  108. NULL);
  109. if (err)
  110. return 0;
  111. return selinux_xfrm_flow_state_match(fl, x);
  112. }
  113. /*
  114. * LSM hook implementation that authorizes that a particular outgoing flow
  115. * can use a given security association.
  116. */
  117. int selinux_xfrm_flow_state_match(struct flowi *fl, struct xfrm_state *xfrm)
  118. {
  119. int rc = 0;
  120. u32 sel_sid = SECINITSID_UNLABELED;
  121. struct xfrm_sec_ctx *ctx;
  122. /* Context sid is either set to label or ANY_ASSOC */
  123. if ((ctx = xfrm->security)) {
  124. if (!selinux_authorizable_ctx(ctx))
  125. return 0;
  126. sel_sid = ctx->ctx_sid;
  127. }
  128. rc = avc_has_perm(fl->secid, sel_sid, SECCLASS_ASSOCIATION,
  129. ASSOCIATION__SENDTO,
  130. NULL)? 0:1;
  131. return rc;
  132. }
  133. /*
  134. * LSM hook implementation that determines the sid for the session.
  135. */
  136. int selinux_xfrm_decode_session(struct sk_buff *skb, u32 *sid, int ckall)
  137. {
  138. struct sec_path *sp;
  139. *sid = SECSID_NULL;
  140. if (skb == NULL)
  141. return 0;
  142. sp = skb->sp;
  143. if (sp) {
  144. int i, sid_set = 0;
  145. for (i = sp->len-1; i >= 0; i--) {
  146. struct xfrm_state *x = sp->xvec[i];
  147. if (selinux_authorizable_xfrm(x)) {
  148. struct xfrm_sec_ctx *ctx = x->security;
  149. if (!sid_set) {
  150. *sid = ctx->ctx_sid;
  151. sid_set = 1;
  152. if (!ckall)
  153. break;
  154. }
  155. else if (*sid != ctx->ctx_sid)
  156. return -EINVAL;
  157. }
  158. }
  159. }
  160. return 0;
  161. }
  162. /*
  163. * Security blob allocation for xfrm_policy and xfrm_state
  164. * CTX does not have a meaningful value on input
  165. */
  166. static int selinux_xfrm_sec_ctx_alloc(struct xfrm_sec_ctx **ctxp,
  167. struct xfrm_user_sec_ctx *uctx, struct xfrm_sec_ctx *pol, u32 sid)
  168. {
  169. int rc = 0;
  170. struct task_security_struct *tsec = current->security;
  171. struct xfrm_sec_ctx *ctx = NULL;
  172. char *ctx_str = NULL;
  173. u32 str_len;
  174. u32 ctx_sid;
  175. BUG_ON(uctx && pol);
  176. if (pol)
  177. goto from_policy;
  178. BUG_ON(!uctx);
  179. if (uctx->ctx_doi != XFRM_SC_ALG_SELINUX)
  180. return -EINVAL;
  181. if (uctx->ctx_len >= PAGE_SIZE)
  182. return -ENOMEM;
  183. *ctxp = ctx = kmalloc(sizeof(*ctx) +
  184. uctx->ctx_len,
  185. GFP_KERNEL);
  186. if (!ctx)
  187. return -ENOMEM;
  188. ctx->ctx_doi = uctx->ctx_doi;
  189. ctx->ctx_len = uctx->ctx_len;
  190. ctx->ctx_alg = uctx->ctx_alg;
  191. memcpy(ctx->ctx_str,
  192. uctx+1,
  193. ctx->ctx_len);
  194. rc = security_context_to_sid(ctx->ctx_str,
  195. ctx->ctx_len,
  196. &ctx->ctx_sid);
  197. if (rc)
  198. goto out;
  199. /*
  200. * Does the subject have permission to set security context?
  201. */
  202. rc = avc_has_perm(tsec->sid, ctx->ctx_sid,
  203. SECCLASS_ASSOCIATION,
  204. ASSOCIATION__SETCONTEXT, NULL);
  205. if (rc)
  206. goto out;
  207. return rc;
  208. from_policy:
  209. BUG_ON(!pol);
  210. rc = security_sid_mls_copy(pol->ctx_sid, sid, &ctx_sid);
  211. if (rc)
  212. goto out;
  213. rc = security_sid_to_context(ctx_sid, &ctx_str, &str_len);
  214. if (rc)
  215. goto out;
  216. *ctxp = ctx = kmalloc(sizeof(*ctx) +
  217. str_len,
  218. GFP_ATOMIC);
  219. if (!ctx) {
  220. rc = -ENOMEM;
  221. goto out;
  222. }
  223. ctx->ctx_doi = XFRM_SC_DOI_LSM;
  224. ctx->ctx_alg = XFRM_SC_ALG_SELINUX;
  225. ctx->ctx_sid = ctx_sid;
  226. ctx->ctx_len = str_len;
  227. memcpy(ctx->ctx_str,
  228. ctx_str,
  229. str_len);
  230. goto out2;
  231. out:
  232. *ctxp = NULL;
  233. kfree(ctx);
  234. out2:
  235. kfree(ctx_str);
  236. return rc;
  237. }
  238. /*
  239. * LSM hook implementation that allocs and transfers uctx spec to
  240. * xfrm_policy.
  241. */
  242. int selinux_xfrm_policy_alloc(struct xfrm_policy *xp, struct xfrm_user_sec_ctx *uctx)
  243. {
  244. int err;
  245. BUG_ON(!xp);
  246. err = selinux_xfrm_sec_ctx_alloc(&xp->security, uctx, NULL, 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. struct xfrm_sec_ctx *pol, u32 secid)
  297. {
  298. int err;
  299. BUG_ON(!x);
  300. err = selinux_xfrm_sec_ctx_alloc(&x->security, uctx, pol, 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. * SELinux internal function to retrieve the context of a connected
  314. * (sk->sk_state == TCP_ESTABLISHED) TCP socket based on its security
  315. * association used to connect to the remote socket.
  316. *
  317. * Retrieve via getsockopt SO_PEERSEC.
  318. */
  319. u32 selinux_socket_getpeer_stream(struct sock *sk)
  320. {
  321. struct dst_entry *dst, *dst_test;
  322. u32 peer_sid = SECSID_NULL;
  323. if (sk->sk_state != TCP_ESTABLISHED)
  324. goto out;
  325. dst = sk_dst_get(sk);
  326. if (!dst)
  327. goto out;
  328. for (dst_test = dst; dst_test != 0;
  329. dst_test = dst_test->child) {
  330. struct xfrm_state *x = dst_test->xfrm;
  331. if (x && selinux_authorizable_xfrm(x)) {
  332. struct xfrm_sec_ctx *ctx = x->security;
  333. peer_sid = ctx->ctx_sid;
  334. break;
  335. }
  336. }
  337. dst_release(dst);
  338. out:
  339. return peer_sid;
  340. }
  341. /*
  342. * SELinux internal function to retrieve the context of a UDP packet
  343. * based on its security association used to connect to the remote socket.
  344. *
  345. * Retrieve via setsockopt IP_PASSSEC and recvmsg with control message
  346. * type SCM_SECURITY.
  347. */
  348. u32 selinux_socket_getpeer_dgram(struct sk_buff *skb)
  349. {
  350. struct sec_path *sp;
  351. if (skb == NULL)
  352. return SECSID_NULL;
  353. if (skb->sk->sk_protocol != IPPROTO_UDP)
  354. return SECSID_NULL;
  355. sp = skb->sp;
  356. if (sp) {
  357. int i;
  358. for (i = sp->len-1; i >= 0; i--) {
  359. struct xfrm_state *x = sp->xvec[i];
  360. if (selinux_authorizable_xfrm(x)) {
  361. struct xfrm_sec_ctx *ctx = x->security;
  362. return ctx->ctx_sid;
  363. }
  364. }
  365. }
  366. return SECSID_NULL;
  367. }
  368. /*
  369. * LSM hook implementation that authorizes deletion of labeled SAs.
  370. */
  371. int selinux_xfrm_state_delete(struct xfrm_state *x)
  372. {
  373. struct task_security_struct *tsec = current->security;
  374. struct xfrm_sec_ctx *ctx = x->security;
  375. int rc = 0;
  376. if (ctx)
  377. rc = avc_has_perm(tsec->sid, ctx->ctx_sid,
  378. SECCLASS_ASSOCIATION,
  379. ASSOCIATION__SETCONTEXT, NULL);
  380. return rc;
  381. }
  382. /*
  383. * LSM hook that controls access to unlabelled packets. If
  384. * a xfrm_state is authorizable (defined by macro) then it was
  385. * already authorized by the IPSec process. If not, then
  386. * we need to check for unlabelled access since this may not have
  387. * gone thru the IPSec process.
  388. */
  389. int selinux_xfrm_sock_rcv_skb(u32 isec_sid, struct sk_buff *skb,
  390. struct avc_audit_data *ad)
  391. {
  392. int i, rc = 0;
  393. struct sec_path *sp;
  394. u32 sel_sid = SECINITSID_UNLABELED;
  395. sp = skb->sp;
  396. if (sp) {
  397. for (i = 0; i < sp->len; i++) {
  398. struct xfrm_state *x = sp->xvec[i];
  399. if (x && selinux_authorizable_xfrm(x)) {
  400. struct xfrm_sec_ctx *ctx = x->security;
  401. sel_sid = ctx->ctx_sid;
  402. break;
  403. }
  404. }
  405. }
  406. rc = avc_has_perm(isec_sid, sel_sid, SECCLASS_ASSOCIATION,
  407. ASSOCIATION__RECVFROM, ad);
  408. return rc;
  409. }
  410. /*
  411. * POSTROUTE_LAST hook's XFRM processing:
  412. * If we have no security association, then we need to determine
  413. * whether the socket is allowed to send to an unlabelled destination.
  414. * If we do have a authorizable security association, then it has already been
  415. * checked in xfrm_policy_lookup hook.
  416. */
  417. int selinux_xfrm_postroute_last(u32 isec_sid, struct sk_buff *skb,
  418. struct avc_audit_data *ad)
  419. {
  420. struct dst_entry *dst;
  421. int rc = 0;
  422. dst = skb->dst;
  423. if (dst) {
  424. struct dst_entry *dst_test;
  425. for (dst_test = dst; dst_test != 0;
  426. dst_test = dst_test->child) {
  427. struct xfrm_state *x = dst_test->xfrm;
  428. if (x && selinux_authorizable_xfrm(x))
  429. goto out;
  430. }
  431. }
  432. rc = avc_has_perm(isec_sid, SECINITSID_UNLABELED, SECCLASS_ASSOCIATION,
  433. ASSOCIATION__SENDTO, ad);
  434. out:
  435. return rc;
  436. }