selftests/bpf: Reject malformed IPv4/IPv6 skb test input
Subsystems (3)
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
@@ -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
[email protected]
AI reviewThis 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
Reported-by [email protected]Signed-off-by Sun Jian <[email protected]>
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?
---
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
Hi,
Syzbot has tested v2 and the reproducer did not trigger the issue.
Quoted 4 lines · Sun Jian
bpf_prog_test_run_skb()derivesskb->protocolfrom the Ethernet header througheth_type_trans(), but it does not verify that the provided linear input is long enough to contain the corresponding L3 base header.
Martin KaFai Lau
reviewerQuoted 29 lines · Sun Jian
bpf_prog_test_run_skb()derivesskb->protocolfrom the Ethernet header througheth_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_runhelpers such asbpf_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.
Reported-by [email protected]Signed-off-by Sun Jian <[email protected]>---
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
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
-0case htons (ETH_P_IP ): sk->sk_family =AF_INET ;base-commit:
cbfffcca2bf0622b601b7eaf477aa29035169184Signature · 1 line
2.43.0
Quoted 61 lines · Martin KaFai Lau
Quoted 29 lines · Sun Jian
bpf_prog_test_run_skb()derivesskb->protocolfrom the Ethernet header througheth_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_runhelpers such asbpf_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.
Reported-by [email protected]Signed-off-by Sun Jian <[email protected]>---
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
-0net/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.cneed to be changed also. imo, This can be bpf-next.pw-bot: cr
Quoted 7 lines
net/bpf/test_run.c +0
-0case 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
Martin KaFai Lau
reviewerQuoted 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.
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