Skip to contentSkip to conversation

Drop packets with invalid headers to prevent KMSAN infoleak

PATCH In review Daniel Yang 5 participants 10 messages Oct 19, 2024 – Oct 29, 2024

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Drop packets with invalid headers to prevent KMSAN infoleak

KMSAN detects uninitialized memory stored to memory by bpf_clone_redirect(). Adding a check to the transmission path to find malformed headers prevents this issue. Specifically, we check if the length of the data stored in skb is less than the minimum device header length. If so, drop the packet since the skb cannot contain a valid device header. Also check if mac_header_len(skb) is outside the range provided of valid device header lengths.

Testing this patch with syzbot removes the bug.

Fixes 88264981f208 ("Merge tag 'sched_ext-for-6.12' of git://git.kernel.org/pub/scm/linux/kernel/git/tj/sched_ext")
Reported-by [email protected]
---
 net/core/filter.c | 7 +++++++
 1 file changed, 7 insertions(+)
net/core/filter.c +7 -0
index cd3524cb3..92d8f2098 100644
@@ -2191,6 +2191,13 @@ static int __bpf_redirect_common(struct sk_buff *skb, struct net_device *dev,
 		return -ERANGE;
 	}
 
+	if (unlikely(skb->len < dev->min_header_len ||
+		     skb_mac_header_len(skb) < dev->min_header_len ||
+		     skb_mac_header_len(skb) > dev->hard_header_len)) {
+		kfree_skb(skb);
+		return -ERANGE;
+	}
+
 	bpf_push_mac_rcsum(skb);
 	return flags & BPF_F_INGRESS ?
 	       __bpf_rx_skb(dev, skb) : __bpf_tx_skb(dev, skb);
Signature · 1 line
2.39.2
#1 lore raw
Quoted 11 lines · Daniel Yang

KMSAN detects uninitialized memory stored to memory by bpf_clone_redirect(). Adding a check to the transmission path to find malformed headers prevents this issue. Specifically, we check if the length of the data stored in skb is less than the minimum device header length. If so, drop the packet since the skb cannot contain a valid device header. Also check if mac_header_len(skb) is outside the range provided of valid device header lengths.

Testing this patch with syzbot removes the bug.

Fixes 88264981f208 ("Merge tag 'sched_ext-for-6.12' of git://git.kernel.org/pub/scm/linux/kernel/git/tj/sched_ext")

I am pretty sure this is the wrong tag.

A test in selftests/bpf is needed to reproduce and better understand this. Only bpf_clone_redirect() is needed to reproduce or other bpf_skb_*() helpers calls are needed to reproduce?

Quoted 25 lines
---
  net/core/filter.c | 7 +++++++
  1 file changed, 7 insertions(+)
net/core/filter.c +7 -0
index cd3524cb3..92d8f2098 100644
@@ -2191,6 +2191,13 @@ static int __bpf_redirect_common(struct sk_buff *skb, struct net_device *dev,
  		return -ERANGE;
  	}
 
+	if (unlikely(skb->len < dev->min_header_len ||
+		     skb_mac_header_len(skb) < dev->min_header_len ||
+		     skb_mac_header_len(skb) > dev->hard_header_len)) {
+		kfree_skb(skb);
+		return -ERANGE;
+	}
+
  	bpf_push_mac_rcsum(skb);
  	return flags & BPF_F_INGRESS ?
  	       __bpf_rx_skb(dev, skb) : __bpf_tx_skb(dev, skb);
#2 lore raw
Quoted 1 line

A test in selftests/bpf is needed to reproduce and better understand this.

I don't know much about self tests but I've just been using the syzbot repro and #syz test at the link in the patch:
https://syzkaller.appspot.com/bug?extid=346474e3bf0b26bd3090. Testing the patch showed that the uninitialized memory was not getting written to memory.

Quoted 2 lines

Only bpf_clone_redirect() is needed to reproduce or other bpf_skb_*() helpers calls are needed to reproduce?

From what I can see in the crash report here:
https://syzkaller.appspot.com/text?tag=CrashReport&x=10ba3ca9980000,
only bpf_clone_redirect() is needed to trigger this issue. The issue seems to be that bpf_try_make_head_writable clones the skb and creates uninitialized memory but __bpf_tx_skb() gets called and the ethernet header never got written, resulting in the skb having a data section without a proper mac header. Current check:

if (unlikely(skb->mac_header >= skb->network_header || skb->len == 0)) {
**drop packet**
}

in __bpf_redirect_common() is insufficient since it only checks if the mac header is misordered or if the data length is 0. So, any packet with a malformed MAC header that is not 14 bytes but is not 0 doesn't get dropped. Adding bounds checks for mac header size should fix this. And from what I see in the syz test of this patch, it does.

Are there any possible unexpected issues that can be caused by this?

maintainer
#3 lore raw
Quoted 1 line · Daniel Yang

Are there any possible unexpected issues that can be caused by this?

This patch is apparently the cause of BPF self-tests failures:

test_empty_skb:FAIL:ret: veth ETH_HLEN+1 packet ingress [redirect_ingress] unexpected ret: veth ETH_HLEN+1 packet ingress [redirect_ingress]: actual -34 != expected 0 test_empty_skb:PASS:err: veth ETH_HLEN+1 packet ingress [redirect_egress] 0 nsec
test_empty_skb:FAIL:ret: veth ETH_HLEN+1 packet ingress [redirect_egress] unexpected ret: veth ETH_HLEN+1 packet ingress [redirect_egress]: actual -34 != expected 1

Before submitting an eventual next revision, please very that BPF self-tests are happy.

Thanks,

Paolo

#4 lore raw
In reply to Daniel Yang
Quoted 9 lines · Daniel Yang
Quoted 1 line

A test in selftests/bpf is needed to reproduce and better understand this.

I don't know much about self tests but I've just been using the syzbot repro and #syz test at the link in the patch:
https://syzkaller.appspot.com/bug?extid=346474e3bf0b26bd3090. Testing the patch showed that the uninitialized memory was not getting written to memory.

Quoted 2 lines

Only bpf_clone_redirect() is needed to reproduce or other bpf_skb_*() helpers calls are needed to reproduce?

If only bpf_clone_redirect() is needed, it should be simple to write a selftest to reproduce it. It also helps to catch future regression.

Please tag the next respin as "bpf" also.

Quoted 21 lines

From what I can see in the crash report here:
https://syzkaller.appspot.com/text?tag=CrashReport&x=10ba3ca9980000,
only bpf_clone_redirect() is needed to trigger this issue. The issue seems to be that bpf_try_make_head_writable clones the skb and creates uninitialized memory but __bpf_tx_skb() gets called and the ethernet header never got written, resulting in the skb having a data section without a proper mac header. Current check:

if (unlikely(skb->mac_header >= skb->network_header || skb->len == 0)) {
**drop packet**
}

in __bpf_redirect_common() is insufficient since it only checks if the mac header is misordered or if the data length is 0. So, any packet with a malformed MAC header that is not 14 bytes but is not 0 doesn't get dropped. Adding bounds checks for mac header size should fix this. And from what I see in the syz test of this patch, it does.

Are there any possible unexpected issues that can be caused by this?

#5 lore raw
Quoted 9 lines · Martin KaFai Lau
Quoted 7 lines · Daniel Yang
Quoted 1 line

A test in selftests/bpf is needed to reproduce and better understand this.

I don't know much about self tests but I've just been using the syzbot repro and #syz test at the link in the patch:
https://syzkaller.appspot.com/bug?extid=346474e3bf0b26bd3090. Testing the patch showed that the uninitialized memory was not getting written to memory.

Quoted 7 lines
Quoted 2 lines
Quoted 2 lines

Only bpf_clone_redirect() is needed to reproduce or other bpf_skb_*() helpers calls are needed to reproduce?

If only bpf_clone_redirect() is needed, it should be simple to write a selftest to reproduce it. It also helps to catch future regression.

Please tag the next respin as "bpf" also.

I have a problem. I can't seem to build the bpf kselftests for some reason. There is always a struct definition error:
In file included from progs/profiler1.c:5:
progs/profiler.inc.h:599:49: error: declaration of 'struct syscall_trace_enter' will not be visible outside of t]
599 | int tracepoint__syscalls__sys_enter_kill(struct
syscall_trace_enter* ctx)
| ^
progs/profiler.inc.h:604:15: error: incomplete definition of type 'struct syscall_trace_enter'
604 | int pid = ctx->args[0];
| ~~~^
progs/profiler.inc.h:599:49: note: forward declaration of 'struct syscall_trace_enter'
599 | int tracepoint__syscalls__sys_enter_kill(struct
syscall_trace_enter* ctx)
| ^
progs/profiler.inc.h:605:15: error: incomplete definition of type 'struct syscall_trace_enter'
605 | int sig = ctx->args[1];
| ~~~^
progs/profiler.inc.h:599:49: note: forward declaration of 'struct syscall_trace_enter'
599 | int tracepoint__syscalls__sys_enter_kill(struct
syscall_trace_enter* ctx)

I just run the following to build:

$ cd tools/testing/selftests/bpf/
$ make

I can't find anyone else encountering the same error.

reviewer
#6 lore raw
Quoted 41 lines · Daniel Yang
Quoted 8 lines · Martin KaFai Lau
Quoted 7 lines · Daniel Yang
Quoted 1 line

A test in selftests/bpf is needed to reproduce and better understand this.

I don't know much about self tests but I've just been using the syzbot repro and #syz test at the link in the patch:
https://syzkaller.appspot.com/bug?extid=346474e3bf0b26bd3090. Testing the patch showed that the uninitialized memory was not getting written to memory.

Quoted 6 lines
Quoted 2 lines
Quoted 2 lines

Only bpf_clone_redirect() is needed to reproduce or other bpf_skb_*() helpers calls are needed to reproduce?

If only bpf_clone_redirect() is needed, it should be simple to write a selftest to reproduce it. It also helps to catch future regression.

Please tag the next respin as "bpf" also.

I have a problem. I can't seem to build the bpf kselftests for some reason. There is always a struct definition error:
In file included from progs/profiler1.c:5:
progs/profiler.inc.h:599:49: error: declaration of 'struct syscall_trace_enter' will not be visible outside of t]
599 | int tracepoint__syscalls__sys_enter_kill(struct
syscall_trace_enter* ctx)
| ^
progs/profiler.inc.h:604:15: error: incomplete definition of type 'struct syscall_trace_enter'
604 | int pid = ctx->args[0];
| ~~~^
progs/profiler.inc.h:599:49: note: forward declaration of 'struct syscall_trace_enter'
599 | int tracepoint__syscalls__sys_enter_kill(struct
syscall_trace_enter* ctx)
| ^
progs/profiler.inc.h:605:15: error: incomplete definition of type 'struct syscall_trace_enter'
605 | int sig = ctx->args[1];
| ~~~^
progs/profiler.inc.h:599:49: note: forward declaration of 'struct syscall_trace_enter'
599 | int tracepoint__syscalls__sys_enter_kill(struct
syscall_trace_enter* ctx)

Quoted 3 lines

I just run the following to build:

$ cd tools/testing/selftests/bpf/
$ make

It might be due to your .config file.
The 'struct syscall_trace_enter' is defined in kernel/trace/trace.h, which is used in kernel/trace/trace_syscalls.c. Maybe your config does not have CONFIG_FTRACE_SYSCALLS?

Quoted 2 lines

I can't find anyone else encountering the same error.

#7 lore raw
Quoted 4 lines · Yonghong Song

It might be due to your .config file.
The 'struct syscall_trace_enter' is defined in kernel/trace/trace.h, which is used in kernel/trace/trace_syscalls.c. Maybe your config does not have CONFIG_FTRACE_SYSCALLS?

I did add it to my config but another error popped up after I tried to rerun the tests. I just ended up using the vmtest.sh script and got the tests working. Seems like there should be a template config file or something to make building locally more convenient. Anyways, thanks for the help.

- Daniel

#8 lore raw
In reply to Daniel Yang

From: Daniel Yang <[email protected]>
Date: Sat, 19 Oct 2024 00:11:39 -0700

Quoted 23 lines

KMSAN detects uninitialized memory stored to memory by bpf_clone_redirect(). Adding a check to the transmission path to find malformed headers prevents this issue. Specifically, we check if the length of the data stored in skb is less than the minimum device header length. If so, drop the packet since the skb cannot contain a valid device header. Also check if mac_header_len(skb) is outside the range provided of valid device header lengths.

Testing this patch with syzbot removes the bug.

Fixes 88264981f208 ("Merge tag 'sched_ext-for-6.12' of git://git.kernel.org/pub/scm/linux/kernel/git/tj/sched_ext")
Reported-by [email protected]
---
 net/core/filter.c | 7 +++++++
 1 file changed, 7 insertions(+)
net/core/filter.c +0 -0
index cd3524cb3..92d8f2098 100644

net/core/filter.c static int __bpf_redirect_common(struct sk_buff *skb, struct net_device *dev, · line 2199

Quoted 9 lines
net/core/filter.c +6 -0
@@ -2191,6 +2191,13 @@ static int __bpf_redirect_common(struct sk_buff *skb, struct net_device *dev,
 		return -ERANGE;
 	}
 
+	if (unlikely(skb->len < dev->min_header_len ||
+		     skb_mac_header_len(skb) < dev->min_header_len ||
+		     skb_mac_header_len(skb) > dev->hard_header_len)) {
+		kfree_skb(skb);
+		return -ERANGE;
+	}

I believe this should go under IS_ENABLED(CONFIG_KMSAN) or CONFIG_DEBUG_NET or so to not affect the regular configurations. Or does this fix some real bug?

net/core/filter.c static int __bpf_redirect_common(struct sk_buff *skb, struct net_device *dev, · line 2203

Quoted 4 lines
net/core/filter.c +1 -0
+
 	bpf_push_mac_rcsum(skb);
 	return flags & BPF_F_INGRESS ?
 	       __bpf_rx_skb(dev, skb) : __bpf_tx_skb(dev, skb);

Thanks,
Olek

#9 lore raw
Quoted 10 lines · Alexander Lobakin

net/core/filter.c static int __bpf_redirect_common(struct sk_buff *skb, struct net_device *dev, · line 2199

Quoted 6 lines
net/core/filter.c +6 -0
+     if (unlikely(skb->len < dev->min_header_len ||
+                  skb_mac_header_len(skb) < dev->min_header_len ||
+                  skb_mac_header_len(skb) > dev->hard_header_len)) {
+             kfree_skb(skb);
+             return -ERANGE;
+     }

I believe this should go under IS_ENABLED(CONFIG_KMSAN) or CONFIG_DEBUG_NET or so to not affect the regular configurations. Or does this fix some real bug?

Well in my opinion, an infoleak is still an infoleak. But, this would likely not get triggered as long as an skb with a properly initialized eth header is passed into the bpf_clone_redirect function. We could initialize the memory to 0 but the performance hit would be too much. If the bpf_clone_redirect() function cannot be called from user space with user-crafted skbs as input, I don't think this is really an issue and we can just put it under the macros to get rid of the syzbot error.

- Daniel