mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] bpf: hashtab: fix 32-bit overflow in memory usage calculation
@ 2025-11-06 20:58 Alexei Safin
  2025-11-07  1:58 ` Yafang Shao
  0 siblings, 1 reply; 4+ messages in thread
From: Alexei Safin @ 2025-11-06 20:58 UTC (permalink / raw)
  To: Alexei Starovoitov
  Cc: Alexei Safin, Daniel Borkmann, Andrii Nakryiko, Martin KaFai Lau,
	Eduard Zingerman, Song Liu, Yonghong Song, John Fastabend,
	KP Singh, Stanislav Fomichev, Hao Luo, Jiri Olsa, Yafang Shao,
	bpf, linux-kernel, lvc-patches, stable

The intermediate product value_size * num_possible_cpus() is evaluated
in 32-bit arithmetic and only then promoted to 64 bits. On systems with
large value_size and many possible CPUs this can overflow and lead to
an underestimated memory usage.

Cast value_size to u64 before multiplying.

Found by Linux Verification Center (linuxtesting.org) with SVACE.

Fixes: 304849a27b34 ("bpf: hashtab memory usage")
Cc: stable@vger.kernel.org
Signed-off-by: Alexei Safin <a.safin@rosa.ru>
---
 kernel/bpf/hashtab.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/kernel/bpf/hashtab.c b/kernel/bpf/hashtab.c
index 570e2f723144..7ad6b5137ba1 100644
--- a/kernel/bpf/hashtab.c
+++ b/kernel/bpf/hashtab.c
@@ -2269,7 +2269,7 @@ static u64 htab_map_mem_usage(const struct bpf_map *map)
 		usage += htab->elem_size * num_entries;
 
 		if (percpu)
-			usage += value_size * num_possible_cpus() * num_entries;
+			usage += (u64)value_size * num_possible_cpus() * num_entries;
 		else if (!lru)
 			usage += sizeof(struct htab_elem *) * num_possible_cpus();
 	} else {
@@ -2281,7 +2281,7 @@ static u64 htab_map_mem_usage(const struct bpf_map *map)
 		usage += (htab->elem_size + LLIST_NODE_SZ) * num_entries;
 		if (percpu) {
 			usage += (LLIST_NODE_SZ + sizeof(void *)) * num_entries;
-			usage += value_size * num_possible_cpus() * num_entries;
+			usage += (u64)value_size * num_possible_cpus() * num_entries;
 		}
 	}
 	return usage;
-- 
2.50.1 (Apple Git-155)


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

end of thread, other threads:[~2025-11-07  7:43 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-11-06 20:58 [PATCH] bpf: hashtab: fix 32-bit overflow in memory usage calculation Alexei Safin
2025-11-07  1:58 ` Yafang Shao
2025-11-07  6:58   ` Алексей Сафин
2025-11-07  7:42     ` Yafang Shao

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®