From 14e0df0cc31bab1f19fc0d391ed512d231589611 Mon Sep 17 00:00:00 2001 From: Aaron Merey Date: Fri, 12 Jul 2024 18:32:34 -0400 Subject: [PATCH] libelf: Fix deadlock in elf_cntl * libelf/elf_cntl.c (elf_cntl): Move rwlock_wrlock, rwlock_unlock, inside case switch statements. v2 changes: Remove unnecessary locking and checking of elf->map_address Signed-off-by: Heather S. McIntyre Signed-off-by: Aaron Merey Signed-off-by: Mark Wielaard --- libelf/elf_cntl.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/libelf/elf_cntl.c b/libelf/elf_cntl.c index 04aa91320..3fbc7d971 100644 --- a/libelf/elf_cntl.c +++ b/libelf/elf_cntl.c @@ -48,13 +48,12 @@ elf_cntl (Elf *elf, Elf_Cmd cmd) return -1; } - rwlock_wrlock (elf->lock); switch (cmd) { case ELF_C_FDREAD: /* If not all of the file is in the memory read it now. */ - if (elf->map_address == NULL && __libelf_readall (elf) == NULL) + if (__libelf_readall (elf) == NULL) { /* We were not able to read everything. */ result = -1; @@ -64,7 +63,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 +74,5 @@ elf_cntl (Elf *elf, Elf_Cmd cmd) break; } - rwlock_unlock (elf->lock); - return result; } -- 2.47.2