mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH bpf] bpf: Reject negative optlen in cgroup getsockopt hook
@ 2026-07-26  7:01 Junseo Lim
  2026-07-29 21:36 ` Emil Tsalapatis
  0 siblings, 1 reply; 4+ messages in thread
From: Junseo Lim @ 2026-07-26  7:01 UTC (permalink / raw)
  To: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
	Eduard Zingerman, Kumar Kartikeya Dwivedi, Martin KaFai Lau
  Cc: Stanislav Fomichev, Song Liu, Yonghong Song, Jiri Olsa,
	Emil Tsalapatis, bpf, linux-kernel, Sechang Lim, 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.

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")
Signed-off-by: Junseo Lim <zirajs7@gmail.com>
---
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(-)

diff --git a/kernel/bpf/cgroup.c b/kernel/bpf/cgroup.c
index 4355ccb78a9c..c04a244fe2e6 100644
--- a/kernel/bpf/cgroup.c
+++ b/kernel/bpf/cgroup.c
@@ -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.
-- 
2.55.0


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH bpf] bpf: Reject negative optlen in cgroup getsockopt hook
  2026-07-26  7:01 [PATCH bpf] bpf: Reject negative optlen in cgroup getsockopt hook Junseo Lim
@ 2026-07-29 21:36 ` Emil Tsalapatis
  2026-08-01  8:26   ` 임준서
  0 siblings, 1 reply; 4+ messages in thread
From: Emil Tsalapatis @ 2026-07-29 21:36 UTC (permalink / raw)
  To: Junseo Lim, Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
	Eduard Zingerman, Kumar Kartikeya Dwivedi, Martin KaFai Lau
  Cc: Stanislav Fomichev, Song Liu, Yonghong Song, Jiri Olsa,
	Emil Tsalapatis, bpf, linux-kernel, Sechang Lim

On Sun Jul 26, 2026 at 3:01 AM EDT, Junseo Lim wrote:
> 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")
> Signed-off-by: Junseo Lim <zirajs7@gmail.com>

Reviewed-by: Emil Tsalapatis <emil@etsalapatis.com>

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

> ---
> 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(-)
>
> diff --git a/kernel/bpf/cgroup.c b/kernel/bpf/cgroup.c
> index 4355ccb78a9c..c04a244fe2e6 100644
> --- a/kernel/bpf/cgroup.c
> +++ b/kernel/bpf/cgroup.c
> @@ -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.


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH bpf] bpf: Reject negative optlen in cgroup getsockopt hook
  2026-07-29 21:36 ` Emil Tsalapatis
@ 2026-08-01  8:26   ` 임준서
  2026-08-05 19:01     ` Daniel Borkmann
  0 siblings, 1 reply; 4+ messages in thread
From: 임준서 @ 2026-08-01  8:26 UTC (permalink / raw)
  To: Emil Tsalapatis
  Cc: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
	Eduard Zingerman, Kumar Kartikeya Dwivedi, Martin KaFai Lau,
	Stanislav Fomichev, Song Liu, Yonghong Song, Jiri Olsa, bpf,
	linux-kernel, Sechang Lim

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?

On Thu, Jul 30, 2026 at 6:36 AM Emil Tsalapatis <emil@etsalapatis.com> wrote:
>
> On Sun Jul 26, 2026 at 3:01 AM EDT, Junseo Lim wrote:
> > 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")
> > Signed-off-by: Junseo Lim <zirajs7@gmail.com>
>
> Reviewed-by: Emil Tsalapatis <emil@etsalapatis.com>
>
> It'd be worth resending with a reproducer setting optlen to negative.
>
> > ---
> > 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(-)
> >
> > diff --git a/kernel/bpf/cgroup.c b/kernel/bpf/cgroup.c
> > index 4355ccb78a9c..c04a244fe2e6 100644
> > --- a/kernel/bpf/cgroup.c
> > +++ b/kernel/bpf/cgroup.c
> > @@ -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.
>

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH bpf] bpf: Reject negative optlen in cgroup getsockopt hook
  2026-08-01  8:26   ` 임준서
@ 2026-08-05 19:01     ` Daniel Borkmann
  0 siblings, 0 replies; 4+ messages in thread
From: Daniel Borkmann @ 2026-08-05 19:01 UTC (permalink / raw)
  To: 임준서, Emil Tsalapatis
  Cc: Alexei Starovoitov, Andrii Nakryiko, Eduard Zingerman,
	Kumar Kartikeya Dwivedi, Martin KaFai Lau, Stanislav Fomichev,
	Song Liu, Yonghong Song, Jiri Olsa, bpf, linux-kernel,
	Sechang Lim

On 8/1/26 10:26 AM, 임준서 wrote:
> 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?
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

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-08-05 19:01 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-07-26  7:01 [PATCH bpf] bpf: Reject negative optlen in cgroup getsockopt hook Junseo Lim
2026-07-29 21:36 ` Emil Tsalapatis
2026-08-01  8:26   ` 임준서
2026-08-05 19:01     ` Daniel Borkmann

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®