ipvs: fix shift-out-of-bounds in ip_vs_rht_desired_size
Subsystems (4)
Julian Anastasov
maintainerPATCH 1/1 ipvs: fix shift-out-of-bounds in ip_vs_rht_desired_size
Calling roundup_pow_of_two() with 0 has undefined result:
UBSAN: shift-out-of-bounds in ./include/linux/log2.h:57:13 shift exponent 64 is too large for 64-bit type 'unsigned long' CPU: 1 UID: 0 PID: 77 Comm: kworker/u8:4 Not tainted syzkaller #0 PREEMPT(full) Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 04/18/2026 Workqueue: events_unbound conn_resize_work_handler Call Trace: <TASK> dump_stack_lvl+0xe8/0x150 lib/dump_stack.c:120 ubsan_epilogue+0xa/0x30 lib/ubsan.c:233 __ubsan_handle_shift_out_of_bounds+0x385/0x410 lib/ubsan.c:494 __roundup_pow_of_two include/linux/log2.h:57 [inline] ip_vs_rht_desired_size+0x2cf/0x410 net/netfilter/ipvs/ip_vs_core.c:240 ip_vs_conn_desired_size net/netfilter/ipvs/ip_vs_conn.c:765 [inline] conn_resize_work_handler+0x1b6/0x14c0 net/netfilter/ipvs/ip_vs_conn.c:822 process_one_work kernel/workqueue.c:3302 [inline] process_scheduled_works+0xb5d/0x1860 kernel/workqueue.c:3385 worker_thread+0xa53/0xfc0 kernel/workqueue.c:3466 kthread+0x388/0x470 kernel/kthread.c:436 ret_from_fork+0x514/0xb70 arch/x86/kernel/process.c:158 ret_from_fork_asm+0x1a/0x30 arch/x86/entry/entry_64.S:245 </TASK>
--- net/netfilter/ipvs/ip_vs_core.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
net/netfilter/ipvs/ip_vs_core.c +1 -1
@@ -237,7 +237,7 @@ int ip_vs_rht_desired_size(struct netns_ipvs *ipvs, struct ip_vs_rht *t, int n, {if (!t)return 1 << min_bits; - n =roundup_pow_of_two (n); + n = n ?roundup_pow_of_two (n) :1 ;if (lfactor <0 ) {int factor =min (-lfactor, max_bits);
Signature · 1 line
2.53.0
Simon Horman
reviewerFrom: 'Simon Horman' <[email protected]>
This is an AI-generated review of your patch. The human sending this email has considered the AI review valid, or at least plausible. Full review at: https://sashiko.dev
---
ipvs: fix shift-out-of-bounds in ip_vs_rht_desired_size
This commit addresses a shift-out-of-bounds issue when roundup_pow_of_two() is called with 0 in ip_vs_rht_desired_size. It updates the assignment to fall back to 1 if n is 0.
net/netfilter/ipvs/ip_vs_core.c int ip_vs_rht_desired_size(struct netns_ipvs *ipvs, struct ip_vs_rht *t, int n, · line 240
Quoted 10 lines
net/netfilter/ipvs/ip_vs_core.c +1
-1@@ -237,7 +237,7 @@ int ip_vs_rht_desired_size(struct netns_ipvs *ipvs, struct ip_vs_rht *t, int n, {if (!t)return 1 << min_bits; - n =roundup_pow_of_two (n); + n = n ?roundup_pow_of_two (n) :1 ;
The parameter n is a signed int. If n happens to be negative, it will evaluate to true and be passed to roundup_pow_of_two().
Because roundup_pow_of_two() casts the input to unsigned long, a negative value like -1 becomes ULONG_MAX. This results in a shift exponent of 64, triggering a shift-out-of-bounds regression. Would it be safer to check for n <= 0 instead?
Additionally, if n exceeds 1.07 billion (1 << 30) on systems with large amounts of RAM, roundup_pow_of_two(n) evaluates to 1UL << 31. Since n is a 32-bit signed int, assigning 1UL << 31 back to n overflows and results in a negative number.
Since subsequent bounds checks use signed comparisons:
Quoted 4 lines
net/netfilter/ipvs/ip_vs_core.c +0
-0if (lfactor <0 ) {int factor =min (-lfactor, max_bits);n =
min(n, 1 << (max_bits - factor));
These checks will fail to constrain the size correctly.
[ ... ]
net/netfilter/ipvs/ip_vs_core.c
Quoted 3 lines
net/netfilter/ipvs/ip_vs_core.c +0
-0/* Shrink but keep it n * 2 to prevent frequent resizing */ return clamp (n <<1 ,1 << min_bits,1 << max_bits); }
Eventually, the function returns clamp(n << 1, 1 << min_bits, 1 << max_bits). Shifting a negative number causes undefined behavior, and the clamp operation will force the hash table to its absolute minimum size.
Could this cause millions of connections to be placed into a minimally sized hash table, causing severe collisions during RCU hash lookups?
Julian Anastasov
maintainerHello,
Quoted 12 lines · Simon Horman
From: 'Simon Horman' <[email protected]>
This is an AI-generated review of your patch. The human sending this email has considered the AI review valid, or at least plausible. Full review at: https://sashiko.dev
---
ipvs: fix shift-out-of-bounds inip_vs_rht_desired_sizeThis commit addresses a shift-out-of-bounds issue when
roundup_pow_of_two()is called with 0 inip_vs_rht_desired_size. It updates the assignment to fall back to 1 if n is 0.
Quoted 10 lines
net/netfilter/ipvs/ip_vs_core.c int ip_vs_rht_desired_size(struct netns_ipvs *ipvs, struct ip_vs_rht *t, int n, · line 240
Quoted 10 lines
net/netfilter/ipvs/ip_vs_core.c +1
-1@@ -237,7 +237,7 @@ int ip_vs_rht_desired_size(struct netns_ipvs *ipvs, struct ip_vs_rht *t, int n, {if (!t)return 1 << min_bits; - n =roundup_pow_of_two (n); + n = n ?roundup_pow_of_two (n) :1 ;
To overflow it on 64-bit we need 600GB+ memory, the
conns are ~300 bytes.
In v2 I changed it to n > 0. To solve it completely
we may need to introduce some kind of "conn_max" sysctl, now per-netns. It can help to limit the conns which are created GFP_ATOMIC while for other structures we will switch to GFP_KERNEL_ACCOUNT.
Quoted 28 lines
The parameter n is a signed int. If n happens to be negative, it will evaluate to true and be passed to
roundup_pow_of_two().Because
roundup_pow_of_two()casts the input to unsigned long, a negative value like -1 becomesULONG_MAX. This results in a shift exponent of 64, triggering a shift-out-of-bounds regression. Would it be safer to check for n <= 0 instead?Additionally, if n exceeds 1.07 billion (1 << 30) on systems with large amounts of RAM,
roundup_pow_of_two(n)evaluates to 1UL << 31. Since n is a 32-bit signed int, assigning 1UL << 31 back to n overflows and results in a negative number.Since subsequent bounds checks use signed comparisons:
Quoted 4 lines
net/netfilter/ipvs/ip_vs_core.c +0
-0if (lfactor <0 ) {int factor =min (-lfactor, max_bits);n =
min(n, 1 << (max_bits - factor));These checks will fail to constrain the size correctly.
[ ... ]
net/netfilter/ipvs/ip_vs_core.c
Quoted 3 lines
net/netfilter/ipvs/ip_vs_core.c +0
-0/* Shrink but keep it n * 2 to prevent frequent resizing */ return clamp (n <<1 ,1 << min_bits,1 << max_bits); }
Quoted 6 lines
Eventually, the function returns
clamp(n << 1, 1 << min_bits, 1 << max_bits). Shifting a negative number causes undefined behavior, and the clamp operation will force the hash table to its absolute minimum size.Could this cause millions of connections to be placed into a minimally sized hash table, causing severe collisions during RCU hash lookups?
All that is valid without conn/mem limits.
Regards
--
Julian Anastasov <[email protected]>
Simon Horman
reviewerQuoted 31 lines · Julian Anastasov
Hello,
Quoted 12 lines · Simon Horman
From: 'Simon Horman' <[email protected]>
This is an AI-generated review of your patch. The human sending this email has considered the AI review valid, or at least plausible. Full review at: https://sashiko.dev
---
ipvs: fix shift-out-of-bounds inip_vs_rht_desired_sizeThis commit addresses a shift-out-of-bounds issue when
roundup_pow_of_two()is called with 0 inip_vs_rht_desired_size. It updates the assignment to fall back to 1 if n is 0.Quoted 10 lines
net/netfilter/ipvs/ip_vs_core.c int ip_vs_rht_desired_size(struct netns_ipvs *ipvs, struct ip_vs_rht *t, int n, · line 240
Quoted 10 lines
net/netfilter/ipvs/ip_vs_core.c +1
-1@@ -237,7 +237,7 @@ int ip_vs_rht_desired_size(struct netns_ipvs *ipvs, struct ip_vs_rht *t, int n, {if (!t)return 1 << min_bits; - n =roundup_pow_of_two (n); + n = n ?roundup_pow_of_two (n) :1 ;To overflow it on 64-bit we need 600GB+ memory, the
conns are ~300 bytes.
Quoted 5 lines
In v2 I changed it to n > 0. To solve it completely
we may need to introduce some kind of "conn_max" sysctl, now per-netns. It can help to limit the conns which are createdGFP_ATOMICwhile for other structures we will switch toGFP_KERNEL_ACCOUNT.
Thanks, I agree with this approach.
Quoted 29 lines
Quoted 29 lines
The parameter n is a signed int. If n happens to be negative, it will evaluate to true and be passed to
roundup_pow_of_two().Because
roundup_pow_of_two()casts the input to unsigned long, a negative value like -1 becomesULONG_MAX. This results in a shift exponent of 64, triggering a shift-out-of-bounds regression. Would it be safer to check for n <= 0 instead?Additionally, if n exceeds 1.07 billion (1 << 30) on systems with large amounts of RAM,
roundup_pow_of_two(n)evaluates to 1UL << 31. Since n is a 32-bit signed int, assigning 1UL << 31 back to n overflows and results in a negative number.Since subsequent bounds checks use signed comparisons:
Quoted 4 lines
net/netfilter/ipvs/ip_vs_core.c +0
-0if (lfactor <0 ) {int factor =min (-lfactor, max_bits);n =
min(n, 1 << (max_bits - factor));These checks will fail to constrain the size correctly.
[ ... ]
Quoted 3 lines
net/netfilter/ipvs/ip_vs_core.c +0
-0/* Shrink but keep it n * 2 to prevent frequent resizing */ return clamp (n <<1 ,1 << min_bits,1 << max_bits); }
Quoted 8 lines
Quoted 6 lines
Eventually, the function returns
clamp(n << 1, 1 << min_bits, 1 << max_bits). Shifting a negative number causes undefined behavior, and the clamp operation will force the hash table to its absolute minimum size.Could this cause millions of connections to be placed into a minimally sized hash table, causing severe collisions during RCU hash lookups?
All that is valid without conn/mem limits.
Sorry, my previous email was supposed to come with some commentary that I think that this is a pre-existing issue that can be treated separately from this patch.