xfrm.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * NSA Security-Enhanced Linux (SELinux) security module
  4. *
  5. * This file contains the SELinux XFRM hook function implementations.
  6. *
  7. * Authors: Serge Hallyn <[email protected]>
  8. * Trent Jaeger <[email protected]>
  9. *
  10. * Updated: Venkat Yekkirala <[email protected]>
  11. *
  12. * Granular IPSec Associations for use in MLS environments.
  13. *
  14. * Copyright (C) 2005 International Business Machines Corporation
  15. * Copyright (C) 2006 Trusted Computer Solutions, Inc.
  16. */
  17. /*
  18. * USAGE:
  19. * NOTES:
  20. * 1. Make sure to enable the following options in your kernel config:
  21. * CONFIG_SECURITY=y
  22. * CONFIG_SECURITY_NETWORK=y
  23. * CONFIG_SECURITY_NETWORK_XFRM=y
  24. * CONFIG_SECURITY_SELINUX=m/y
  25. * ISSUES:
  26. * 1. Caching packets, so they are not dropped during negotiation
  27. * 2. Emulating a reasonable SO_PEERSEC across machines
  28. * 3. Testing addition of sk_policy's with security context via setsockopt
  29. */
  30. #include <linux/kernel.h>
  31. #include <linux/init.h>
  32. #include <linux/security.h>
  33. #include <linux/types.h>
  34. #include <linux/slab.h>
  35. #include <linux/ip.h>
  36. #include <linux/tcp.h>
  37. #include <linux/skbuff.h>
  38. #include <linux/xfrm.h>
  39. #include <net/xfrm.h>
  40. #include <net/checksum.h>
  41. #include <net/udp.h>
  42. #include <linux/atomic.h>
  43. #include "avc.h"
  44. #include "objsec.h"
  45. #include "xfrm.h"
  46. /* Labeled XFRM instance counter */
  47. atomic_t selinux_xfrm_refcount __read_mostly = ATOMIC_INIT(0);
  48. /*
  49. * Returns true if the context is an LSM/SELinux context.
  50. */
  51. static inline int selinux_authorizable_ctx(struct xfrm_sec_ctx *ctx)
  52. {
  53. return (ctx &&
  54. (ctx->ctx_doi == XFRM_SC_DOI_LSM) &&
  55. (ctx->ctx_alg == XFRM_SC_ALG_SELINUX));
  56. }
  57. /*
  58. * Returns true if the xfrm contains a security blob for SELinux.
  59. */
  60. static inline int selinux_authorizable_xfrm(struct xfrm_state *x)
  61. {
  62. return selinux_authorizable_ctx(x->security);
  63. }
  64. /*
  65. * Allocates a xfrm_sec_state and populates it using the supplied security
  66. * xfrm_user_sec_ctx context.
  67. */
  68. static int selinux_xfrm_alloc_user(struct xfrm_sec_ctx **ctxp,
  69. struct xfrm_user_sec_ctx *uctx,
  70. gfp_t gfp)
  71. {
  72. int rc;
  73. const struct task_security_struct *tsec = selinux_cred(current_cred());
  74. struct xfrm_sec_ctx *ctx = NULL;
  75. u32 str_len;
  76. if (ctxp == NULL || uctx == NULL ||
  77. uctx->ctx_doi != XFRM_SC_DOI_LSM ||
  78. uctx->ctx_alg != XFRM_SC_ALG_SELINUX)
  79. return -EINVAL;
  80. str_len = uctx->ctx_len;
  81. if (str_len >= PAGE_SIZE)
  82. return -ENOMEM;
  83. ctx = kmalloc(struct_size(ctx, ctx_str, str_len + 1), gfp);
  84. if (!ctx)
  85. return -ENOMEM;
  86. ctx->ctx_doi = XFRM_SC_DOI_LSM;
  87. ctx->ctx_alg = XFRM_SC_ALG_SELINUX;
  88. ctx->ctx_len = str_len;
  89. memcpy(ctx->ctx_str, &uctx[1], str_len);
  90. ctx->ctx_str[str_len] = '\0';
  91. rc = security_context_to_sid(&selinux_state, ctx->ctx_str, str_len,
  92. &ctx->ctx_sid, gfp);
  93. if (rc)
  94. goto err;
  95. rc = avc_has_perm(&selinux_state,
  96. tsec->sid, ctx->ctx_sid,
  97. SECCLASS_ASSOCIATION, ASSOCIATION__SETCONTEXT, NULL);
  98. if (rc)
  99. goto err;
  100. *ctxp = ctx;
  101. atomic_inc(&selinux_xfrm_refcount);
  102. return 0;
  103. err:
  104. kfree(ctx);
  105. return rc;
  106. }
  107. /*
  108. * Free the xfrm_sec_ctx structure.
  109. */
  110. static void selinux_xfrm_free(struct xfrm_sec_ctx *ctx)
  111. {
  112. if (!ctx)
  113. return;
  114. atomic_dec(&selinux_xfrm_refcount);
  115. kfree(ctx);
  116. }
  117. /*
  118. * Authorize the deletion of a labeled SA or policy rule.
  119. */
  120. static int selinux_xfrm_delete(struct xfrm_sec_ctx *ctx)
  121. {
  122. const struct task_security_struct *tsec = selinux_cred(current_cred());
  123. if (!ctx)
  124. return 0;
  125. return avc_has_perm(&selinux_state,
  126. tsec->sid, ctx->ctx_sid,
  127. SECCLASS_ASSOCIATION, ASSOCIATION__SETCONTEXT,
  128. NULL);
  129. }
  130. /*
  131. * LSM hook implementation that authorizes that a flow can use a xfrm policy
  132. * rule.
  133. */
  134. int selinux_xfrm_policy_lookup(struct xfrm_sec_ctx *ctx, u32 fl_secid)
  135. {
  136. int rc;
  137. /* All flows should be treated as polmatch'ing an otherwise applicable
  138. * "non-labeled" policy. This would prevent inadvertent "leaks". */
  139. if (!ctx)
  140. return 0;
  141. /* Context sid is either set to label or ANY_ASSOC */
  142. if (!selinux_authorizable_ctx(ctx))
  143. return -EINVAL;
  144. rc = avc_has_perm(&selinux_state,
  145. fl_secid, ctx->ctx_sid,
  146. SECCLASS_ASSOCIATION, ASSOCIATION__POLMATCH, NULL);
  147. return (rc == -EACCES ? -ESRCH : rc);
  148. }
  149. /*
  150. * LSM hook implementation that authorizes that a state matches
  151. * the given policy, flow combo.
  152. */
  153. int selinux_xfrm_state_pol_flow_match(struct xfrm_state *x,
  154. struct xfrm_policy *xp,
  155. const struct flowi_common *flic)
  156. {
  157. u32 state_sid;
  158. u32 flic_sid;
  159. if (!xp->security)
  160. if (x->security)
  161. /* unlabeled policy and labeled SA can't match */
  162. return 0;
  163. else
  164. /* unlabeled policy and unlabeled SA match all flows */
  165. return 1;
  166. else
  167. if (!x->security)
  168. /* unlabeled SA and labeled policy can't match */
  169. return 0;
  170. else
  171. if (!selinux_authorizable_xfrm(x))
  172. /* Not a SELinux-labeled SA */
  173. return 0;
  174. state_sid = x->security->ctx_sid;
  175. flic_sid = flic->flowic_secid;
  176. if (flic_sid != state_sid)
  177. return 0;
  178. /* We don't need a separate SA Vs. policy polmatch check since the SA
  179. * is now of the same label as the flow and a flow Vs. policy polmatch
  180. * check had already happened in selinux_xfrm_policy_lookup() above. */
  181. return (avc_has_perm(&selinux_state, flic_sid, state_sid,
  182. SECCLASS_ASSOCIATION, ASSOCIATION__SENDTO,
  183. NULL) ? 0 : 1);
  184. }
  185. static u32 selinux_xfrm_skb_sid_egress(struct sk_buff *skb)
  186. {
  187. struct dst_entry *dst = skb_dst(skb);
  188. struct xfrm_state *x;
  189. if (dst == NULL)
  190. return SECSID_NULL;
  191. x = dst->xfrm;
  192. if (x == NULL || !selinux_authorizable_xfrm(x))
  193. return SECSID_NULL;
  194. return x->security->ctx_sid;
  195. }
  196. static int selinux_xfrm_skb_sid_ingress(struct sk_buff *skb,
  197. u32 *sid, int ckall)
  198. {
  199. u32 sid_session = SECSID_NULL;
  200. struct sec_path *sp = skb_sec_path(skb);
  201. if (sp) {
  202. int i;
  203. for (i = sp->len - 1; i >= 0; i--) {
  204. struct xfrm_state *x = sp->xvec[i];
  205. if (selinux_authorizable_xfrm(x)) {
  206. struct xfrm_sec_ctx *ctx = x->security;
  207. if (sid_session == SECSID_NULL) {
  208. sid_session = ctx->ctx_sid;
  209. if (!ckall)
  210. goto out;
  211. } else if (sid_session != ctx->ctx_sid) {
  212. *sid = SECSID_NULL;
  213. return -EINVAL;
  214. }
  215. }
  216. }
  217. }
  218. out:
  219. *sid = sid_session;
  220. return 0;
  221. }
  222. /*
  223. * LSM hook implementation that checks and/or returns the xfrm sid for the
  224. * incoming packet.
  225. */
  226. int selinux_xfrm_decode_session(struct sk_buff *skb, u32 *sid, int ckall)
  227. {
  228. if (skb == NULL) {
  229. *sid = SECSID_NULL;
  230. return 0;
  231. }
  232. return selinux_xfrm_skb_sid_ingress(skb, sid, ckall);
  233. }
  234. int selinux_xfrm_skb_sid(struct sk_buff *skb, u32 *sid)
  235. {
  236. int rc;
  237. rc = selinux_xfrm_skb_sid_ingress(skb, sid, 0);
  238. if (rc == 0 && *sid == SECSID_NULL)
  239. *sid = selinux_xfrm_skb_sid_egress(skb);
  240. return rc;
  241. }
  242. /*
  243. * LSM hook implementation that allocs and transfers uctx spec to xfrm_policy.
  244. */
  245. int selinux_xfrm_policy_alloc(struct xfrm_sec_ctx **ctxp,
  246. struct xfrm_user_sec_ctx *uctx,
  247. gfp_t gfp)
  248. {
  249. return selinux_xfrm_alloc_user(ctxp, uctx, gfp);
  250. }
  251. /*
  252. * LSM hook implementation that copies security data structure from old to new
  253. * for policy cloning.
  254. */
  255. int selinux_xfrm_policy_clone(struct xfrm_sec_ctx *old_ctx,
  256. struct xfrm_sec_ctx **new_ctxp)
  257. {
  258. struct xfrm_sec_ctx *new_ctx;
  259. if (!old_ctx)
  260. return 0;
  261. new_ctx = kmemdup(old_ctx, sizeof(*old_ctx) + old_ctx->ctx_len,
  262. GFP_ATOMIC);
  263. if (!new_ctx)
  264. return -ENOMEM;
  265. atomic_inc(&selinux_xfrm_refcount);
  266. *new_ctxp = new_ctx;
  267. return 0;
  268. }
  269. /*
  270. * LSM hook implementation that frees xfrm_sec_ctx security information.
  271. */
  272. void selinux_xfrm_policy_free(struct xfrm_sec_ctx *ctx)
  273. {
  274. selinux_xfrm_free(ctx);
  275. }
  276. /*
  277. * LSM hook implementation that authorizes deletion of labeled policies.
  278. */
  279. int selinux_xfrm_policy_delete(struct xfrm_sec_ctx *ctx)
  280. {
  281. return selinux_xfrm_delete(ctx);
  282. }
  283. /*
  284. * LSM hook implementation that allocates a xfrm_sec_state, populates it using
  285. * the supplied security context, and assigns it to the xfrm_state.
  286. */
  287. int selinux_xfrm_state_alloc(struct xfrm_state *x,
  288. struct xfrm_user_sec_ctx *uctx)
  289. {
  290. return selinux_xfrm_alloc_user(&x->security, uctx, GFP_KERNEL);
  291. }
  292. /*
  293. * LSM hook implementation that allocates a xfrm_sec_state and populates based
  294. * on a secid.
  295. */
  296. int selinux_xfrm_state_alloc_acquire(struct xfrm_state *x,
  297. struct xfrm_sec_ctx *polsec, u32 secid)
  298. {
  299. int rc;
  300. struct xfrm_sec_ctx *ctx;
  301. char *ctx_str = NULL;
  302. u32 str_len;
  303. if (!polsec)
  304. return 0;
  305. if (secid == 0)
  306. return -EINVAL;
  307. rc = security_sid_to_context(&selinux_state, secid, &ctx_str,
  308. &str_len);
  309. if (rc)
  310. return rc;
  311. ctx = kmalloc(struct_size(ctx, ctx_str, str_len), GFP_ATOMIC);
  312. if (!ctx) {
  313. rc = -ENOMEM;
  314. goto out;
  315. }
  316. ctx->ctx_doi = XFRM_SC_DOI_LSM;
  317. ctx->ctx_alg = XFRM_SC_ALG_SELINUX;
  318. ctx->ctx_sid = secid;
  319. ctx->ctx_len = str_len;
  320. memcpy(ctx->ctx_str, ctx_str, str_len);
  321. x->security = ctx;
  322. atomic_inc(&selinux_xfrm_refcount);
  323. out:
  324. kfree(ctx_str);
  325. return rc;
  326. }
  327. /*
  328. * LSM hook implementation that frees xfrm_state security information.
  329. */
  330. void selinux_xfrm_state_free(struct xfrm_state *x)
  331. {
  332. selinux_xfrm_free(x->security);
  333. }
  334. /*
  335. * LSM hook implementation that authorizes deletion of labeled SAs.
  336. */
  337. int selinux_xfrm_state_delete(struct xfrm_state *x)
  338. {
  339. return selinux_xfrm_delete(x->security);
  340. }
  341. /*
  342. * LSM hook that controls access to unlabelled packets. If
  343. * a xfrm_state is authorizable (defined by macro) then it was
  344. * already authorized by the IPSec process. If not, then
  345. * we need to check for unlabelled access since this may not have
  346. * gone thru the IPSec process.
  347. */
  348. int selinux_xfrm_sock_rcv_skb(u32 sk_sid, struct sk_buff *skb,
  349. struct common_audit_data *ad)
  350. {
  351. int i;
  352. struct sec_path *sp = skb_sec_path(skb);
  353. u32 peer_sid = SECINITSID_UNLABELED;
  354. if (sp) {
  355. for (i = 0; i < sp->len; i++) {
  356. struct xfrm_state *x = sp->xvec[i];
  357. if (x && selinux_authorizable_xfrm(x)) {
  358. struct xfrm_sec_ctx *ctx = x->security;
  359. peer_sid = ctx->ctx_sid;
  360. break;
  361. }
  362. }
  363. }
  364. /* This check even when there's no association involved is intended,
  365. * according to Trent Jaeger, to make sure a process can't engage in
  366. * non-IPsec communication unless explicitly allowed by policy. */
  367. return avc_has_perm(&selinux_state,
  368. sk_sid, peer_sid,
  369. SECCLASS_ASSOCIATION, ASSOCIATION__RECVFROM, ad);
  370. }
  371. /*
  372. * POSTROUTE_LAST hook's XFRM processing:
  373. * If we have no security association, then we need to determine
  374. * whether the socket is allowed to send to an unlabelled destination.
  375. * If we do have a authorizable security association, then it has already been
  376. * checked in the selinux_xfrm_state_pol_flow_match hook above.
  377. */
  378. int selinux_xfrm_postroute_last(u32 sk_sid, struct sk_buff *skb,
  379. struct common_audit_data *ad, u8 proto)
  380. {
  381. struct dst_entry *dst;
  382. switch (proto) {
  383. case IPPROTO_AH:
  384. case IPPROTO_ESP:
  385. case IPPROTO_COMP:
  386. /* We should have already seen this packet once before it
  387. * underwent xfrm(s). No need to subject it to the unlabeled
  388. * check. */
  389. return 0;
  390. default:
  391. break;
  392. }
  393. dst = skb_dst(skb);
  394. if (dst) {
  395. struct dst_entry *iter;
  396. for (iter = dst; iter != NULL; iter = xfrm_dst_child(iter)) {
  397. struct xfrm_state *x = iter->xfrm;
  398. if (x && selinux_authorizable_xfrm(x))
  399. return 0;
  400. }
  401. }
  402. /* This check even when there's no association involved is intended,
  403. * according to Trent Jaeger, to make sure a process can't engage in
  404. * non-IPsec communication unless explicitly allowed by policy. */
  405. return avc_has_perm(&selinux_state, sk_sid, SECINITSID_UNLABELED,
  406. SECCLASS_ASSOCIATION, ASSOCIATION__SENDTO, ad);
  407. }