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

bf7c0a
diff -rup binutils.orig/bfd/bfd-in2.h binutils-2.35.1/bfd/bfd-in2.h
bf7c0a
--- binutils.orig/bfd/bfd-in2.h	2021-01-29 11:14:51.848568548 +0000
bf7c0a
+++ binutils-2.35.1/bfd/bfd-in2.h	2021-01-29 11:15:33.431322133 +0000
bf7c0a
@@ -583,6 +583,8 @@ bfd *bfd_openr (const char *filename, co
bf7c0a
 
bf7c0a
 bfd *bfd_fdopenr (const char *filename, const char *target, int fd);
bf7c0a
 
bf7c0a
+bfd *bfd_fdopenw (const char *filename, const char *target, int fd);
bf7c0a
+
bf7c0a
 bfd *bfd_openstreamr (const char * filename, const char * target,
bf7c0a
     void * stream);
bf7c0a
 
bf7c0a
diff -rup binutils.orig/bfd/opncls.c binutils-2.35.1/bfd/opncls.c
bf7c0a
--- binutils.orig/bfd/opncls.c	2021-01-29 11:14:51.846568560 +0000
bf7c0a
+++ binutils-2.35.1/bfd/opncls.c	2021-01-29 11:15:33.431322133 +0000
bf7c0a
@@ -395,6 +395,39 @@ bfd_fdopenr (const char *filename, const
bf7c0a
 
bf7c0a
 /*
bf7c0a
 FUNCTION
bf7c0a
+	bfd_fdopenw
bf7c0a
+
bf7c0a
+SYNOPSIS
bf7c0a
+	bfd *bfd_fdopenw (const char *filename, const char *target, int fd);
bf7c0a
+
bf7c0a
+DESCRIPTION
bf7c0a
+	<<bfd_fdopenw>> is exactly like <<bfd_fdopenr>> with the exception that
bf7c0a
+	the resulting BFD is suitable for output.
bf7c0a
+*/
bf7c0a
+
bf7c0a
+bfd *
bf7c0a
+bfd_fdopenw (const char *filename, const char *target, int fd)
bf7c0a
+{
bf7c0a
+  bfd *out = bfd_fdopenr (filename, target, fd);
bf7c0a
+
bf7c0a
+  if (out != NULL)
bf7c0a
+    {
bf7c0a
+      if (!bfd_write_p (out))
bf7c0a
+	{
bf7c0a
+	  close (fd);
bf7c0a
+	  _bfd_delete_bfd (out);
bf7c0a
+	  out = NULL;
bf7c0a
+	  bfd_set_error (bfd_error_invalid_operation);
bf7c0a
+	}
bf7c0a
+      else
bf7c0a
+	out->direction = write_direction;
bf7c0a
+    }
bf7c0a
+
bf7c0a
+  return out;
bf7c0a
+}
bf7c0a
+
bf7c0a
+/*
bf7c0a
+FUNCTION
bf7c0a
 	bfd_openstreamr
bf7c0a
 
bf7c0a
 SYNOPSIS
bf7c0a
diff -rup binutils.orig/binutils/ar.c binutils-2.35.1/binutils/ar.c
bf7c0a
--- binutils.orig/binutils/ar.c	2021-01-29 11:14:51.344571539 +0000
bf7c0a
+++ binutils-2.35.1/binutils/ar.c	2021-01-29 11:15:56.174187367 +0000
bf7c0a
@@ -25,6 +25,7 @@
bf7c0a
 
bf7c0a
 #include "sysdep.h"
bf7c0a
 #include "bfd.h"
bf7c0a
+#include "libbfd.h"
bf7c0a
 #include "libiberty.h"
bf7c0a
 #include "progress.h"
bf7c0a
 #include "getopt.h"
bf7c0a
@@ -1195,20 +1196,26 @@ write_archive (bfd *iarch)
bf7c0a
   bfd *obfd;
bf7c0a
   char *old_name, *new_name;
bf7c0a
   bfd *contents_head = iarch->archive_next;
bf7c0a
+  int ofd = -1;
bf7c0a
+  struct stat target_stat;
bf7c0a
+  bfd_boolean skip_stat = FALSE;
bf7c0a
 
bf7c0a
   old_name = (char *) xmalloc (strlen (bfd_get_filename (iarch)) + 1);
bf7c0a
   strcpy (old_name, bfd_get_filename (iarch));
bf7c0a
-  new_name = make_tempname (old_name);
bf7c0a
+  new_name = make_tempname (old_name, &ofd;;
bf7c0a
 
bf7c0a
   if (new_name == NULL)
bf7c0a
     bfd_fatal (_("could not create temporary file whilst writing archive"));
bf7c0a
 
bf7c0a
   output_filename = new_name;
bf7c0a
 
bf7c0a
-  obfd = bfd_openw (new_name, bfd_get_target (iarch));
bf7c0a
+  obfd = bfd_fdopenw (new_name, bfd_get_target (iarch), ofd);
bf7c0a
 
bf7c0a
   if (obfd == NULL)
bf7c0a
-    bfd_fatal (old_name);
bf7c0a
+    {
bf7c0a
+      close (ofd);
bf7c0a
+      bfd_fatal (old_name);
bf7c0a
+    }
bf7c0a
 
bf7c0a
   output_bfd = obfd;
bf7c0a
 
bf7c0a
@@ -1237,6 +1244,14 @@ write_archive (bfd *iarch)
bf7c0a
   if (!bfd_set_archive_head (obfd, contents_head))
bf7c0a
     bfd_fatal (old_name);
bf7c0a
 
bf7c0a
+#if !defined (_WIN32) || defined (__CYGWIN32__)
bf7c0a
+  ofd = dup (ofd);
bf7c0a
+  if (iarch == NULL || iarch->iostream == NULL)
bf7c0a
+    skip_stat = TRUE;
bf7c0a
+  else if (ofd == -1 || fstat (fileno (iarch->iostream), &target_stat) != 0)
bf7c0a
+    bfd_fatal (old_name);
bf7c0a
+#endif
bf7c0a
+
bf7c0a
   if (!bfd_close (obfd))
bf7c0a
     bfd_fatal (old_name);
bf7c0a
 
bf7c0a
@@ -1246,7 +1261,7 @@ write_archive (bfd *iarch)
bf7c0a
   /* We don't care if this fails; we might be creating the archive.  */
bf7c0a
   bfd_close (iarch);
bf7c0a
 
bf7c0a
-  if (smart_rename (new_name, old_name, 0) != 0)
bf7c0a
+  if (smart_rename (new_name, old_name, ofd, skip_stat ? NULL : &target_stat, 0) != 0)
bf7c0a
     xexit (1);
bf7c0a
   free (old_name);
bf7c0a
   free (new_name);
bf7c0a
diff -rup binutils.orig/binutils/arsup.c binutils-2.35.1/binutils/arsup.c
bf7c0a
--- binutils.orig/binutils/arsup.c	2021-01-29 11:14:51.350571503 +0000
bf7c0a
+++ binutils-2.35.1/binutils/arsup.c	2021-01-29 11:15:56.174187367 +0000
bf7c0a
@@ -345,13 +345,25 @@ ar_save (void)
bf7c0a
   else
bf7c0a
     {
bf7c0a
       char *ofilename = xstrdup (bfd_get_filename (obfd));
bf7c0a
+      bfd_boolean skip_stat = FALSE;
bf7c0a
+      struct stat target_stat;
bf7c0a
+      int ofd = -1;
bf7c0a
 
bf7c0a
       if (deterministic > 0)
bf7c0a
         obfd->flags |= BFD_DETERMINISTIC_OUTPUT;
bf7c0a
 
bf7c0a
+#if !defined (_WIN32) || defined (__CYGWIN32__)
bf7c0a
+      /* It's OK to fail; at worst it will result in SMART_RENAME using a slow
bf7c0a
+         copy fallback to write the output.  */
bf7c0a
+      ofd = dup (fileno (obfd->iostream));
bf7c0a
+      if (lstat (real_name, &target_stat) != 0)
bf7c0a
+	skip_stat = TRUE;
bf7c0a
+#endif
bf7c0a
+
bf7c0a
       bfd_close (obfd);
bf7c0a
 
bf7c0a
-      smart_rename (ofilename, real_name, 0);
bf7c0a
+      smart_rename (ofilename, real_name, ofd,
bf7c0a
+		    skip_stat ? NULL : &target_stat, 0);
bf7c0a
       obfd = 0;
bf7c0a
       free (ofilename);
bf7c0a
     }
bf7c0a
diff -rup binutils.orig/binutils/bucomm.c binutils-2.35.1/binutils/bucomm.c
bf7c0a
--- binutils.orig/binutils/bucomm.c	2021-01-29 11:14:51.422571073 +0000
bf7c0a
+++ binutils-2.35.1/binutils/bucomm.c	2021-01-29 11:15:33.431322133 +0000
bf7c0a
@@ -532,7 +532,7 @@ template_in_dir (const char *path)
bf7c0a
    as FILENAME.  */
bf7c0a
 
bf7c0a
 char *
bf7c0a
-make_tempname (char *filename)
bf7c0a
+make_tempname (const char *filename, int *ofd)
bf7c0a
 {
bf7c0a
   char *tmpname = template_in_dir (filename);
bf7c0a
   int fd;
bf7c0a
@@ -550,7 +550,7 @@ make_tempname (const char *filename)
bf7c0a
       free (tmpname);
bf7c0a
       return NULL;
bf7c0a
     }
bf7c0a
-  close (fd);
bf7c0a
+  *ofd = fd;
bf7c0a
   return tmpname;
bf7c0a
 }
bf7c0a
 
bf7c0a
diff -rup binutils.orig/binutils/bucomm.h binutils-2.35.1/binutils/bucomm.h
bf7c0a
--- binutils.orig/binutils/bucomm.h	2021-01-29 11:14:51.350571503 +0000
bf7c0a
+++ binutils-2.35.1/binutils/bucomm.h	2021-01-29 11:15:56.174187367 +0000
bf7c0a
@@ -51,7 +51,7 @@ int display_info (void);
bf7c0a
 
bf7c0a
 void print_arelt_descr (FILE *, bfd *, bfd_boolean);
bf7c0a
 
bf7c0a
-char *make_tempname (char *);
bf7c0a
+char *make_tempname (const char *, int *);
bf7c0a
 char *make_tempdir (char *);
bf7c0a
 
bf7c0a
 bfd_vma parse_vma (const char *, const char *);
bf7c0a
@@ -71,7 +71,8 @@ extern void print_version (const char *)
bf7c0a
 /* In rename.c.  */
bf7c0a
 extern void set_times (const char *, const struct stat *);
bf7c0a
 
bf7c0a
-extern int smart_rename (const char *, const char *, int);
bf7c0a
+extern int smart_rename (const char *, const char *, int, struct stat *, int);
bf7c0a
+
bf7c0a
 
bf7c0a
 /* In libiberty.  */
bf7c0a
 void *xmalloc (size_t);
bf7c0a
diff -rup binutils.orig/binutils/objcopy.c binutils-2.35.1/binutils/objcopy.c
bf7c0a
--- binutils.orig/binutils/objcopy.c	2021-01-29 11:14:51.342571551 +0000
bf7c0a
+++ binutils-2.35.1/binutils/objcopy.c	2021-01-29 11:15:56.175187361 +0000
bf7c0a
@@ -20,6 +20,7 @@
bf7c0a
 
bf7c0a
 #include "sysdep.h"
bf7c0a
 #include "bfd.h"
bf7c0a
+#include "libbfd.h"
bf7c0a
 #include "progress.h"
bf7c0a
 #include "getopt.h"
bf7c0a
 #include "libiberty.h"
bf7c0a
@@ -3711,9 +3712,9 @@ set_long_section_mode (bfd *output_bfd,
bf7c0a
 /* The top-level control.  */
bf7c0a
 
bf7c0a
 static void
bf7c0a
-copy_file (const char *input_filename, const char *output_filename,
bf7c0a
-	   const char *input_target,   const char *output_target,
bf7c0a
-	   const bfd_arch_info_type *input_arch)
bf7c0a
+copy_file (const char *input_filename, const char *output_filename, int ofd,
bf7c0a
+	   struct stat *in_stat, const char *input_target,
bf7c0a
+	   const char *output_target, const bfd_arch_info_type *input_arch)
bf7c0a
 {
bf7c0a
   bfd *ibfd;
bf7c0a
   char **obj_matching;
bf7c0a
@@ -3732,7 +3733,7 @@ copy_file (const char *input_filename, c
bf7c0a
   /* To allow us to do "strip *" without dying on the first
bf7c0a
      non-object file, failures are nonfatal.  */
bf7c0a
   ibfd = bfd_openr (input_filename, input_target);
bf7c0a
-  if (ibfd == NULL)
bf7c0a
+  if (ibfd == NULL || fstat (fileno (ibfd->iostream), in_stat) != 0)
bf7c0a
     {
bf7c0a
       bfd_nonfatal_message (input_filename, NULL, NULL, NULL);
bf7c0a
       status = 1;
bf7c0a
@@ -3786,9 +3787,14 @@ copy_file (const char *input_filename, c
bf7c0a
       else
bf7c0a
 	force_output_target = TRUE;
bf7c0a
 
bf7c0a
-      obfd = bfd_openw (output_filename, output_target);
bf7c0a
+      if (ofd >= 0)
bf7c0a
+	obfd = bfd_fdopenw (output_filename, output_target, ofd);
bf7c0a
+      else
bf7c0a
+	obfd = bfd_openw (output_filename, output_target);
bf7c0a
+
bf7c0a
       if (obfd == NULL)
bf7c0a
 	{
bf7c0a
+	  close (ofd);
bf7c0a
 	  bfd_nonfatal_message (output_filename, NULL, NULL, NULL);
bf7c0a
 	  status = 1;
bf7c0a
 	  return;
bf7c0a
@@ -3816,13 +3822,19 @@ copy_file (const char *input_filename, c
bf7c0a
       if (output_target == NULL)
bf7c0a
 	output_target = bfd_get_target (ibfd);
bf7c0a
 
bf7c0a
-      obfd = bfd_openw (output_filename, output_target);
bf7c0a
+      if (ofd >= 0)
bf7c0a
+	obfd = bfd_fdopenw (output_filename, output_target, ofd);
bf7c0a
+      else
bf7c0a
+	obfd = bfd_openw (output_filename, output_target);
bf7c0a
+
bf7c0a
       if (obfd == NULL)
bf7c0a
  	{
bf7c0a
+	  close (ofd);
bf7c0a
  	  bfd_nonfatal_message (output_filename, NULL, NULL, NULL);
bf7c0a
  	  status = 1;
bf7c0a
  	  return;
bf7c0a
  	}
bf7c0a
+
bf7c0a
       /* This is a no-op on non-Coff targets.  */
bf7c0a
       set_long_section_mode (obfd, ibfd, long_section_names);
bf7c0a
 
bf7c0a
@@ -4786,6 +4798,8 @@ strip_main (int argc, char *argv[])
bf7c0a
       int hold_status = status;
bf7c0a
       struct stat statbuf;
bf7c0a
       char *tmpname;
bf7c0a
+      int tmpfd = -1;
bf7c0a
+      int copyfd = -1;
bf7c0a
 
bf7c0a
       if (get_file_size (argv[i]) < 1)
bf7c0a
 	{
bf7c0a
@@ -4793,18 +4807,18 @@ strip_main (int argc, char *argv[])
bf7c0a
 	  continue;
bf7c0a
 	}
bf7c0a
 
bf7c0a
-      if (preserve_dates)
bf7c0a
-	/* No need to check the return value of stat().
bf7c0a
-	   It has already been checked in get_file_size().  */
bf7c0a
-	stat (argv[i], &statbuf);
bf7c0a
-
bf7c0a
       if (output_file == NULL
bf7c0a
 	  || filename_cmp (argv[i], output_file) == 0)
bf7c0a
-	tmpname = make_tempname (argv[i]);
bf7c0a
+	tmpname = make_tempname (argv[i], &tmpfd);
bf7c0a
       else
bf7c0a
 	tmpname = output_file;
bf7c0a
 
bf7c0a
-      if (tmpname == NULL)
bf7c0a
+      if (tmpname == NULL
bf7c0a
+#if !defined (_WIN32) || defined (__CYGWIN32__)
bf7c0a
+	  /* Retain a copy of TMPFD since we will need it for SMART_RENAME.  */
bf7c0a
+	  || (tmpfd >= 0 && (copyfd = dup (tmpfd)) == -1)
bf7c0a
+#endif
bf7c0a
+      )
bf7c0a
 	{
bf7c0a
 	  bfd_nonfatal_message (argv[i], NULL, NULL,
bf7c0a
 				_("could not create temporary file to hold stripped copy"));
bf7c0a
@@ -4813,7 +4827,8 @@ strip_main (int argc, char *argv[])
bf7c0a
 	}
bf7c0a
 
bf7c0a
       status = 0;
bf7c0a
-      copy_file (argv[i], tmpname, input_target, output_target, NULL);
bf7c0a
+      copy_file (argv[i], tmpname, tmpfd, &statbuf, input_target,
bf7c0a
+		 output_target, NULL);
bf7c0a
       if (status == 0)
bf7c0a
 	{
bf7c0a
 	  if (preserve_dates)
bf7c0a
@@ -4821,12 +4836,18 @@ strip_main (int argc, char *argv[])
bf7c0a
 	  if (output_file != tmpname)
bf7c0a
 	    status = (smart_rename (tmpname,
bf7c0a
 				    output_file ? output_file : argv[i],
bf7c0a
-				    preserve_dates) != 0);
bf7c0a
+				    copyfd, &statbuf, preserve_dates) != 0);
bf7c0a
 	  if (status == 0)
bf7c0a
 	    status = hold_status;
bf7c0a
 	}
bf7c0a
       else
bf7c0a
-	unlink_if_ordinary (tmpname);
bf7c0a
+	{
bf7c0a
+#if !defined (_WIN32) || defined (__CYGWIN32__)
bf7c0a
+	  if (copyfd >= 0)
bf7c0a
+	    close (copyfd);
bf7c0a
+#endif
bf7c0a
+	  unlink_if_ordinary (tmpname);
bf7c0a
+	}
bf7c0a
       if (output_file != tmpname)
bf7c0a
 	free (tmpname);
bf7c0a
     }
bf7c0a
@@ -5033,7 +5054,8 @@ copy_main (int argc, char *argv[])
bf7c0a
   bfd_boolean show_version = FALSE;
bf7c0a
   bfd_boolean change_warn = TRUE;
bf7c0a
   bfd_boolean formats_info = FALSE;
bf7c0a
-  int c;
bf7c0a
+  int c, tmpfd = -1;
bf7c0a
+  int copyfd = -1;
bf7c0a
   struct stat statbuf;
bf7c0a
   const bfd_arch_info_type *input_arch = NULL;
bf7c0a
 
bf7c0a
@@ -5870,34 +5892,43 @@ copy_main (int argc, char *argv[])
bf7c0a
       convert_efi_target (efi);
bf7c0a
     }
bf7c0a
 
bf7c0a
-  if (preserve_dates)
bf7c0a
-    if (stat (input_filename, & statbuf) < 0)
bf7c0a
-      fatal (_("warning: could not locate '%s'.  System error message: %s"),
bf7c0a
-	     input_filename, strerror (errno));
bf7c0a
-
bf7c0a
   /* If there is no destination file, or the source and destination files
bf7c0a
      are the same, then create a temp and rename the result into the input.  */
bf7c0a
   if (output_filename == NULL
bf7c0a
       || filename_cmp (input_filename, output_filename) == 0)
bf7c0a
-    tmpname = make_tempname (input_filename);
bf7c0a
+    tmpname = make_tempname (input_filename, &tmpfd);
bf7c0a
   else
bf7c0a
     tmpname = output_filename;
bf7c0a
 
bf7c0a
-  if (tmpname == NULL)
bf7c0a
-    fatal (_("warning: could not create temporary file whilst copying '%s', (error: %s)"),
bf7c0a
-	   input_filename, strerror (errno));
bf7c0a
+  if (tmpname == NULL
bf7c0a
+#if !defined (_WIN32) || defined (__CYGWIN32__)
bf7c0a
+      /* Retain a copy of TMPFD since we will need it for SMART_RENAME.  */
bf7c0a
+      || (tmpfd >= 0 && (copyfd = dup (tmpfd)) == -1)
bf7c0a
+#endif
bf7c0a
+  )
bf7c0a
+    {
bf7c0a
+      fatal (_("warning: could not create temporary file whilst copying '%s', (error: %s)"),
bf7c0a
+	     input_filename, strerror (errno));
bf7c0a
+    }
bf7c0a
 
bf7c0a
-  copy_file (input_filename, tmpname, input_target, output_target, input_arch);
bf7c0a
+  copy_file (input_filename, tmpname, tmpfd, &statbuf, input_target,
bf7c0a
+	     output_target, input_arch);
bf7c0a
   if (status == 0)
bf7c0a
     {
bf7c0a
       if (preserve_dates)
bf7c0a
 	set_times (tmpname, &statbuf);
bf7c0a
       if (tmpname != output_filename)
bf7c0a
-	status = (smart_rename (tmpname, input_filename,
bf7c0a
+	status = (smart_rename (tmpname, input_filename, copyfd, &statbuf,
bf7c0a
 				preserve_dates) != 0);
bf7c0a
     }
bf7c0a
   else
bf7c0a
-    unlink_if_ordinary (tmpname);
bf7c0a
+    {
bf7c0a
+#if !defined (_WIN32) || defined (__CYGWIN32__)
bf7c0a
+      if (copyfd >= 0)
bf7c0a
+	close (copyfd);
bf7c0a
+#endif
bf7c0a
+      unlink_if_ordinary (tmpname);
bf7c0a
+    }
bf7c0a
 
bf7c0a
   if (tmpname != output_filename)
bf7c0a
     free (tmpname);
bf7c0a
diff -rup binutils.orig/binutils/rename.c binutils-2.35.1/binutils/rename.c
bf7c0a
--- binutils.orig/binutils/rename.c	2021-01-29 11:14:51.422571073 +0000
bf7c0a
+++ binutils-2.35.1/binutils/rename.c	2021-01-29 11:15:56.175187361 +0000
bf7c0a
@@ -131,17 +131,55 @@ set_times (const char *destination, cons
bf7c0a
 #endif
bf7c0a
 #endif
bf7c0a
 
bf7c0a
-/* Rename FROM to TO, copying if TO is a link.
bf7c0a
-   Return 0 if ok, -1 if error.  */
bf7c0a
+#if !defined (_WIN32) || defined (__CYGWIN32__)
bf7c0a
+/* Try to preserve the permission bits and ownership of an existing file when
bf7c0a
+   rename overwrites it.  FD is the file being renamed and TARGET_STAT has the
bf7c0a
+   status of the file that was overwritten.  */
bf7c0a
+static void
bf7c0a
+try_preserve_permissions (int fd, struct stat *target_stat)
bf7c0a
+{
bf7c0a
+  struct stat from_stat;
bf7c0a
+  int ret = 0;
bf7c0a
+
bf7c0a
+  if (fstat (fd, &from_stat) != 0)
bf7c0a
+    return;
bf7c0a
+
bf7c0a
+  int from_mode = from_stat.st_mode & 0777;
bf7c0a
+  int to_mode = target_stat->st_mode & 0777;
bf7c0a
+
bf7c0a
+  /* Fix up permissions before we potentially lose ownership with fchown.
bf7c0a
+     Clear the setxid bits because in case the fchown below fails then we don't
bf7c0a
+     want to end up with a sxid file owned by the invoking user.  If the user
bf7c0a
+     hasn't changed or if fchown succeeded, we add back the sxid bits at the
bf7c0a
+     end.  */
bf7c0a
+  if (from_mode != to_mode)
bf7c0a
+    fchmod (fd, to_mode);
bf7c0a
+
bf7c0a
+  /* Fix up ownership, this will clear the setxid bits.  */
bf7c0a
+  if (from_stat.st_uid != target_stat->st_uid
bf7c0a
+      || from_stat.st_gid != target_stat->st_gid)
bf7c0a
+    ret = fchown (fd, target_stat->st_uid, target_stat->st_gid);
bf7c0a
+
bf7c0a
+  /* Fix up the sxid bits if either the fchown wasn't needed or it
bf7c0a
+     succeeded.  */
bf7c0a
+  if (ret == 0)
bf7c0a
+    fchmod (fd, target_stat->st_mode & 07777);
bf7c0a
+}
bf7c0a
+#endif
bf7c0a
+
bf7c0a
+/* Rename FROM to TO, copying if TO is either a link or is not a regular file.
bf7c0a
+   FD is an open file descriptor pointing to FROM that we can use to safely fix
bf7c0a
+   up permissions of the file after renaming.  TARGET_STAT has the file status
bf7c0a
+   that is used to fix up permissions and timestamps after rename.  Return 0 if
bf7c0a
+   ok, -1 if error and FD is closed before returning.  */
bf7c0a
 
bf7c0a
 int
bf7c0a
-smart_rename (const char *from, const char *to, int preserve_dates ATTRIBUTE_UNUSED)
bf7c0a
+smart_rename (const char *from, const char *to, int fd ATTRIBUTE_UNUSED,
bf7c0a
+	      struct stat *target_stat ATTRIBUTE_UNUSED,
bf7c0a
+	      int preserve_dates ATTRIBUTE_UNUSED)
bf7c0a
 {
bf7c0a
-  bfd_boolean exists;
bf7c0a
-  struct stat s;
bf7c0a
   int ret = 0;
bf7c0a
-
bf7c0a
-  exists = lstat (to, &s) == 0;
bf7c0a
+  bfd_boolean exists = target_stat != NULL;
bf7c0a
 
bf7c0a
 #if defined (_WIN32) && !defined (__CYGWIN32__)
bf7c0a
   /* Win32, unlike unix, will not erase `to' in `rename(from, to)' but
bf7c0a
@@ -158,36 +196,35 @@ smart_rename (const char *from, const ch
bf7c0a
       unlink (from);
bf7c0a
     }
bf7c0a
 #else
bf7c0a
-  /* Use rename only if TO is not a symbolic link and has
bf7c0a
-     only one hard link, and we have permission to write to it.  */
bf7c0a
+  /* Avoid a full copy and use rename if we can fix up permissions of the
bf7c0a
+     file after renaming, i.e.:
bf7c0a
+
bf7c0a
+     - TO is not a symbolic link
bf7c0a
+     - TO is a regular file with only one hard link
bf7c0a
+     - We have permission to write to TO
bf7c0a
+     - FD is available to safely fix up permissions to be the same as the file
bf7c0a
+       we overwrote with the rename.
bf7c0a
+
bf7c0a
+     Note though that the actual file on disk that TARGET_STAT describes may
bf7c0a
+     have changed and we're only trying to preserve the status we know about.
bf7c0a
+     At no point do we try to interact with the new file changes, so there can
bf7c0a
+     only be two outcomes, i.e. either the external file change survives
bf7c0a
+     without knowledge of our change (if it happens after the rename syscall)
bf7c0a
+     or our rename and permissions fixup survive without any knowledge of the
bf7c0a
+     external change.  */
bf7c0a
   if (! exists
bf7c0a
-      || (!S_ISLNK (s.st_mode)
bf7c0a
-	  && S_ISREG (s.st_mode)
bf7c0a
-	  && (s.st_mode & S_IWUSR)
bf7c0a
-	  && s.st_nlink == 1)
bf7c0a
+      || (fd >= 0
bf7c0a
+	  && !S_ISLNK (target_stat->st_mode)
bf7c0a
+	  && S_ISREG (target_stat->st_mode)
bf7c0a
+	  && (target_stat->st_mode & S_IWUSR)
bf7c0a
+	  && target_stat->st_nlink == 1)
bf7c0a
       )
bf7c0a
     {
bf7c0a
       ret = rename (from, to);
bf7c0a
       if (ret == 0)
bf7c0a
 	{
bf7c0a
 	  if (exists)
bf7c0a
-	    {
bf7c0a
-	      /* Try to preserve the permission bits and ownership of
bf7c0a
-		 TO.  First get the mode right except for the setuid
bf7c0a
-		 bit.  Then change the ownership.  Then fix the setuid
bf7c0a
-		 bit.  We do the chmod before the chown because if the
bf7c0a
-		 chown succeeds, and we are a normal user, we won't be
bf7c0a
-		 able to do the chmod afterward.  We don't bother to
bf7c0a
-		 fix the setuid bit first because that might introduce
bf7c0a
-		 a fleeting security problem, and because the chown
bf7c0a
-		 will clear the setuid bit anyhow.  We only fix the
bf7c0a
-		 setuid bit if the chown succeeds, because we don't
bf7c0a
-		 want to introduce an unexpected setuid file owned by
bf7c0a
-		 the user running objcopy.  */
bf7c0a
-	      chmod (to, s.st_mode & 0777);
bf7c0a
-	      if (chown (to, s.st_uid, s.st_gid) >= 0)
bf7c0a
-		chmod (to, s.st_mode & 07777);
bf7c0a
-	    }
bf7c0a
+	    try_preserve_permissions (fd, target_stat);
bf7c0a
 	}
bf7c0a
       else
bf7c0a
 	{
bf7c0a
@@ -203,9 +240,11 @@ smart_rename (const char *from, const ch
bf7c0a
 	non_fatal (_("unable to copy file '%s'; reason: %s"), to, strerror (errno));
bf7c0a
 
bf7c0a
       if (preserve_dates)
bf7c0a
-	set_times (to, &s);
bf7c0a
+	set_times (to, target_stat);
bf7c0a
       unlink (from);
bf7c0a
     }
bf7c0a
+  if (fd >= 0)
bf7c0a
+    close (fd);
bf7c0a
 #endif /* _WIN32 && !__CYGWIN32__ */
bf7c0a
 
bf7c0a
   return ret;
bf7c0a
diff -rup binutils.orig/binutils/ar.c binutils-2.35.1/binutils/ar.c
bf7c0a
--- binutils.orig/binutils/ar.c	2021-02-02 13:01:42.257734944 +0000
bf7c0a
+++ binutils-2.35.1/binutils/ar.c	2021-02-02 13:11:13.340958352 +0000
bf7c0a
@@ -25,7 +25,6 @@
bf7c0a
 
bf7c0a
 #include "sysdep.h"
bf7c0a
 #include "bfd.h"
bf7c0a
-#include "libbfd.h"
bf7c0a
 #include "libiberty.h"
bf7c0a
 #include "progress.h"
bf7c0a
 #include "getopt.h"
bf7c0a
@@ -1198,10 +1197,8 @@ write_archive (bfd *iarch)
bf7c0a
   bfd *contents_head = iarch->archive_next;
bf7c0a
   int ofd = -1;
bf7c0a
   struct stat target_stat;
bf7c0a
-  bfd_boolean skip_stat = FALSE;
bf7c0a
 
bf7c0a
-  old_name = (char *) xmalloc (strlen (bfd_get_filename (iarch)) + 1);
bf7c0a
-  strcpy (old_name, bfd_get_filename (iarch));
bf7c0a
+  old_name = xstrdup (bfd_get_filename (iarch));
bf7c0a
   new_name = make_tempname (old_name, &ofd;;
bf7c0a
 
bf7c0a
   if (new_name == NULL)
bf7c0a
@@ -1246,11 +1243,9 @@ write_archive (bfd *iarch)
bf7c0a
 
bf7c0a
 #if !defined (_WIN32) || defined (__CYGWIN32__)
bf7c0a
   ofd = dup (ofd);
bf7c0a
-  if (iarch == NULL || iarch->iostream == NULL)
bf7c0a
-    skip_stat = TRUE;
bf7c0a
-  else if (ofd == -1 || fstat (fileno (iarch->iostream), &target_stat) != 0)
bf7c0a
-    bfd_fatal (old_name);
bf7c0a
 #endif
bf7c0a
+  if (ofd == -1 || bfd_stat (iarch, &target_stat) != 0)
bf7c0a
+    bfd_fatal (old_name);
bf7c0a
 
bf7c0a
   if (!bfd_close (obfd))
bf7c0a
     bfd_fatal (old_name);
bf7c0a
@@ -1261,7 +1256,7 @@ write_archive (bfd *iarch)
bf7c0a
   /* We don't care if this fails; we might be creating the archive.  */
bf7c0a
   bfd_close (iarch);
bf7c0a
 
bf7c0a
-  if (smart_rename (new_name, old_name, ofd, skip_stat ? NULL : &target_stat, 0) != 0)
bf7c0a
+  if (smart_rename (new_name, old_name, ofd, &target_stat, 0) != 0)
bf7c0a
     xexit (1);
bf7c0a
   free (old_name);
bf7c0a
   free (new_name);
bf7c0a
Only in binutils-2.35.1/binutils/: ar.c.orig
bf7c0a
Only in binutils-2.35.1/binutils/: ar.c.rej
bf7c0a
diff -rup binutils.orig/binutils/arsup.c binutils-2.35.1/binutils/arsup.c
bf7c0a
--- binutils.orig/binutils/arsup.c	2021-02-02 13:01:42.208735269 +0000
bf7c0a
+++ binutils-2.35.1/binutils/arsup.c	2021-02-02 13:11:55.725678308 +0000
bf7c0a
@@ -42,6 +42,8 @@ extern int deterministic;
bf7c0a
 
bf7c0a
 static bfd *obfd;
bf7c0a
 static char *real_name;
bf7c0a
+static char *temp_name;
bf7c0a
+static int real_ofd;
bf7c0a
 static FILE *outfile;
bf7c0a
 
bf7c0a
 static void
bf7c0a
@@ -149,20 +151,24 @@ maybequit (void)
bf7c0a
 void
bf7c0a
 ar_open (char *name, int t)
bf7c0a
 {
bf7c0a
-  char *tname = (char *) xmalloc (strlen (name) + 10);
bf7c0a
-  const char *bname = lbasename (name);
bf7c0a
-  real_name = name;
bf7c0a
-
bf7c0a
-  /* Prepend tmp- to the beginning, to avoid file-name clashes after
bf7c0a
-     truncation on filesystems with limited namespaces (DOS).  */
bf7c0a
-  sprintf (tname, "%.*stmp-%s", (int) (bname - name), name, bname);
bf7c0a
+  real_name = xstrdup (name);
bf7c0a
+  temp_name = make_tempname (real_name, &real_ofd);
bf7c0a
+
bf7c0a
+  if (temp_name == NULL)
bf7c0a
+     {
bf7c0a
+      fprintf (stderr, _("%s: Can't open temporary file (%s)\n"),
bf7c0a
+ 	       program_name, strerror(errno));
bf7c0a
+       maybequit ();
bf7c0a
+       return;
bf7c0a
+     }
bf7c0a
+
bf7c0a
-  obfd = bfd_openw (tname, NULL);
bf7c0a
+  obfd = bfd_fdopenw (temp_name, NULL, real_ofd);
bf7c0a
 
bf7c0a
   if (!obfd)
bf7c0a
     {
bf7c0a
       fprintf (stderr,
bf7c0a
 	       _("%s: Can't open output archive %s\n"),
bf7c0a
-	       program_name,  tname);
bf7c0a
+	       program_name, temp_name);
bf7c0a
 
bf7c0a
       maybequit ();
bf7c0a
     }
bf7c0a
@@ -344,10 +343,9 @@ ar_save (void)
bf7c0a
     }
bf7c0a
   else
bf7c0a
     {
bf7c0a
-      char *ofilename = xstrdup (bfd_get_filename (obfd));
bf7c0a
       bfd_boolean skip_stat = FALSE;
bf7c0a
       struct stat target_stat;
bf7c0a
-      int ofd = -1;
bf7c0a
+      int ofd = real_ofd;
bf7c0a
 
bf7c0a
       if (deterministic > 0)
bf7c0a
         obfd->flags |= BFD_DETERMINISTIC_OUTPUT;
bf7c0a
@@ -355,17 +353,18 @@ ar_save (void)
bf7c0a
 #if !defined (_WIN32) || defined (__CYGWIN32__)
bf7c0a
       /* It's OK to fail; at worst it will result in SMART_RENAME using a slow
bf7c0a
          copy fallback to write the output.  */
bf7c0a
-      ofd = dup (fileno (obfd->iostream));
bf7c0a
-      if (lstat (real_name, &target_stat) != 0)
bf7c0a
-	skip_stat = TRUE;
bf7c0a
+      ofd = dup (ofd);
bf7c0a
 #endif
bf7c0a
-
bf7c0a
       bfd_close (obfd);
bf7c0a
 
bf7c0a
-      smart_rename (ofilename, real_name, ofd,
bf7c0a
+      if (ofd == -1 || fstat (ofd, &target_stat) != 0)
bf7c0a
+	skip_stat = TRUE;
bf7c0a
+
bf7c0a
+      smart_rename (temp_name, real_name, ofd,
bf7c0a
 		    skip_stat ? NULL : &target_stat, 0);
bf7c0a
       obfd = 0;
bf7c0a
-      free (ofilename);
bf7c0a
+      free (temp_name);
bf7c0a
+      free (real_name);
bf7c0a
     }
bf7c0a
 }
bf7c0a
 
bf7c0a
diff -rup binutils.orig/binutils/objcopy.c binutils-2.35.1/binutils/objcopy.c
bf7c0a
--- binutils.orig/binutils/objcopy.c	2021-02-02 13:01:42.214735229 +0000
bf7c0a
+++ binutils-2.35.1/binutils/objcopy.c	2021-02-02 13:13:27.613071192 +0000
bf7c0a
@@ -20,7 +20,6 @@
bf7c0a
 
bf7c0a
 #include "sysdep.h"
bf7c0a
 #include "bfd.h"
bf7c0a
-#include "libbfd.h"
bf7c0a
 #include "progress.h"
bf7c0a
 #include "getopt.h"
bf7c0a
 #include "libiberty.h"
bf7c0a
@@ -3733,7 +3732,7 @@ copy_file (const char *input_filename, c
bf7c0a
   /* To allow us to do "strip *" without dying on the first
bf7c0a
      non-object file, failures are nonfatal.  */
bf7c0a
   ibfd = bfd_openr (input_filename, input_target);
bf7c0a
-  if (ibfd == NULL || fstat (fileno (ibfd->iostream), in_stat) != 0)
bf7c0a
+  if (ibfd == NULL || bfd_stat (ibfd, in_stat) != 0)
bf7c0a
     {
bf7c0a
       bfd_nonfatal_message (input_filename, NULL, NULL, NULL);
bf7c0a
       status = 1;
bf7c0a
--- binutils.orig/binutils/arsup.c	2021-02-04 10:42:03.265729780 +0000
bf7c0a
+++ binutils-2.35.1/binutils/arsup.c	2021-02-04 10:45:48.439166658 +0000
bf7c0a
@@ -357,8 +357,21 @@ ar_save (void)
bf7c0a
 #endif
bf7c0a
       bfd_close (obfd);
bf7c0a
 
bf7c0a
-      if (ofd == -1 || fstat (ofd, &target_stat) != 0)
bf7c0a
-	skip_stat = TRUE;
bf7c0a
+      if (lstat (real_name, &target_stat) != 0)
bf7c0a
+	{
bf7c0a
+	  /* The temp file created in ar_open has mode 0600 as per mkstemp.
bf7c0a
+	     Create the real empty output file here so smart_rename will
bf7c0a
+	     update the mode according to the process umask.  */
bf7c0a
+	  obfd = bfd_openw (real_name, NULL);
bf7c0a
+	  if (obfd == NULL
bf7c0a
+	      || bfd_stat (obfd, &target_stat) != 0)
bf7c0a
+	    skip_stat = TRUE;
bf7c0a
+	  if (obfd != NULL)
bf7c0a
+	    {
bf7c0a
+	      bfd_set_format (obfd, bfd_archive);
bf7c0a
+	      bfd_close (obfd);
bf7c0a
+	    }
bf7c0a
+	}
bf7c0a
 
bf7c0a
       smart_rename (temp_name, real_name, ofd,
bf7c0a
 		    skip_stat ? NULL : &target_stat, 0);
bf7c0a
--- binutils.orig/binutils/rename.c	2021-02-08 11:02:58.767933783 +0000
bf7c0a
+++ binutils-2.35.1/binutils/rename.c	2021-02-08 11:20:37.539179363 +0000
bf7c0a
@@ -179,7 +179,10 @@ smart_rename (const char *from, const ch
bf7c0a
 	      int preserve_dates ATTRIBUTE_UNUSED)
bf7c0a
 {
bf7c0a
   int ret = 0;
bf7c0a
-  bfd_boolean exists = target_stat != NULL;
bf7c0a
+  struct stat to_stat;
bf7c0a
+  bfd_boolean exists;
bf7c0a
+
bf7c0a
+  exists = lstat (to, &to_stat) == 0;
bf7c0a
 
bf7c0a
 #if defined (_WIN32) && !defined (__CYGWIN32__)
bf7c0a
   /* Win32, unlike unix, will not erase `to' in `rename(from, to)' but
bf7c0a
@@ -214,16 +217,16 @@ smart_rename (const char *from, const ch
bf7c0a
      external change.  */
bf7c0a
   if (! exists
bf7c0a
       || (fd >= 0
bf7c0a
-	  && !S_ISLNK (target_stat->st_mode)
bf7c0a
-	  && S_ISREG (target_stat->st_mode)
bf7c0a
-	  && (target_stat->st_mode & S_IWUSR)
bf7c0a
-	  && target_stat->st_nlink == 1)
bf7c0a
+	  && !S_ISLNK (to_stat.st_mode)
bf7c0a
+	  && S_ISREG (to_stat.st_mode)
bf7c0a
+	  && (to_stat.st_mode & S_IWUSR)
bf7c0a
+	  && to_stat.st_nlink == 1)
bf7c0a
       )
bf7c0a
     {
bf7c0a
       ret = rename (from, to);
bf7c0a
       if (ret == 0)
bf7c0a
 	{
bf7c0a
-	  if (exists)
bf7c0a
+	  if (exists && target_stat != NULL)
bf7c0a
 	    try_preserve_permissions (fd, target_stat);
bf7c0a
 	}
bf7c0a
       else
bf7c0a
@@ -239,7 +242,7 @@ smart_rename (const char *from, const ch
bf7c0a
       if (ret != 0)
bf7c0a
 	non_fatal (_("unable to copy file '%s'; reason: %s"), to, strerror (errno));
bf7c0a
 
bf7c0a
-      if (preserve_dates)
bf7c0a
+      if (preserve_dates && target_stat != NULL)
bf7c0a
 	set_times (to, target_stat);
bf7c0a
       unlink (from);
bf7c0a
     }
bf7c0a
diff -rup binutils.orig/binutils/ar.c binutils-2.35.1/binutils/ar.c
bf7c0a
--- binutils.orig/binutils/ar.c	2021-03-11 12:38:18.183422774 +0000
bf7c0a
+++ binutils-2.35.1/binutils/ar.c	2021-03-11 12:45:09.279716067 +0000
bf7c0a
@@ -1195,22 +1195,21 @@ write_archive (bfd *iarch)
bf7c0a
   bfd *obfd;
bf7c0a
   char *old_name, *new_name;
bf7c0a
   bfd *contents_head = iarch->archive_next;
bf7c0a
-  int ofd = -1;
bf7c0a
-  struct stat target_stat;
bf7c0a
+  int tmpfd = -1;
bf7c0a
 
bf7c0a
   old_name = xstrdup (bfd_get_filename (iarch));
bf7c0a
-  new_name = make_tempname (old_name, &ofd;;
bf7c0a
+  new_name = make_tempname (old_name, &tmpfd);
bf7c0a
 
bf7c0a
   if (new_name == NULL)
bf7c0a
     bfd_fatal (_("could not create temporary file whilst writing archive"));
bf7c0a
 
bf7c0a
   output_filename = new_name;
bf7c0a
 
bf7c0a
-  obfd = bfd_fdopenw (new_name, bfd_get_target (iarch), ofd);
bf7c0a
+  obfd = bfd_fdopenw (new_name, bfd_get_target (iarch), tmpfd);
bf7c0a
 
bf7c0a
   if (obfd == NULL)
bf7c0a
     {
bf7c0a
-      close (ofd);
bf7c0a
+      close (tmpfd);
bf7c0a
       bfd_fatal (old_name);
bf7c0a
     }
bf7c0a
 
bf7c0a
@@ -1241,12 +1240,7 @@ write_archive (bfd *iarch)
bf7c0a
   if (!bfd_set_archive_head (obfd, contents_head))
bf7c0a
     bfd_fatal (old_name);
bf7c0a
 
bf7c0a
-#if !defined (_WIN32) || defined (__CYGWIN32__)
bf7c0a
-  ofd = dup (ofd);
bf7c0a
-#endif
bf7c0a
-  if (ofd == -1 || bfd_stat (iarch, &target_stat) != 0)
bf7c0a
-    bfd_fatal (old_name);
bf7c0a
-
bf7c0a
+  tmpfd = dup (tmpfd);
bf7c0a
   if (!bfd_close (obfd))
bf7c0a
     bfd_fatal (old_name);
bf7c0a
 
bf7c0a
@@ -1256,7 +1250,7 @@ write_archive (bfd *iarch)
bf7c0a
   /* We don't care if this fails; we might be creating the archive.  */
bf7c0a
   bfd_close (iarch);
bf7c0a
 
bf7c0a
-  if (smart_rename (new_name, old_name, ofd, &target_stat, 0) != 0)
bf7c0a
+  if (smart_rename (new_name, old_name, tmpfd, NULL, FALSE) != 0)
bf7c0a
     xexit (1);
bf7c0a
   free (old_name);
bf7c0a
   free (new_name);
bf7c0a
diff -rup binutils.orig/binutils/arsup.c binutils-2.35.1/binutils/arsup.c
bf7c0a
--- binutils.orig/binutils/arsup.c	2021-03-11 12:38:18.182422781 +0000
bf7c0a
+++ binutils-2.35.1/binutils/arsup.c	2021-03-11 12:47:43.246702325 +0000
bf7c0a
@@ -43,7 +43,7 @@ extern int deterministic;
bf7c0a
 static bfd *obfd;
bf7c0a
 static char *real_name;
bf7c0a
 static char *temp_name;
bf7c0a
-static int real_ofd;
bf7c0a
+static int temp_fd;
bf7c0a
 static FILE *outfile;
bf7c0a
 
bf7c0a
 static void
bf7c0a
@@ -152,7 +152,7 @@ void
bf7c0a
 ar_open (char *name, int t)
bf7c0a
 {
bf7c0a
   real_name = xstrdup (name);
bf7c0a
-  temp_name = make_tempname (real_name, &real_ofd);
bf7c0a
+  temp_name = make_tempname (real_name, &temp_fd);
bf7c0a
 
bf7c0a
   if (temp_name == NULL)
bf7c0a
      {
bf7c0a
@@ -162,7 +162,7 @@ ar_open (char *name, int t)
bf7c0a
        return;
bf7c0a
      }
bf7c0a
 
bf7c0a
-  obfd = bfd_fdopenw (temp_name, NULL, real_ofd);
bf7c0a
+  obfd = bfd_fdopenw (temp_name, NULL, temp_fd);
bf7c0a
 
bf7c0a
   if (!obfd)
bf7c0a
     {
bf7c0a
@@ -343,29 +343,20 @@ ar_save (void)
bf7c0a
     }
bf7c0a
   else
bf7c0a
     {
bf7c0a
-      bfd_boolean skip_stat = FALSE;
bf7c0a
       struct stat target_stat;
bf7c0a
-      int ofd = real_ofd;
bf7c0a
 
bf7c0a
       if (deterministic > 0)
bf7c0a
         obfd->flags |= BFD_DETERMINISTIC_OUTPUT;
bf7c0a
 
bf7c0a
-#if !defined (_WIN32) || defined (__CYGWIN32__)
bf7c0a
-      /* It's OK to fail; at worst it will result in SMART_RENAME using a slow
bf7c0a
-         copy fallback to write the output.  */
bf7c0a
-      ofd = dup (ofd);
bf7c0a
-#endif
bf7c0a
+      temp_fd = dup (temp_fd);
bf7c0a
       bfd_close (obfd);
bf7c0a
 
bf7c0a
-      if (lstat (real_name, &target_stat) != 0)
bf7c0a
+      if (stat (real_name, &target_stat) != 0)
bf7c0a
 	{
bf7c0a
 	  /* The temp file created in ar_open has mode 0600 as per mkstemp.
bf7c0a
 	     Create the real empty output file here so smart_rename will
bf7c0a
 	     update the mode according to the process umask.  */
bf7c0a
 	  obfd = bfd_openw (real_name, NULL);
bf7c0a
-	  if (obfd == NULL
bf7c0a
-	      || bfd_stat (obfd, &target_stat) != 0)
bf7c0a
-	    skip_stat = TRUE;
bf7c0a
 	  if (obfd != NULL)
bf7c0a
 	    {
bf7c0a
 	      bfd_set_format (obfd, bfd_archive);
bf7c0a
@@ -373,9 +364,8 @@ ar_save (void)
bf7c0a
 	    }
bf7c0a
 	}
bf7c0a
 
bf7c0a
-      smart_rename (temp_name, real_name, ofd,
bf7c0a
-		    skip_stat ? NULL : &target_stat, 0);
bf7c0a
-      obfd = 0;
bf7c0a
+      smart_rename (temp_name, real_name, temp_fd, NULL, FALSE);
bf7c0a
+      obfd = NULL;
bf7c0a
       free (temp_name);
bf7c0a
       free (real_name);
bf7c0a
     }
bf7c0a
diff -rup binutils.orig/binutils/bucomm.h binutils-2.35.1/binutils/bucomm.h
bf7c0a
--- binutils.orig/binutils/bucomm.h	2021-03-11 12:38:18.183422774 +0000
bf7c0a
+++ binutils-2.35.1/binutils/bucomm.h	2021-03-11 12:42:22.320815334 +0000
bf7c0a
@@ -71,7 +71,8 @@ extern void print_version (const char *)
bf7c0a
 /* In rename.c.  */
bf7c0a
 extern void set_times (const char *, const struct stat *);
bf7c0a
 
bf7c0a
-extern int smart_rename (const char *, const char *, int, struct stat *, int);
bf7c0a
+extern int smart_rename (const char *, const char *, int,
bf7c0a
+			 struct stat *, bfd_boolean);
bf7c0a
 
bf7c0a
 
bf7c0a
 /* In libiberty.  */
bf7c0a
diff -rup binutils.orig/binutils/objcopy.c binutils-2.35.1/binutils/objcopy.c
bf7c0a
--- binutils.orig/binutils/objcopy.c	2021-03-11 12:38:18.181422787 +0000
bf7c0a
+++ binutils-2.35.1/binutils/objcopy.c	2021-03-11 12:51:09.486344417 +0000
bf7c0a
@@ -4802,12 +4802,7 @@ strip_main (int argc, char *argv[])
bf7c0a
       else
bf7c0a
 	tmpname = output_file;
bf7c0a
 
bf7c0a
-      if (tmpname == NULL
bf7c0a
-#if !defined (_WIN32) || defined (__CYGWIN32__)
bf7c0a
-	  /* Retain a copy of TMPFD since we will need it for SMART_RENAME.  */
bf7c0a
-	  || (tmpfd >= 0 && (copyfd = dup (tmpfd)) == -1)
bf7c0a
-#endif
bf7c0a
-      )
bf7c0a
+      if (tmpname == NULL)
bf7c0a
 	{
bf7c0a
 	  bfd_nonfatal_message (argv[i], NULL, NULL,
bf7c0a
 				_("could not create temporary file to hold stripped copy"));
bf7c0a
@@ -4820,21 +4815,17 @@ strip_main (int argc, char *argv[])
bf7c0a
 		 output_target, NULL);
bf7c0a
       if (status == 0)
bf7c0a
 	{
bf7c0a
-	  if (preserve_dates)
bf7c0a
-	    set_times (tmpname, &statbuf);
bf7c0a
 	  if (output_file != tmpname)
bf7c0a
-	    status = (smart_rename (tmpname,
bf7c0a
-				    output_file ? output_file : argv[i],
bf7c0a
-				    copyfd, &statbuf, preserve_dates) != 0);
bf7c0a
+	    status = smart_rename (tmpname,
bf7c0a
+				   output_file ? output_file : argv[i],
bf7c0a
+				   copyfd, &statbuf, preserve_dates) != 0;
bf7c0a
 	  if (status == 0)
bf7c0a
 	    status = hold_status;
bf7c0a
 	}
bf7c0a
       else
bf7c0a
 	{
bf7c0a
-#if !defined (_WIN32) || defined (__CYGWIN32__)
bf7c0a
 	  if (copyfd >= 0)
bf7c0a
 	    close (copyfd);
bf7c0a
-#endif
bf7c0a
 	  unlink_if_ordinary (tmpname);
bf7c0a
 	}
bf7c0a
       if (output_file != tmpname)
bf7c0a
@@ -5043,8 +5034,9 @@ copy_main (int argc, char *argv[])
bf7c0a
   bfd_boolean show_version = FALSE;
bf7c0a
   bfd_boolean change_warn = TRUE;
bf7c0a
   bfd_boolean formats_info = FALSE;
bf7c0a
-  int c, tmpfd = -1;
bf7c0a
-  int copyfd = -1;
bf7c0a
+  int c;
bf7c0a
+  int tmpfd = -1;
bf7c0a
+  int copyfd;
bf7c0a
   struct stat statbuf;
bf7c0a
   const bfd_arch_info_type *input_arch = NULL;
bf7c0a
 
bf7c0a
@@ -5882,19 +5874,19 @@ copy_main (int argc, char *argv[])
bf7c0a
     }
bf7c0a
 
bf7c0a
   /* If there is no destination file, or the source and destination files
bf7c0a
-     are the same, then create a temp and rename the result into the input.  */
bf7c0a
+     are the same, then create a temp and copy the result into the input.  */
bf7c0a
+  copyfd = -1;
bf7c0a
   if (output_filename == NULL
bf7c0a
       || filename_cmp (input_filename, output_filename) == 0)
bf7c0a
-    tmpname = make_tempname (input_filename, &tmpfd);
bf7c0a
+    {
bf7c0a
+      tmpname = make_tempname (input_filename, &tmpfd);
bf7c0a
+      if (tmpfd >= 0)
bf7c0a
+	copyfd = dup (tmpfd);
bf7c0a
+    }
bf7c0a
   else
bf7c0a
     tmpname = output_filename;
bf7c0a
 
bf7c0a
-  if (tmpname == NULL
bf7c0a
-#if !defined (_WIN32) || defined (__CYGWIN32__)
bf7c0a
-      /* Retain a copy of TMPFD since we will need it for SMART_RENAME.  */
bf7c0a
-      || (tmpfd >= 0 && (copyfd = dup (tmpfd)) == -1)
bf7c0a
-#endif
bf7c0a
-  )
bf7c0a
+  if (tmpname == NULL)
bf7c0a
     {
bf7c0a
       fatal (_("warning: could not create temporary file whilst copying '%s', (error: %s)"),
bf7c0a
 	     input_filename, strerror (errno));
bf7c0a
@@ -5904,18 +5896,14 @@ copy_main (int argc, char *argv[])
bf7c0a
 	     output_target, input_arch);
bf7c0a
   if (status == 0)
bf7c0a
     {
bf7c0a
-      if (preserve_dates)
bf7c0a
-	set_times (tmpname, &statbuf);
bf7c0a
       if (tmpname != output_filename)
bf7c0a
-	status = (smart_rename (tmpname, input_filename, copyfd, &statbuf,
bf7c0a
-				preserve_dates) != 0);
bf7c0a
+	status = smart_rename (tmpname, input_filename, copyfd,
bf7c0a
+			       &statbuf, preserve_dates) != 0;
bf7c0a
     }
bf7c0a
   else
bf7c0a
     {
bf7c0a
-#if !defined (_WIN32) || defined (__CYGWIN32__)
bf7c0a
       if (copyfd >= 0)
bf7c0a
 	close (copyfd);
bf7c0a
-#endif
bf7c0a
       unlink_if_ordinary (tmpname);
bf7c0a
     }
bf7c0a
 
bf7c0a
diff -rup binutils.orig/binutils/rename.c binutils-2.35.1/binutils/rename.c
bf7c0a
--- binutils.orig/binutils/rename.c	2021-03-11 12:38:18.183422774 +0000
bf7c0a
+++ binutils-2.35.1/binutils/rename.c	2021-03-11 12:41:41.824081969 +0000
bf7c0a
@@ -30,30 +30,25 @@
bf7c0a
 #endif /* HAVE_UTIMES */
bf7c0a
 #endif /* ! HAVE_GOOD_UTIME_H */
bf7c0a
 
bf7c0a
-#if ! defined (_WIN32) || defined (__CYGWIN32__)
bf7c0a
-static int simple_copy (const char *, const char *);
bf7c0a
-
bf7c0a
 /* The number of bytes to copy at once.  */
bf7c0a
 #define COPY_BUF 8192
bf7c0a
 
bf7c0a
-/* Copy file FROM to file TO, performing no translations.
bf7c0a
+/* Copy file FROMFD to file TO, performing no translations.
bf7c0a
    Return 0 if ok, -1 if error.  */
bf7c0a
 
bf7c0a
 static int
bf7c0a
-simple_copy (const char *from, const char *to)
bf7c0a
+simple_copy (int fromfd, const char *to,
bf7c0a
+	     struct stat *target_stat ATTRIBUTE_UNUSED)
bf7c0a
 {
bf7c0a
-  int fromfd, tofd, nread;
bf7c0a
+  int tofd, nread;
bf7c0a
   int saved;
bf7c0a
   char buf[COPY_BUF];
bf7c0a
 
bf7c0a
-  fromfd = open (from, O_RDONLY | O_BINARY);
bf7c0a
-  if (fromfd < 0)
bf7c0a
+  if (fromfd < 0
bf7c0a
+      || lseek (fromfd, 0, SEEK_SET) != 0)
bf7c0a
     return -1;
bf7c0a
-#ifdef O_CREAT
bf7c0a
-  tofd = open (to, O_CREAT | O_WRONLY | O_TRUNC | O_BINARY, 0777);
bf7c0a
-#else
bf7c0a
-  tofd = creat (to, 0777);
bf7c0a
-#endif
bf7c0a
+
bf7c0a
+  tofd = open (to, O_WRONLY | O_TRUNC | O_BINARY);
bf7c0a
   if (tofd < 0)
bf7c0a
     {
bf7c0a
       saved = errno;
bf7c0a
@@ -61,6 +56,7 @@ simple_copy (const char *from, const cha
bf7c0a
       errno = saved;
bf7c0a
       return -1;
bf7c0a
     }
bf7c0a
+
bf7c0a
   while ((nread = read (fromfd, buf, sizeof buf)) > 0)
bf7c0a
     {
bf7c0a
       if (write (tofd, buf, nread) != nread)
bf7c0a
@@ -72,7 +68,16 @@ simple_copy (const char *from, const cha
bf7c0a
 	  return -1;
bf7c0a
 	}
bf7c0a
     }
bf7c0a
+
bf7c0a
   saved = errno;
bf7c0a
+
bf7c0a
+#if !defined (_WIN32) || defined (__CYGWIN32__)
bf7c0a
+  /* Writing to a setuid/setgid file may clear S_ISUID and S_ISGID.
bf7c0a
+     Try to restore them, ignoring failure.  */
bf7c0a
+  if (target_stat != NULL)
bf7c0a
+    fchmod (tofd, target_stat->st_mode);
bf7c0a
+#endif
bf7c0a
+
bf7c0a
   close (fromfd);
bf7c0a
   close (tofd);
bf7c0a
   if (nread < 0)
bf7c0a
@@ -82,7 +87,6 @@ simple_copy (const char *from, const cha
bf7c0a
     }
bf7c0a
   return 0;
bf7c0a
 }
bf7c0a
-#endif /* __CYGWIN32__ or not _WIN32 */
bf7c0a
 
bf7c0a
 /* Set the times of the file DESTINATION to be the same as those in
bf7c0a
    STATBUF.  */
bf7c0a
@@ -91,164 +95,52 @@ void
bf7c0a
 set_times (const char *destination, const struct stat *statbuf)
bf7c0a
 {
bf7c0a
   int result;
bf7c0a
-
bf7c0a
-  {
bf7c0a
 #ifdef HAVE_GOOD_UTIME_H
bf7c0a
-    struct utimbuf tb;
bf7c0a
+  struct utimbuf tb;
bf7c0a
 
bf7c0a
-    tb.actime = statbuf->st_atime;
bf7c0a
-    tb.modtime = statbuf->st_mtime;
bf7c0a
-    result = utime (destination, &tb);
bf7c0a
-#else /* ! HAVE_GOOD_UTIME_H */
bf7c0a
-#ifndef HAVE_UTIMES
bf7c0a
-    long tb[2];
bf7c0a
-
bf7c0a
-    tb[0] = statbuf->st_atime;
bf7c0a
-    tb[1] = statbuf->st_mtime;
bf7c0a
-    result = utime (destination, tb);
bf7c0a
-#else /* HAVE_UTIMES */
bf7c0a
-    struct timeval tv[2];
bf7c0a
-
bf7c0a
-    tv[0].tv_sec = statbuf->st_atime;
bf7c0a
-    tv[0].tv_usec = 0;
bf7c0a
-    tv[1].tv_sec = statbuf->st_mtime;
bf7c0a
-    tv[1].tv_usec = 0;
bf7c0a
-    result = utimes (destination, tv);
bf7c0a
-#endif /* HAVE_UTIMES */
bf7c0a
-#endif /* ! HAVE_GOOD_UTIME_H */
bf7c0a
-  }
bf7c0a
-
bf7c0a
-  if (result != 0)
bf7c0a
-    non_fatal (_("%s: cannot set time: %s"), destination, strerror (errno));
bf7c0a
-}
bf7c0a
-
bf7c0a
-#ifndef S_ISLNK
bf7c0a
-#ifdef S_IFLNK
bf7c0a
-#define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK)
bf7c0a
+  tb.actime = statbuf->st_atime;
bf7c0a
+  tb.modtime = statbuf->st_mtime;
bf7c0a
+  result = utime (destination, &tb);
bf7c0a
+#elif defined HAVE_UTIMES
bf7c0a
+  struct timeval tv[2];
bf7c0a
+
bf7c0a
+  tv[0].tv_sec = statbuf->st_atime;
bf7c0a
+  tv[0].tv_usec = 0;
bf7c0a
+  tv[1].tv_sec = statbuf->st_mtime;
bf7c0a
+  tv[1].tv_usec = 0;
bf7c0a
+  result = utimes (destination, tv);
bf7c0a
 #else
bf7c0a
-#define S_ISLNK(m) 0
bf7c0a
-#define lstat stat
bf7c0a
-#endif
bf7c0a
-#endif
bf7c0a
-
bf7c0a
-#if !defined (_WIN32) || defined (__CYGWIN32__)
bf7c0a
-/* Try to preserve the permission bits and ownership of an existing file when
bf7c0a
-   rename overwrites it.  FD is the file being renamed and TARGET_STAT has the
bf7c0a
-   status of the file that was overwritten.  */
bf7c0a
-static void
bf7c0a
-try_preserve_permissions (int fd, struct stat *target_stat)
bf7c0a
-{
bf7c0a
-  struct stat from_stat;
bf7c0a
-  int ret = 0;
bf7c0a
-
bf7c0a
-  if (fstat (fd, &from_stat) != 0)
bf7c0a
-    return;
bf7c0a
+  long tb[2];
bf7c0a
 
bf7c0a
-  int from_mode = from_stat.st_mode & 0777;
bf7c0a
-  int to_mode = target_stat->st_mode & 0777;
bf7c0a
+  tb[0] = statbuf->st_atime;
bf7c0a
+  tb[1] = statbuf->st_mtime;
bf7c0a
+  result = utime (destination, tb);
bf7c0a
+#endif
bf7c0a
 
bf7c0a
-  /* Fix up permissions before we potentially lose ownership with fchown.
bf7c0a
-     Clear the setxid bits because in case the fchown below fails then we don't
bf7c0a
-     want to end up with a sxid file owned by the invoking user.  If the user
bf7c0a
-     hasn't changed or if fchown succeeded, we add back the sxid bits at the
bf7c0a
-     end.  */
bf7c0a
-  if (from_mode != to_mode)
bf7c0a
-    fchmod (fd, to_mode);
bf7c0a
-
bf7c0a
-  /* Fix up ownership, this will clear the setxid bits.  */
bf7c0a
-  if (from_stat.st_uid != target_stat->st_uid
bf7c0a
-      || from_stat.st_gid != target_stat->st_gid)
bf7c0a
-    ret = fchown (fd, target_stat->st_uid, target_stat->st_gid);
bf7c0a
-
bf7c0a
-  /* Fix up the sxid bits if either the fchown wasn't needed or it
bf7c0a
-     succeeded.  */
bf7c0a
-  if (ret == 0)
bf7c0a
-    fchmod (fd, target_stat->st_mode & 07777);
bf7c0a
+  if (result != 0)
bf7c0a
+    non_fatal (_("%s: cannot set time: %s"), destination, strerror (errno));
bf7c0a
 }
bf7c0a
-#endif
bf7c0a
 
bf7c0a
-/* Rename FROM to TO, copying if TO is either a link or is not a regular file.
bf7c0a
-   FD is an open file descriptor pointing to FROM that we can use to safely fix
bf7c0a
-   up permissions of the file after renaming.  TARGET_STAT has the file status
bf7c0a
-   that is used to fix up permissions and timestamps after rename.  Return 0 if
bf7c0a
-   ok, -1 if error and FD is closed before returning.  */
bf7c0a
+/* Copy FROM to TO.  TARGET_STAT has the file status that, if non-NULL,
bf7c0a
+   is used to fix up timestamps.  Return 0 if ok, -1 if error.
bf7c0a
+   At one time this function renamed files, but file permissions are
bf7c0a
+   tricky to update given the number of different schemes used by
bf7c0a
+   various systems.  So now we just copy.  */
bf7c0a
 
bf7c0a
 int
bf7c0a
-smart_rename (const char *from, const char *to, int fd ATTRIBUTE_UNUSED,
bf7c0a
-	      struct stat *target_stat ATTRIBUTE_UNUSED,
bf7c0a
-	      int preserve_dates ATTRIBUTE_UNUSED)
bf7c0a
+smart_rename (const char *from, const char *to, int fromfd,
bf7c0a
+	      struct stat *target_stat, bfd_boolean preserve_dates)
bf7c0a
 {
bf7c0a
-  int ret = 0;
bf7c0a
-  struct stat to_stat;
bf7c0a
-  bfd_boolean exists;
bf7c0a
-
bf7c0a
-  exists = lstat (to, &to_stat) == 0;
bf7c0a
-
bf7c0a
-#if defined (_WIN32) && !defined (__CYGWIN32__)
bf7c0a
-  /* Win32, unlike unix, will not erase `to' in `rename(from, to)' but
bf7c0a
-     fail instead.  Also, chown is not present.  */
bf7c0a
+  int ret;
bf7c0a
 
bf7c0a
-  if (exists)
bf7c0a
-    remove (to);
bf7c0a
-
bf7c0a
-  ret = rename (from, to);
bf7c0a
+  ret = simple_copy (fromfd, to, target_stat);
bf7c0a
   if (ret != 0)
bf7c0a
-    {
bf7c0a
-      /* We have to clean up here.  */
bf7c0a
-      non_fatal (_("unable to rename '%s'; reason: %s"), to, strerror (errno));
bf7c0a
-      unlink (from);
bf7c0a
-    }
bf7c0a
-#else
bf7c0a
-  /* Avoid a full copy and use rename if we can fix up permissions of the
bf7c0a
-     file after renaming, i.e.:
bf7c0a
+    non_fatal (_("unable to copy file '%s'; reason: %s"),
bf7c0a
+	       to, strerror (errno));
bf7c0a
 
bf7c0a
-     - TO is not a symbolic link
bf7c0a
-     - TO is a regular file with only one hard link
bf7c0a
-     - We have permission to write to TO
bf7c0a
-     - FD is available to safely fix up permissions to be the same as the file
bf7c0a
-       we overwrote with the rename.
bf7c0a
-
bf7c0a
-     Note though that the actual file on disk that TARGET_STAT describes may
bf7c0a
-     have changed and we're only trying to preserve the status we know about.
bf7c0a
-     At no point do we try to interact with the new file changes, so there can
bf7c0a
-     only be two outcomes, i.e. either the external file change survives
bf7c0a
-     without knowledge of our change (if it happens after the rename syscall)
bf7c0a
-     or our rename and permissions fixup survive without any knowledge of the
bf7c0a
-     external change.  */
bf7c0a
-  if (! exists
bf7c0a
-      || (fd >= 0
bf7c0a
-	  && !S_ISLNK (to_stat.st_mode)
bf7c0a
-	  && S_ISREG (to_stat.st_mode)
bf7c0a
-	  && (to_stat.st_mode & S_IWUSR)
bf7c0a
-	  && to_stat.st_nlink == 1)
bf7c0a
-      )
bf7c0a
-    {
bf7c0a
-      ret = rename (from, to);
bf7c0a
-      if (ret == 0)
bf7c0a
-	{
bf7c0a
-	  if (exists && target_stat != NULL)
bf7c0a
-	    try_preserve_permissions (fd, target_stat);
bf7c0a
-	}
bf7c0a
-      else
bf7c0a
-	{
bf7c0a
-	  /* We have to clean up here.  */
bf7c0a
-	  non_fatal (_("unable to rename '%s'; reason: %s"), to, strerror (errno));
bf7c0a
-	  unlink (from);
bf7c0a
-	}
bf7c0a
-    }
bf7c0a
-  else
bf7c0a
-    {
bf7c0a
-      ret = simple_copy (from, to);
bf7c0a
-      if (ret != 0)
bf7c0a
-	non_fatal (_("unable to copy file '%s'; reason: %s"), to, strerror (errno));
bf7c0a
-
bf7c0a
-      if (preserve_dates && target_stat != NULL)
bf7c0a
-	set_times (to, target_stat);
bf7c0a
-      unlink (from);
bf7c0a
-    }
bf7c0a
-  if (fd >= 0)
bf7c0a
-    close (fd);
bf7c0a
-#endif /* _WIN32 && !__CYGWIN32__ */
bf7c0a
+  if (preserve_dates)
bf7c0a
+    set_times (to, target_stat);
bf7c0a
+  unlink (from);
bf7c0a
 
bf7c0a
   return ret;
bf7c0a
 }
bf7c0a
diff -rup binutils.orig/binutils/objcopy.c binutils-2.35.1/binutils/objcopy.c
bf7c0a
--- binutils.orig/binutils/objcopy.c	2021-03-11 13:21:44.780223078 +0000
bf7c0a
+++ binutils-2.35.1/binutils/objcopy.c	2021-03-11 13:23:01.041718818 +0000
bf7c0a
@@ -4798,7 +4798,11 @@ strip_main (int argc, char *argv[])
bf7c0a
 
bf7c0a
       if (output_file == NULL
bf7c0a
 	  || filename_cmp (argv[i], output_file) == 0)
bf7c0a
-	tmpname = make_tempname (argv[i], &tmpfd);
bf7c0a
+	{
bf7c0a
+	  tmpname = make_tempname (argv[i], &tmpfd);
bf7c0a
+	  if (tmpfd >= 0)
bf7c0a
+	    copyfd = dup (tmpfd);
bf7c0a
+	}
bf7c0a
       else
bf7c0a
 	tmpname = output_file;
bf7c0a