Following a lot of further investigation I believe I have determined the root 
cause of this issue. Surprisingly this appears to be a bug in the memalign 
implementation within seL4's fork of the musl library.

I note that the memory allocation system within the mainline musl was 
completely reworked / replaced a number of years ago so there does not appear 
to be any scope to raise a bug report on the musl project.


Fault summary:

The correctness of the memory allocation bookkeeping system relies upon the 
constraint that the minimum size of a memory 'chunk' is 4xsizeof(size_t). If 
this constraint is broken then bad things happen and the bookkeeping system 
becomes corrupted, specifically:

1. Arithmetic wrap-around of x occurs in the routines bin_index and 
bin_index_up within malloc.c. This can lead to chunks below the minimum size 
limit to be considered to be large unallocated chunks of memory. Subsequent 
allocation of these unallocated chunks (considered to be large but in reality 
tiny) allows previously allocated chunks to be re-used / overwritten.

2. The 'next' and 'prev' pointers held in an unallocated chunk (used to 
maintained a doubly linked list of unallocated chunks) that is below the 
minimum size limit may be overlayed with the bookkeeping of the following chunk.

The malloc routine enforces this minimum chunk size limit, however the code of 
the __memalign routine within memalign.c can break this minimum size constraint 
and therefore lead to corruption of the bookkeeping. 

The __memalign routine works by malloc'ing sufficient memory to ensure the 
requested amount of memory is available, at the requested alignment, somewhere 
within the malloc'ed region. This means that there may be some unused memory 
allocated before the start of the aligned memory area. This is handled by 
splitting the chunk allocated by malloc into two chunks, a chunk of memory 
prior to the start of the aligned memory followed by a chunk that starts at the 
requested alignment. __memalign then calls 'free' on the first chunk which 
wasn't required. So far so good, however __memalign fails to enforce the 
minimum chunk size constraint on either of the two split chunks.

In our case the first chunk (the one to be freed) was only 16 bytes whilst 
4xsizeof(size_t) is 32 bytes, thereby breaking the minimum chunk size 
constraint and so led to the detected corruption.


Stephen

> On 23 Mar 2022, at 00:30, Kent Mcleod <[email protected]> wrote:
> 
> ***This mail has been sent from an external source***
> 
> On Tue, Mar 22, 2022 at 6:17 PM Roderick Chapman <[email protected]> wrote:
>> 
>> On 22/03/2022 04:50, Kent Mcleod wrote:
>>> The memalign definition provided by musl is a weak symbol and would be
>>> overridden by any stronger definition encountered in sources such as
>>> from U-Boot.
>> 
>> Good grief!
>> 
>> Is there a linker warning that complains about that sort of overriding?
>> 
> 
> I'm not aware of one.  If you add `-Wl,-M` to the CMake
> `target_link_libraries` command then the linker will print out a
> mapping file which contains which object file each symbol comes from.
> This may be enough to confirm whether symbols are getting replaced
> unexpectedly.
> 
>>  - Rod
>> 
>> _______________________________________________
>> Devel mailing list -- [email protected]
>> To unsubscribe send an email to [email protected]
> _______________________________________________
> Devel mailing list -- [email protected]
> To unsubscribe send an email to [email protected]
> 

This message contains information that may be privileged or confidential and is 
the property of the Capgemini Group. It is intended only for the person to whom 
it is addressed. If you are not the intended recipient, you are not authorized 
to read, print, retain, copy, disseminate, distribute, or use this message or 
any part thereof. If you receive this message in error, please notify the 
sender immediately and delete all copies of this message.

_______________________________________________
Devel mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to