Browse Source

[LibOS] Fix memory leak in __map_elf_object()

Pointer l can point to a newly allocated object (via new_elf_object()).
Ensure l is freed on error (on call_lose code path).
Gary 6 years ago
parent
commit
d19b266ed4
1 changed files with 13 additions and 4 deletions
  1. 13 4
      LibOS/shim/src/elf/shim_rtld.c

+ 13 - 4
LibOS/shim/src/elf/shim_rtld.c

@@ -366,11 +366,14 @@ __map_elf_object (struct shim_handle * file,
     if (file && (!read || !mmap || !seek))
         return NULL;
 
-    struct link_map * l = remap ? :
+    struct link_map * l = remap ? remap :
                           new_elf_object(file ? (!qstrempty(&file->path) ?
                                          qstrgetstr(&file->path) :
                                          qstrgetstr(&file->uri)) : "", type);
 
+    if (!l)
+        return NULL;
+
     const char * errstring __attribute__((unused)) = NULL;
     int errval = 0;
     int ret;
@@ -378,9 +381,7 @@ __map_elf_object (struct shim_handle * file,
     if (type != OBJECT_INTERNAL && !file) {
         errstring = "shared object has to be backed by file";
         errval = -EINVAL;
-call_lose:
-        debug("loading %s: %s\n", l->l_name, errstring);
-        return NULL;
+        goto call_lose;
     }
 
     /* Scan the program header table, collecting its load commands.  */
@@ -728,6 +729,14 @@ postmap:
     setup_elf_hash(l);
 
     return l;
+
+call_lose:
+    debug("loading %s: %s\n", l->l_name, errstring);
+    if (l != remap) {
+        /* l was allocated via new_elf_object() */
+        free(l);
+    }
+    return NULL;
 }
 
 static inline