From: Heather McIntyre Date: Tue, 10 Oct 2023 13:42:48 +0000 (+0200) Subject: libelf: Fix deadlock in elf_cntl X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=cdf0657d69b2b0cd0ba2609b1a0b9a25f9b9763b;p=thirdparty%2Felfutils.git libelf: Fix deadlock in elf_cntl * libelf/elf_cntl.c (elf_cntl): Move rwlock_wrlock, rwlock_unlock, inside case switch statements. Signed-off-by: Heather S. McIntyre Signed-off-by: Mark Wielaard --- diff --git a/libelf/elf_cntl.c b/libelf/elf_cntl.c index 04aa9132..64087c7d 100644 --- a/libelf/elf_cntl.c +++ b/libelf/elf_cntl.c @@ -48,13 +48,16 @@ elf_cntl (Elf *elf, Elf_Cmd cmd) return -1; } - rwlock_wrlock (elf->lock); + switch (cmd) { case ELF_C_FDREAD: + rwlock_rdlock (elf->lock); + int addr_isnull = elf->map_address == NULL; + rwlock_unlock(elf->lock); /* If not all of the file is in the memory read it now. */ - if (elf->map_address == NULL && __libelf_readall (elf) == NULL) + if (addr_isnull && __libelf_readall (elf) == NULL) { /* We were not able to read everything. */ result = -1; @@ -64,7 +67,9 @@ elf_cntl (Elf *elf, Elf_Cmd cmd) case ELF_C_FDDONE: /* Mark the file descriptor as not usable. */ + rwlock_wrlock (elf->lock); elf->fildes = -1; + rwlock_unlock (elf->lock); break; default: @@ -73,7 +78,5 @@ elf_cntl (Elf *elf, Elf_Cmd cmd) break; } - rwlock_unlock (elf->lock); - return result; }