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

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