Netdev List
 help / color / mirror / Atom feed
From: Ido Schimmel <idosch@nvidia.com>
To: Yingjie Wang <1075151112@qq.com>
Cc: netdev@vger.kernel.org, davem@davemloft.net, edumazet@kernel.org,
	kuba@kernel.org, pabeni@redhat.com, horms@kernel.org,
	dsahern@kernel.org, corbet@lwn.net, skhan@linuxfoundation.org,
	rdunlap@infradead.org, linux-doc@vger.kernel.org,
	alexhenrie24@gmail.com
Subject: Re: [PATCH net v2] ipv6: bound temporary address regeneration advance
Date: Thu, 29 Oct 2026 19:58:27 +0200	[thread overview]
Message-ID: <20261029175827.GA1411453@shredder> (raw)
In-Reply-To: <tencent_C2E4FE9B7C0A05615861782611715EAA9C0A@qq.com>

On Thu, Oct 29, 2026 at 11:26:27PM +0800, Yingjie Wang wrote:
> ipv6_get_regen_advance() combines regen_min_advance, regen_max_retry,
> dad_transmits and the neighbor retransmission time. The first three are
> signed sysctls, and the product is evaluated as int. Negative values or
> signed overflow can make the unsigned long result much larger than
> intended.
> 
> The retransmission time can also be supplied by an on-link Router
> Advertisement. On an x86-64 HZ=1000 host with regen_min_advance=2,
> regen_max_retry=3 and dad_transmits=1, an RA Retrans Timer of
> 1431654765 ms makes the old int product wrap to -3001. Division by HZ
> and addition of regen_min_advance then produce -1, which is returned as
> ULONG_MAX. This gives an on-link trigger on hosts that accept RAs and
> use temporary addresses without requiring a sysctl write.
> 
> When ipv6_create_tempaddr() clamps the preferred lifetime, it adds age
> and one to this advance and stores it in a u32. The sum can wrap or be
> truncated to a short preferred lifetime. addrconf_verify_rtnl() then
> finds the new address due for regeneration and restarts its scan after
> creating another one. The temporary-address path has had no
> max_addresses check since commit 4b08a8f1bd8c ("ipv6: remove
> max_addresses check from ipv6_create_tempaddr"). In a baseline namespace
> test, setting dad_transmits=-1 and adding a temporary-address template
> with mngtmpaddr produced 2859 loopback addresses in two seconds.
> 
> Reject negative values for all three sysctls with -EINVAL. Writes that
> used to succeed now fail validation; negative values have no useful
> meaning (negative regen_max_retry already disables temporary addresses
> on the first attempt). Keep zero valid. Use checked u64 arithmetic and
> div_u64() to calculate the advance, including values supplied by an RA,
> and cap it at U32_MAX. Compare the required preferred lifetime in u64
> before narrowing it to u32. Calculate the desync margin without an
> unsigned subtraction wrap on 32-bit builds. Clamp the effective maximum
> to that non-negative margin so a negative max_desync_factor cannot make
> the u32 comparison architecture-dependent.
> 
> Tested on x86-64 KASAN:
> - negative writes to the three sysctls were rejected without changing
>   their values, and zero remained valid;
> - normal temporary-address setup and a case with writable
>   max_desync_factor=-1 each produced three IPv6 addresses;
> - with HZ=1000 and a one-second retransmission time,
>   regen_min_advance=0, regen_max_retry=2147483647 and dad_transmits=1
>   caused the baseline kernel to reach 2847 loopback addresses after
>   two seconds; the fixed kernel left two.
> 
> Kernel selftests were not run.
> 
> An i386 defconfig with CONFIG_IPV6=y and HZ=1000 built addrconf.o with
> W=1; nm -u showed no __udivdi3 reference.
> 
> Fixes: f4bcbf360ac8 ("net: ipv6/addrconf: clamp preferred_lft to the minimum required")
> Signed-off-by: Yingjie Wang <1075151112@qq.com>
> ---
> Changes in v2:
> - Reorder the declarations for the netdev reverse-Christmas-tree convention.
> - Use an intermediate variable to make the max_desync_factor calculation
>   easier to read, as requested by Paolo Abeni.
> - Clamp the effective max_desync_factor to a non-negative range so the u32
>   comparison has the same result on 32-bit and 64-bit builds.
> - Document the RA-supplied Retrans Timer trigger raised in review.
> 
> Link: https://patchwork.kernel.org/project/netdevbpf/patch/tencent_F6E8C7CAC6D390C8E912EEE4FD9FE4333505@qq.com/
> 
>  Documentation/networking/ip-sysctl.rst |  3 ++
>  net/ipv6/addrconf.c                    | 53 ++++++++++++++++++--------
>  2 files changed, 41 insertions(+), 15 deletions(-)

This patch is very hard to understand.

My understanding is that ipv6_create_tempaddr() makes sure that it's not
creating a temporary address that requires immediate regeneration. It's
doing that by setting the preferred lifetime of such an address to
'regen_advance + age + 1', so that at least one second will pass before
a new temporary address needs to be generated.

The problem is that this expression can overflow to 0 or be truncated when
assigned to 'preferred_lft' if it's larger than U32_MAX, resulting in a
preferred lifetime that requires immediate regeneration.

Can't we solve it with the following diff?

diff --git a/net/ipv6/addrconf.c b/net/ipv6/addrconf.c
index c8c0dd04982d..d9d13e78bf13 100644
--- a/net/ipv6/addrconf.c
+++ b/net/ipv6/addrconf.c
@@ -1487,7 +1487,8 @@ static int ipv6_create_tempaddr(struct inet6_ifaddr *ifp, bool block)
 	age = (now - tmp_tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ;
 	if (cfg.preferred_lft <= regen_advance + age) {
 		cfg.preferred_lft = regen_advance + age + 1;
-		if (cfg.preferred_lft > cfg.valid_lft ||
+		if (cfg.preferred_lft <= regen_advance + age ||
+		    cfg.preferred_lft > cfg.valid_lft ||
 		    cfg.preferred_lft > if_public_preferred_lft) {
 			in6_ifa_put(ifp);
 			in6_dev_put(idev);


      parent reply	other threads:[~2026-10-29 17:58 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-26  8:22 [PATCH net] ipv6: bound temporary address regeneration advance Yingjie Wang
2026-10-27 23:23 ` netdev-bot+sashiko
2026-10-29 12:09 ` Paolo Abeni
2026-10-29 15:26 ` [PATCH net v2] " Yingjie Wang
2026-10-29 15:28   ` netdev-bot+sinfo
2026-10-29 17:58   ` Ido Schimmel [this message]

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=20261029175827.GA1411453@shredder \
    --to=idosch@nvidia.com \
    --cc=1075151112@qq.com \
    --cc=alexhenrie24@gmail.com \
    --cc=corbet@lwn.net \
    --cc=davem@davemloft.net \
    --cc=dsahern@kernel.org \
    --cc=edumazet@kernel.org \
    --cc=horms@kernel.org \
    --cc=kuba@kernel.org \
    --cc=linux-doc@vger.kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=rdunlap@infradead.org \
    --cc=skhan@linuxfoundation.org \
    /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