Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit d28d1e08 authored by Trent Jaeger's avatar Trent Jaeger Committed by David S. Miller
Browse files

[LSM-IPSec]: Per-packet access control.



This patch series implements per packet access control via the
extension of the Linux Security Modules (LSM) interface by hooks in
the XFRM and pfkey subsystems that leverage IPSec security
associations to label packets.  Extensions to the SELinux LSM are
included that leverage the patch for this purpose.

This patch implements the changes necessary to the SELinux LSM to
create, deallocate, and use security contexts for policies
(xfrm_policy) and security associations (xfrm_state) that enable
control of a socket's ability to send and receive packets.

Patch purpose:

The patch is designed to enable the SELinux LSM to implement access
control on individual packets based on the strongly authenticated
IPSec security association.  Such access controls augment the existing
ones in SELinux based on network interface and IP address.  The former
are very coarse-grained, and the latter can be spoofed.  By using
IPSec, the SELinux can control access to remote hosts based on
cryptographic keys generated using the IPSec mechanism.  This enables
access control on a per-machine basis or per-application if the remote
machine is running the same mechanism and trusted to enforce the
access control policy.

Patch design approach:

The patch's main function is to authorize a socket's access to a IPSec
policy based on their security contexts.  Since the communication is
implemented by a security association, the patch ensures that the
security association's negotiated and used have the same security
context.  The patch enables allocation and deallocation of such
security contexts for policies and security associations.  It also
enables copying of the security context when policies are cloned.
Lastly, the patch ensures that packets that are sent without using a
IPSec security assocation with a security context are allowed to be
sent in that manner.

A presentation available at
www.selinux-symposium.org/2005/presentations/session2/2-3-jaeger.pdf
from the SELinux symposium describes the overall approach.

Patch implementation details:

The function which authorizes a socket to perform a requested
operation (send/receive) on a IPSec policy (xfrm_policy) is
selinux_xfrm_policy_lookup.  The Netfilter and rcv_skb hooks ensure
that if a IPSec SA with a securit y association has not been used,
then the socket is allowed to send or receive the packet,
respectively.

The patch implements SELinux function for allocating security contexts
when policies (xfrm_policy) are created via the pfkey or xfrm_user
interfaces via selinux_xfrm_policy_alloc.  When a security association
is built, SELinux allocates the security context designated by the
XFRM subsystem which is based on that of the authorized policy via
selinux_xfrm_state_alloc.

When a xfrm_policy is cloned, the security context of that policy, if
any, is copied to the clone via selinux_xfrm_policy_clone.

When a xfrm_policy or xfrm_state is freed, its security context, if
any is also freed at selinux_xfrm_policy_free or
selinux_xfrm_state_free.

Testing:

The SELinux authorization function is tested using ipsec-tools.  We
created policies and security associations with particular security
contexts and added SELinux access control policy entries to verify the
authorization decision.  We also made sure that packets for which no
security context was supplied (which either did or did not use
security associations) were authorized using an unlabelled context.

Signed-off-by: default avatarTrent Jaeger <tjaeger@cse.psu.edu>
Signed-off-by: default avatarHerbert Xu <herbert@gondor.apana.org.au>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parent df71837d
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -8,5 +8,7 @@ selinux-y := avc.o hooks.o selinuxfs.o netlink.o nlmsgtab.o

selinux-$(CONFIG_SECURITY_NETWORK) += netif.o

selinux-$(CONFIG_SECURITY_NETWORK_XFRM) += xfrm.o

EXTRA_CFLAGS += -Isecurity/selinux/include
+39 −0
Original line number Diff line number Diff line
@@ -73,6 +73,7 @@
#include "avc.h"
#include "objsec.h"
#include "netif.h"
#include "xfrm.h"

#define XATTR_SELINUX_SUFFIX "selinux"
#define XATTR_NAME_SELINUX XATTR_SECURITY_PREFIX XATTR_SELINUX_SUFFIX
@@ -3349,6 +3350,10 @@ static int selinux_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
		err = avc_has_perm(sock_sid, port_sid,
				   sock_class, recv_perm, &ad);
	}

	if (!err)
		err = selinux_xfrm_sock_rcv_skb(sock_sid, skb);

out:	
	return err;
}
@@ -3401,6 +3406,24 @@ static void selinux_sk_free_security(struct sock *sk)
	sk_free_security(sk);
}

static unsigned int selinux_sk_getsid_security(struct sock *sk, struct flowi *fl, u8 dir)
{
	struct inode_security_struct *isec;
	u32 sock_sid = SECINITSID_ANY_SOCKET;

	if (!sk)
		return selinux_no_sk_sid(fl);

	read_lock_bh(&sk->sk_callback_lock);
	isec = get_sock_isec(sk);

	if (isec)
		sock_sid = isec->sid;

	read_unlock_bh(&sk->sk_callback_lock);
	return sock_sid;
}

static int selinux_nlmsg_perm(struct sock *sk, struct sk_buff *skb)
{
	int err = 0;
@@ -3536,6 +3559,11 @@ static unsigned int selinux_ip_postroute_last(unsigned int hooknum,
		                   send_perm, &ad) ? NF_DROP : NF_ACCEPT;
	}

	if (err != NF_ACCEPT)
		goto out;

	err = selinux_xfrm_postroute_last(isec->sid, skb);

out:
	return err;
}
@@ -4380,6 +4408,16 @@ static struct security_operations selinux_ops = {
	.socket_getpeersec =		selinux_socket_getpeersec,
	.sk_alloc_security =		selinux_sk_alloc_security,
	.sk_free_security =		selinux_sk_free_security,
	.sk_getsid = 			selinux_sk_getsid_security,
#endif

#ifdef CONFIG_SECURITY_NETWORK_XFRM
	.xfrm_policy_alloc_security =	selinux_xfrm_policy_alloc,
	.xfrm_policy_clone_security =	selinux_xfrm_policy_clone,
	.xfrm_policy_free_security =	selinux_xfrm_policy_free,
	.xfrm_state_alloc_security =	selinux_xfrm_state_alloc,
	.xfrm_state_free_security =	selinux_xfrm_state_free,
	.xfrm_policy_lookup = 		selinux_xfrm_policy_lookup,
#endif
};

@@ -4491,6 +4529,7 @@ static int __init selinux_nf_ip_init(void)
		panic("SELinux: nf_register_hook for IPv6: error %d\n", err);

#endif	/* IPV6 */

out:
	return err;
}
+2 −0
Original line number Diff line number Diff line
@@ -238,3 +238,5 @@
   S_(SECCLASS_NSCD, NSCD__SHMEMHOST, "shmemhost")
   S_(SECCLASS_ASSOCIATION, ASSOCIATION__SENDTO, "sendto")
   S_(SECCLASS_ASSOCIATION, ASSOCIATION__RECVFROM, "recvfrom")
   S_(SECCLASS_ASSOCIATION, ASSOCIATION__RELABELFROM, "relabelfrom")
   S_(SECCLASS_ASSOCIATION, ASSOCIATION__RELABELTO, "relabelto")
+2 −0
Original line number Diff line number Diff line
@@ -908,6 +908,8 @@

#define ASSOCIATION__SENDTO                       0x00000001UL
#define ASSOCIATION__RECVFROM                     0x00000002UL
#define ASSOCIATION__RELABELFROM                  0x00000004UL
#define ASSOCIATION__RELABELTO                    0x00000008UL

#define NETLINK_KOBJECT_UEVENT_SOCKET__IOCTL      0x00000001UL
#define NETLINK_KOBJECT_UEVENT_SOCKET__READ       0x00000002UL
+54 −0
Original line number Diff line number Diff line
/*
 * SELinux support for the XFRM LSM hooks
 *
 * Author : Trent Jaeger, <jaegert@us.ibm.com>
 */
#ifndef _SELINUX_XFRM_H_
#define _SELINUX_XFRM_H_

int selinux_xfrm_policy_alloc(struct xfrm_policy *xp, struct xfrm_user_sec_ctx *sec_ctx);
int selinux_xfrm_policy_clone(struct xfrm_policy *old, struct xfrm_policy *new);
void selinux_xfrm_policy_free(struct xfrm_policy *xp);
int selinux_xfrm_state_alloc(struct xfrm_state *x, struct xfrm_user_sec_ctx *sec_ctx);
void selinux_xfrm_state_free(struct xfrm_state *x);
int selinux_xfrm_policy_lookup(struct xfrm_policy *xp, u32 sk_sid, u8 dir);

/*
 * Extract the security blob from the sock (it's actually on the socket)
 */
static inline struct inode_security_struct *get_sock_isec(struct sock *sk)
{
	if (!sk->sk_socket)
		return NULL;

	return SOCK_INODE(sk->sk_socket)->i_security;
}


static inline u32 selinux_no_sk_sid(struct flowi *fl)
{
	/* NOTE: no sock occurs on ICMP reply, forwards, ... */
	/* icmp_reply: authorize as kernel packet */
	if (fl && fl->proto == IPPROTO_ICMP) {
		return SECINITSID_KERNEL;
	}

	return SECINITSID_ANY_SOCKET;
}

#ifdef CONFIG_SECURITY_NETWORK_XFRM
int selinux_xfrm_sock_rcv_skb(u32 sid, struct sk_buff *skb);
int selinux_xfrm_postroute_last(u32 isec_sid, struct sk_buff *skb);
#else
static inline int selinux_xfrm_sock_rcv_skb(u32 isec_sid, struct sk_buff *skb)
{
	return 0;
}

static inline int selinux_xfrm_postroute_last(u32 isec_sid, struct sk_buff *skb)
{
	return NF_ACCEPT;
}
#endif

#endif /* _SELINUX_XFRM_H_ */
Loading