Use dummy allocator to make accesses defined as per standard
authorSiddhesh Poyarekar <siddhesh@gotplt.org>
Tue, 13 Dec 2022 21:54:36 +0000 (16:54 -0500)
committerLennart Poettering <lennart@poettering.net>
Wed, 14 Dec 2022 16:49:47 +0000 (17:49 +0100)
commit7929e180aa47a2692ad4f053afac2857d7198758
tree75acc7c7959371ca5106891f13df1d8662a2685d
parentcc137d53e36da5e57b060be5e621864f572b2cac
Use dummy allocator to make accesses defined as per standard

systemd uses malloc_usable_size() everywhere to use memory blocks
obtained through malloc, but that is abuse since the
malloc_usable_size() interface isn't meant for this kind of use, it is
for diagnostics only.  This is also why systemd behaviour is flaky when
built with _FORTIFY_SOURCE.

One way to make this more standard (and hence safer) is to, at every
malloc_usable_size() call, also 'reallocate' the block so that the
compiler can see the larger size.  This is done through a dummy
reallocator whose only purpose is to tell the compiler about the larger
usable size, it doesn't do any actual reallocation.

Florian Weimer pointed out that this doesn't solve the problem of an
allocator potentially growing usable size at will, which will break the
implicit assumption in systemd use that the value returned remains
constant as long as the object is valid.  The safest way to fix that is
for systemd to step away from using malloc_usable_size() like this.

Resolves #22801.
src/basic/alloc-util.c
src/basic/alloc-util.h