Subject: Re: [syzbot] UBSAN: shift-out-of-bounds in xfrm_set_default

From: Pavel Skripkin <[email-masked]>
Date: Tue, 27 Jul 2021 17:43:18 +0300

On Tue, 27 Jul 2021 05:47:21 -0700
syzbot <[email-masked]> wrote:

> Hello,
> 
> syzbot found the following issue on:
> 
> HEAD commit:    90d856e71443 Add linux-next specific files for
> 20210723 git tree:       linux-next
> console output:
> https://syzkaller.appspot.com/x/log.txt?x=133fd00a300000 kernel
> config:  https://syzkaller.appspot.com/x/.config?x=298516715f6ad5cd
> dashboard link:
> https://syzkaller.appspot.com/bug?extid=9cd5837a045bbee5b810
> compiler:       gcc (Debian 10.2.1-6) 10.2.1 20210110, GNU ld (GNU
> Binutils for Debian) 2.35.1 syz repro:
> https://syzkaller.appspot.com/x/repro.syz?x=1263bba6300000 C
> reproducer:   https://syzkaller.appspot.com/x/repro.c?x=1066b4d4300000
> 
> IMPORTANT: if you fix the issue, please add the following tag to the
> commit: Reported-by:
> [email-masked]
> 
> netlink: 228 bytes leftover after parsing attributes in process
> `syz-executor669'.
> ================================================================================

The first thing that comes in mind is to check up->dirmask value

#syz test
git://git.kernel.org/pub/scm/linux/kernel/git/next/linux-next.git master 

With regards,
Pavel Skripkin

>From 30db223b1f724ca241c7fa15769d0c65eada3b66 Mon Sep 17 00:00:00 2001
From: Pavel Skripkin <[email-masked]>
Date: Tue, 27 Jul 2021 17:38:24 +0300
Subject: [PATCH] net: xfrm: fix shift-out-of-bounce

We need to check up->dirmask to avoid shift-out-of-bounce bug,
since up->dirmask comes from userspace.

Signed-off-by: Pavel Skripkin <[email-masked]>
---
 net/xfrm/xfrm_user.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/net/xfrm/xfrm_user.c b/net/xfrm/xfrm_user.c
index acc3a0dab331..5f3fe2295519 100644
--- a/net/xfrm/xfrm_user.c
+++ b/net/xfrm/xfrm_user.c
@@ -1966,9 +1966,14 @@ 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 = (1 << up->dirmask) & XFRM_POL_DEFAULT_MASK;
+	u8 dirmask;
 	u8 old_default = net->xfrm.policy_default;
 
+	if (up->dirmask >= sizeof(up->action) * 8)
+		return -EINVAL;
+
+	dirmask = (1 << up->dirmask) & XFRM_POL_DEFAULT_MASK
+
 	net->xfrm.policy_default = (old_default & (0xff ^ dirmask))
 				    | (up->action << up->dirmask);
 
-- 
2.32.0