]> git.ipfire.org Git - thirdparty/elfutils.git/commitdiff
libelf: Fix possible memory leak in elf_getdata_rawchunk
authorMark Wielaard <mark@klomp.org>
Wed, 8 Nov 2023 12:50:01 +0000 (13:50 +0100)
committerMark Wielaard <mark@klomp.org>
Tue, 14 Nov 2023 13:24:44 +0000 (14:24 +0100)
If the rawchunk is not properly aligned we'll create a new buffer
that is correctly aligned and put the data in that new buffer with
memcpy or the conversion function. In such cases the rawchunk leaks
because the new buffer is put into the Elf_Data_Chunk.

* libelf/elf_getdata_rawchunk.c (elf_getdata_rawchunk):
Call free on the rawchunk if new buffer was allocated.

Signed-off-by: Mark Wielaard <mark@klomp.org>
libelf/elf_getdata_rawchunk.c

index 05ff329c75c909b7190a4de176a0651ae046eb7b..1751878d6f2a86f24f98d72089f681c5afe4bb96 100644 (file)
@@ -175,6 +175,8 @@ elf_getdata_rawchunk (Elf *elf, int64_t offset, size_t size, Elf_Type type)
 
          /* The copy will be appropriately aligned for direct access.  */
          memcpy (buffer, rawchunk, size);
+
+         free (rawchunk);
        }
     }
   else
@@ -191,6 +193,9 @@ elf_getdata_rawchunk (Elf *elf, int64_t offset, size_t size, Elf_Type type)
 
       /* Call the conversion function.  */
       (*__elf_xfctstom[elf->class - 1][type])(buffer, rawchunk, size, 0);
+
+      if (!flags)
+       free (rawchunk);
     }
 
   /* Allocate the dummy container to point at this buffer.  */