Skip to content

Commit 722c094

Browse files
Alexei SafinKernel Patches Daemon
authored andcommitted
bpf: hashtab: fix 32-bit overflow in memory usage calculation
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: 304849a ("bpf: hashtab memory usage") Cc: [email protected] Signed-off-by: Alexei Safin <[email protected]>
1 parent 03f9e36 commit 722c094

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

kernel/bpf/hashtab.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2216,7 +2216,7 @@ static u64 htab_map_mem_usage(const struct bpf_map *map)
22162216
usage += htab->elem_size * num_entries;
22172217

22182218
if (percpu)
2219-
usage += value_size * num_possible_cpus() * num_entries;
2219+
usage += (u64)value_size * num_possible_cpus() * num_entries;
22202220
else if (!lru)
22212221
usage += sizeof(struct htab_elem *) * num_possible_cpus();
22222222
} else {
@@ -2228,7 +2228,7 @@ static u64 htab_map_mem_usage(const struct bpf_map *map)
22282228
usage += (htab->elem_size + LLIST_NODE_SZ) * num_entries;
22292229
if (percpu) {
22302230
usage += (LLIST_NODE_SZ + sizeof(void *)) * num_entries;
2231-
usage += value_size * num_possible_cpus() * num_entries;
2231+
usage += (u64)value_size * num_possible_cpus() * num_entries;
22322232
}
22332233
}
22342234
return usage;

0 commit comments

Comments
 (0)