Repository navigation
mm/tlsf, libs/libc/elf: Handle alignment 0 and reject invalid ELF sections. - #21215
Merged
Merged
Conversation
|
mm_memalign() of the TLSF heap gave a zero alignment to mempool_multiple_memalign(), where ALIGN_UP(blk, 0) divides by zero. The ELF loader can pass zero: sh_addralign 0 means no alignment. Treat zero as a request for the natural malloc alignment, as the default heap does since 8773241 ("mm/heap: Handle zero memalign alignment"). Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: Marco Casaroli <[email protected]>
The ELF specification says that sh_addralign 0 and 1 mean that the section has no alignment constraint. The local _ALIGN_UP(v, 0) gave 0, because ~(0 - 1) is 0. So a section with sh_addralign 0 set the running text or data size back to 0, and the loader allocated too little memory for the sections. When the loader copied the sections, it also set the copy address to 0 for such a section. libelf_loadhdrs() now changes an alignment of 0 to 1 in the section headers when it reads them, before any use. The loader then uses ALIGN_UP() and ALIGN_DOWN() of nuttx/nuttx.h instead of its own macros. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: Marco Casaroli <[email protected]>
libelf_elfsize() added the sizes and alignments of the file without any check: - sh_addralign may be 0 or a power of two only. - The sums of sh_size, of p_memsz and of the FDPIC descriptors could wrap, and a later round-up to pages could wrap too. Then the loader got a too small buffer and wrote the sections past its end, for example memset() of a large SHT_NOBITS section. In a kernel build the kernel wrote to unmapped user memory and stopped with a data abort, with a file that a user program gives to exec(). Check each value and each sum, limit them to half of the address space (no image can be that large), and fail the load with ENOEXEC when one is invalid. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: Marco Casaroli <[email protected]>
xiaoxiang781216
approved these changes
Oct 31, 2026
acassis
approved these changes
Oct 31, 2026
1 task done
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
#21067 made
mm_memalign()of the default heap accept an alignment of 0. Two places still fail with it, and the ELF loader, which passessh_addralignfrom the file, does not check the section headers at all. Three commits:mm_memalign()gave 0 tomempool_multiple_memalign(), whereALIGN_UP(blk, 0)divides by zero: on arm64 the result is 0, so the pool block is lost and the call falls through totlsf_memalign(). The same check as mm/heap: Handle zero memalign alignment #21067: 0 meansMM_ALIGN.sh_addralign0 and 1 mean no alignment constraint._ALIGN_UP(v, 0)inelf_load.cgave 0, because~(0 - 1)is 0. So a section with alignment 0 set the running data or text size back to 0 (the buffer was too small), and the loader set the copy address of the section to 0.libelf_elfsize()now changes an alignment of 0 to 1 in the section headers before any use, and the loader usesALIGN_UP()andALIGN_DOWN()ofnuttx/nuttx.hinstead of its own macros.libelf_elfsize()used the values of the file without a check. An alignment that is not 0 or a power of two is not valid ELF. The sums ofsh_size, ofp_memszand of the FDPIC descriptors could wrap, and a later round-up to pages could wrap too; then the loader wrote the sections past a too small buffer. In a kernel build this is the kernel, with a file that a user program gives toexec().libelf_elfsize()now checks each value and each sum, limits them to half of the address space (no image can be that large, and the later round-ups then cannot wrap), and fails the load withENOEXEC.Impact
All build modes and architectures that use the ELF loader or the TLSF heap. No change for valid files with a non-zero alignment, and no change for
memalign()with a non-zero alignment.memalign(0, n)on the TLSF heap now behaves asmalloc(n), as on the default heap since mm/heap: Handle zero memalign alignment #21067.sh_addralign0 now loads correctly (it crashed the loader before).ENOEXEC. Such a file is not valid ELF; before, the test file with alignment 3 loaded and ran.Testing
Master here is 23f5a4f + #21067: master 7505b87 does not build for arm64 (fixed by #21210). The branch is the same base + these three commits. QEMU with HVF.
The old macro, on the host: the
_ALIGN_UPline of master, compiled into a test, against the exact result (the smallest multiple ofathat is >=v; 0 and 1:v). This PR removes the macro: 0 becomes 1, thenALIGN_UP().ALIGN_UP())_ALIGN_UP(v, 0),v= 1 ... 4095v.data16 B (align 8), a section of 12 B (align 0),.bss256 B_ALIGN_UP(0x40001004, 0)(
ALIGN_UP()rounds to a non-power-of-two alignment correctly, but such a value is not valid ELF: the third commit rejects it.)The ELF loader, qemu-armv8a:knsh (kernel build): a test program with a writable section
.align0, a.dataarray and a.bssarray checks its data after the start; a script changes one section header after the link:elfalign0:.align0sh_addralign8 -> 0elfalign3:.align0sh_addralign8 -> 3_ALIGN_UP()value)ENOEXEC: "Bad size or alignment of section 8"elfbigbss:.bsssh_size0x105 -> 0xfffffffffffff000ENOEXEC: "Bad size or alignment of section 11"ostest exits with status 0 on qemu-armv8a:knsh and qemu-armv8a:nsh with this PR.
The TLSF heap, qemu-armv8a:nsh (flat) with
CONFIG_MM_TLSF_MANAGER=yandCONFIG_MM_HEAP_MEMPOOL_THRESHOLD=256: a test program callsmemalign(0, 24)+free()200 times. Master: the heap grows from 58080 to 62912 bytes in use (a lost pool block each time). This PR: no growth; ostest exits with status 0. The default heap (control, fixed by #21067): no growth on both.Build, each with
EXTRAFLAGS="-Wno-cpp -Werror", OK: rv-virt:elf (flat, rv32), rv-virt:pnsh64 (protected, rv64), rv-virt:knsh64 (kernel, rv64), mps3-an547:picostest (flat, Arm, PIC modules), qemu-armv8a:knsh and qemu-armv8a:nsh with TLSF (from the runs). pimoroni-pico-2-plus:xipfs-fdpic:elf_load.c(with the FDPIC part, which has no#ifdef) compiles; the build then stops at the apps FDPIC modules, which needarm-uclinuxfdpiceabi-ld(in the CI image, not on this host).tools/checkpatch.sh -c -u -m -gpasses.