2 * NSA Security-Enhanced Linux (SELinux) security module
4 * This file contains the SELinux XFRM hook function implementations.
6 * Authors: Serge Hallyn <sergeh@us.ibm.com>
7 * Trent Jaeger <jaegert@us.ibm.com>
9 * Updated: Venkat Yekkirala <vyekkirala@TrustedCS.com>
11 * Granular IPSec Associations for use in MLS environments.
13 * Copyright (C) 2005 International Business Machines Corporation
14 * Copyright (C) 2006 Trusted Computer Solutions, Inc.
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.
24 * 1. Make sure to enable the following options in your kernel config:
26 * CONFIG_SECURITY_NETWORK=y
27 * CONFIG_SECURITY_NETWORK_XFRM=y
28 * CONFIG_SECURITY_SELINUX=m/y
30 * 1. Caching packets, so they are not dropped during negotiation
31 * 2. Emulating a reasonable SO_PEERSEC across machines
32 * 3. Testing addition of sk_policy's with security context via setsockopt
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>
42 #include <linux/tcp.h>
43 #include <linux/skbuff.h>
44 #include <linux/xfrm.h>
46 #include <net/checksum.h>
48 #include <asm/semaphore.h>
49 #include <asm/atomic.h>
55 /* Labeled XFRM instance counter */
56 atomic_t selinux_xfrm_refcount = ATOMIC_INIT(0);
59 * Returns true if an LSM/SELinux context
61 static inline int selinux_authorizable_ctx(struct xfrm_sec_ctx *ctx)
64 (ctx->ctx_doi == XFRM_SC_DOI_LSM) &&
65 (ctx->ctx_alg == XFRM_SC_ALG_SELINUX));
69 * Returns true if the xfrm contains a security blob for SELinux
71 static inline int selinux_authorizable_xfrm(struct xfrm_state *x)
73 return selinux_authorizable_ctx(x->security);
77 * LSM hook implementation that authorizes that a flow can use
80 int selinux_xfrm_policy_lookup(struct xfrm_policy *xp, u32 fl_secid, u8 dir)
84 struct xfrm_sec_ctx *ctx;
86 /* Context sid is either set to label or ANY_ASSOC */
87 if ((ctx = xp->security)) {
88 if (!selinux_authorizable_ctx(ctx))
91 sel_sid = ctx->ctx_sid;
95 * All flows should be treated as polmatch'ing an
96 * otherwise applicable "non-labeled" policy. This
97 * would prevent inadvertent "leaks".
101 rc = avc_has_perm(fl_secid, sel_sid, SECCLASS_ASSOCIATION,
102 ASSOCIATION__POLMATCH,
112 * LSM hook implementation that authorizes that a state matches
113 * the given policy, flow combo.
116 int selinux_xfrm_state_pol_flow_match(struct xfrm_state *x, struct xfrm_policy *xp,
124 /* unlabeled policy and labeled SA can't match */
127 /* unlabeled policy and unlabeled SA match all flows */
131 /* unlabeled SA and labeled policy can't match */
134 if (!selinux_authorizable_xfrm(x))
135 /* Not a SELinux-labeled SA */
138 state_sid = x->security->ctx_sid;
140 if (fl->secid != state_sid)
143 rc = avc_has_perm(fl->secid, state_sid, SECCLASS_ASSOCIATION,
148 * We don't need a separate SA Vs. policy polmatch check
149 * since the SA is now of the same label as the flow and
150 * a flow Vs. policy polmatch check had already happened
151 * in selinux_xfrm_policy_lookup() above.
158 * LSM hook implementation that checks and/or returns the xfrm sid for the
162 int selinux_xfrm_decode_session(struct sk_buff *skb, u32 *sid, int ckall)
175 for (i = sp->len-1; i >= 0; i--) {
176 struct xfrm_state *x = sp->xvec[i];
177 if (selinux_authorizable_xfrm(x)) {
178 struct xfrm_sec_ctx *ctx = x->security;
187 else if (*sid != ctx->ctx_sid)
197 * Security blob allocation for xfrm_policy and xfrm_state
198 * CTX does not have a meaningful value on input
200 static int selinux_xfrm_sec_ctx_alloc(struct xfrm_sec_ctx **ctxp,
201 struct xfrm_user_sec_ctx *uctx, u32 sid)
204 struct task_security_struct *tsec = current->security;
205 struct xfrm_sec_ctx *ctx = NULL;
206 char *ctx_str = NULL;
214 if (uctx->ctx_doi != XFRM_SC_ALG_SELINUX)
217 str_len = uctx->ctx_len;
218 if (str_len >= PAGE_SIZE)
221 *ctxp = ctx = kmalloc(sizeof(*ctx) +
228 ctx->ctx_doi = uctx->ctx_doi;
229 ctx->ctx_len = str_len;
230 ctx->ctx_alg = uctx->ctx_alg;
235 ctx->ctx_str[str_len] = 0;
236 rc = security_context_to_sid(ctx->ctx_str,
244 * Does the subject have permission to set security context?
246 rc = avc_has_perm(tsec->sid, ctx->ctx_sid,
247 SECCLASS_ASSOCIATION,
248 ASSOCIATION__SETCONTEXT, NULL);
255 rc = security_sid_to_context(sid, &ctx_str, &str_len);
259 *ctxp = ctx = kmalloc(sizeof(*ctx) +
268 ctx->ctx_doi = XFRM_SC_DOI_LSM;
269 ctx->ctx_alg = XFRM_SC_ALG_SELINUX;
271 ctx->ctx_len = str_len;
287 * LSM hook implementation that allocs and transfers uctx spec to
290 int selinux_xfrm_policy_alloc(struct xfrm_policy *xp,
291 struct xfrm_user_sec_ctx *uctx)
298 err = selinux_xfrm_sec_ctx_alloc(&xp->security, uctx, 0);
300 atomic_inc(&selinux_xfrm_refcount);
307 * LSM hook implementation that copies security data structure from old to
308 * new for policy cloning.
310 int selinux_xfrm_policy_clone(struct xfrm_policy *old, struct xfrm_policy *new)
312 struct xfrm_sec_ctx *old_ctx, *new_ctx;
314 old_ctx = old->security;
317 new_ctx = new->security = kmalloc(sizeof(*new_ctx) +
324 memcpy(new_ctx, old_ctx, sizeof(*new_ctx));
325 memcpy(new_ctx->ctx_str, old_ctx->ctx_str, new_ctx->ctx_len);
331 * LSM hook implementation that frees xfrm_policy security information.
333 void selinux_xfrm_policy_free(struct xfrm_policy *xp)
335 struct xfrm_sec_ctx *ctx = xp->security;
341 * LSM hook implementation that authorizes deletion of labeled policies.
343 int selinux_xfrm_policy_delete(struct xfrm_policy *xp)
345 struct task_security_struct *tsec = current->security;
346 struct xfrm_sec_ctx *ctx = xp->security;
350 rc = avc_has_perm(tsec->sid, ctx->ctx_sid,
351 SECCLASS_ASSOCIATION,
352 ASSOCIATION__SETCONTEXT, NULL);
354 atomic_dec(&selinux_xfrm_refcount);
361 * LSM hook implementation that allocs and transfers sec_ctx spec to
364 int selinux_xfrm_state_alloc(struct xfrm_state *x, struct xfrm_user_sec_ctx *uctx,
371 err = selinux_xfrm_sec_ctx_alloc(&x->security, uctx, secid);
373 atomic_inc(&selinux_xfrm_refcount);
378 * LSM hook implementation that frees xfrm_state security information.
380 void selinux_xfrm_state_free(struct xfrm_state *x)
382 struct xfrm_sec_ctx *ctx = x->security;
388 * LSM hook implementation that authorizes deletion of labeled SAs.
390 int selinux_xfrm_state_delete(struct xfrm_state *x)
392 struct task_security_struct *tsec = current->security;
393 struct xfrm_sec_ctx *ctx = x->security;
397 rc = avc_has_perm(tsec->sid, ctx->ctx_sid,
398 SECCLASS_ASSOCIATION,
399 ASSOCIATION__SETCONTEXT, NULL);
401 atomic_dec(&selinux_xfrm_refcount);
408 * LSM hook that controls access to unlabelled packets. If
409 * a xfrm_state is authorizable (defined by macro) then it was
410 * already authorized by the IPSec process. If not, then
411 * we need to check for unlabelled access since this may not have
412 * gone thru the IPSec process.
414 int selinux_xfrm_sock_rcv_skb(u32 isec_sid, struct sk_buff *skb,
415 struct avc_audit_data *ad)
419 u32 sel_sid = SECINITSID_UNLABELED;
424 for (i = 0; i < sp->len; i++) {
425 struct xfrm_state *x = sp->xvec[i];
427 if (x && selinux_authorizable_xfrm(x)) {
428 struct xfrm_sec_ctx *ctx = x->security;
429 sel_sid = ctx->ctx_sid;
436 * This check even when there's no association involved is
437 * intended, according to Trent Jaeger, to make sure a
438 * process can't engage in non-ipsec communication unless
439 * explicitly allowed by policy.
442 rc = avc_has_perm(isec_sid, sel_sid, SECCLASS_ASSOCIATION,
443 ASSOCIATION__RECVFROM, ad);
449 * POSTROUTE_LAST hook's XFRM processing:
450 * If we have no security association, then we need to determine
451 * whether the socket is allowed to send to an unlabelled destination.
452 * If we do have a authorizable security association, then it has already been
453 * checked in the selinux_xfrm_state_pol_flow_match hook above.
455 int selinux_xfrm_postroute_last(u32 isec_sid, struct sk_buff *skb,
456 struct avc_audit_data *ad, u8 proto)
458 struct dst_entry *dst;
464 struct dst_entry *dst_test;
466 for (dst_test = dst; dst_test != NULL;
467 dst_test = dst_test->child) {
468 struct xfrm_state *x = dst_test->xfrm;
470 if (x && selinux_authorizable_xfrm(x))
480 * We should have already seen this packet once before
481 * it underwent xfrm(s). No need to subject it to the
490 * This check even when there's no association involved is
491 * intended, according to Trent Jaeger, to make sure a
492 * process can't engage in non-ipsec communication unless
493 * explicitly allowed by policy.
496 rc = avc_has_perm(isec_sid, SECINITSID_UNLABELED, SECCLASS_ASSOCIATION,
497 ASSOCIATION__SENDTO, ad);