binutils-gdb/libctf
Nick Alcock 37ed36fc8b
libctf: fix leak of entire dict when dict opening fails
Ever since commit 1fa7a0c24e ("libctf: sort out potential refcount
loops") ctf_dict_close has only freed anything if the refcount on entry
to the function is precisely 1.  >1 obviously just decrements the
refcount, but the linker machinery can sometimes cause freeing to recurse
from a dict to another dict and then back to the first dict again, so
we interpret a refcount of 0 as an indication that this is a recursive call
and we should just return, because a caller is already freeing this dict.

Unfortunately there is one situation in which this is not true: the bad:
codepath in ctf_bufopen entered when opening fails.  Because the refcount is
bumped only at the very end of ctf_bufopen, any failure causes
ctf_dict_close to be entered with a refcount of zero, and it frees nothing
and we leak the entire dict.

The solution is to bump the refcount to 1 right before freeing... but this
codepath is clearly delicate enough that we need to properly validate it,
so we add a test that uses malloc interposition to count allocations and
frees, creates a dict, writes it out, intentionally corrupts it (by setting
a bunch of bytes after the header to a value high enough that it is
definitely not a valid CTF type kind), then tries to open it again and
counts the malloc/free pairs to make sure they're matched.  (Test run only
on *-linux-gnu, because malloc interposition is not a thing you can rely
upon working everywhere, and this test is not arch-dependent so if it
passes on one arch it can be assumed to pass on all of them.)

libctf/
	* ctf-open.c (ctf_bufopen): Bump the refcount on failure.
	* testsuite/libctf-regression/open-error-free.*: New test.
2024-05-17 12:58:18 +01:00
..
doc Update year range in copyright notice of binutils files 2024-01-04 22:58:12 +10:30
testsuite libctf: fix leak of entire dict when dict opening fails 2024-05-17 12:58:18 +01:00
.gitignore libctf: restructure error handling to reduce relocations 2020-07-22 17:57:20 +01:00
aclocal.m4 mmap: Avoid the sanitizer configure check failure 2024-04-10 03:57:46 -07:00
ChangeLog Add markers for 2.42 branch 2024-01-15 14:42:15 +00:00
ChangeLog-2020 libctf: fix old ChangeLog typo 2021-01-05 14:53:40 +00:00
config.h.in Finalized intl-update patches 2023-11-15 12:53:04 +00:00
configure libctf: do not include undefined functions in libctf.ver 2024-04-19 16:14:48 +01:00
configure.ac libctf: do not include undefined functions in libctf.ver 2024-04-19 16:14:48 +01:00
ctf-archive.c libctf: ctf_archive_iter: fix tiny leak 2024-05-17 12:58:17 +01:00
ctf-create.c libctf: make ctf_serialize() actually serialize 2024-04-19 16:14:47 +01:00
ctf-decl.c Update year range in copyright notice of binutils files 2024-01-04 22:58:12 +10:30
ctf-decls.h Update year range in copyright notice of binutils files 2024-01-04 22:58:12 +10:30
ctf-dedup.c libctf: don't pass errno into ctf_err_warn so often 2024-04-19 16:14:48 +01:00
ctf-dump.c libctf: improve handling of type dumping errors 2024-04-19 16:14:47 +01:00
ctf-endian.h Update year range in copyright notice of binutils files 2024-01-04 22:58:12 +10:30
ctf-error.c Update year range in copyright notice of binutils files 2024-01-04 22:58:12 +10:30
ctf-hash.c Revert "libctf: do not corrupt strings across ctf_serialize" 2024-04-19 16:14:46 +01:00
ctf-impl.h libctf: make ctf_serialize() actually serialize 2024-04-19 16:14:47 +01:00
ctf-inlines.h Update year range in copyright notice of binutils files 2024-01-04 22:58:12 +10:30
ctf-intl.h Update year range in copyright notice of binutils files 2024-01-04 22:58:12 +10:30
ctf-labels.c Update year range in copyright notice of binutils files 2024-01-04 22:58:12 +10:30
ctf-link.c libctf: don't pass errno into ctf_err_warn so often 2024-04-19 16:14:48 +01:00
ctf-lookup.c libctf: don't pass errno into ctf_err_warn so often 2024-04-19 16:14:48 +01:00
ctf-open-bfd.c Update year range in copyright notice of binutils files 2024-01-04 22:58:12 +10:30
ctf-open.c libctf: fix leak of entire dict when dict opening fails 2024-05-17 12:58:18 +01:00
ctf-qsort_r.c Update year range in copyright notice of binutils files 2024-01-04 22:58:12 +10:30
ctf-serialize.c libctf: make ctf_serialize() actually serialize 2024-04-19 16:14:47 +01:00
ctf-sha1.c Update year range in copyright notice of binutils files 2024-01-04 22:58:12 +10:30
ctf-sha1.h Update year range in copyright notice of binutils files 2024-01-04 22:58:12 +10:30
ctf-string.c libctf: make ctf_serialize() actually serialize 2024-04-19 16:14:47 +01:00
ctf-subr.c libctf: don't pass errno into ctf_err_warn so often 2024-04-19 16:14:48 +01:00
ctf-types.c libctf: support addition of types to dicts read via ctf_open() 2024-04-19 16:14:46 +01:00
ctf-util.c libctf: replace 'pending refs' abstraction 2024-04-19 16:14:46 +01:00
elf.h Update year range in copyright notice of binutils files 2024-01-04 22:58:12 +10:30
libctf.ver libctf: Remove undefined functions from ver. map 2024-04-19 16:14:48 +01:00
Makefile.am Update year range in copyright notice of binutils files 2024-01-04 22:58:12 +10:30
Makefile.in mmap: Avoid the sanitizer configure check failure 2024-04-10 03:57:46 -07:00
NEWS include, libctf, ld: extend variable section to contain functions too 2022-03-23 13:48:32 +00:00
swap.h libctf warnings 2024-04-17 09:24:36 +09:30