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

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