Bug dereferencing NULL args for newlib-nano in __call_atexit.c
Jeff Johnston
jjohnstn@redhat.com
Wed Oct 7 18:24:12 GMT 2026
Hi Michael,
Fix pushed. Thanks.
-- Jeff J.
On Sat, Oct 3, 2026 at 4:53 PM <michael@volt-software.nl> wrote:
> Hi,
>
> Commit 7c7d9cf585d0 ("newlib: copy args for atexit()'ed function before
> unlock the mutex") reads args->_fntypes, args->_is_cxa and args->_fnargs[n]
> in __call_exitprocs before checking args for NULL. With _REENT_SMALL, args
> is p->_on_exit_args_ptr. That pointer stays NULL unless __on_exit_args is
> linked, which happens only through on_exit() or __cxa_atexit(). A program
> that registers its handlers only with atexit() therefore reads NULL+0x100,
> NULL+0x104 and NULL+4*n at exit. Because args has now been dereferenced,
> GCC also removes the later !args || test before the call. On a target with
> an MPU null guard this faults; where address 0 is readable, the junk value
> decides how fn is called.
>
> Seen in the Arm GNU Toolchain 15.3.rel1 libc_nano.a (thumb/v7+fp/hard and
> v8-m.main+fp/hard). The 15.2.rel1 build still has the check. Reproducer:
>
> #include <stdlib.h>
> static void hook(void) {}
> int main(void) { atexit(hook); return 0; }
>
> arm-none-eabi-gcc -mcpu=cortex-m33 -mthumb -O2 --specs=nano.specs
> --specs=rdimon.specs repro.c -o repro.elf
> arm-none-eabi-objdump -d repro.elf | sed -n '/<__call_exitprocs>:/,/^$/p'
>
> __call_exitprocs then contains ldr rX, [rY, #256] and #260 with no NULL
> check on that path.
>
> Proposed fix:
>
> --- a/newlib/libc/stdlib/__call_atexit.c
> +++ b/newlib/libc/stdlib/__call_atexit.c
> @@
> ind = p->_ind;
>
> - fntypes = args->_fntypes;
> - is_cxa = args->_is_cxa;
> - fnarg = args->_fnargs[n];
> + if (args)
> + {
> + fntypes = args->_fntypes;
> + is_cxa = args->_is_cxa;
> + fnarg = args->_fnargs[n];
> + }
>
> Met vriendelijke groet,
> Michael de Lang
> +316-20802339 <+31%206%2020802339>
> www.volt-software.nl
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://sourceware.org/pipermail/newlib/attachments/20261007/658f0688/attachment-0001.htm>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image.png
Type: image/png
Size: 23859 bytes
Desc: not available
URL: <https://sourceware.org/pipermail/newlib/attachments/20261007/658f0688/attachment-0001.png>
More information about the Newlib
mailing list