linux/tools/include
Danish Khateeb 2111eb4273 tools/nolibc: check for overflow in malloc()
malloc() adds the size of its header to the requested size without
checking for overflow. For a size within the header size of SIZE_MAX,
the sum wraps around to a few bytes, and malloc() returns a single page
instead of NULL.

calloc() checks its multiplication for overflow, but then passes the
product to malloc(), so calloc(SIZE_MAX, 1) returns a single page too.
So does realloc(ptr, SIZE_MAX).

Such a size is usually a bug in the caller, for instance a length of -1
used as a size_t, and returning a page instead of NULL can turn it into
a heap overflow.

Fail with ENOMEM when adding the header overflows, as glibc does for
sizes this large.

Fixes: 0e0ff63840 ("tools/nolibc/stdlib: Implement `malloc()`, `calloc()`, `realloc()` and `free()`")
Assisted-by: LLM
Signed-off-by: Danish Khateeb <danishkhateeb03@gmail.com>
Link: https://patch.msgid.link/20260926152414.251214-3-danishkhateeb03@gmail.com
Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
2026-09-30 21:27:03 +02:00
..
asm tools headers: Import x86 MMIO helper overrides 2025-08-27 12:14:06 -06:00
asm-generic tools headers asm: Sync fls headers header with the kernel sources 2025-11-03 13:35:06 -03:00
generated
io_uring selftests/io_uring: support NO_SQARRAY in miniliburing 2026-01-21 07:55:13 -07:00
linux mm.git review status for master..mm-nonmm-stable 2026-08-23 08:07:11 -07:00
nolibc tools/nolibc: check for overflow in malloc() 2026-09-30 21:27:03 +02:00
perf
tools
trace/events
uapi Including fixes from Bluetooth, IPSec and Netfilter. 2026-08-27 13:53:43 -07:00
vdso tools headers: Update the linux/unaligned.h copy with the kernel sources 2026-01-14 08:56:41 +01:00