Netdev List
 help / color / mirror / Atom feed
From: netdev-bot+sashiko@kernel.org
To: edumazet@google.com
Cc: davem@davemloft.net, kuba@kernel.org, pabeni@redhat.com,
	weiwan@google.com, horms@kernel.org, caixinchen1@huawei.com,
	netdev@vger.kernel.org, edumazet@kernel.org
Subject: Re: [PATCH net 1/2] net: restrict SO_RESERVE_MEM to TCP sockets and cap max value
Date: Fri, 25 Sep 2026 12:49:01 +0000	[thread overview]
Message-ID: <179034054135.2160803.17217974915706503423@kernel.org> (raw)
In-Reply-To: <20260924123602.1979090-2-edumazet@google.com>

Thank you for your contribution! Sashiko AI review found more severe
issue(s) in other patches of this series. This mail is only an FYI, in case
you want to address the nit picks below when you respin.

Low priority / nit picks for your consideration:

- [Low] getsockopt and setsockopt for SO_RESERVE_MEM now disagree on the
  valid range.

--

net: restrict SO_RESERVE_MEM to TCP sockets and cap max value

This patch limits sock_reserve_memory() to TCP sockets. UDP protects
sk_forward_alloc with a different lock, and udp_rmem_release() does not
honor the reserved memory. It also makes sk_setsockopt(SO_RESERVE_MEM)
reject values above INT_MAX >> 1, so that the page conversions in
sock_reserve_memory() cannot overflow.

Closes: https://lore.kernel.org/netdev/5a88421d-10ef-4fca-9acb-85a27a3c1173@huawei.com/

> diff --git a/net/core/sock.c b/net/core/sock.c
> index d23333bb4f3fa..cfc6700a62e6f 100644
> --- a/net/core/sock.c
> +++ b/net/core/sock.c

[ ... ]

> @@ -1661,7 +1661,7 @@ int sk_setsockopt(struct sock *sk, int level, int optname,
>  	{
>  		int delta;
>  
> -		if (val < 0) {
> +		if (val < 0 || val > INT_MAX >> 1) {

[Severity: Low]
Could this cap leave getsockopt and setsockopt disagreeing about the
valid range for SO_RESERVE_MEM?

The check is made on the raw byte count. sock_reserve_memory() then rounds
the request up to whole pages before it stores the value:

net/core/sock.c:sock_reserve_memory() {
    ...
	pages = sk_mem_pages(bytes);
    ...
	WRITE_ONCE(sk->sk_reserved_mem,
		   sk->sk_reserved_mem + (pages << PAGE_SHIFT));
    ...
}

If val is 0x3FFFFFFF, the largest value this check accepts, sk_mem_pages()
returns 0x40000 pages with 4K pages (0x4000 with 64K pages). So
sk_reserved_mem becomes 0x40000000. The same holds for any val within one
page below the cap.

sk_getsockopt() then reports that rounded value back to userspace:

	case SO_RESERVE_MEM:
		v.val = READ_ONCE(sk->sk_reserved_mem);

If that value (1073741824) is passed back to setsockopt(SO_RESERVE_MEM), as
a save/restore or checkpoint/restore flow would, it is now above
INT_MAX >> 1 and fails with -EINVAL. Before this patch, setting the value
returned by getsockopt always worked.

Nothing overflows here, since 0x40000000 still fits in an int. The only
effect is an unexpected -EINVAL on that round trip.

Would it make sense to apply the cap after page rounding? For example,
reject when PAGE_ALIGN(val) > INT_MAX >> 1, or state the cap in
page-aligned terms.

>  			ret = -EINVAL;
>  			break;
>  		}

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20260924123602.1979090-1-edumazet%40google.com

  parent reply	other threads:[~2026-09-25 12:49 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-24 12:36 [PATCH net 0/2] net: fix SO_RESERVE_MEM socket type restriction and value bounds Eric Dumazet
2026-09-24 12:36 ` [PATCH net 1/2] net: restrict SO_RESERVE_MEM to TCP sockets and cap max value Eric Dumazet
2026-09-24 17:32   ` Wei Wang
2026-09-25 12:49   ` netdev-bot+sashiko [this message]
2026-09-24 12:36 ` [PATCH net 2/2] selftests/net: add SO_RESERVE_MEM test Eric Dumazet
2026-09-25 12:49   ` netdev-bot+sashiko
2026-09-25 13:06     ` Eric Dumazet
2026-09-25 13:21       ` Eric Dumazet
2026-09-25 13:49     ` Matthieu Baerts

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=179034054135.2160803.17217974915706503423@kernel.org \
    --to=netdev-bot+sashiko@kernel.org \
    --cc=caixinchen1@huawei.com \
    --cc=davem@davemloft.net \
    --cc=edumazet@google.com \
    --cc=edumazet@kernel.org \
    --cc=horms@kernel.org \
    --cc=kuba@kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=weiwan@google.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox