Skip to contentSkip to conversation

selftests/bpf: Reject malformed IPv4/IPv6 skb test input

PATCH In review Sun Jian 3 participants 7 messages Mar 29 – Apr 2

Subsystems (3)
lore
Share & tools
lore

Sun Jian

author
#0 lore raw

PATCH v2 1/1 selftests/bpf: Reject malformed IPv4/IPv6 skb test input

bpf_prog_test_run_skb() derives skb->protocol from the Ethernet header through eth_type_trans(), but it does not verify that the provided linear input is long enough to contain the corresponding L3 base header.

This can result in an inconsistent skb being passed to test_run helpers such as bpf_skb_adjust_room(), where inferred protocol offsets can lead to operating on uninitialized memory, triggering KMSAN errors.

To reject such malformed test input, we check that the linear head is sufficiently large to contain the corresponding L3 base header (IPv4 or IPv6) before running the program.

---
v2:
- Ensured that the linear head is large enough to accommodate the corresponding L3 base header (IPv4 or IPv6), before running the program.

 net/bpf/test_run.c | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
net/bpf/test_run.c +19 -0
index 178c4738e63b..4790bee535b9 100644
@@ -1118,6 +1118,25 @@ int bpf_prog_test_run_skb(struct bpf_prog *prog, const union bpf_attr *kattr,
 	skb->protocol = eth_type_trans(skb, dev);
 	skb_reset_network_header(skb);
 
+	switch (skb->protocol) {
+	case htons(ETH_P_IP):
+		if (skb_headlen(skb) < sizeof(struct iphdr)) {
+			ret = -EINVAL;
+			goto out;
+		}
+		break;
+#if IS_ENABLED(CONFIG_IPV6)
+	case htons(ETH_P_IPV6):
+		if (skb_headlen(skb) < sizeof(struct ipv6hdr)) {
+			ret = -EINVAL;
+			goto out;
+		}
+		break;
+#endif
+	default:
+		break;
+	}
+
 	switch (skb->protocol) {
 	case htons(ETH_P_IP):
 		sk->sk_family = AF_INET;

base-commit: cbfffcca2bf0622b601b7eaf477aa29035169184

Signature · 1 line
2.43.0
#1 lore raw
This patch fixes a bug that has existed since bpf_prog_test_run_skb() was first introduced. Should it carry a Fixes: tag pointing at the original commit?
Quoted 3 lines

This patch fixes a bug that has existed since bpf_prog_test_run_skb() was first introduced. Should it carry a Fixes: tag pointing at the original commit?

Fixes 1cf1cae963c2 ("bpf: introduce BPF_PROG_TEST_RUN command")

---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md

CI run summary: https://github.com/kernel-patches/bpf/actions/runs/23713682616

author Tested-by [email protected] on v2 1/1
#2 lore raw
In reply to Sun Jian

Hi,

Syzbot has tested v2 and the reproducer did not trigger the issue.

Quoted 4 lines · Sun Jian

bpf_prog_test_run_skb() derives skb->protocol from the Ethernet header through eth_type_trans(), but it does not verify that the provided linear input is long enough to contain the corresponding L3 base header.

#3 lore raw
In reply to Sun Jian
Quoted 29 lines · Sun Jian

bpf_prog_test_run_skb() derives skb->protocol from the Ethernet header through eth_type_trans(), but it does not verify that the provided linear input is long enough to contain the corresponding L3 base header.

This can result in an inconsistent skb being passed to test_run helpers such as bpf_skb_adjust_room(), where inferred protocol offsets can lead to operating on uninitialized memory, triggering KMSAN errors.

To reject such malformed test input, we check that the linear head is sufficiently large to contain the corresponding L3 base header (IPv4 or IPv6) before running the program.

---
v2:
- Ensured that the linear head is large enough to accommodate the corresponding L3 base header (IPv4 or IPv6), before running the program.

 net/bpf/test_run.c | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
net/bpf/test_run.c +0 -0
index 178c4738e63b..4790bee535b9 100644

net/bpf/test_run.c int bpf_prog_test_run_skb(struct bpf_prog *prog, const union bpf_attr *kattr, · line 1121

Quoted 4 lines
net/bpf/test_run.c +1 -0
@@ -1118,6 +1118,25 @@ int bpf_prog_test_run_skb(struct bpf_prog *prog, const union bpf_attr *kattr,
 	skb->protocol = eth_type_trans(skb, dev);
 	skb_reset_network_header(skb);
 
+	switch (skb->protocol) {

The implementation is poor.

Quoted 9 lines
net/bpf/test_run.c +9 -0
+	case htons(ETH_P_IP):
+		if (skb_headlen(skb) < sizeof(struct iphdr)) {
+			ret = -EINVAL;
+			goto out;
+		}
+		break;
+#if IS_ENABLED(CONFIG_IPV6)
+	case htons(ETH_P_IPV6):
+		if (skb_headlen(skb) < sizeof(struct ipv6hdr)) {

net/bpf/test_run.c int bpf_prog_test_run_skb(struct bpf_prog *prog, const union bpf_attr *kattr, · line 1140

Quoted 10 lines
net/bpf/test_run.c +9 -0
+			ret = -EINVAL;
+			goto out;
+		}
+		break;
+#endif
+	default:
+		break;
+	}
+
 	switch (skb->protocol) {

There is an exact same switch and it has the same check on skb_headlen(skb).

A selftest is needed. Check if the tests in empty_skb.c need to be changed also. imo, This can be bpf-next.

pw-bot: cr

Quoted 7 lines
net/bpf/test_run.c +0 -0
 	case htons(ETH_P_IP):
 		sk->sk_family = AF_INET;

base-commit: cbfffcca2bf0622b601b7eaf477aa29035169184

Signature · 1 line
2.43.0
author
#4 lore raw
Quoted 61 lines · Martin KaFai Lau
Quoted 29 lines · Sun Jian

bpf_prog_test_run_skb() derives skb->protocol from the Ethernet header through eth_type_trans(), but it does not verify that the provided linear input is long enough to contain the corresponding L3 base header.

This can result in an inconsistent skb being passed to test_run helpers such as bpf_skb_adjust_room(), where inferred protocol offsets can lead to operating on uninitialized memory, triggering KMSAN errors.

To reject such malformed test input, we check that the linear head is sufficiently large to contain the corresponding L3 base header (IPv4 or IPv6) before running the program.

---
v2:
- Ensured that the linear head is large enough to accommodate the corresponding L3 base header (IPv4 or IPv6), before running the program.

 net/bpf/test_run.c | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
net/bpf/test_run.c +0 -0
index 178c4738e63b..4790bee535b9 100644

net/bpf/test_run.c int bpf_prog_test_run_skb(struct bpf_prog *prog, const union bpf_attr *kattr, · line 1121

Quoted 4 lines
net/bpf/test_run.c +1 -0
@@ -1118,6 +1118,25 @@ int bpf_prog_test_run_skb(struct bpf_prog *prog, const union bpf_attr *kattr,
      skb->protocol = eth_type_trans(skb, dev);
      skb_reset_network_header(skb);
 
+     switch (skb->protocol) {

The implementation is poor.

Quoted 9 lines
net/bpf/test_run.c +9 -0
+     case htons(ETH_P_IP):
+             if (skb_headlen(skb) < sizeof(struct iphdr)) {
+                     ret = -EINVAL;
+                     goto out;
+             }
+             break;
+#if IS_ENABLED(CONFIG_IPV6)
+     case htons(ETH_P_IPV6):
+             if (skb_headlen(skb) < sizeof(struct ipv6hdr)) {

net/bpf/test_run.c int bpf_prog_test_run_skb(struct bpf_prog *prog, const union bpf_attr *kattr, · line 1140

Quoted 10 lines
net/bpf/test_run.c +9 -0
+                     ret = -EINVAL;
+                     goto out;
+             }
+             break;
+#endif
+     default:
+             break;
+     }
+
      switch (skb->protocol) {

There is an exact same switch and it has the same check on skb_headlen(skb).

Quoted 12 lines

A selftest is needed. Check if the tests in empty_skb.c need to be changed also. imo, This can be bpf-next.

pw-bot: cr

Quoted 7 lines
net/bpf/test_run.c +0 -0
      case htons(ETH_P_IP):
              sk->sk_family = AF_INET;

base-commit: cbfffcca2bf0622b601b7eaf477aa29035169184 --
2.43.0

Ack, I'll respin a v2.

BTW, v1 was mainly meant as a minimal proof of the fix, so I kept the existing structure intact.

Sun Jian

#5 lore raw
Quoted 4 lines · sun jian

Ack, I'll respin a v2.

BTW, v1 was mainly meant as a minimal proof of the fix, so I kept the existing structure intact.

This is already v2.

The minimal proof of the fix is a selftest for a tricky case like this, instead of spamming the list, and now also the AI-review tokens, with an unlandable patch.

It is a few line change, and I don't see how duplicating the existing switch case makes the RFC review easier.

author
#6 lore raw
Quoted 7 lines · Martin KaFai Lau
Quoted 4 lines · sun jian

Ack, I'll respin a v2.

BTW, v1 was mainly meant as a minimal proof of the fix, so I kept the existing structure intact.

Quoted 8 lines

This is already v2.

The minimal proof of the fix is a selftest for a tricky case like this, instead of spamming the list, and now also the AI-review tokens, with an unlandable patch.

It is a few line change, and I don't see how duplicating the existing switch case makes the RFC review easier.

You‘re right, I just noticed that I mixed up the version number. I'll respin a v3 and include the selftest.

Sun Jian