Browse Source

[LibOS] Fix a memory leak in __map_elf_object() about variable pointer new_phdr

new_phdr is allocated using malloc, if failed, it may cause unpredictable behavior on read operation,
also causing resource leakage of new_phdr if read operation failed.

Signed-off-by: Gary <gang1.wang@intel.com>
Gary 6 years ago
parent
commit
ddd4e3d20d
1 changed files with 5 additions and 0 deletions
  1. 5 0
      LibOS/shim/src/elf/shim_rtld.c

+ 5 - 0
LibOS/shim/src/elf/shim_rtld.c

@@ -404,10 +404,15 @@ call_lose:
     if (type == OBJECT_LOAD &&
         header->e_phoff + maplength <= (size_t) fbp_len) {
         ElfW(Phdr) * new_phdr = (ElfW(Phdr) *) malloc (maplength);
+        if (!new_phdr) {
+            errstring = "new_phdr malloc failure";
+            goto call_lose;
+        }
         if ((ret = (*seek) (file, header->e_phoff, SEEK_SET)) < 0 ||
             (ret = (*read) (file, new_phdr, maplength)) < 0) {
             errstring = "cannot read file data";
             errval = ret;
+            free(new_phdr);
             goto call_lose;
         }
         phdr = new_phdr;