xfrm.c 10.6 KB
Newer Older
1 2 3 4 5 6 7 8
/*
 *  NSA Security-Enhanced Linux (SELinux) security module
 *
 *  This file contains the SELinux XFRM hook function implementations.
 *
 *  Authors:  Serge Hallyn <sergeh@us.ibm.com>
 *	      Trent Jaeger <jaegert@us.ibm.com>
 *
9 10 11 12
 *  Updated: Venkat Yekkirala <vyekkirala@TrustedCS.com>
 *
 *           Granular IPSec Associations for use in MLS environments.
 *
13
 *  Copyright (C) 2005 International Business Machines Corporation
14
 *  Copyright (C) 2006 Trusted Computer Solutions, Inc.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
 *
 *	This program is free software; you can redistribute it and/or modify
 *	it under the terms of the GNU General Public License version 2,
 *	as published by the Free Software Foundation.
 */

/*
 * USAGE:
 * NOTES:
 *   1. Make sure to enable the following options in your kernel config:
 *	CONFIG_SECURITY=y
 *	CONFIG_SECURITY_NETWORK=y
 *	CONFIG_SECURITY_NETWORK_XFRM=y
 *	CONFIG_SECURITY_SELINUX=m/y
 * ISSUES:
 *   1. Caching packets, so they are not dropped during negotiation
 *   2. Emulating a reasonable SO_PEERSEC across machines
 *   3. Testing addition of sk_policy's with security context via setsockopt
 */
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/security.h>
#include <linux/types.h>
#include <linux/netfilter.h>
#include <linux/netfilter_ipv4.h>
#include <linux/netfilter_ipv6.h>
#include <linux/ip.h>
#include <linux/tcp.h>
#include <linux/skbuff.h>
#include <linux/xfrm.h>
#include <net/xfrm.h>
#include <net/checksum.h>
#include <net/udp.h>
48
#include <asm/atomic.h>
49 50 51 52 53

#include "avc.h"
#include "objsec.h"
#include "xfrm.h"

54 55
/* Labeled XFRM instance counter */
atomic_t selinux_xfrm_refcount = ATOMIC_INIT(0);
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75

/*
 * Returns true if an LSM/SELinux context
 */
static inline int selinux_authorizable_ctx(struct xfrm_sec_ctx *ctx)
{
	return (ctx &&
		(ctx->ctx_doi == XFRM_SC_DOI_LSM) &&
		(ctx->ctx_alg == XFRM_SC_ALG_SELINUX));
}

/*
 * Returns true if the xfrm contains a security blob for SELinux
 */
static inline int selinux_authorizable_xfrm(struct xfrm_state *x)
{
	return selinux_authorizable_ctx(x->security);
}

/*
76 77
 * LSM hook implementation that authorizes that a flow can use
 * a xfrm policy rule.
78
 */
79
int selinux_xfrm_policy_lookup(struct xfrm_sec_ctx *ctx, u32 fl_secid, u8 dir)
80
{
81 82
	int rc;
	u32 sel_sid;
83 84

	/* Context sid is either set to label or ANY_ASSOC */
85
	if (ctx) {
86 87 88 89
		if (!selinux_authorizable_ctx(ctx))
			return -EINVAL;

		sel_sid = ctx->ctx_sid;
90
	} else
91 92 93 94 95 96
		/*
		 * All flows should be treated as polmatch'ing an
		 * otherwise applicable "non-labeled" policy. This
		 * would prevent inadvertent "leaks".
		 */
		return 0;
97

98 99
	rc = avc_has_perm(fl_secid, sel_sid, SECCLASS_ASSOCIATION,
			  ASSOCIATION__POLMATCH,
100 101
			  NULL);

102
	if (rc == -EACCES)
103
		return -ESRCH;
104

105 106 107
	return rc;
}

108 109 110 111 112 113 114 115 116
/*
 * LSM hook implementation that authorizes that a state matches
 * the given policy, flow combo.
 */

int selinux_xfrm_state_pol_flow_match(struct xfrm_state *x, struct xfrm_policy *xp,
			struct flowi *fl)
{
	u32 state_sid;
117
	int rc;
118

119
	if (!xp->security)
120 121 122 123 124 125 126
		if (x->security)
			/* unlabeled policy and labeled SA can't match */
			return 0;
		else
			/* unlabeled policy and unlabeled SA match all flows */
			return 1;
	else
127 128
		if (!x->security)
			/* unlabeled SA and labeled policy can't match */
129
			return 0;
130 131 132 133
		else
			if (!selinux_authorizable_xfrm(x))
				/* Not a SELinux-labeled SA */
				return 0;
134

135
	state_sid = x->security->ctx_sid;
136

137 138
	if (fl->secid != state_sid)
		return 0;
139

140
	rc = avc_has_perm(fl->secid, state_sid, SECCLASS_ASSOCIATION,
141 142 143
			  ASSOCIATION__SENDTO,
			  NULL)? 0:1;

144 145 146 147 148 149 150
	/*
	 * We don't need a separate SA Vs. policy polmatch check
	 * since the SA is now of the same label as the flow and
	 * a flow Vs. policy polmatch check had already happened
	 * in selinux_xfrm_policy_lookup() above.
	 */

151 152 153 154
	return rc;
}

/*
155 156
 * LSM hook implementation that checks and/or returns the xfrm sid for the
 * incoming packet.
157 158
 */

V
Venkat Yekkirala 已提交
159
int selinux_xfrm_decode_session(struct sk_buff *skb, u32 *sid, int ckall)
160 161 162
{
	struct sec_path *sp;

V
Venkat Yekkirala 已提交
163
	*sid = SECSID_NULL;
164 165 166 167 168 169 170 171 172 173 174 175 176 177

	if (skb == NULL)
		return 0;

	sp = skb->sp;
	if (sp) {
		int i, sid_set = 0;

		for (i = sp->len-1; i >= 0; i--) {
			struct xfrm_state *x = sp->xvec[i];
			if (selinux_authorizable_xfrm(x)) {
				struct xfrm_sec_ctx *ctx = x->security;

				if (!sid_set) {
V
Venkat Yekkirala 已提交
178
					*sid = ctx->ctx_sid;
179
					sid_set = 1;
V
Venkat Yekkirala 已提交
180 181 182

					if (!ckall)
						break;
183
				} else if (*sid != ctx->ctx_sid)
184 185 186 187 188 189 190 191
					return -EINVAL;
			}
		}
	}

	return 0;
}

192 193 194 195
/*
 * Security blob allocation for xfrm_policy and xfrm_state
 * CTX does not have a meaningful value on input
 */
196
static int selinux_xfrm_sec_ctx_alloc(struct xfrm_sec_ctx **ctxp,
197
	struct xfrm_user_sec_ctx *uctx, u32 sid)
198 199
{
	int rc = 0;
200
	const struct task_security_struct *tsec = current_security();
201 202 203 204
	struct xfrm_sec_ctx *ctx = NULL;
	char *ctx_str = NULL;
	u32 str_len;

205
	BUG_ON(uctx && sid);
206

207 208
	if (!uctx)
		goto not_from_user;
209 210 211

	if (uctx->ctx_doi != XFRM_SC_ALG_SELINUX)
		return -EINVAL;
212

213 214
	str_len = uctx->ctx_len;
	if (str_len >= PAGE_SIZE)
215 216 217
		return -ENOMEM;

	*ctxp = ctx = kmalloc(sizeof(*ctx) +
218
			      str_len + 1,
219 220 221 222 223 224
			      GFP_KERNEL);

	if (!ctx)
		return -ENOMEM;

	ctx->ctx_doi = uctx->ctx_doi;
225
	ctx->ctx_len = str_len;
226 227 228 229
	ctx->ctx_alg = uctx->ctx_alg;

	memcpy(ctx->ctx_str,
	       uctx+1,
230 231
	       str_len);
	ctx->ctx_str[str_len] = 0;
232
	rc = security_context_to_sid(ctx->ctx_str,
233
				     str_len,
234 235 236 237 238 239
				     &ctx->ctx_sid);

	if (rc)
		goto out;

	/*
C
Catherine Zhang 已提交
240
	 * Does the subject have permission to set security context?
241 242 243
	 */
	rc = avc_has_perm(tsec->sid, ctx->ctx_sid,
			  SECCLASS_ASSOCIATION,
244
			  ASSOCIATION__SETCONTEXT, NULL);
245 246 247 248 249
	if (rc)
		goto out;

	return rc;

250
not_from_user:
251
	rc = security_sid_to_context(sid, &ctx_str, &str_len);
252 253 254 255 256 257 258 259 260 261 262 263 264 265
	if (rc)
		goto out;

	*ctxp = ctx = kmalloc(sizeof(*ctx) +
			      str_len,
			      GFP_ATOMIC);

	if (!ctx) {
		rc = -ENOMEM;
		goto out;
	}

	ctx->ctx_doi = XFRM_SC_DOI_LSM;
	ctx->ctx_alg = XFRM_SC_ALG_SELINUX;
266
	ctx->ctx_sid = sid;
267 268 269 270 271 272 273
	ctx->ctx_len = str_len;
	memcpy(ctx->ctx_str,
	       ctx_str,
	       str_len);

	goto out2;

274
out:
L
Luiz Capitulino 已提交
275
	*ctxp = NULL;
276
	kfree(ctx);
277 278
out2:
	kfree(ctx_str);
279 280 281 282 283 284 285
	return rc;
}

/*
 * LSM hook implementation that allocs and transfers uctx spec to
 * xfrm_policy.
 */
286 287
int selinux_xfrm_policy_alloc(struct xfrm_sec_ctx **ctxp,
			      struct xfrm_user_sec_ctx *uctx)
288 289 290
{
	int err;

291
	BUG_ON(!uctx);
292

293
	err = selinux_xfrm_sec_ctx_alloc(ctxp, uctx, 0);
294 295 296
	if (err == 0)
		atomic_inc(&selinux_xfrm_refcount);

297 298 299 300 301 302 303 304
	return err;
}


/*
 * LSM hook implementation that copies security data structure from old to
 * new for policy cloning.
 */
305 306
int selinux_xfrm_policy_clone(struct xfrm_sec_ctx *old_ctx,
			      struct xfrm_sec_ctx **new_ctxp)
307
{
308
	struct xfrm_sec_ctx *new_ctx;
309 310

	if (old_ctx) {
311 312
		new_ctx = kmalloc(sizeof(*old_ctx) + old_ctx->ctx_len,
				  GFP_KERNEL);
313 314 315 316 317
		if (!new_ctx)
			return -ENOMEM;

		memcpy(new_ctx, old_ctx, sizeof(*new_ctx));
		memcpy(new_ctx->ctx_str, old_ctx->ctx_str, new_ctx->ctx_len);
318
		*new_ctxp = new_ctx;
319 320 321 322 323
	}
	return 0;
}

/*
324
 * LSM hook implementation that frees xfrm_sec_ctx security information.
325
 */
326
void selinux_xfrm_policy_free(struct xfrm_sec_ctx *ctx)
327
{
328
	kfree(ctx);
329 330
}

C
Catherine Zhang 已提交
331 332 333
/*
 * LSM hook implementation that authorizes deletion of labeled policies.
 */
334
int selinux_xfrm_policy_delete(struct xfrm_sec_ctx *ctx)
C
Catherine Zhang 已提交
335
{
336
	const struct task_security_struct *tsec = current_security();
C
Catherine Zhang 已提交
337 338
	int rc = 0;

339
	if (ctx) {
C
Catherine Zhang 已提交
340 341 342
		rc = avc_has_perm(tsec->sid, ctx->ctx_sid,
				  SECCLASS_ASSOCIATION,
				  ASSOCIATION__SETCONTEXT, NULL);
343 344 345
		if (rc == 0)
			atomic_dec(&selinux_xfrm_refcount);
	}
C
Catherine Zhang 已提交
346 347 348 349

	return rc;
}

350 351 352 353
/*
 * LSM hook implementation that allocs and transfers sec_ctx spec to
 * xfrm_state.
 */
354
int selinux_xfrm_state_alloc(struct xfrm_state *x, struct xfrm_user_sec_ctx *uctx,
355
		u32 secid)
356 357 358 359 360
{
	int err;

	BUG_ON(!x);

361
	err = selinux_xfrm_sec_ctx_alloc(&x->security, uctx, secid);
362 363
	if (err == 0)
		atomic_inc(&selinux_xfrm_refcount);
364 365 366 367 368 369 370 371 372
	return err;
}

/*
 * LSM hook implementation that frees xfrm_state security information.
 */
void selinux_xfrm_state_free(struct xfrm_state *x)
{
	struct xfrm_sec_ctx *ctx = x->security;
373
	kfree(ctx);
374 375
}

C
Catherine Zhang 已提交
376 377 378 379 380
 /*
  * LSM hook implementation that authorizes deletion of labeled SAs.
  */
int selinux_xfrm_state_delete(struct xfrm_state *x)
{
381
	const struct task_security_struct *tsec = current_security();
C
Catherine Zhang 已提交
382 383 384
	struct xfrm_sec_ctx *ctx = x->security;
	int rc = 0;

385
	if (ctx) {
C
Catherine Zhang 已提交
386 387 388
		rc = avc_has_perm(tsec->sid, ctx->ctx_sid,
				  SECCLASS_ASSOCIATION,
				  ASSOCIATION__SETCONTEXT, NULL);
389 390 391
		if (rc == 0)
			atomic_dec(&selinux_xfrm_refcount);
	}
C
Catherine Zhang 已提交
392 393 394 395

	return rc;
}

396 397 398 399 400 401 402
/*
 * LSM hook that controls access to unlabelled packets.  If
 * a xfrm_state is authorizable (defined by macro) then it was
 * already authorized by the IPSec process.  If not, then
 * we need to check for unlabelled access since this may not have
 * gone thru the IPSec process.
 */
403
int selinux_xfrm_sock_rcv_skb(u32 isec_sid, struct sk_buff *skb,
404
				struct common_audit_data *ad)
405 406 407
{
	int i, rc = 0;
	struct sec_path *sp;
408
	u32 sel_sid = SECINITSID_UNLABELED;
409 410 411 412 413

	sp = skb->sp;

	if (sp) {
		for (i = 0; i < sp->len; i++) {
414
			struct xfrm_state *x = sp->xvec[i];
415

416 417 418 419 420
			if (x && selinux_authorizable_xfrm(x)) {
				struct xfrm_sec_ctx *ctx = x->security;
				sel_sid = ctx->ctx_sid;
				break;
			}
421 422 423
		}
	}

424 425 426 427 428 429 430
	/*
	 * This check even when there's no association involved is
	 * intended, according to Trent Jaeger, to make sure a
	 * process can't engage in non-ipsec communication unless
	 * explicitly allowed by policy.
	 */

431 432
	rc = avc_has_perm(isec_sid, sel_sid, SECCLASS_ASSOCIATION,
			  ASSOCIATION__RECVFROM, ad);
433 434 435 436 437 438 439 440 441

	return rc;
}

/*
 * POSTROUTE_LAST hook's XFRM processing:
 * If we have no security association, then we need to determine
 * whether the socket is allowed to send to an unlabelled destination.
 * If we do have a authorizable security association, then it has already been
442
 * checked in the selinux_xfrm_state_pol_flow_match hook above.
443
 */
444
int selinux_xfrm_postroute_last(u32 isec_sid, struct sk_buff *skb,
445
					struct common_audit_data *ad, u8 proto)
446 447 448 449
{
	struct dst_entry *dst;
	int rc = 0;

E
Eric Dumazet 已提交
450
	dst = skb_dst(skb);
451 452 453 454

	if (dst) {
		struct dst_entry *dst_test;

455
		for (dst_test = dst; dst_test != NULL;
456 457 458 459
		     dst_test = dst_test->child) {
			struct xfrm_state *x = dst_test->xfrm;

			if (x && selinux_authorizable_xfrm(x))
460
				goto out;
461 462 463
		}
	}

464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484
	switch (proto) {
	case IPPROTO_AH:
	case IPPROTO_ESP:
	case IPPROTO_COMP:
		/*
		 * We should have already seen this packet once before
		 * it underwent xfrm(s). No need to subject it to the
		 * unlabeled check.
		 */
		goto out;
	default:
		break;
	}

	/*
	 * This check even when there's no association involved is
	 * intended, according to Trent Jaeger, to make sure a
	 * process can't engage in non-ipsec communication unless
	 * explicitly allowed by policy.
	 */

485
	rc = avc_has_perm(isec_sid, SECINITSID_UNLABELED, SECCLASS_ASSOCIATION,
486
			  ASSOCIATION__SENDTO, ad);
487 488
out:
	return rc;
489
}