[binutils-gdb] Fix: Stripping Rust static libraries fails because of overly zealous illegal path check

Nick Clifton nickc@sourceware.org
Fri Jan 26 11:54:52 GMT 2024


https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=c8567a87a60cf5820ebc5d98c025e91e2735f373

commit c8567a87a60cf5820ebc5d98c025e91e2735f373
Author: Nick Clifton <nickc@redhat.com>
Date:   Fri Jan 26 11:54:08 2024 +0000

    Fix: Stripping Rust static libraries fails because of overly zealous illegal path check
    
      PR 31250
      * objcopy.c (copy_archive): Improve the handling of archives that contain elements with invalid pathnames.

Diff:
---
 binutils/ChangeLog |  6 ++++++
 binutils/objcopy.c | 26 ++++++++++++++------------
 2 files changed, 20 insertions(+), 12 deletions(-)

diff --git a/binutils/ChangeLog b/binutils/ChangeLog
index 80da5196322..438a45d77a8 100644
--- a/binutils/ChangeLog
+++ b/binutils/ChangeLog
@@ -1,3 +1,9 @@
+2024-01-26  Nick Clifton  <nickc@redhat.com>
+
+	PR 31250
+	* objcopy.c (copy_archive): Improve the handling of archives that
+	contain elements with invalid pathnames.
+
 2024-01-15  Nick Clifton  <nickc@redhat.com>
 
 	* 2.42 branch point.
diff --git a/binutils/objcopy.c b/binutils/objcopy.c
index a85d26203e9..a8e0f156f3e 100644
--- a/binutils/objcopy.c
+++ b/binutils/objcopy.c
@@ -3672,21 +3672,25 @@ copy_archive (bfd *ibfd, bfd *obfd, const char *output_target,
       struct stat buf;
       int stat_status = 0;
       bool ok_object;
+      const char *element_name;
 
+      element_name = bfd_get_filename (this_element);
       /* PR binutils/17533: Do not allow directory traversal
 	 outside of the current directory tree by archive members.  */
-      if (! is_valid_archive_path (bfd_get_filename (this_element)))
+      if (! is_valid_archive_path (element_name))
 	{
-	  non_fatal (_("illegal pathname found in archive member: %s"),
-		     bfd_get_filename (this_element));
-	  bfd_close (this_element);
-	  status = 1;
-	  goto cleanup_and_exit;
+	  non_fatal (_("warning: illegal pathname found in archive member: %s"),
+		     element_name);
+	  /* PR binutils/31250: But there tools which create archives
+	     containing absolute paths, so instead of failing here, try to
+	     create a suitable alternative pathname.  */
+	  element_name = lbasename (element_name);
+	  non_fatal (_("warning: using the basename of the member instead: %s"),
+		     element_name);
 	}
 
       /* Create an output file for this member.  */
-      output_name = concat (dir, "/",
-			    bfd_get_filename (this_element), (char *) 0);
+      output_name = concat (dir, "/", element_name, (char *) 0);
 
       /* If the file already exists, make another temp dir.  */
       if (stat (output_name, &buf) >= 0)
@@ -3708,8 +3712,7 @@ copy_archive (bfd *ibfd, bfd *obfd, const char *output_target,
 	  l->next = list;
 	  l->obfd = NULL;
 	  list = l;
-	  output_name = concat (tmpdir, "/",
-				bfd_get_filename (this_element), (char *) 0);
+	  output_name = concat (tmpdir, "/", element_name, (char *) 0);
 	}
 
       if (preserve_dates)
@@ -3718,8 +3721,7 @@ copy_archive (bfd *ibfd, bfd *obfd, const char *output_target,
 	  stat_status = bfd_stat_arch_elt (this_element, &buf);
 
 	  if (stat_status != 0)
-	    non_fatal (_("internal stat error on %s"),
-		       bfd_get_filename (this_element));
+	    non_fatal (_("internal stat error on %s"), element_name);
 	}
 
       struct name_list *l = xmalloc (sizeof (*l));


More information about the Binutils-cvs mailing list