xfrm.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  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;
  75. u32 sel_sid;
  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. 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. rc = -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. struct flowi *fl)
  103. {
  104. u32 state_sid;
  105. u32 pol_sid;
  106. int err;
  107. if (xp->security) {
  108. if (!x->security)
  109. /* unlabeled SA and labeled policy can't match */
  110. return 0;
  111. else
  112. state_sid = x->security->ctx_sid;
  113. pol_sid = xp->security->ctx_sid;
  114. } else
  115. if (x->security)
  116. /* unlabeled policy and labeled SA can't match */
  117. return 0;
  118. else
  119. /* unlabeled policy and unlabeled SA match all flows */
  120. return 1;
  121. err = avc_has_perm(state_sid, pol_sid, SECCLASS_ASSOCIATION,
  122. ASSOCIATION__POLMATCH,
  123. NULL);
  124. if (err)
  125. return 0;
  126. err = avc_has_perm(fl->secid, state_sid, SECCLASS_ASSOCIATION,
  127. ASSOCIATION__SENDTO,
  128. NULL)? 0:1;
  129. return err;
  130. }
  131. /*
  132. * LSM hook implementation that authorizes that a particular outgoing flow
  133. * can use a given security association.
  134. */
  135. int selinux_xfrm_flow_state_match(struct flowi *fl, struct xfrm_state *xfrm,
  136. struct xfrm_policy *xp)
  137. {
  138. int rc = 0;
  139. u32 sel_sid = SECINITSID_UNLABELED;
  140. struct xfrm_sec_ctx *ctx;
  141. if (!xp->security)
  142. if (!xfrm->security)
  143. return 1;
  144. else
  145. return 0;
  146. else
  147. if (!xfrm->security)
  148. return 0;
  149. /* Context sid is either set to label or ANY_ASSOC */
  150. if ((ctx = xfrm->security)) {
  151. if (!selinux_authorizable_ctx(ctx))
  152. return 0;
  153. sel_sid = ctx->ctx_sid;
  154. }
  155. rc = avc_has_perm(fl->secid, sel_sid, SECCLASS_ASSOCIATION,
  156. ASSOCIATION__SENDTO,
  157. NULL)? 0:1;
  158. return rc;
  159. }
  160. /*
  161. * LSM hook implementation that determines the sid for the session.
  162. */
  163. int selinux_xfrm_decode_session(struct sk_buff *skb, u32 *sid, int ckall)
  164. {
  165. struct sec_path *sp;
  166. *sid = SECSID_NULL;
  167. if (skb == NULL)
  168. return 0;
  169. sp = skb->sp;
  170. if (sp) {
  171. int i, sid_set = 0;
  172. for (i = sp->len-1; i >= 0; i--) {
  173. struct xfrm_state *x = sp->xvec[i];
  174. if (selinux_authorizable_xfrm(x)) {
  175. struct xfrm_sec_ctx *ctx = x->security;
  176. if (!sid_set) {
  177. *sid = ctx->ctx_sid;
  178. sid_set = 1;
  179. if (!ckall)
  180. break;
  181. }
  182. else if (*sid != ctx->ctx_sid)
  183. return -EINVAL;
  184. }
  185. }
  186. }
  187. return 0;
  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. 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_doi != XFRM_SC_ALG_SELINUX)
  205. return -EINVAL;
  206. if (uctx->ctx_len >= PAGE_SIZE)
  207. return -ENOMEM;
  208. *ctxp = ctx = kmalloc(sizeof(*ctx) +
  209. uctx->ctx_len,
  210. GFP_KERNEL);
  211. if (!ctx)
  212. return -ENOMEM;
  213. ctx->ctx_doi = uctx->ctx_doi;
  214. ctx->ctx_len = uctx->ctx_len;
  215. ctx->ctx_alg = uctx->ctx_alg;
  216. memcpy(ctx->ctx_str,
  217. uctx+1,
  218. ctx->ctx_len);
  219. rc = security_context_to_sid(ctx->ctx_str,
  220. ctx->ctx_len,
  221. &ctx->ctx_sid);
  222. if (rc)
  223. goto out;
  224. /*
  225. * Does the subject have permission to set security context?
  226. */
  227. rc = avc_has_perm(tsec->sid, ctx->ctx_sid,
  228. SECCLASS_ASSOCIATION,
  229. ASSOCIATION__SETCONTEXT, NULL);
  230. if (rc)
  231. goto out;
  232. return rc;
  233. not_from_user:
  234. rc = security_sid_to_context(sid, &ctx_str, &str_len);
  235. if (rc)
  236. goto out;
  237. *ctxp = ctx = kmalloc(sizeof(*ctx) +
  238. str_len,
  239. GFP_ATOMIC);
  240. if (!ctx) {
  241. rc = -ENOMEM;
  242. goto out;
  243. }
  244. ctx->ctx_doi = XFRM_SC_DOI_LSM;
  245. ctx->ctx_alg = XFRM_SC_ALG_SELINUX;
  246. ctx->ctx_sid = sid;
  247. ctx->ctx_len = str_len;
  248. memcpy(ctx->ctx_str,
  249. ctx_str,
  250. str_len);
  251. goto out2;
  252. out:
  253. *ctxp = NULL;
  254. kfree(ctx);
  255. out2:
  256. kfree(ctx_str);
  257. return rc;
  258. }
  259. /*
  260. * LSM hook implementation that allocs and transfers uctx spec to
  261. * xfrm_policy.
  262. */
  263. int selinux_xfrm_policy_alloc(struct xfrm_policy *xp,
  264. struct xfrm_user_sec_ctx *uctx)
  265. {
  266. int err;
  267. BUG_ON(!xp);
  268. BUG_ON(!uctx);
  269. err = selinux_xfrm_sec_ctx_alloc(&xp->security, uctx, 0);
  270. return err;
  271. }
  272. /*
  273. * LSM hook implementation that copies security data structure from old to
  274. * new for policy cloning.
  275. */
  276. int selinux_xfrm_policy_clone(struct xfrm_policy *old, struct xfrm_policy *new)
  277. {
  278. struct xfrm_sec_ctx *old_ctx, *new_ctx;
  279. old_ctx = old->security;
  280. if (old_ctx) {
  281. new_ctx = new->security = kmalloc(sizeof(*new_ctx) +
  282. old_ctx->ctx_len,
  283. GFP_KERNEL);
  284. if (!new_ctx)
  285. return -ENOMEM;
  286. memcpy(new_ctx, old_ctx, sizeof(*new_ctx));
  287. memcpy(new_ctx->ctx_str, old_ctx->ctx_str, new_ctx->ctx_len);
  288. }
  289. return 0;
  290. }
  291. /*
  292. * LSM hook implementation that frees xfrm_policy security information.
  293. */
  294. void selinux_xfrm_policy_free(struct xfrm_policy *xp)
  295. {
  296. struct xfrm_sec_ctx *ctx = xp->security;
  297. if (ctx)
  298. kfree(ctx);
  299. }
  300. /*
  301. * LSM hook implementation that authorizes deletion of labeled policies.
  302. */
  303. int selinux_xfrm_policy_delete(struct xfrm_policy *xp)
  304. {
  305. struct task_security_struct *tsec = current->security;
  306. struct xfrm_sec_ctx *ctx = xp->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. return rc;
  313. }
  314. /*
  315. * LSM hook implementation that allocs and transfers sec_ctx spec to
  316. * xfrm_state.
  317. */
  318. int selinux_xfrm_state_alloc(struct xfrm_state *x, struct xfrm_user_sec_ctx *uctx,
  319. u32 secid)
  320. {
  321. int err;
  322. BUG_ON(!x);
  323. err = selinux_xfrm_sec_ctx_alloc(&x->security, uctx, secid);
  324. return err;
  325. }
  326. /*
  327. * LSM hook implementation that frees xfrm_state security information.
  328. */
  329. void selinux_xfrm_state_free(struct xfrm_state *x)
  330. {
  331. struct xfrm_sec_ctx *ctx = x->security;
  332. if (ctx)
  333. kfree(ctx);
  334. }
  335. /*
  336. * SELinux internal function to retrieve the context of a connected
  337. * (sk->sk_state == TCP_ESTABLISHED) TCP socket based on its security
  338. * association used to connect to the remote socket.
  339. *
  340. * Retrieve via getsockopt SO_PEERSEC.
  341. */
  342. u32 selinux_socket_getpeer_stream(struct sock *sk)
  343. {
  344. struct dst_entry *dst, *dst_test;
  345. u32 peer_sid = SECSID_NULL;
  346. if (sk->sk_state != TCP_ESTABLISHED)
  347. goto out;
  348. dst = sk_dst_get(sk);
  349. if (!dst)
  350. goto out;
  351. for (dst_test = dst; dst_test != 0;
  352. dst_test = dst_test->child) {
  353. struct xfrm_state *x = dst_test->xfrm;
  354. if (x && selinux_authorizable_xfrm(x)) {
  355. struct xfrm_sec_ctx *ctx = x->security;
  356. peer_sid = ctx->ctx_sid;
  357. break;
  358. }
  359. }
  360. dst_release(dst);
  361. out:
  362. return peer_sid;
  363. }
  364. /*
  365. * SELinux internal function to retrieve the context of a UDP packet
  366. * based on its security association used to connect to the remote socket.
  367. *
  368. * Retrieve via setsockopt IP_PASSSEC and recvmsg with control message
  369. * type SCM_SECURITY.
  370. */
  371. u32 selinux_socket_getpeer_dgram(struct sk_buff *skb)
  372. {
  373. struct sec_path *sp;
  374. if (skb == NULL)
  375. return SECSID_NULL;
  376. if (skb->sk->sk_protocol != IPPROTO_UDP)
  377. return SECSID_NULL;
  378. sp = skb->sp;
  379. if (sp) {
  380. int i;
  381. for (i = sp->len-1; i >= 0; i--) {
  382. struct xfrm_state *x = sp->xvec[i];
  383. if (selinux_authorizable_xfrm(x)) {
  384. struct xfrm_sec_ctx *ctx = x->security;
  385. return ctx->ctx_sid;
  386. }
  387. }
  388. }
  389. return SECSID_NULL;
  390. }
  391. /*
  392. * LSM hook implementation that authorizes deletion of labeled SAs.
  393. */
  394. int selinux_xfrm_state_delete(struct xfrm_state *x)
  395. {
  396. struct task_security_struct *tsec = current->security;
  397. struct xfrm_sec_ctx *ctx = x->security;
  398. int rc = 0;
  399. if (ctx)
  400. rc = avc_has_perm(tsec->sid, ctx->ctx_sid,
  401. SECCLASS_ASSOCIATION,
  402. ASSOCIATION__SETCONTEXT, NULL);
  403. return rc;
  404. }
  405. /*
  406. * LSM hook that controls access to unlabelled packets. If
  407. * a xfrm_state is authorizable (defined by macro) then it was
  408. * already authorized by the IPSec process. If not, then
  409. * we need to check for unlabelled access since this may not have
  410. * gone thru the IPSec process.
  411. */
  412. int selinux_xfrm_sock_rcv_skb(u32 isec_sid, struct sk_buff *skb,
  413. struct avc_audit_data *ad)
  414. {
  415. int i, rc = 0;
  416. struct sec_path *sp;
  417. u32 sel_sid = SECINITSID_UNLABELED;
  418. sp = skb->sp;
  419. if (sp) {
  420. for (i = 0; i < sp->len; i++) {
  421. struct xfrm_state *x = sp->xvec[i];
  422. if (x && selinux_authorizable_xfrm(x)) {
  423. struct xfrm_sec_ctx *ctx = x->security;
  424. sel_sid = ctx->ctx_sid;
  425. break;
  426. }
  427. }
  428. }
  429. rc = avc_has_perm(isec_sid, sel_sid, SECCLASS_ASSOCIATION,
  430. ASSOCIATION__RECVFROM, ad);
  431. return rc;
  432. }
  433. /*
  434. * POSTROUTE_LAST hook's XFRM processing:
  435. * If we have no security association, then we need to determine
  436. * whether the socket is allowed to send to an unlabelled destination.
  437. * If we do have a authorizable security association, then it has already been
  438. * checked in xfrm_policy_lookup hook.
  439. */
  440. int selinux_xfrm_postroute_last(u32 isec_sid, struct sk_buff *skb,
  441. struct avc_audit_data *ad)
  442. {
  443. struct dst_entry *dst;
  444. int rc = 0;
  445. dst = skb->dst;
  446. if (dst) {
  447. struct dst_entry *dst_test;
  448. for (dst_test = dst; dst_test != 0;
  449. dst_test = dst_test->child) {
  450. struct xfrm_state *x = dst_test->xfrm;
  451. if (x && selinux_authorizable_xfrm(x))
  452. goto out;
  453. }
  454. }
  455. rc = avc_has_perm(isec_sid, SECINITSID_UNLABELED, SECCLASS_ASSOCIATION,
  456. ASSOCIATION__SENDTO, ad);
  457. out:
  458. return rc;
  459. }