Blame SOURCES/binutils-CVE-2021-20197.patch

ccd7a4
diff -rup binutils.orig/bfd/bfd-in2.h binutils-2.35.1/bfd/bfd-in2.h
ccd7a4
--- binutils.orig/bfd/bfd-in2.h	2021-01-29 11:14:51.848568548 +0000
ccd7a4
+++ binutils-2.35.1/bfd/bfd-in2.h	2021-01-29 11:15:33.431322133 +0000
ccd7a4
@@ -583,6 +583,8 @@ bfd *bfd_openr (const char *filename, co
ccd7a4
 
ccd7a4
 bfd *bfd_fdopenr (const char *filename, const char *target, int fd);
ccd7a4
 
ccd7a4
+bfd *bfd_fdopenw (const char *filename, const char *target, int fd);
ccd7a4
+
ccd7a4
 bfd *bfd_openstreamr (const char * filename, const char * target,
ccd7a4
     void * stream);
ccd7a4
 
ccd7a4
diff -rup binutils.orig/bfd/opncls.c binutils-2.35.1/bfd/opncls.c
ccd7a4
--- binutils.orig/bfd/opncls.c	2021-01-29 11:14:51.846568560 +0000
ccd7a4
+++ binutils-2.35.1/bfd/opncls.c	2021-01-29 11:15:33.431322133 +0000
ccd7a4
@@ -395,6 +395,39 @@ bfd_fdopenr (const char *filename, const
ccd7a4
 
ccd7a4
 /*
ccd7a4
 FUNCTION
ccd7a4
+	bfd_fdopenw
ccd7a4
+
ccd7a4
+SYNOPSIS
ccd7a4
+	bfd *bfd_fdopenw (const char *filename, const char *target, int fd);
ccd7a4
+
ccd7a4
+DESCRIPTION
ccd7a4
+	<<bfd_fdopenw>> is exactly like <<bfd_fdopenr>> with the exception that
ccd7a4
+	the resulting BFD is suitable for output.
ccd7a4
+*/
ccd7a4
+
ccd7a4
+bfd *
ccd7a4
+bfd_fdopenw (const char *filename, const char *target, int fd)
ccd7a4
+{
ccd7a4
+  bfd *out = bfd_fdopenr (filename, target, fd);
ccd7a4
+
ccd7a4
+  if (out != NULL)
ccd7a4
+    {
ccd7a4
+      if (!bfd_write_p (out))
ccd7a4
+	{
ccd7a4
+	  close (fd);
ccd7a4
+	  _bfd_delete_bfd (out);
ccd7a4
+	  out = NULL;
ccd7a4
+	  bfd_set_error (bfd_error_invalid_operation);
ccd7a4
+	}
ccd7a4
+      else
ccd7a4
+	out->direction = write_direction;
ccd7a4
+    }
ccd7a4
+
ccd7a4
+  return out;
ccd7a4
+}
ccd7a4
+
ccd7a4
+/*
ccd7a4
+FUNCTION
ccd7a4
 	bfd_openstreamr
ccd7a4
 
ccd7a4
 SYNOPSIS
ccd7a4
diff -rup binutils.orig/binutils/ar.c binutils-2.35.1/binutils/ar.c
ccd7a4
--- binutils.orig/binutils/ar.c	2021-01-29 11:14:51.344571539 +0000
ccd7a4
+++ binutils-2.35.1/binutils/ar.c	2021-01-29 11:15:56.174187367 +0000
ccd7a4
@@ -25,6 +25,7 @@
ccd7a4
 
ccd7a4
 #include "sysdep.h"
ccd7a4
 #include "bfd.h"
ccd7a4
+#include "libbfd.h"
ccd7a4
 #include "libiberty.h"
ccd7a4
 #include "progress.h"
ccd7a4
 #include "getopt.h"
ccd7a4
@@ -1195,20 +1196,26 @@ write_archive (bfd *iarch)
ccd7a4
   bfd *obfd;
ccd7a4
   char *old_name, *new_name;
ccd7a4
   bfd *contents_head = iarch->archive_next;
ccd7a4
+  int ofd = -1;
ccd7a4
+  struct stat target_stat;
ccd7a4
+  bfd_boolean skip_stat = FALSE;
ccd7a4
 
ccd7a4
   old_name = (char *) xmalloc (strlen (bfd_get_filename (iarch)) + 1);
ccd7a4
   strcpy (old_name, bfd_get_filename (iarch));
ccd7a4
-  new_name = make_tempname (old_name);
ccd7a4
+  new_name = make_tempname (old_name, &ofd;;
ccd7a4
 
ccd7a4
   if (new_name == NULL)
ccd7a4
     bfd_fatal (_("could not create temporary file whilst writing archive"));
ccd7a4
 
ccd7a4
   output_filename = new_name;
ccd7a4
 
ccd7a4
-  obfd = bfd_openw (new_name, bfd_get_target (iarch));
ccd7a4
+  obfd = bfd_fdopenw (new_name, bfd_get_target (iarch), ofd);
ccd7a4
 
ccd7a4
   if (obfd == NULL)
ccd7a4
-    bfd_fatal (old_name);
ccd7a4
+    {
ccd7a4
+      close (ofd);
ccd7a4
+      bfd_fatal (old_name);
ccd7a4
+    }
ccd7a4
 
ccd7a4
   output_bfd = obfd;
ccd7a4
 
ccd7a4
@@ -1237,6 +1244,14 @@ write_archive (bfd *iarch)
ccd7a4
   if (!bfd_set_archive_head (obfd, contents_head))
ccd7a4
     bfd_fatal (old_name);
ccd7a4
 
ccd7a4
+#if !defined (_WIN32) || defined (__CYGWIN32__)
ccd7a4
+  ofd = dup (ofd);
ccd7a4
+  if (iarch == NULL || iarch->iostream == NULL)
ccd7a4
+    skip_stat = TRUE;
ccd7a4
+  else if (ofd == -1 || fstat (fileno (iarch->iostream), &target_stat) != 0)
ccd7a4
+    bfd_fatal (old_name);
ccd7a4
+#endif
ccd7a4
+
ccd7a4
   if (!bfd_close (obfd))
ccd7a4
     bfd_fatal (old_name);
ccd7a4
 
ccd7a4
@@ -1246,7 +1261,7 @@ write_archive (bfd *iarch)
ccd7a4
   /* We don't care if this fails; we might be creating the archive.  */
ccd7a4
   bfd_close (iarch);
ccd7a4
 
ccd7a4
-  if (smart_rename (new_name, old_name, 0) != 0)
ccd7a4
+  if (smart_rename (new_name, old_name, ofd, skip_stat ? NULL : &target_stat, 0) != 0)
ccd7a4
     xexit (1);
ccd7a4
   free (old_name);
ccd7a4
   free (new_name);
ccd7a4
diff -rup binutils.orig/binutils/arsup.c binutils-2.35.1/binutils/arsup.c
ccd7a4
--- binutils.orig/binutils/arsup.c	2021-01-29 11:14:51.350571503 +0000
ccd7a4
+++ binutils-2.35.1/binutils/arsup.c	2021-01-29 11:15:56.174187367 +0000
ccd7a4
@@ -345,13 +345,25 @@ ar_save (void)
ccd7a4
   else
ccd7a4
     {
ccd7a4
       char *ofilename = xstrdup (bfd_get_filename (obfd));
ccd7a4
+      bfd_boolean skip_stat = FALSE;
ccd7a4
+      struct stat target_stat;
ccd7a4
+      int ofd = -1;
ccd7a4
 
ccd7a4
       if (deterministic > 0)
ccd7a4
         obfd->flags |= BFD_DETERMINISTIC_OUTPUT;
ccd7a4
 
ccd7a4
+#if !defined (_WIN32) || defined (__CYGWIN32__)
ccd7a4
+      /* It's OK to fail; at worst it will result in SMART_RENAME using a slow
ccd7a4
+         copy fallback to write the output.  */
ccd7a4
+      ofd = dup (fileno (obfd->iostream));
ccd7a4
+      if (lstat (real_name, &target_stat) != 0)
ccd7a4
+	skip_stat = TRUE;
ccd7a4
+#endif
ccd7a4
+
ccd7a4
       bfd_close (obfd);
ccd7a4
 
ccd7a4
-      smart_rename (ofilename, real_name, 0);
ccd7a4
+      smart_rename (ofilename, real_name, ofd,
ccd7a4
+		    skip_stat ? NULL : &target_stat, 0);
ccd7a4
       obfd = 0;
ccd7a4
       free (ofilename);
ccd7a4
     }
ccd7a4
diff -rup binutils.orig/binutils/bucomm.c binutils-2.35.1/binutils/bucomm.c
ccd7a4
--- binutils.orig/binutils/bucomm.c	2021-01-29 11:14:51.422571073 +0000
ccd7a4
+++ binutils-2.35.1/binutils/bucomm.c	2021-01-29 11:15:33.431322133 +0000
ccd7a4
@@ -532,7 +532,7 @@ template_in_dir (const char *path)
ccd7a4
    as FILENAME.  */
ccd7a4
 
ccd7a4
 char *
ccd7a4
-make_tempname (const char *filename)
ccd7a4
+make_tempname (const char *filename, int *ofd)
ccd7a4
 {
ccd7a4
   char *tmpname = template_in_dir (filename);
ccd7a4
   int fd;
ccd7a4
@@ -550,7 +550,7 @@ make_tempname (const char *filename)
ccd7a4
       free (tmpname);
ccd7a4
       return NULL;
ccd7a4
     }
ccd7a4
-  close (fd);
ccd7a4
+  *ofd = fd;
ccd7a4
   return tmpname;
ccd7a4
 }
ccd7a4
 
ccd7a4
diff -rup binutils.orig/binutils/bucomm.h binutils-2.35.1/binutils/bucomm.h
ccd7a4
--- binutils.orig/binutils/bucomm.h	2021-01-29 11:14:51.350571503 +0000
ccd7a4
+++ binutils-2.35.1/binutils/bucomm.h	2021-01-29 11:15:56.174187367 +0000
ccd7a4
@@ -51,7 +51,7 @@ int display_info (void);
ccd7a4
 
ccd7a4
 void print_arelt_descr (FILE *, bfd *, bfd_boolean, bfd_boolean);
ccd7a4
 
ccd7a4
-char *make_tempname (const char *);
ccd7a4
+char *make_tempname (const char *, int *);
ccd7a4
 char *make_tempdir (const char *);
ccd7a4
 
ccd7a4
 bfd_vma parse_vma (const char *, const char *);
ccd7a4
@@ -71,7 +71,8 @@ extern void print_version (const char *)
ccd7a4
 /* In rename.c.  */
ccd7a4
 extern void set_times (const char *, const struct stat *);
ccd7a4
 
ccd7a4
-extern int smart_rename (const char *, const char *, int);
ccd7a4
+extern int smart_rename (const char *, const char *, int, struct stat *, int);
ccd7a4
+
ccd7a4
 
ccd7a4
 /* In libiberty.  */
ccd7a4
 void *xmalloc (size_t);
ccd7a4
diff -rup binutils.orig/binutils/objcopy.c binutils-2.35.1/binutils/objcopy.c
ccd7a4
--- binutils.orig/binutils/objcopy.c	2021-01-29 11:14:51.342571551 +0000
ccd7a4
+++ binutils-2.35.1/binutils/objcopy.c	2021-01-29 11:15:56.175187361 +0000
ccd7a4
@@ -20,6 +20,7 @@
ccd7a4
 
ccd7a4
 #include "sysdep.h"
ccd7a4
 #include "bfd.h"
ccd7a4
+#include "libbfd.h"
ccd7a4
 #include "progress.h"
ccd7a4
 #include "getopt.h"
ccd7a4
 #include "libiberty.h"
ccd7a4
@@ -3711,9 +3712,9 @@ set_long_section_mode (bfd *output_bfd,
ccd7a4
 /* The top-level control.  */
ccd7a4
 
ccd7a4
 static void
ccd7a4
-copy_file (const char *input_filename, const char *output_filename,
ccd7a4
-	   const char *input_target,   const char *output_target,
ccd7a4
-	   const bfd_arch_info_type *input_arch)
ccd7a4
+copy_file (const char *input_filename, const char *output_filename, int ofd,
ccd7a4
+	   struct stat *in_stat, const char *input_target,
ccd7a4
+	   const char *output_target, const bfd_arch_info_type *input_arch)
ccd7a4
 {
ccd7a4
   bfd *ibfd;
ccd7a4
   char **obj_matching;
ccd7a4
@@ -3732,7 +3733,7 @@ copy_file (const char *input_filename, c
ccd7a4
   /* To allow us to do "strip *" without dying on the first
ccd7a4
      non-object file, failures are nonfatal.  */
ccd7a4
   ibfd = bfd_openr (input_filename, input_target);
ccd7a4
-  if (ibfd == NULL)
ccd7a4
+  if (ibfd == NULL || fstat (fileno (ibfd->iostream), in_stat) != 0)
ccd7a4
     {
ccd7a4
       bfd_nonfatal_message (input_filename, NULL, NULL, NULL);
ccd7a4
       status = 1;
ccd7a4
@@ -3786,9 +3787,14 @@ copy_file (const char *input_filename, c
ccd7a4
       else
ccd7a4
 	force_output_target = TRUE;
ccd7a4
 
ccd7a4
-      obfd = bfd_openw (output_filename, output_target);
ccd7a4
+      if (ofd >= 0)
ccd7a4
+	obfd = bfd_fdopenw (output_filename, output_target, ofd);
ccd7a4
+      else
ccd7a4
+	obfd = bfd_openw (output_filename, output_target);
ccd7a4
+
ccd7a4
       if (obfd == NULL)
ccd7a4
 	{
ccd7a4
+	  close (ofd);
ccd7a4
 	  bfd_nonfatal_message (output_filename, NULL, NULL, NULL);
ccd7a4
 	  status = 1;
ccd7a4
 	  return;
ccd7a4
@@ -3816,13 +3822,19 @@ copy_file (const char *input_filename, c
ccd7a4
       if (output_target == NULL)
ccd7a4
 	output_target = bfd_get_target (ibfd);
ccd7a4
 
ccd7a4
-      obfd = bfd_openw (output_filename, output_target);
ccd7a4
+      if (ofd >= 0)
ccd7a4
+	obfd = bfd_fdopenw (output_filename, output_target, ofd);
ccd7a4
+      else
ccd7a4
+	obfd = bfd_openw (output_filename, output_target);
ccd7a4
+
ccd7a4
       if (obfd == NULL)
ccd7a4
  	{
ccd7a4
+	  close (ofd);
ccd7a4
  	  bfd_nonfatal_message (output_filename, NULL, NULL, NULL);
ccd7a4
  	  status = 1;
ccd7a4
  	  return;
ccd7a4
  	}
ccd7a4
+
ccd7a4
       /* This is a no-op on non-Coff targets.  */
ccd7a4
       set_long_section_mode (obfd, ibfd, long_section_names);
ccd7a4
 
ccd7a4
@@ -4786,6 +4798,8 @@ strip_main (int argc, char *argv[])
ccd7a4
       int hold_status = status;
ccd7a4
       struct stat statbuf;
ccd7a4
       char *tmpname;
ccd7a4
+      int tmpfd = -1;
ccd7a4
+      int copyfd = -1;
ccd7a4
 
ccd7a4
       if (get_file_size (argv[i]) < 1)
ccd7a4
 	{
ccd7a4
@@ -4793,18 +4807,18 @@ strip_main (int argc, char *argv[])
ccd7a4
 	  continue;
ccd7a4
 	}
ccd7a4
 
ccd7a4
-      if (preserve_dates)
ccd7a4
-	/* No need to check the return value of stat().
ccd7a4
-	   It has already been checked in get_file_size().  */
ccd7a4
-	stat (argv[i], &statbuf);
ccd7a4
-
ccd7a4
       if (output_file == NULL
ccd7a4
 	  || filename_cmp (argv[i], output_file) == 0)
ccd7a4
-	tmpname = make_tempname (argv[i]);
ccd7a4
+	tmpname = make_tempname (argv[i], &tmpfd);
ccd7a4
       else
ccd7a4
 	tmpname = output_file;
ccd7a4
 
ccd7a4
-      if (tmpname == NULL)
ccd7a4
+      if (tmpname == NULL
ccd7a4
+#if !defined (_WIN32) || defined (__CYGWIN32__)
ccd7a4
+	  /* Retain a copy of TMPFD since we will need it for SMART_RENAME.  */
ccd7a4
+	  || (tmpfd >= 0 && (copyfd = dup (tmpfd)) == -1)
ccd7a4
+#endif
ccd7a4
+      )
ccd7a4
 	{
ccd7a4
 	  bfd_nonfatal_message (argv[i], NULL, NULL,
ccd7a4
 				_("could not create temporary file to hold stripped copy"));
ccd7a4
@@ -4813,7 +4827,8 @@ strip_main (int argc, char *argv[])
ccd7a4
 	}
ccd7a4
 
ccd7a4
       status = 0;
ccd7a4
-      copy_file (argv[i], tmpname, input_target, output_target, NULL);
ccd7a4
+      copy_file (argv[i], tmpname, tmpfd, &statbuf, input_target,
ccd7a4
+		 output_target, NULL);
ccd7a4
       if (status == 0)
ccd7a4
 	{
ccd7a4
 	  if (preserve_dates)
ccd7a4
@@ -4821,12 +4836,18 @@ strip_main (int argc, char *argv[])
ccd7a4
 	  if (output_file != tmpname)
ccd7a4
 	    status = (smart_rename (tmpname,
ccd7a4
 				    output_file ? output_file : argv[i],
ccd7a4
-				    preserve_dates) != 0);
ccd7a4
+				    copyfd, &statbuf, preserve_dates) != 0);
ccd7a4
 	  if (status == 0)
ccd7a4
 	    status = hold_status;
ccd7a4
 	}
ccd7a4
       else
ccd7a4
-	unlink_if_ordinary (tmpname);
ccd7a4
+	{
ccd7a4
+#if !defined (_WIN32) || defined (__CYGWIN32__)
ccd7a4
+	  if (copyfd >= 0)
ccd7a4
+	    close (copyfd);
ccd7a4
+#endif
ccd7a4
+	  unlink_if_ordinary (tmpname);
ccd7a4
+	}
ccd7a4
       if (output_file != tmpname)
ccd7a4
 	free (tmpname);
ccd7a4
     }
ccd7a4
@@ -5033,7 +5054,8 @@ copy_main (int argc, char *argv[])
ccd7a4
   bfd_boolean formats_info = FALSE;
ccd7a4
   bfd_boolean use_globalize = FALSE;
ccd7a4
   bfd_boolean use_keep_global = FALSE;
ccd7a4
-  int c;
ccd7a4
+  int c, tmpfd = -1;
ccd7a4
+  int copyfd = -1;
ccd7a4
   struct stat statbuf;
ccd7a4
   const bfd_arch_info_type *input_arch = NULL;
ccd7a4
 
ccd7a4
@@ -5870,34 +5892,43 @@ copy_main (int argc, char *argv[])
ccd7a4
       convert_efi_target (efi);
ccd7a4
     }
ccd7a4
 
ccd7a4
-  if (preserve_dates)
ccd7a4
-    if (stat (input_filename, & statbuf) < 0)
ccd7a4
-      fatal (_("warning: could not locate '%s'.  System error message: %s"),
ccd7a4
-	     input_filename, strerror (errno));
ccd7a4
-
ccd7a4
   /* If there is no destination file, or the source and destination files
ccd7a4
      are the same, then create a temp and rename the result into the input.  */
ccd7a4
   if (output_filename == NULL
ccd7a4
       || filename_cmp (input_filename, output_filename) == 0)
ccd7a4
-    tmpname = make_tempname (input_filename);
ccd7a4
+    tmpname = make_tempname (input_filename, &tmpfd);
ccd7a4
   else
ccd7a4
     tmpname = output_filename;
ccd7a4
 
ccd7a4
-  if (tmpname == NULL)
ccd7a4
-    fatal (_("warning: could not create temporary file whilst copying '%s', (error: %s)"),
ccd7a4
-	   input_filename, strerror (errno));
ccd7a4
+  if (tmpname == NULL
ccd7a4
+#if !defined (_WIN32) || defined (__CYGWIN32__)
ccd7a4
+      /* Retain a copy of TMPFD since we will need it for SMART_RENAME.  */
ccd7a4
+      || (tmpfd >= 0 && (copyfd = dup (tmpfd)) == -1)
ccd7a4
+#endif
ccd7a4
+  )
ccd7a4
+    {
ccd7a4
+      fatal (_("warning: could not create temporary file whilst copying '%s', (error: %s)"),
ccd7a4
+	     input_filename, strerror (errno));
ccd7a4
+    }
ccd7a4
 
ccd7a4
-  copy_file (input_filename, tmpname, input_target, output_target, input_arch);
ccd7a4
+  copy_file (input_filename, tmpname, tmpfd, &statbuf, input_target,
ccd7a4
+	     output_target, input_arch);
ccd7a4
   if (status == 0)
ccd7a4
     {
ccd7a4
       if (preserve_dates)
ccd7a4
 	set_times (tmpname, &statbuf);
ccd7a4
       if (tmpname != output_filename)
ccd7a4
-	status = (smart_rename (tmpname, input_filename,
ccd7a4
+	status = (smart_rename (tmpname, input_filename, copyfd, &statbuf,
ccd7a4
 				preserve_dates) != 0);
ccd7a4
     }
ccd7a4
   else
ccd7a4
-    unlink_if_ordinary (tmpname);
ccd7a4
+    {
ccd7a4
+#if !defined (_WIN32) || defined (__CYGWIN32__)
ccd7a4
+      if (copyfd >= 0)
ccd7a4
+	close (copyfd);
ccd7a4
+#endif
ccd7a4
+      unlink_if_ordinary (tmpname);
ccd7a4
+    }
ccd7a4
 
ccd7a4
   if (tmpname != output_filename)
ccd7a4
     free (tmpname);
ccd7a4
diff -rup binutils.orig/binutils/rename.c binutils-2.35.1/binutils/rename.c
ccd7a4
--- binutils.orig/binutils/rename.c	2021-01-29 11:14:51.422571073 +0000
ccd7a4
+++ binutils-2.35.1/binutils/rename.c	2021-01-29 11:15:56.175187361 +0000
ccd7a4
@@ -131,17 +131,55 @@ set_times (const char *destination, cons
ccd7a4
 #endif
ccd7a4
 #endif
ccd7a4
 
ccd7a4
-/* Rename FROM to TO, copying if TO is a link.
ccd7a4
-   Return 0 if ok, -1 if error.  */
ccd7a4
+#if !defined (_WIN32) || defined (__CYGWIN32__)
ccd7a4
+/* Try to preserve the permission bits and ownership of an existing file when
ccd7a4
+   rename overwrites it.  FD is the file being renamed and TARGET_STAT has the
ccd7a4
+   status of the file that was overwritten.  */
ccd7a4
+static void
ccd7a4
+try_preserve_permissions (int fd, struct stat *target_stat)
ccd7a4
+{
ccd7a4
+  struct stat from_stat;
ccd7a4
+  int ret = 0;
ccd7a4
+
ccd7a4
+  if (fstat (fd, &from_stat) != 0)
ccd7a4
+    return;
ccd7a4
+
ccd7a4
+  int from_mode = from_stat.st_mode & 0777;
ccd7a4
+  int to_mode = target_stat->st_mode & 0777;
ccd7a4
+
ccd7a4
+  /* Fix up permissions before we potentially lose ownership with fchown.
ccd7a4
+     Clear the setxid bits because in case the fchown below fails then we don't
ccd7a4
+     want to end up with a sxid file owned by the invoking user.  If the user
ccd7a4
+     hasn't changed or if fchown succeeded, we add back the sxid bits at the
ccd7a4
+     end.  */
ccd7a4
+  if (from_mode != to_mode)
ccd7a4
+    fchmod (fd, to_mode);
ccd7a4
+
ccd7a4
+  /* Fix up ownership, this will clear the setxid bits.  */
ccd7a4
+  if (from_stat.st_uid != target_stat->st_uid
ccd7a4
+      || from_stat.st_gid != target_stat->st_gid)
ccd7a4
+    ret = fchown (fd, target_stat->st_uid, target_stat->st_gid);
ccd7a4
+
ccd7a4
+  /* Fix up the sxid bits if either the fchown wasn't needed or it
ccd7a4
+     succeeded.  */
ccd7a4
+  if (ret == 0)
ccd7a4
+    fchmod (fd, target_stat->st_mode & 07777);
ccd7a4
+}
ccd7a4
+#endif
ccd7a4
+
ccd7a4
+/* Rename FROM to TO, copying if TO is either a link or is not a regular file.
ccd7a4
+   FD is an open file descriptor pointing to FROM that we can use to safely fix
ccd7a4
+   up permissions of the file after renaming.  TARGET_STAT has the file status
ccd7a4
+   that is used to fix up permissions and timestamps after rename.  Return 0 if
ccd7a4
+   ok, -1 if error and FD is closed before returning.  */
ccd7a4
 
ccd7a4
 int
ccd7a4
-smart_rename (const char *from, const char *to, int preserve_dates ATTRIBUTE_UNUSED)
ccd7a4
+smart_rename (const char *from, const char *to, int fd ATTRIBUTE_UNUSED,
ccd7a4
+	      struct stat *target_stat ATTRIBUTE_UNUSED,
ccd7a4
+	      int preserve_dates ATTRIBUTE_UNUSED)
ccd7a4
 {
ccd7a4
-  bfd_boolean exists;
ccd7a4
-  struct stat s;
ccd7a4
   int ret = 0;
ccd7a4
-
ccd7a4
-  exists = lstat (to, &s) == 0;
ccd7a4
+  bfd_boolean exists = target_stat != NULL;
ccd7a4
 
ccd7a4
 #if defined (_WIN32) && !defined (__CYGWIN32__)
ccd7a4
   /* Win32, unlike unix, will not erase `to' in `rename(from, to)' but
ccd7a4
@@ -158,36 +196,35 @@ smart_rename (const char *from, const ch
ccd7a4
       unlink (from);
ccd7a4
     }
ccd7a4
 #else
ccd7a4
-  /* Use rename only if TO is not a symbolic link and has
ccd7a4
-     only one hard link, and we have permission to write to it.  */
ccd7a4
+  /* Avoid a full copy and use rename if we can fix up permissions of the
ccd7a4
+     file after renaming, i.e.:
ccd7a4
+
ccd7a4
+     - TO is not a symbolic link
ccd7a4
+     - TO is a regular file with only one hard link
ccd7a4
+     - We have permission to write to TO
ccd7a4
+     - FD is available to safely fix up permissions to be the same as the file
ccd7a4
+       we overwrote with the rename.
ccd7a4
+
ccd7a4
+     Note though that the actual file on disk that TARGET_STAT describes may
ccd7a4
+     have changed and we're only trying to preserve the status we know about.
ccd7a4
+     At no point do we try to interact with the new file changes, so there can
ccd7a4
+     only be two outcomes, i.e. either the external file change survives
ccd7a4
+     without knowledge of our change (if it happens after the rename syscall)
ccd7a4
+     or our rename and permissions fixup survive without any knowledge of the
ccd7a4
+     external change.  */
ccd7a4
   if (! exists
ccd7a4
-      || (!S_ISLNK (s.st_mode)
ccd7a4
-	  && S_ISREG (s.st_mode)
ccd7a4
-	  && (s.st_mode & S_IWUSR)
ccd7a4
-	  && s.st_nlink == 1)
ccd7a4
+      || (fd >= 0
ccd7a4
+	  && !S_ISLNK (target_stat->st_mode)
ccd7a4
+	  && S_ISREG (target_stat->st_mode)
ccd7a4
+	  && (target_stat->st_mode & S_IWUSR)
ccd7a4
+	  && target_stat->st_nlink == 1)
ccd7a4
       )
ccd7a4
     {
ccd7a4
       ret = rename (from, to);
ccd7a4
       if (ret == 0)
ccd7a4
 	{
ccd7a4
 	  if (exists)
ccd7a4
-	    {
ccd7a4
-	      /* Try to preserve the permission bits and ownership of
ccd7a4
-		 TO.  First get the mode right except for the setuid
ccd7a4
-		 bit.  Then change the ownership.  Then fix the setuid
ccd7a4
-		 bit.  We do the chmod before the chown because if the
ccd7a4
-		 chown succeeds, and we are a normal user, we won't be
ccd7a4
-		 able to do the chmod afterward.  We don't bother to
ccd7a4
-		 fix the setuid bit first because that might introduce
ccd7a4
-		 a fleeting security problem, and because the chown
ccd7a4
-		 will clear the setuid bit anyhow.  We only fix the
ccd7a4
-		 setuid bit if the chown succeeds, because we don't
ccd7a4
-		 want to introduce an unexpected setuid file owned by
ccd7a4
-		 the user running objcopy.  */
ccd7a4
-	      chmod (to, s.st_mode & 0777);
ccd7a4
-	      if (chown (to, s.st_uid, s.st_gid) >= 0)
ccd7a4
-		chmod (to, s.st_mode & 07777);
ccd7a4
-	    }
ccd7a4
+	    try_preserve_permissions (fd, target_stat);
ccd7a4
 	}
ccd7a4
       else
ccd7a4
 	{
ccd7a4
@@ -203,9 +240,11 @@ smart_rename (const char *from, const ch
ccd7a4
 	non_fatal (_("unable to copy file '%s'; reason: %s"), to, strerror (errno));
ccd7a4
 
ccd7a4
       if (preserve_dates)
ccd7a4
-	set_times (to, &s);
ccd7a4
+	set_times (to, target_stat);
ccd7a4
       unlink (from);
ccd7a4
     }
ccd7a4
+  if (fd >= 0)
ccd7a4
+    close (fd);
ccd7a4
 #endif /* _WIN32 && !__CYGWIN32__ */
ccd7a4
 
ccd7a4
   return ret;
ccd7a4
diff -rup binutils.orig/binutils/ar.c binutils-2.35.1/binutils/ar.c
ccd7a4
--- binutils.orig/binutils/ar.c	2021-02-02 13:01:42.257734944 +0000
ccd7a4
+++ binutils-2.35.1/binutils/ar.c	2021-02-02 13:11:13.340958352 +0000
ccd7a4
@@ -25,7 +25,6 @@
ccd7a4
 
ccd7a4
 #include "sysdep.h"
ccd7a4
 #include "bfd.h"
ccd7a4
-#include "libbfd.h"
ccd7a4
 #include "libiberty.h"
ccd7a4
 #include "progress.h"
ccd7a4
 #include "getopt.h"
ccd7a4
@@ -1082,7 +1081,7 @@ open_output_file (bfd * abfd)
ccd7a4
 		 output_filename, base);
ccd7a4
       output_filename = base;
ccd7a4
     }
ccd7a4
-  
ccd7a4
+
ccd7a4
   if (output_dir)
ccd7a4
     {
ccd7a4
       size_t len = strlen (output_dir);
ccd7a4
@@ -1099,7 +1098,7 @@ open_output_file (bfd * abfd)
ccd7a4
 
ccd7a4
   if (verbose)
ccd7a4
     printf ("x - %s\n", output_filename);
ccd7a4
-  
ccd7a4
+
ccd7a4
   FILE * ostream = fopen (output_filename, FOPEN_WB);
ccd7a4
   if (ostream == NULL)
ccd7a4
     {
ccd7a4
@@ -1198,10 +1197,8 @@ write_archive (bfd *iarch)
ccd7a4
   bfd *contents_head = iarch->archive_next;
ccd7a4
   int ofd = -1;
ccd7a4
   struct stat target_stat;
ccd7a4
-  bfd_boolean skip_stat = FALSE;
ccd7a4
 
ccd7a4
-  old_name = (char *) xmalloc (strlen (bfd_get_filename (iarch)) + 1);
ccd7a4
-  strcpy (old_name, bfd_get_filename (iarch));
ccd7a4
+  old_name = xstrdup (bfd_get_filename (iarch));
ccd7a4
   new_name = make_tempname (old_name, &ofd;;
ccd7a4
 
ccd7a4
   if (new_name == NULL)
ccd7a4
@@ -1246,11 +1243,9 @@ write_archive (bfd *iarch)
ccd7a4
 
ccd7a4
 #if !defined (_WIN32) || defined (__CYGWIN32__)
ccd7a4
   ofd = dup (ofd);
ccd7a4
-  if (iarch == NULL || iarch->iostream == NULL)
ccd7a4
-    skip_stat = TRUE;
ccd7a4
-  else if (ofd == -1 || fstat (fileno (iarch->iostream), &target_stat) != 0)
ccd7a4
-    bfd_fatal (old_name);
ccd7a4
 #endif
ccd7a4
+  if (ofd == -1 || bfd_stat (iarch, &target_stat) != 0)
ccd7a4
+    bfd_fatal (old_name);
ccd7a4
 
ccd7a4
   if (!bfd_close (obfd))
ccd7a4
     bfd_fatal (old_name);
ccd7a4
@@ -1261,7 +1256,7 @@ write_archive (bfd *iarch)
ccd7a4
   /* We don't care if this fails; we might be creating the archive.  */
ccd7a4
   bfd_close (iarch);
ccd7a4
 
ccd7a4
-  if (smart_rename (new_name, old_name, ofd, skip_stat ? NULL : &target_stat, 0) != 0)
ccd7a4
+  if (smart_rename (new_name, old_name, ofd, &target_stat, 0) != 0)
ccd7a4
     xexit (1);
ccd7a4
   free (old_name);
ccd7a4
   free (new_name);
ccd7a4
Only in binutils-2.35.1/binutils/: ar.c.orig
ccd7a4
Only in binutils-2.35.1/binutils/: ar.c.rej
ccd7a4
diff -rup binutils.orig/binutils/arsup.c binutils-2.35.1/binutils/arsup.c
ccd7a4
--- binutils.orig/binutils/arsup.c	2021-02-02 13:01:42.208735269 +0000
ccd7a4
+++ binutils-2.35.1/binutils/arsup.c	2021-02-02 13:11:55.725678308 +0000
ccd7a4
@@ -42,6 +42,8 @@ extern int deterministic;
ccd7a4
 
ccd7a4
 static bfd *obfd;
ccd7a4
 static char *real_name;
ccd7a4
+static char *temp_name;
ccd7a4
+static int real_ofd;
ccd7a4
 static FILE *outfile;
ccd7a4
 
ccd7a4
 static void
ccd7a4
@@ -149,27 +151,24 @@ maybequit (void)
ccd7a4
 void
ccd7a4
 ar_open (char *name, int t)
ccd7a4
 {
ccd7a4
-  char *tname;
ccd7a4
-  const char *bname = lbasename (name);
ccd7a4
-  real_name = name;
ccd7a4
-
ccd7a4
-  /* Prepend tmp- to the beginning, to avoid file-name clashes after
ccd7a4
-     truncation on filesystems with limited namespaces (DOS).  */
ccd7a4
-  if (asprintf (&tname, "%.*stmp-%s", (int) (bname - name), name, bname) == -1)
ccd7a4
+  real_name = xstrdup (name);
ccd7a4
+  temp_name = make_tempname (real_name, &real_ofd);
ccd7a4
+
ccd7a4
+  if (temp_name == NULL)
ccd7a4
     {
ccd7a4
-      fprintf (stderr, _("%s: Can't allocate memory for temp name (%s)\n"),
ccd7a4
+      fprintf (stderr, _("%s: Can't open temporary file (%s)\n"),
ccd7a4
 	       program_name, strerror(errno));
ccd7a4
       maybequit ();
ccd7a4
       return;
ccd7a4
     }
ccd7a4
 
ccd7a4
-  obfd = bfd_openw (tname, NULL);
ccd7a4
+  obfd = bfd_fdopenw (temp_name, NULL, real_ofd);
ccd7a4
 
ccd7a4
   if (!obfd)
ccd7a4
     {
ccd7a4
       fprintf (stderr,
ccd7a4
 	       _("%s: Can't open output archive %s\n"),
ccd7a4
-	       program_name,  tname);
ccd7a4
+	       program_name, temp_name);
ccd7a4
 
ccd7a4
       maybequit ();
ccd7a4
     }
ccd7a4
@@ -344,10 +343,9 @@ ar_save (void)
ccd7a4
     }
ccd7a4
   else
ccd7a4
     {
ccd7a4
-      char *ofilename = xstrdup (bfd_get_filename (obfd));
ccd7a4
       bfd_boolean skip_stat = FALSE;
ccd7a4
       struct stat target_stat;
ccd7a4
-      int ofd = -1;
ccd7a4
+      int ofd = real_ofd;
ccd7a4
 
ccd7a4
       if (deterministic > 0)
ccd7a4
         obfd->flags |= BFD_DETERMINISTIC_OUTPUT;
ccd7a4
@@ -355,17 +353,18 @@ ar_save (void)
ccd7a4
 #if !defined (_WIN32) || defined (__CYGWIN32__)
ccd7a4
       /* It's OK to fail; at worst it will result in SMART_RENAME using a slow
ccd7a4
          copy fallback to write the output.  */
ccd7a4
-      ofd = dup (fileno (obfd->iostream));
ccd7a4
-      if (lstat (real_name, &target_stat) != 0)
ccd7a4
-	skip_stat = TRUE;
ccd7a4
+      ofd = dup (ofd);
ccd7a4
 #endif
ccd7a4
-
ccd7a4
       bfd_close (obfd);
ccd7a4
 
ccd7a4
-      smart_rename (ofilename, real_name, ofd,
ccd7a4
+      if (ofd == -1 || fstat (ofd, &target_stat) != 0)
ccd7a4
+	skip_stat = TRUE;
ccd7a4
+
ccd7a4
+      smart_rename (temp_name, real_name, ofd,
ccd7a4
 		    skip_stat ? NULL : &target_stat, 0);
ccd7a4
       obfd = 0;
ccd7a4
-      free (ofilename);
ccd7a4
+      free (temp_name);
ccd7a4
+      free (real_name);
ccd7a4
     }
ccd7a4
 }
ccd7a4
 
ccd7a4
Only in binutils-2.35.1/binutils/: arsup.c.orig
ccd7a4
Only in binutils-2.35.1/binutils/: arsup.c.rej
ccd7a4
diff -rup binutils.orig/binutils/objcopy.c binutils-2.35.1/binutils/objcopy.c
ccd7a4
--- binutils.orig/binutils/objcopy.c	2021-02-02 13:01:42.214735229 +0000
ccd7a4
+++ binutils-2.35.1/binutils/objcopy.c	2021-02-02 13:13:27.613071192 +0000
ccd7a4
@@ -20,7 +20,6 @@
ccd7a4
 
ccd7a4
 #include "sysdep.h"
ccd7a4
 #include "bfd.h"
ccd7a4
-#include "libbfd.h"
ccd7a4
 #include "progress.h"
ccd7a4
 #include "getopt.h"
ccd7a4
 #include "libiberty.h"
ccd7a4
@@ -3733,7 +3732,7 @@ copy_file (const char *input_filename, c
ccd7a4
   /* To allow us to do "strip *" without dying on the first
ccd7a4
      non-object file, failures are nonfatal.  */
ccd7a4
   ibfd = bfd_openr (input_filename, input_target);
ccd7a4
-  if (ibfd == NULL || fstat (fileno (ibfd->iostream), in_stat) != 0)
ccd7a4
+  if (ibfd == NULL || bfd_stat (ibfd, in_stat) != 0)
ccd7a4
     {
ccd7a4
       bfd_nonfatal_message (input_filename, NULL, NULL, NULL);
ccd7a4
       status = 1;
ccd7a4
Only in binutils-2.35.1/binutils/: objcopy.c.orig
ccd7a4
Only in binutils-2.35.1/binutils/: objcopy.c.rej
ccd7a4
--- binutils.orig/binutils/arsup.c	2021-02-04 10:42:03.265729780 +0000
ccd7a4
+++ binutils-2.35.1/binutils/arsup.c	2021-02-04 10:45:48.439166658 +0000
ccd7a4
@@ -357,8 +357,21 @@ ar_save (void)
ccd7a4
 #endif
ccd7a4
       bfd_close (obfd);
ccd7a4
 
ccd7a4
-      if (ofd == -1 || fstat (ofd, &target_stat) != 0)
ccd7a4
-	skip_stat = TRUE;
ccd7a4
+      if (lstat (real_name, &target_stat) != 0)
ccd7a4
+	{
ccd7a4
+	  /* The temp file created in ar_open has mode 0600 as per mkstemp.
ccd7a4
+	     Create the real empty output file here so smart_rename will
ccd7a4
+	     update the mode according to the process umask.  */
ccd7a4
+	  obfd = bfd_openw (real_name, NULL);
ccd7a4
+	  if (obfd == NULL
ccd7a4
+	      || bfd_stat (obfd, &target_stat) != 0)
ccd7a4
+	    skip_stat = TRUE;
ccd7a4
+	  if (obfd != NULL)
ccd7a4
+	    {
ccd7a4
+	      bfd_set_format (obfd, bfd_archive);
ccd7a4
+	      bfd_close (obfd);
ccd7a4
+	    }
ccd7a4
+	}
ccd7a4
 
ccd7a4
       smart_rename (temp_name, real_name, ofd,
ccd7a4
 		    skip_stat ? NULL : &target_stat, 0);
ccd7a4
--- binutils.orig/binutils/rename.c	2021-02-08 11:02:58.767933783 +0000
ccd7a4
+++ binutils-2.35.1/binutils/rename.c	2021-02-08 11:20:37.539179363 +0000
ccd7a4
@@ -179,7 +179,10 @@ smart_rename (const char *from, const ch
ccd7a4
 	      int preserve_dates ATTRIBUTE_UNUSED)
ccd7a4
 {
ccd7a4
   int ret = 0;
ccd7a4
-  bfd_boolean exists = target_stat != NULL;
ccd7a4
+  struct stat to_stat;
ccd7a4
+  bfd_boolean exists;
ccd7a4
+
ccd7a4
+  exists = lstat (to, &to_stat) == 0;
ccd7a4
 
ccd7a4
 #if defined (_WIN32) && !defined (__CYGWIN32__)
ccd7a4
   /* Win32, unlike unix, will not erase `to' in `rename(from, to)' but
ccd7a4
@@ -214,16 +217,16 @@ smart_rename (const char *from, const ch
ccd7a4
      external change.  */
ccd7a4
   if (! exists
ccd7a4
       || (fd >= 0
ccd7a4
-	  && !S_ISLNK (target_stat->st_mode)
ccd7a4
-	  && S_ISREG (target_stat->st_mode)
ccd7a4
-	  && (target_stat->st_mode & S_IWUSR)
ccd7a4
-	  && target_stat->st_nlink == 1)
ccd7a4
+	  && !S_ISLNK (to_stat.st_mode)
ccd7a4
+	  && S_ISREG (to_stat.st_mode)
ccd7a4
+	  && (to_stat.st_mode & S_IWUSR)
ccd7a4
+	  && to_stat.st_nlink == 1)
ccd7a4
       )
ccd7a4
     {
ccd7a4
       ret = rename (from, to);
ccd7a4
       if (ret == 0)
ccd7a4
 	{
ccd7a4
-	  if (exists)
ccd7a4
+	  if (exists && target_stat != NULL)
ccd7a4
 	    try_preserve_permissions (fd, target_stat);
ccd7a4
 	}
ccd7a4
       else
ccd7a4
@@ -239,7 +242,7 @@ smart_rename (const char *from, const ch
ccd7a4
       if (ret != 0)
ccd7a4
 	non_fatal (_("unable to copy file '%s'; reason: %s"), to, strerror (errno));
ccd7a4
 
ccd7a4
-      if (preserve_dates)
ccd7a4
+      if (preserve_dates && target_stat != NULL)
ccd7a4
 	set_times (to, target_stat);
ccd7a4
       unlink (from);
ccd7a4
     }