Skip to contentSkip to conversation

lsm: Check and handle error priority for socket_bind and socket_connect

PATCH In review Mickaël Salaün 4 participants 7 messages Mar 27, 2024 – Mar 29, 2024

Subsystems (5)
lore
Share & tools
lore

Mickaël Salaün

author maintainer
#0 lore raw

PATCH 1/2 lsm: Check and handle error priority for socket_bind and socket_connect

Because the security_socket_bind and the security_socket_bind hooks are called before the network stack, it is easy to introduce error code inconsistencies. Instead of adding new checks to current and future LSMs, let's fix the related hook instead. The new checks are already (partially) implemented by SELinux and Landlock, and it should not change user space behavior but improve error code consistency instead.

The first check is about the minimal sockaddr length according to the address family. This improves the security of the AF_INET and AF_INET6 sockaddr parsing for current and future LSMs.

The second check is about AF_UNSPEC. This fixes error priority for bind on PF_INET6 socket when SELinux (and potentially others) is enabled. Indeed, the IPv6 network stack first checks the sockaddr length (-EINVAL error) before checking the family (-EAFNOSUPPORT error). See commit bbf5a1d0e5d0 ("selinux: Fix error priority for bind with AF_UNSPEC on PF_INET6 socket").

The third check is about consistency between socket family and address family. Only AF_INET and AF_INET6 are tested (by Landlock tests), so no other protocols are checked for now.

These new checks should enable to simplify current LSM implementations, but we may want to first land this patch on all stable branches.

A following patch adds new tests improving AF_UNSPEC test coverage for Landlock.

Cc Alexey Kodanev <[email protected]>
Cc Eric Dumazet <[email protected]>
Cc Günther Noack <[email protected]>
Cc Ivanov Mikhail <[email protected]>
Cc Konstantin Meskhidze <[email protected]>
Cc Muhammad Usama Anjum <[email protected]>
Cc Paul Moore <[email protected]>
Cc Serge E. Hallyn <[email protected]>
Fixes 20510f2f4e2d ("security: Convert LSM into a static interface")
---
 security/security.c | 96 +++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 96 insertions(+)
security/security.c +96 -0
index 7e118858b545..64fe07a73b14 100644
@@ -28,7 +28,9 @@
 #include <linux/xattr.h>
 #include <linux/msg.h>
 #include <linux/overflow.h>
+#include <linux/in.h>
 #include <net/flow.h>
+#include <net/ipv6.h>
 
 /* How many LSMs were built into the kernel? */
 #define LSM_COUNT (__end_lsm_info - __start_lsm_info)
@@ -4415,6 +4417,82 @@ int security_socket_socketpair(struct socket *socka, struct socket *sockb)
 }
 EXPORT_SYMBOL(security_socket_socketpair);
 
+static int validate_inet_addr(struct socket *sock, struct sockaddr *address,
+			      int addrlen, bool bind)
+{
+	const int sock_family = sock->sk->sk_family;
+
+	/* Checks for minimal header length to safely read sa_family. */
+	if (addrlen < offsetofend(typeof(*address), sa_family))
+		return -EINVAL;
+
+	/* Only handle inet sockets for now. */
+	switch (sock_family) {
+	case PF_INET:
+	case PF_INET6:
+		break;
+	default:
+		return 0;
+	}
+
+	/* Checks minimal address length for inet sockets. */
+	switch (address->sa_family) {
+	case AF_UNSPEC: {
+		const struct sockaddr_in *sa_in;
+
+		/* Cf. inet_dgram_connect(), __inet_stream_connect() */
+		if (!bind)
+			return 0;
+
+		if (sock_family == PF_INET6) {
+			/* Length check from inet6_bind_sk() */
+			if (addrlen < SIN6_LEN_RFC2133)
+				return -EINVAL;
+
+			/* Family check from __inet6_bind() */
+			goto err_af;
+		}
+
+		/* Length check from inet_bind_sk() */
+		if (addrlen < sizeof(struct sockaddr_in))
+			return -EINVAL;
+
+		sa_in = (struct sockaddr_in *)address;
+		if (sa_in->sin_addr.s_addr != htonl(INADDR_ANY))
+			goto err_af;
+
+		return 0;
+	}
+	case AF_INET:
+		/* Length check from inet_bind_sk() */
+		if (addrlen < sizeof(struct sockaddr_in))
+			return -EINVAL;
+		break;
+	case AF_INET6:
+		/* Length check from inet6_bind_sk() */
+		if (addrlen < SIN6_LEN_RFC2133)
+			return -EINVAL;
+		break;
+	}
+
+	/*
+	 * Checks sa_family consistency to not wrongfully return -EACCES
+	 * instead of -EINVAL.  Valid sa_family changes are only (from AF_INET
+	 * or AF_INET6) to AF_UNSPEC.
+	 */
+	if (address->sa_family != sock_family)
+		return -EINVAL;
+
+	return 0;
+
+err_af:
+	/* SCTP services expect -EINVAL, others -EAFNOSUPPORT. */
+	if (sock->sk->sk_protocol == IPPROTO_SCTP)
+		return -EINVAL;
+
+	return -EAFNOSUPPORT;
+}
+
 /**
  * security_socket_bind() - Check if a socket bind operation is allowed
  * @sock: socket
@@ -4425,11 +4503,23 @@ EXPORT_SYMBOL(security_socket_socketpair);
  * and the socket @sock is bound to the address specified in the @address
  * parameter.
  *
+ * For security reasons and to get consistent error code whatever LSM are
+ * enabled, we first do the same sanity checks against sockaddr as the ones
+ * done by the network stack (executed after hook).  Currently only AF_UNSPEC,
+ * AF_INET, and AF_INET6 are handled.  Please add support for other family
+ * specificities when handled by an LSM.
+ *
  * Return: Returns 0 if permission is granted.
  */
 int security_socket_bind(struct socket *sock,
 			 struct sockaddr *address, int addrlen)
 {
+	int err;
+
+	err = validate_inet_addr(sock, address, addrlen, true);
+	if (err)
+		return err;
+
 	return call_int_hook(socket_bind, sock, address, addrlen);
 }
 
@@ -4447,6 +4537,12 @@ int security_socket_bind(struct socket *sock,
 int security_socket_connect(struct socket *sock,
 			    struct sockaddr *address, int addrlen)
 {
+	int err;
+
+	err = validate_inet_addr(sock, address, addrlen, false);
+	if (err)
+		return err;
+
 	return call_int_hook(socket_connect, sock, address, addrlen);
 }
 
Signature · 1 line
2.44.0
author maintainer
#1 lore raw

PATCH 2/2 selftests/landlock: Improve AF_UNSPEC tests

Test that an IPv6 socket requested to be binded with AF_UNSPEC returns -EAFNOSUPPORT if the sockaddr length is valid.

Cc Eric Dumazet <[email protected]>
Cc Günther Noack <[email protected]>
Cc Ivanov Mikhail <[email protected]>
Cc Konstantin Meskhidze <[email protected]>
Cc Paul Moore <[email protected]>
Cc Serge E. Hallyn <[email protected]>
---
 tools/testing/selftests/landlock/net_test.c | 37 +++++++++++++++++++++
 1 file changed, 37 insertions(+)
tools/testing/selftests/landlock/net_test.c +37 -0
index f21cfbbc3638..f15cf565f856 100644
@@ -673,6 +673,7 @@ TEST_F(protocol, bind_unspec)
 		.port = self->srv0.port,
 	};
 	int bind_fd, ret;
+	socklen_t inet_len, inet6_len, unix_len;
 
 	if (variant->sandbox == TCP_SANDBOX) {
 		const int ruleset_fd = landlock_create_ruleset(
@@ -736,12 +737,48 @@ TEST_F(protocol, bind_unspec)
 	if (variant->prot.domain == AF_INET) {
 		EXPECT_EQ(-EAFNOSUPPORT, ret);
 	} else {
+		/* The sockaddr length is less than SIN6_LEN_RFC2133. */
 		EXPECT_EQ(-EINVAL, ret)
 		{
 			TH_LOG("Wrong bind error: %s", strerror(errno));
 		}
 	}
 	EXPECT_EQ(0, close(bind_fd));
+
+	/* Stores the minimal sockaddr lengths per family. */
+	self->unspec_srv0.protocol.domain = AF_INET;
+	inet_len = get_addrlen(&self->unspec_srv0, true);
+	self->unspec_srv0.protocol.domain = AF_INET6;
+	inet6_len = get_addrlen(&self->unspec_srv0, true);
+	self->unspec_srv0.protocol.domain = AF_UNIX;
+	unix_len = get_addrlen(&self->unspec_srv0, true);
+	self->unspec_srv0.protocol.domain = AF_UNSPEC;
+
+	/* Checks bind with AF_UNSPEC and less than IPv4 sockaddr length. */
+	bind_fd = socket_variant(&self->srv0);
+	ASSERT_LE(0, bind_fd);
+	ret = bind_variant_addrlen(bind_fd, &self->unspec_srv0, inet_len - 1);
+	EXPECT_EQ(-EINVAL, ret);
+	EXPECT_EQ(0, close(bind_fd));
+
+	/* Checks bind with AF_UNSPEC and IPv6 sockaddr length. */
+	bind_fd = socket_variant(&self->srv0);
+	ASSERT_LE(0, bind_fd);
+	ret = bind_variant_addrlen(bind_fd, &self->unspec_srv0, inet6_len);
+	if (variant->prot.domain == AF_INET ||
+	    variant->prot.domain == AF_INET6) {
+		EXPECT_EQ(-EAFNOSUPPORT, ret);
+	} else {
+		EXPECT_EQ(-EINVAL, ret);
+	}
+	EXPECT_EQ(0, close(bind_fd));
+
+	/* Checks bind with AF_UNSPEC and unix sockaddr length. */
+	bind_fd = socket_variant(&self->srv0);
+	ASSERT_LE(0, bind_fd);
+	ret = bind_variant_addrlen(bind_fd, &self->unspec_srv0, unix_len);
+	EXPECT_EQ(-EINVAL, ret);
+	EXPECT_EQ(0, close(bind_fd));
 }
 
 TEST_F(protocol, connect_unspec)
Signature · 1 line
2.44.0
#2 lore raw
In reply to Mickaël Salaün
Quoted 67 lines · Mickaël Salaün

Because the security_socket_bind and the security_socket_bind hooks are called before the network stack, it is easy to introduce error code inconsistencies. Instead of adding new checks to current and future LSMs, let's fix the related hook instead. The new checks are already (partially) implemented by SELinux and Landlock, and it should not change user space behavior but improve error code consistency instead.

The first check is about the minimal sockaddr length according to the address family. This improves the security of the AF_INET and AF_INET6 sockaddr parsing for current and future LSMs.

The second check is about AF_UNSPEC. This fixes error priority for bind on PF_INET6 socket when SELinux (and potentially others) is enabled. Indeed, the IPv6 network stack first checks the sockaddr length (-EINVAL error) before checking the family (-EAFNOSUPPORT error). See commit bbf5a1d0e5d0 ("selinux: Fix error priority for bind with AF_UNSPEC on PF_INET6 socket").

The third check is about consistency between socket family and address family. Only AF_INET and AF_INET6 are tested (by Landlock tests), so no other protocols are checked for now.

These new checks should enable to simplify current LSM implementations, but we may want to first land this patch on all stable branches.

A following patch adds new tests improving AF_UNSPEC test coverage for Landlock.

Cc Alexey Kodanev <[email protected]>
Cc Eric Dumazet <[email protected]>
Cc Günther Noack <[email protected]>
Cc Ivanov Mikhail <[email protected]>
Cc Konstantin Meskhidze <[email protected]>
Cc Muhammad Usama Anjum <[email protected]>
Cc Paul Moore <[email protected]>
Cc Serge E. Hallyn <[email protected]>
Fixes 20510f2f4e2d ("security: Convert LSM into a static interface")
---
 security/security.c | 96 +++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 96 insertions(+)
security/security.c +9 -0
index 7e118858b545..64fe07a73b14 100644
@@ -28,7 +28,9 @@
 #include <linux/xattr.h>
 #include <linux/msg.h>
 #include <linux/overflow.h>
+#include <linux/in.h>
 #include <net/flow.h>
+#include <net/ipv6.h>
 
 /* How many LSMs were built into the kernel? */
 #define LSM_COUNT (__end_lsm_info - __start_lsm_info)
@@ -4415,6 +4417,82 @@ int security_socket_socketpair(struct socket *socka, struct socket *sockb)
 }
 EXPORT_SYMBOL(security_socket_socketpair);
 
+static int validate_inet_addr(struct socket *sock, struct sockaddr *address,
+			      int addrlen, bool bind)
+{
+	const int sock_family = sock->sk->sk_family;
+
+	/* Checks for minimal header length to safely read sa_family. */
+	if (addrlen < offsetofend(typeof(*address), sa_family))

security/security.c int security_socket_socketpair(struct socket *socka, struct socket *sockb) · line 4436

Quoted 10 lines
security/security.c +10 -0
+		return -EINVAL;
+
+	/* Only handle inet sockets for now. */
+	switch (sock_family) {
+	case PF_INET:
+	case PF_INET6:
+		break;
+	default:
+		return 0;
+	}

Seems like a clunky way to say:

	if (sock_family != PF_INET && sock_family != PF_INET6)
		return 0;
Quoted 87 lines
security/security.c +71 -0
+
+	/* Checks minimal address length for inet sockets. */
+	switch (address->sa_family) {
+	case AF_UNSPEC: {
+		const struct sockaddr_in *sa_in;
+
+		/* Cf. inet_dgram_connect(), __inet_stream_connect() */
+		if (!bind)
+			return 0;
+
+		if (sock_family == PF_INET6) {
+			/* Length check from inet6_bind_sk() */
+			if (addrlen < SIN6_LEN_RFC2133)
+				return -EINVAL;
+
+			/* Family check from __inet6_bind() */
+			goto err_af;
+		}
+
+		/* Length check from inet_bind_sk() */
+		if (addrlen < sizeof(struct sockaddr_in))
+			return -EINVAL;
+
+		sa_in = (struct sockaddr_in *)address;
+		if (sa_in->sin_addr.s_addr != htonl(INADDR_ANY))
+			goto err_af;
+
+		return 0;
+	}
+	case AF_INET:
+		/* Length check from inet_bind_sk() */
+		if (addrlen < sizeof(struct sockaddr_in))
+			return -EINVAL;
+		break;
+	case AF_INET6:
+		/* Length check from inet6_bind_sk() */
+		if (addrlen < SIN6_LEN_RFC2133)
+			return -EINVAL;
+		break;
+	}
+
+	/*
+	 * Checks sa_family consistency to not wrongfully return -EACCES
+	 * instead of -EINVAL.  Valid sa_family changes are only (from AF_INET
+	 * or AF_INET6) to AF_UNSPEC.
+	 */
+	if (address->sa_family != sock_family)
+		return -EINVAL;
+
+	return 0;
+
+err_af:
+	/* SCTP services expect -EINVAL, others -EAFNOSUPPORT. */
+	if (sock->sk->sk_protocol == IPPROTO_SCTP)
+		return -EINVAL;
+
+	return -EAFNOSUPPORT;
+}
+
 /**
  * security_socket_bind() - Check if a socket bind operation is allowed
  * @sock: socket
@@ -4425,11 +4503,23 @@ EXPORT_SYMBOL(security_socket_socketpair);
  * and the socket @sock is bound to the address specified in the @address
  * parameter.
  *
+ * For security reasons and to get consistent error code whatever LSM are
+ * enabled, we first do the same sanity checks against sockaddr as the ones
+ * done by the network stack (executed after hook).  Currently only AF_UNSPEC,
+ * AF_INET, and AF_INET6 are handled.  Please add support for other family
+ * specificities when handled by an LSM.
+ *
  * Return: Returns 0 if permission is granted.
  */
 int security_socket_bind(struct socket *sock,
 			 struct sockaddr *address, int addrlen)
 {
+	int err;
+
+	err = validate_inet_addr(sock, address, addrlen, true);
+	if (err)
+		return err;
+
 	return call_int_hook(socket_bind, sock, address, addrlen);
 }
 

security/security.c int security_socket_bind(struct socket *sock, · line 4544

Quoted 8 lines
security/security.c +5 -0
@@ -4447,6 +4537,12 @@ int security_socket_bind(struct socket *sock,
 int security_socket_connect(struct socket *sock,
 			    struct sockaddr *address, int addrlen)
 {
+	int err;
+
+	err = validate_inet_addr(sock, address, addrlen, false);
+	if (err)
+		return err;

The smack_socket_connect() code is among my ugliest. I don't think this would do any harm, but if you haven't run the Smack test suite you probably should.

Quoted 4 lines
security/security.c +1 -0
+
 	return call_int_hook(socket_connect, sock, address, addrlen);
 }
 
#3 lore raw
In reply to Mickaël Salaün
Quoted 6 lines · Mickaël Salaün

Because the security_socket_bind and the security_socket_bind hooks are called before the network stack, it is easy to introduce error code inconsistencies. Instead of adding new checks to current and future LSMs, let's fix the related hook instead. The new checks are already (partially) implemented by SELinux and Landlock, and it should not change user space behavior but improve error code consistency instead.

It would probably be better to allow the network stack to perform such checks before calling LSM hooks. This may lead to following improvements:

1. Fixing extra checks. In the current design, (address)checks are performed both in validate_inet_addr() function and in network stack methods.

2. The network stack can choose which error cases should not be hidden during the LSM access check, and which ones can be.

3. LSM will not be responsible for performing all necessary checks for every (necessary) protocol.

This may result in adding new method to socket->ops.

Quoted 170 lines

The first check is about the minimal sockaddr length according to the address family. This improves the security of the AF_INET and AF_INET6 sockaddr parsing for current and future LSMs.

The second check is about AF_UNSPEC. This fixes error priority for bind on PF_INET6 socket when SELinux (and potentially others) is enabled. Indeed, the IPv6 network stack first checks the sockaddr length (-EINVAL error) before checking the family (-EAFNOSUPPORT error). See commit bbf5a1d0e5d0 ("selinux: Fix error priority for bind with AF_UNSPEC on PF_INET6 socket").

The third check is about consistency between socket family and address family. Only AF_INET and AF_INET6 are tested (by Landlock tests), so no other protocols are checked for now.

These new checks should enable to simplify current LSM implementations, but we may want to first land this patch on all stable branches.

A following patch adds new tests improving AF_UNSPEC test coverage for Landlock.

Cc Alexey Kodanev <[email protected]>
Cc Eric Dumazet <[email protected]>
Cc Günther Noack <[email protected]>
Cc Ivanov Mikhail <[email protected]>
Cc Konstantin Meskhidze <[email protected]>
Cc Muhammad Usama Anjum <[email protected]>
Cc Paul Moore <[email protected]>
Cc Serge E. Hallyn <[email protected]>
Fixes 20510f2f4e2d ("security: Convert LSM into a static interface")
---
  security/security.c | 96 +++++++++++++++++++++++++++++++++++++++++++++
  1 file changed, 96 insertions(+)
security/security.c +96 -0
index 7e118858b545..64fe07a73b14 100644
@@ -28,7 +28,9 @@
  #include <linux/xattr.h>
  #include <linux/msg.h>
  #include <linux/overflow.h>
+#include <linux/in.h>
  #include <net/flow.h>
+#include <net/ipv6.h>
 
  /* How many LSMs were built into the kernel? */
  #define LSM_COUNT (__end_lsm_info - __start_lsm_info)
@@ -4415,6 +4417,82 @@ int security_socket_socketpair(struct socket *socka, struct socket *sockb)
  }
  EXPORT_SYMBOL(security_socket_socketpair);
 
+static int validate_inet_addr(struct socket *sock, struct sockaddr *address,
+			      int addrlen, bool bind)
+{
+	const int sock_family = sock->sk->sk_family;
+
+	/* Checks for minimal header length to safely read sa_family. */
+	if (addrlen < offsetofend(typeof(*address), sa_family))
+		return -EINVAL;
+
+	/* Only handle inet sockets for now. */
+	switch (sock_family) {
+	case PF_INET:
+	case PF_INET6:
+		break;
+	default:
+		return 0;
+	}
+
+	/* Checks minimal address length for inet sockets. */
+	switch (address->sa_family) {
+	case AF_UNSPEC: {
+		const struct sockaddr_in *sa_in;
+
+		/* Cf. inet_dgram_connect(), __inet_stream_connect() */
+		if (!bind)
+			return 0;
+
+		if (sock_family == PF_INET6) {
+			/* Length check from inet6_bind_sk() */
+			if (addrlen < SIN6_LEN_RFC2133)
+				return -EINVAL;
+
+			/* Family check from __inet6_bind() */
+			goto err_af;
+		}
+
+		/* Length check from inet_bind_sk() */
+		if (addrlen < sizeof(struct sockaddr_in))
+			return -EINVAL;
+
+		sa_in = (struct sockaddr_in *)address;
+		if (sa_in->sin_addr.s_addr != htonl(INADDR_ANY))
+			goto err_af;
+
+		return 0;
+	}
+	case AF_INET:
+		/* Length check from inet_bind_sk() */
+		if (addrlen < sizeof(struct sockaddr_in))
+			return -EINVAL;
+		break;
+	case AF_INET6:
+		/* Length check from inet6_bind_sk() */
+		if (addrlen < SIN6_LEN_RFC2133)
+			return -EINVAL;
+		break;
+	}
+
+	/*
+	 * Checks sa_family consistency to not wrongfully return -EACCES
+	 * instead of -EINVAL.  Valid sa_family changes are only (from AF_INET
+	 * or AF_INET6) to AF_UNSPEC.
+	 */
+	if (address->sa_family != sock_family)
+		return -EINVAL;
+
+	return 0;
+
+err_af:
+	/* SCTP services expect -EINVAL, others -EAFNOSUPPORT. */
+	if (sock->sk->sk_protocol == IPPROTO_SCTP)
+		return -EINVAL;
+
+	return -EAFNOSUPPORT;
+}
+
  /**
   * security_socket_bind() - Check if a socket bind operation is allowed
   * @sock: socket
@@ -4425,11 +4503,23 @@ EXPORT_SYMBOL(security_socket_socketpair);
   * and the socket @sock is bound to the address specified in the @address
   * parameter.
   *
+ * For security reasons and to get consistent error code whatever LSM are
+ * enabled, we first do the same sanity checks against sockaddr as the ones
+ * done by the network stack (executed after hook).  Currently only AF_UNSPEC,
+ * AF_INET, and AF_INET6 are handled.  Please add support for other family
+ * specificities when handled by an LSM.
+ *
   * Return: Returns 0 if permission is granted.
   */
  int security_socket_bind(struct socket *sock,
  			 struct sockaddr *address, int addrlen)
  {
+	int err;
+
+	err = validate_inet_addr(sock, address, addrlen, true);
+	if (err)
+		return err;
+
  	return call_int_hook(socket_bind, sock, address, addrlen);
  }
 
@@ -4447,6 +4537,12 @@ int security_socket_bind(struct socket *sock,
  int security_socket_connect(struct socket *sock,
  			    struct sockaddr *address, int addrlen)
  {
+	int err;
+
+	err = validate_inet_addr(sock, address, addrlen, false);
+	if (err)
+		return err;
+
  	return call_int_hook(socket_connect, sock, address, addrlen);
  }
 
maintainer
#4 lore raw
Quoted 10 lines · Ivanov Mikhail
Quoted 6 lines · Mickaël Salaün

Because the security_socket_bind and the security_socket_bind hooks are called before the network stack, it is easy to introduce error code inconsistencies. Instead of adding new checks to current and future LSMs, let's fix the related hook instead. The new checks are already (partially) implemented by SELinux and Landlock, and it should not change user space behavior but improve error code consistency instead.

It would probably be better to allow the network stack to perform such checks before calling LSM hooks. This may lead to following improvements:

...

Quoted 1 line

This may result in adding new method to socket->ops.

I don't think there is a "may result" here, it will *require* a new socket::proto_ops function (addr_validate?). If you want to pursue this with the netdev folks to see if they would be willing to adopt such an approach I think that would be a great idea. Just be warned, there have been difficulties in the past when trying to get any sort of LSM accommodations from the netdev folks.

Signature · 1 line
paul-moore.com
maintainer
#5 lore raw
Quoted 16 lines · Paul Moore
Quoted 10 lines · Ivanov Mikhail
Quoted 6 lines · Mickaël Salaün

Because the security_socket_bind and the security_socket_bind hooks are called before the network stack, it is easy to introduce error code inconsistencies. Instead of adding new checks to current and future LSMs, let's fix the related hook instead. The new checks are already (partially) implemented by SELinux and Landlock, and it should not change user space behavior but improve error code consistency instead.

It would probably be better to allow the network stack to perform such checks before calling LSM hooks. This may lead to following improvements:

...

Quoted 8 lines
Quoted 1 line

This may result in adding new method to socket->ops.

I don't think there is a "may result" here, it will *require* a new socket::proto_ops function (addr_validate?). If you want to pursue this with the netdev folks to see if they would be willing to adopt such an approach I think that would be a great idea. Just be warned, there have been difficulties in the past when trying to get any sort of LSM accommodations from the netdev folks.

[Dropping [email protected] due to email errors (unknown recipient)]

I'm looking at the possibility of doing the above and this is slowly coming back to me as to why we haven't seriously tried this in the past. It's not as simple as calling one level down into a proto_ops function table, the proto_ops function table would then need to jump down into an associated proto function table. Granted we aren't talking per-packet overhead, but I can see this having a measurable impact on connections/second benchmarks which likely isn't going to be a welcome change.

Signature · 1 line
paul-moore.com
maintainer
#6 lore raw
In reply to Mickaël Salaün
Quoted 19 lines · Mickaël Salaün

Because the security_socket_bind and the security_socket_bind hooks are called before the network stack, it is easy to introduce error code inconsistencies. Instead of adding new checks to current and future LSMs, let's fix the related hook instead. The new checks are already (partially) implemented by SELinux and Landlock, and it should not change user space behavior but improve error code consistency instead.

The first check is about the minimal sockaddr length according to the address family. This improves the security of the AF_INET and AF_INET6 sockaddr parsing for current and future LSMs.

The second check is about AF_UNSPEC. This fixes error priority for bind on PF_INET6 socket when SELinux (and potentially others) is enabled. Indeed, the IPv6 network stack first checks the sockaddr length (-EINVAL error) before checking the family (-EAFNOSUPPORT error). See commit bbf5a1d0e5d0 ("selinux: Fix error priority for bind with AF_UNSPEC on PF_INET6 socket").

Quoted 6 lines

The third check is about consistency between socket family and address family. Only AF_INET and AF_INET6 are tested (by Landlock tests), so no other protocols are checked for now.

These new checks should enable to simplify current LSM implementations, but we may want to first land this patch on all stable branches.

[Dropping Alexey Kodanev due to email problems]

This isn't something I would want to see backported to the various stable trees, this is a consolidation and cleanup for future work, not really a bugfix. If an individual LSM is currently missing an address sanity check that should be resolved with a targeted patch that can be safely backported without affecting other LSMs.

Now, all that doesn't mean I don't think this is a good idea. Assuming we can't get the network stack to validate addresses before calling into these LSM hooks, I think this is an improvement over the current approach. I would like to see the patchset include individual patches which do the desired adjustments to the Smack, TOMOYO, AppArmor, Landlock, and SELinux code now that the sanity checks have migrated to the LSM layer. I expect that to be fairly straightforward, but given all the corner cases I want to make sure all the individual LSMs are okay with the changes.

Quoted 16 lines

A following patch adds new tests improving AF_UNSPEC test coverage for Landlock.

Cc Alexey Kodanev <[email protected]>
Cc Eric Dumazet <[email protected]>
Cc Günther Noack <[email protected]>
Cc Ivanov Mikhail <[email protected]>
Cc Konstantin Meskhidze <[email protected]>
Cc Muhammad Usama Anjum <[email protected]>
Cc Paul Moore <[email protected]>
Cc Serge E. Hallyn <[email protected]>
Fixes 20510f2f4e2d ("security: Convert LSM into a static interface")
---
 security/security.c | 96 +++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 96 insertions(+)
Signature · 1 line
paul-moore.com