diff mbox series

[2/2] elf: Fix failure handling in _dl_map_object_from_fd

Message ID da97a6c72dddb9ac9cf7471c96498c92318cb362.1607703178.git.szabolcs.nagy@arm.com
State New
Headers show
Series elf: dl-load error handling refactoring | expand

Commit Message

Szabolcs Nagy Dec. 11, 2020, 4:35 p.m. UTC
The failure paths in _dl_map_object_from_fd did not clean every
potentially allocated resource up.

Handle l_phdr, l_libname and mapped segments in the common failure
handling code.

There are various bits that may not be cleaned properly on failure
(e.g. executable stack, incomplete dl_map_segments) fixing those
need further changes.
---
 elf/dl-load.c | 25 +++++++++++++++----------
 1 file changed, 15 insertions(+), 10 deletions(-)

Comments

Adhemerval Zanella Netto Dec. 14, 2020, 6:31 p.m. UTC | #1
On 11/12/2020 13:35, Szabolcs Nagy via Libc-alpha wrote:
> The failure paths in _dl_map_object_from_fd did not clean every
> potentially allocated resource up.
> 
> Handle l_phdr, l_libname and mapped segments in the common failure
> handling code.
> 
> There are various bits that may not be cleaned properly on failure
> (e.g. executable stack, incomplete dl_map_segments) fixing those
> need further changes.

LGTM, thanks.

Reviewed-by: Adhemerval Zanella  <adhemerval.zanella@linaro.org>


> ---
>  elf/dl-load.c | 25 +++++++++++++++----------
>  1 file changed, 15 insertions(+), 10 deletions(-)
> 
> diff --git a/elf/dl-load.c b/elf/dl-load.c
> index 10ccca20d0..a9a2a1b63f 100644
> --- a/elf/dl-load.c
> +++ b/elf/dl-load.c
> @@ -955,8 +955,14 @@ _dl_map_object_from_fd (const char *name, const char *origname, int fd,
>  	  /* The file might already be closed.  */
>  	  if (fd != -1)
>  	    (void) __close_nocancel (fd);
> +	  if (l != NULL && l->l_map_start != 0)
> +	    _dl_unmap_segments (l);
>  	  if (l != NULL && l->l_origin != (char *) -1l)
>  	    free ((char *) l->l_origin);
> +	  if (l != NULL && !l->l_libname->dont_free)
> +	    free (l->l_libname);
> +	  if (l != NULL && l->l_phdr_allocated)
> +	    free ((void *) l->l_phdr);
>  	  free (l);
>  	  free (realname);
>  

Ok.

> @@ -1251,7 +1257,11 @@ _dl_map_object_from_fd (const char *name, const char *origname, int fd,
>      errstring = _dl_map_segments (l, fd, header, type, loadcmds, nloadcmds,
>  				  maplength, has_holes, loader);
>      if (__glibc_unlikely (errstring != NULL))
> -      goto lose;
> +      {
> +	/* Mappings can be in an inconsistent state: avoid unmap.  */
> +	l->l_map_start = l->l_map_end = 0;
> +	goto lose;
> +      }
>  
>      /* Process program headers again after load segments are mapped in
>         case processing requires accessing those segments.  Scan program

Ok.

> @@ -1289,15 +1299,6 @@ _dl_map_object_from_fd (const char *name, const char *origname, int fd,
>        || (__glibc_unlikely (l->l_flags_1 & DF_1_PIE)
>  	  && __glibc_unlikely ((mode & __RTLD_OPENEXEC) == 0)))
>      {
> -      /* We are not supposed to load this object.  Free all resources.  */
> -      _dl_unmap_segments (l);
> -
> -      if (!l->l_libname->dont_free)
> -	free (l->l_libname);
> -
> -      if (l->l_phdr_allocated)
> -	free ((void *) l->l_phdr);
> -
>        if (l->l_flags_1 & DF_1_PIE)
>  	errstring
>  	  = N_("cannot dynamically load position-independent executable");

Ok.

> @@ -1402,6 +1403,10 @@ cannot enable executable stack as shared object requires");
>    /* Signal that we closed the file.  */
>    fd = -1;
>  
> +  /* Failures before this point are handled locally via lose.
> +     There are no more failures in this function until return,
> +     to change that the cleanup handling needs to be updated.  */
> +
>    /* If this is ET_EXEC, we should have loaded it as lt_executable.  */
>    assert (type != ET_EXEC || l->l_type == lt_executable);
>  
> 

Ok.
diff mbox series

Patch

diff --git a/elf/dl-load.c b/elf/dl-load.c
index 10ccca20d0..a9a2a1b63f 100644
--- a/elf/dl-load.c
+++ b/elf/dl-load.c
@@ -955,8 +955,14 @@  _dl_map_object_from_fd (const char *name, const char *origname, int fd,
 	  /* The file might already be closed.  */
 	  if (fd != -1)
 	    (void) __close_nocancel (fd);
+	  if (l != NULL && l->l_map_start != 0)
+	    _dl_unmap_segments (l);
 	  if (l != NULL && l->l_origin != (char *) -1l)
 	    free ((char *) l->l_origin);
+	  if (l != NULL && !l->l_libname->dont_free)
+	    free (l->l_libname);
+	  if (l != NULL && l->l_phdr_allocated)
+	    free ((void *) l->l_phdr);
 	  free (l);
 	  free (realname);
 
@@ -1251,7 +1257,11 @@  _dl_map_object_from_fd (const char *name, const char *origname, int fd,
     errstring = _dl_map_segments (l, fd, header, type, loadcmds, nloadcmds,
 				  maplength, has_holes, loader);
     if (__glibc_unlikely (errstring != NULL))
-      goto lose;
+      {
+	/* Mappings can be in an inconsistent state: avoid unmap.  */
+	l->l_map_start = l->l_map_end = 0;
+	goto lose;
+      }
 
     /* Process program headers again after load segments are mapped in
        case processing requires accessing those segments.  Scan program
@@ -1289,15 +1299,6 @@  _dl_map_object_from_fd (const char *name, const char *origname, int fd,
       || (__glibc_unlikely (l->l_flags_1 & DF_1_PIE)
 	  && __glibc_unlikely ((mode & __RTLD_OPENEXEC) == 0)))
     {
-      /* We are not supposed to load this object.  Free all resources.  */
-      _dl_unmap_segments (l);
-
-      if (!l->l_libname->dont_free)
-	free (l->l_libname);
-
-      if (l->l_phdr_allocated)
-	free ((void *) l->l_phdr);
-
       if (l->l_flags_1 & DF_1_PIE)
 	errstring
 	  = N_("cannot dynamically load position-independent executable");
@@ -1402,6 +1403,10 @@  cannot enable executable stack as shared object requires");
   /* Signal that we closed the file.  */
   fd = -1;
 
+  /* Failures before this point are handled locally via lose.
+     There are no more failures in this function until return,
+     to change that the cleanup handling needs to be updated.  */
+
   /* If this is ET_EXEC, we should have loaded it as lt_executable.  */
   assert (type != ET_EXEC || l->l_type == lt_executable);