]> git.ipfire.org Git - thirdparty/elfutils.git/commitdiff
libelf: Fix deadlock in elf_cntl
authorHeather McIntyre <hsm2@rice.edu>
Fri, 12 Jul 2024 22:32:34 +0000 (18:32 -0400)
committerAaron Merey <amerey@redhat.com>
Tue, 20 Aug 2024 21:13:01 +0000 (17:13 -0400)
        * libelf/elf_cntl.c (elf_cntl): Move rwlock_wrlock, rwlock_unlock,
        inside case switch statements.  Remove unnecessary early return.

Signed-off-by: Heather S. McIntyre <hsm2@rice.edu>
Signed-off-by: Aaron Merey <amerey@redhat.com>
Signed-off-by: Mark Wielaard <mark@klomp.org>
libelf/elf_cntl.c

index 04aa913205160cba1e6c36ec7dec33cec2894c24..da4ea999c840ecc1432bfd9124b807fcadda6b36 100644 (file)
@@ -42,19 +42,11 @@ elf_cntl (Elf *elf, Elf_Cmd cmd)
   if (elf == NULL)
     return -1;
 
-  if (elf->fildes == -1)
-    {
-      __libelf_seterrno (ELF_E_INVALID_HANDLE);
-      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 +56,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 +67,5 @@ elf_cntl (Elf *elf, Elf_Cmd cmd)
       break;
     }
 
-  rwlock_unlock (elf->lock);
-
   return result;
 }