Skip to contentSkip to conversation

pull request (net): ipsec 2021-10-07

PATCH New Steffen Klassert 2 participants 7 messages Oct 7, 2021

Subsystems (4)
lore
Share & tools
lore

Steffen Klassert

author maintainer
#0 lore raw

1) Fix a sysbot reported shift-out-of-bounds in xfrm_get_default. From Pavel Skripkin.

2) Fix XFRM_MSG_MAPPING ABI breakage. The new XFRM_MSG_MAPPING messages were accidentally not paced at the end. Fix by Eugene Syromiatnikov.

3) Fix the uapi for the default policy, use explicit field and macros and make it accessible to userland.
From Nicolas Dichtel.

4) Fix a missing rcu lock in xfrm_notify_userpolicy(). From Nicolas Dichtel.

Please pull or let me know if there are problems.

Thanks!

The following changes since commit 626bf91a292e2035af5b9d9cce35c5c138dfe06d:

Merge tag 'net-5.15-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/netdev/net (2021-09-07 14:02:58 -0700)

are available in the Git repository at:

git://git.kernel.org/pub/scm/linux/kernel/git/klassert/ipsec.git master

for you to fetch changes up to 93ec1320b0170d7a207eda2d119c669b673401ed:

xfrm: fix rcu lock in xfrm_notify_userpolicy() (2021-09-23 10:11:12 +0200)

----------------------------------------------------------------
Eugene Syromiatnikov (1):
include/uapi/linux/xfrm.h: Fix XFRM_MSG_MAPPING ABI breakage

Nicolas Dichtel (3):
xfrm: make user policy API complete
xfrm: notify default policy on update
xfrm: fix rcu lock in xfrm_notify_userpolicy()

Pavel Skripkin (1):
net: xfrm: fix shift-out-of-bounds in xfrm_get_default

Steffen Klassert (1):
Merge branch 'xfrm: fix uapi for the default policy'

 include/uapi/linux/xfrm.h   | 15 ++++++----
 net/xfrm/xfrm_user.c        | 67 +++++++++++++++++++++++++++++++++++++--------
 security/selinux/nlmsgtab.c |  4 ++-
 3 files changed, 67 insertions(+), 19 deletions(-)
author maintainer
#1 lore raw

PATCH 1/5 net: xfrm: fix shift-out-of-bounds in xfrm_get_default

From: Pavel Skripkin <[email protected]>

Syzbot hit shift-out-of-bounds in xfrm_get_default. The problem was in missing validation check for user data.

up->dirmask comes from user-space, so we need to check if this value is less than XFRM_USERPOLICY_DIRMASK_MAX to avoid shift-out-of-bounds bugs.

Fixes 2d151d39073a ("xfrm: Add possibility to set the default to block if we have no policy")
Reported-and-tested-by [email protected]
---
 net/xfrm/xfrm_user.c | 5 +++++
 1 file changed, 5 insertions(+)
net/xfrm/xfrm_user.c +5 -0
index 03b66d154b2b..4719a6d54aa6 100644
@@ -2005,6 +2005,11 @@ static int xfrm_get_default(struct sk_buff *skb, struct nlmsghdr *nlh,
 		return -EMSGSIZE;
 	}
 
+	if (up->dirmask >= XFRM_USERPOLICY_DIRMASK_MAX) {
+		kfree_skb(r_skb);
+		return -EINVAL;
+	}
+
 	r_up = nlmsg_data(r_nlh);
 
 	r_up->action = ((net->xfrm.policy_default & (1 << up->dirmask)) >> up->dirmask);
Signature · 1 line
2.25.1
author maintainer
#2 lore raw

PATCH 2/5 include/uapi/linux/xfrm.h: Fix XFRM_MSG_MAPPING ABI breakage

In reply to Steffen Klassert

From: Eugene Syromiatnikov <[email protected]>

Commit 2d151d39073a ("xfrm: Add possibility to set the default to block if we have no policy") broke ABI by changing the value of the XFRM_MSG_MAPPING enum item, thus also evading the build-time check
in security/selinux/nlmsgtab.c:selinux_nlmsg_lookup for presence of proper security permission checks in nlmsg_xfrm_perms. Fix it by placing XFRM_MSG_SETDEFAULT/XFRM_MSG_GETDEFAULT to the end of the enum, right before __XFRM_MSG_MAX, and updating the nlmsg_xfrm_perms accordingly.

Fixes 2d151d39073a ("xfrm: Add possibility to set the default to block if we have no policy")

References: https://lore.kernel.org/netdev/[email protected]/

---
 include/uapi/linux/xfrm.h   | 6 +++---
 security/selinux/nlmsgtab.c | 4 +++-
 2 files changed, 6 insertions(+), 4 deletions(-)
include/uapi/linux/xfrm.h +3 -3
index b96c1ea7166d..26f456b1f33e 100644
@@ -213,13 +213,13 @@ enum {
 	XFRM_MSG_GETSPDINFO,
 #define XFRM_MSG_GETSPDINFO XFRM_MSG_GETSPDINFO
 
+	XFRM_MSG_MAPPING,
+#define XFRM_MSG_MAPPING XFRM_MSG_MAPPING
+
 	XFRM_MSG_SETDEFAULT,
 #define XFRM_MSG_SETDEFAULT XFRM_MSG_SETDEFAULT
 	XFRM_MSG_GETDEFAULT,
 #define XFRM_MSG_GETDEFAULT XFRM_MSG_GETDEFAULT
-
-	XFRM_MSG_MAPPING,
-#define XFRM_MSG_MAPPING XFRM_MSG_MAPPING
 	__XFRM_MSG_MAX
 };
 #define XFRM_MSG_MAX (__XFRM_MSG_MAX - 1)
security/selinux/nlmsgtab.c +3 -1
index d59276f48d4f..94ea2a8b2bb7 100644
@@ -126,6 +126,8 @@ static const struct nlmsg_perm nlmsg_xfrm_perms[] =
 	{ XFRM_MSG_NEWSPDINFO,	NETLINK_XFRM_SOCKET__NLMSG_WRITE },
 	{ XFRM_MSG_GETSPDINFO,	NETLINK_XFRM_SOCKET__NLMSG_READ  },
 	{ XFRM_MSG_MAPPING,	NETLINK_XFRM_SOCKET__NLMSG_READ  },
+	{ XFRM_MSG_SETDEFAULT,	NETLINK_XFRM_SOCKET__NLMSG_WRITE },
+	{ XFRM_MSG_GETDEFAULT,	NETLINK_XFRM_SOCKET__NLMSG_READ  },
 };
 
 static const struct nlmsg_perm nlmsg_audit_perms[] =
@@ -189,7 +191,7 @@ int selinux_nlmsg_lookup(u16 sclass, u16 nlmsg_type, u32 *perm)
 		 * structures at the top of this file with the new mappings
 		 * before updating the BUILD_BUG_ON() macro!
 		 */
-		BUILD_BUG_ON(XFRM_MSG_MAX != XFRM_MSG_MAPPING);
+		BUILD_BUG_ON(XFRM_MSG_MAX != XFRM_MSG_GETDEFAULT);
 		err = nlmsg_perm(nlmsg_type, perm, nlmsg_xfrm_perms,
 				 sizeof(nlmsg_xfrm_perms));
 		break;
Signature · 1 line
2.25.1
author maintainer
#3 lore raw

PATCH 3/5 xfrm: make user policy API complete

In reply to Steffen Klassert

From: Nicolas Dichtel <[email protected]>

Quoted 1 line

From a userland POV, this API was based on some magic values:

- dirmask and action were bitfields but meaning of bits (XFRM_POL_DEFAULT_*) are not exported;
- action is confusing, if a bit is set, does it mean drop or accept?

Let's try to simplify this uapi by using explicit field and macros.

Fixes 2d151d39073a ("xfrm: Add possibility to set the default to block if we have no policy")
---
 include/uapi/linux/xfrm.h |  9 ++++++---
 net/xfrm/xfrm_user.c      | 36 +++++++++++++++++++-----------------
 2 files changed, 25 insertions(+), 20 deletions(-)
include/uapi/linux/xfrm.h +6 -3
index 26f456b1f33e..eda0426ec4c2 100644
@@ -514,9 +514,12 @@ struct xfrm_user_offload {
 #define XFRM_OFFLOAD_INBOUND	2
 
 struct xfrm_userpolicy_default {
-#define XFRM_USERPOLICY_DIRMASK_MAX	(sizeof(__u8) * 8)
-	__u8				dirmask;
-	__u8				action;
+#define XFRM_USERPOLICY_UNSPEC	0
+#define XFRM_USERPOLICY_BLOCK	1
+#define XFRM_USERPOLICY_ACCEPT	2
+	__u8				in;
+	__u8				fwd;
+	__u8				out;
 };
 
 #ifndef __KERNEL__
net/xfrm/xfrm_user.c +19 -17
index 4719a6d54aa6..90c88390f1fe 100644
@@ -1966,16 +1966,21 @@ static int xfrm_set_default(struct sk_buff *skb, struct nlmsghdr *nlh,
 {
 	struct net *net = sock_net(skb->sk);
 	struct xfrm_userpolicy_default *up = nlmsg_data(nlh);
-	u8 dirmask;
-	u8 old_default = net->xfrm.policy_default;
 
-	if (up->dirmask >= XFRM_USERPOLICY_DIRMASK_MAX)
-		return -EINVAL;
+	if (up->in == XFRM_USERPOLICY_BLOCK)
+		net->xfrm.policy_default |= XFRM_POL_DEFAULT_IN;
+	else if (up->in == XFRM_USERPOLICY_ACCEPT)
+		net->xfrm.policy_default &= ~XFRM_POL_DEFAULT_IN;
 
-	dirmask = (1 << up->dirmask) & XFRM_POL_DEFAULT_MASK;
+	if (up->fwd == XFRM_USERPOLICY_BLOCK)
+		net->xfrm.policy_default |= XFRM_POL_DEFAULT_FWD;
+	else if (up->fwd == XFRM_USERPOLICY_ACCEPT)
+		net->xfrm.policy_default &= ~XFRM_POL_DEFAULT_FWD;
 
-	net->xfrm.policy_default = (old_default & (0xff ^ dirmask))
-				    | (up->action << up->dirmask);
+	if (up->out == XFRM_USERPOLICY_BLOCK)
+		net->xfrm.policy_default |= XFRM_POL_DEFAULT_OUT;
+	else if (up->out == XFRM_USERPOLICY_ACCEPT)
+		net->xfrm.policy_default &= ~XFRM_POL_DEFAULT_OUT;
 
 	rt_genid_bump_all(net);
 
@@ -1988,13 +1993,11 @@ static int xfrm_get_default(struct sk_buff *skb, struct nlmsghdr *nlh,
 	struct sk_buff *r_skb;
 	struct nlmsghdr *r_nlh;
 	struct net *net = sock_net(skb->sk);
-	struct xfrm_userpolicy_default *r_up, *up;
+	struct xfrm_userpolicy_default *r_up;
 	int len = NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_default));
 	u32 portid = NETLINK_CB(skb).portid;
 	u32 seq = nlh->nlmsg_seq;
 
-	up = nlmsg_data(nlh);
-
 	r_skb = nlmsg_new(len, GFP_ATOMIC);
 	if (!r_skb)
 		return -ENOMEM;
@@ -2005,15 +2008,14 @@ static int xfrm_get_default(struct sk_buff *skb, struct nlmsghdr *nlh,
 		return -EMSGSIZE;
 	}
 
-	if (up->dirmask >= XFRM_USERPOLICY_DIRMASK_MAX) {
-		kfree_skb(r_skb);
-		return -EINVAL;
-	}
-
 	r_up = nlmsg_data(r_nlh);
 
-	r_up->action = ((net->xfrm.policy_default & (1 << up->dirmask)) >> up->dirmask);
-	r_up->dirmask = up->dirmask;
+	r_up->in = net->xfrm.policy_default & XFRM_POL_DEFAULT_IN ?
+			XFRM_USERPOLICY_BLOCK : XFRM_USERPOLICY_ACCEPT;
+	r_up->fwd = net->xfrm.policy_default & XFRM_POL_DEFAULT_FWD ?
+			XFRM_USERPOLICY_BLOCK : XFRM_USERPOLICY_ACCEPT;
+	r_up->out = net->xfrm.policy_default & XFRM_POL_DEFAULT_OUT ?
+			XFRM_USERPOLICY_BLOCK : XFRM_USERPOLICY_ACCEPT;
 	nlmsg_end(r_skb, r_nlh);
 
 	return nlmsg_unicast(net->xfrm.nlsk, r_skb, portid);
Signature · 1 line
2.25.1
author maintainer
#4 lore raw

PATCH 4/5 xfrm: notify default policy on update

In reply to Steffen Klassert

From: Nicolas Dichtel <[email protected]>

This configuration knob is very sensible, it should be notified when changing.

Fixes 2d151d39073a ("xfrm: Add possibility to set the default to block if we have no policy")
---
 net/xfrm/xfrm_user.c | 31 +++++++++++++++++++++++++++++++
 1 file changed, 31 insertions(+)
net/xfrm/xfrm_user.c +31 -0
index 90c88390f1fe..0eba0c27c665 100644
@@ -1961,6 +1961,36 @@ static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
 	return skb;
 }
 
+static int xfrm_notify_userpolicy(struct net *net)
+{
+	struct xfrm_userpolicy_default *up;
+	int len = NLMSG_ALIGN(sizeof(*up));
+	struct nlmsghdr *nlh;
+	struct sk_buff *skb;
+
+	skb = nlmsg_new(len, GFP_ATOMIC);
+	if (skb == NULL)
+		return -ENOMEM;
+
+	nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_GETDEFAULT, sizeof(*up), 0);
+	if (nlh == NULL) {
+		kfree_skb(skb);
+		return -EMSGSIZE;
+	}
+
+	up = nlmsg_data(nlh);
+	up->in = net->xfrm.policy_default & XFRM_POL_DEFAULT_IN ?
+			XFRM_USERPOLICY_BLOCK : XFRM_USERPOLICY_ACCEPT;
+	up->fwd = net->xfrm.policy_default & XFRM_POL_DEFAULT_FWD ?
+			XFRM_USERPOLICY_BLOCK : XFRM_USERPOLICY_ACCEPT;
+	up->out = net->xfrm.policy_default & XFRM_POL_DEFAULT_OUT ?
+			XFRM_USERPOLICY_BLOCK : XFRM_USERPOLICY_ACCEPT;
+
+	nlmsg_end(skb, nlh);
+
+	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY);
+}
+
 static int xfrm_set_default(struct sk_buff *skb, struct nlmsghdr *nlh,
 			    struct nlattr **attrs)
 {
@@ -1984,6 +2014,7 @@ static int xfrm_set_default(struct sk_buff *skb, struct nlmsghdr *nlh,
 
 	rt_genid_bump_all(net);
 
+	xfrm_notify_userpolicy(net);
 	return 0;
 }
 
Signature · 1 line
2.25.1
author maintainer
#5 lore raw

PATCH 5/5 xfrm: fix rcu lock in xfrm_notify_userpolicy()

In reply to Steffen Klassert

From: Nicolas Dichtel <[email protected]>

As stated in the comment above xfrm_nlmsg_multicast(), rcu read lock must be held before calling this function.

Reported-by [email protected]
Fixes 703b94b93c19 ("xfrm: notify default policy on update")
---
 net/xfrm/xfrm_user.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
net/xfrm/xfrm_user.c +6 -1
index 0eba0c27c665..3a3cb09eec12 100644
@@ -1967,6 +1967,7 @@ static int xfrm_notify_userpolicy(struct net *net)
 	int len = NLMSG_ALIGN(sizeof(*up));
 	struct nlmsghdr *nlh;
 	struct sk_buff *skb;
+	int err;
 
 	skb = nlmsg_new(len, GFP_ATOMIC);
 	if (skb == NULL)
@@ -1988,7 +1989,11 @@ static int xfrm_notify_userpolicy(struct net *net)
 
 	nlmsg_end(skb, nlh);
 
-	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY);
+	rcu_read_lock();
+	err = xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY);
+	rcu_read_unlock();
+
+	return err;
 }
 
 static int xfrm_set_default(struct sk_buff *skb, struct nlmsghdr *nlh,
Signature · 1 line
2.25.1
#6 lore raw
In reply to Steffen Klassert
This pull request was applied to netdev/net.git (refs/heads/master) by Steffen Klassert <[email protected]>:

Hello:

This pull request was applied to netdev/net.git (refs/heads/master) by Steffen Klassert <[email protected]>:

Quoted 8 lines · On Thu, 7 Oct 2021 07:55:19 +0200 you wrote:

1) Fix a sysbot reported shift-out-of-bounds in xfrm_get_default. From Pavel Skripkin.

2) Fix XFRM_MSG_MAPPING ABI breakage. The new XFRM_MSG_MAPPING messages were accidentally not paced at the end. Fix by Eugene Syromiatnikov.

[...]

Here is the summary with links:
- pull request (net): ipsec 2021-10-07

    https://git.kernel.org/netdev/net/c/578f3932273f
  - [2/5] include/uapi/linux/xfrm.h: Fix XFRM_MSG_MAPPING ABI breakage
    https://git.kernel.org/netdev/net/c/844f7eaaed92
  - [3/5] xfrm: make user policy API complete
    https://git.kernel.org/netdev/net/c/f8d858e607b2
  - [4/5] xfrm: notify default policy on update
    https://git.kernel.org/netdev/net/c/88d0adb5f13b
  - [5/5] xfrm: fix rcu lock in xfrm_notify_userpolicy()
    https://git.kernel.org/netdev/net/c/93ec1320b017

You are awesome, thank you!
--
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html