drop use of getdelim/stdio in dynamic linker
authorRich Felker <dalias@aerifal.cx>
Wed, 11 Nov 2020 00:32:09 +0000 (19:32 -0500)
committerRich Felker <dalias@aerifal.cx>
Wed, 11 Nov 2020 15:55:13 +0000 (10:55 -0500)
commitc1e5d243b7e39b2fbfb17144608ce045575d8e95
treef9d8c0743f16e278d30f53a7063412b8e7e4d257
parentcbecda0b506c7d49a2f7fe3dc44e0e3dcf663764
drop use of getdelim/stdio in dynamic linker

the only place stdio was used here was for reading the ldso path file,
taking advantage of getdelim to automatically allocate and resize the
buffer. the motivation for use here was that, with shared libraries,
stdio is already available anyway and free to use. this has long been
a nuisance to users because getdelim's use of realloc here triggered a
valgrind bug, but removing it doesn't really fix that; on some archs
even calling the valgrind-interposed malloc at this point will crash.

the actual motivation for this change is moving towards getting rid of
use of application-provided malloc in parts of libc where it would be
called with libc-internal locks held, leading to the possibility of
deadlock if the malloc implementation doesn't follow unwritten rules
about which libc functions are safe for it to call. since getdelim is
required to produce a pointer as if by malloc (i.e. that can be passed
to reallor or free), it necessarily must use the public malloc.

instead of performing a realloc loop as the path file is read, first
query its size with fstat and allocate only once. this produces
slightly different truncation behavior when racing with writes to a
file, but neither behavior is or could be made safe anyway; on a live
system, ldso path files should be replaced by atomic rename only. the
change should also reduce memory waste.
ldso/dynlink.c