Skip to contentSkip to conversation

bpf: Reject negative optlen in cgroup getsockopt hook

PATCH Superseded Junseo Lim 3 participants 4 messages Jul 26 – Aug 5 BPF

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Junseo Lim

author
#0 lore raw

PATCH 1/1 bpf: Reject negative optlen in cgroup getsockopt hook

A cgroup getsockopt BPF program can shrink ctx->optlen after the kernel getsockopt handler has run. The kernel-buffer variant, used by TCP_ZEROCOPY_RECEIVE, only rejects values larger than the original length.

If BPF writes a negative optlen, that value is accepted and propagated back to the TCP getsockopt code. It can then be passed to copy_to_sockptr() as a size_t and trigger the hardened usercopy bytes > INT_MAX warning.

Reject negative ctx.optlen in __cgroup_bpf_run_filter_getsockopt_kern(), matching the lower-bound validation already present in the sockptr-based getsockopt hook.

Fixes 9cacf81f8161 ("bpf: Remove extra lock_sock for TCP_ZEROCOPY_RECEIVE")

---
Reproducer and warning:
https://gist.github.com/ZirAjs/a177ec6d8f2c8ed7d93edca6313a9255

Tested by building and booting the patched kernel. The reproducer returns -EFAULT and no longer triggers the hardened usercopy warning.

 kernel/bpf/cgroup.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/bpf/cgroup.c +1 -1
index 4355ccb78a9c..c04a244fe2e6 100644
@@ -2235,7 +2235,7 @@ int __cgroup_bpf_run_filter_getsockopt_kern(struct sock *sk, int level,
 	if (ret < 0)
 		return ret;
 
-	if (ctx.optlen > *optlen)
+	if (ctx.optlen > *optlen || ctx.optlen < 0)
 		return -EFAULT;
 
 	/* BPF programs can shrink the buffer, export the modifications.
Signature · 1 line
2.55.0
reviewer Reviewed-by on v1 1/1
#1 lore raw
Quoted 10 lines · Junseo Lim

A cgroup getsockopt BPF program can shrink ctx->optlen after the kernel getsockopt handler has run. The kernel-buffer variant, used by TCP_ZEROCOPY_RECEIVE, only rejects values larger than the original length.

If BPF writes a negative optlen, that value is accepted and propagated back to the TCP getsockopt code. It can then be passed to copy_to_sockptr() as a size_t and trigger the hardened usercopy bytes > INT_MAX warning.

Quoted 6 lines

Reject negative ctx.optlen in __cgroup_bpf_run_filter_getsockopt_kern(), matching the lower-bound validation already present in the sockptr-based getsockopt hook.

Fixes 9cacf81f8161 ("bpf: Remove extra lock_sock for TCP_ZEROCOPY_RECEIVE")

It'd be worth resending with a reproducer setting optlen to negative.

Quoted 23 lines

---
Reproducer and warning:
https://gist.github.com/ZirAjs/a177ec6d8f2c8ed7d93edca6313a9255

Tested by building and booting the patched kernel. The reproducer returns -EFAULT and no longer triggers the hardened usercopy warning.

 kernel/bpf/cgroup.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/bpf/cgroup.c +1 -1
index 4355ccb78a9c..c04a244fe2e6 100644
@@ -2235,7 +2235,7 @@ int __cgroup_bpf_run_filter_getsockopt_kern(struct sock *sk, int level,
 	if (ret < 0)
 		return ret;
 
-	if (ctx.optlen > *optlen)
+	if (ctx.optlen > *optlen || ctx.optlen < 0)
 		return -EFAULT;
 
 	/* BPF programs can shrink the buffer, export the modifications.
author
#2 lore raw

Thanks for the review.

The reproducer already sets ctx->optlen to a negative value (-11) in the cgroup BPF program:

/* ctx->optlen = -11; */ BPF_MOV64_IMM(BPF_REG_2, -11), BPF_STX_MEM(BPF_W, BPF_REG_1, BPF_REG_2,
offsetof(struct bpf_sockopt, optlen)),

https://gist.github.com/ZirAjs/a177ec6d8f2c8ed7d93edca6313a9255

The PASS/FAIL output only checks the behavior before and after applying the patch. Without the patch, the negative value reaches copy_to_sockptr() and triggers the warning. With the patch, getsockopt() rejects it with -EFAULT.

Would you prefer that I include the reproducer directly in the patch submission rather than only linking to the gist?

Quoted 47 lines · Emil Tsalapatis
Quoted 10 lines · Junseo Lim

A cgroup getsockopt BPF program can shrink ctx->optlen after the kernel getsockopt handler has run. The kernel-buffer variant, used by TCP_ZEROCOPY_RECEIVE, only rejects values larger than the original length.

If BPF writes a negative optlen, that value is accepted and propagated back to the TCP getsockopt code. It can then be passed to copy_to_sockptr() as a size_t and trigger the hardened usercopy bytes > INT_MAX warning.

Quoted 6 lines

Reject negative ctx.optlen in __cgroup_bpf_run_filter_getsockopt_kern(), matching the lower-bound validation already present in the sockptr-based getsockopt hook.

Fixes 9cacf81f8161 ("bpf: Remove extra lock_sock for TCP_ZEROCOPY_RECEIVE")

It'd be worth resending with a reproducer setting optlen to negative.

Quoted 23 lines

---
Reproducer and warning:
https://gist.github.com/ZirAjs/a177ec6d8f2c8ed7d93edca6313a9255

Tested by building and booting the patched kernel. The reproducer returns -EFAULT and no longer triggers the hardened usercopy warning.

 kernel/bpf/cgroup.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/bpf/cgroup.c +1 -1
index 4355ccb78a9c..c04a244fe2e6 100644
@@ -2235,7 +2235,7 @@ int __cgroup_bpf_run_filter_getsockopt_kern(struct sock *sk, int level,
      if (ret < 0)
              return ret;
 
-     if (ctx.optlen > *optlen)
+     if (ctx.optlen > *optlen || ctx.optlen < 0)
              return -EFAULT;
 
      /* BPF programs can shrink the buffer, export the modifications.
maintainer
#3 lore raw
Quoted 10 lines · 임준서

Thanks for the review.

The reproducer already sets ctx->optlen to a negative value (-11) in the cgroup BPF program:

/* ctx->optlen = -11; */ BPF_MOV64_IMM(BPF_REG_2, -11), BPF_STX_MEM(BPF_W, BPF_REG_1, BPF_REG_2,
offsetof(struct bpf_sockopt, optlen)),

Quoted 8 lines

https://gist.github.com/ZirAjs/a177ec6d8f2c8ed7d93edca6313a9255

The PASS/FAIL output only checks the behavior before and after applying the patch. Without the patch, the negative value reaches copy_to_sockptr() and triggers the warning. With the patch, getsockopt() rejects it with -EFAULT.

Would you prefer that I include the reproducer directly in the patch submission rather than only linking to the gist?

Yes, please add it as 2nd commit and check git log on how to integrate it into test_progs (tools/testing/selftests/bpf/) so it can run via CI.

Thanks,
Daniel