]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
binutils: Avoid renaming over existing files
authorSiddhesh Poyarekar <siddhesh@gotplt.org>
Mon, 22 Feb 2021 15:15:50 +0000 (20:45 +0530)
committerSiddhesh Poyarekar <siddhesh@gotplt.org>
Mon, 22 Feb 2021 15:15:50 +0000 (20:45 +0530)
Renaming over existing files needs additional care to restore
permissions and ownership, which may not always succeed.
Additionally, other properties of the file such as extended attributes
may be lost, making the operation flaky.

For predictable results, resort to rename() only if the file does not
exist, otherwise copy the file contents into the existing file.  This
ensures that no additional tricks are needed to retain file
properties.

This also allows dropping of the redundant set_times on the tmpfile in
objcopy/strip since now we no longer rename over existing files.

binutils/

* ar.c (write_archive): Adjust call to SMART_RENAME.
* arsup.c (ar_save): Likewise.
* objcopy (strip_main): Don't set times on temporary file and
adjust call to SMART_RENAME.
(copy_main): Likewise.
* rename.c [!S_ISLNK]: Remove definitions.
(try_preserve_permissions): Remove function.
(smart_rename): Replace PRESERVE_DATES argument with
TARGET_STAT.  Use rename system call only if TO does not exist.
* bucomm.h (smart_rename): Adjust declaration.

(cherry picked from commit 3685de750e6a091663a0abe42528cad29e960e35)

binutils/ChangeLog
binutils/ar.c
binutils/arsup.c
binutils/bucomm.h
binutils/objcopy.c
binutils/rename.c

index b60e55d163243564117131056a9071d4fe022752..a725898321de19ad5a35913756142bd382f2f0c3 100644 (file)
@@ -1,3 +1,16 @@
+2021-02-22  Siddhesh Poyarekar  <siddhesh@gotplt.org>
+
+       * ar.c (write_archive): Adjust call to SMART_RENAME.
+       * arsup.c (ar_save): Likewise.
+       * objcopy (strip_main): Don't set times on temporary file and
+       adjust call to SMART_RENAME.
+       (copy_main): Likewise.
+       * rename.c [!S_ISLNK]: Remove definitions.
+       (try_preserve_permissions): Remove function.
+       (smart_rename): Replace PRESERVE_DATES argument with
+       TARGET_STAT.  Use rename system call only if TO does not exist.
+       * bucomm.h (smart_rename): Adjust declaration.
+
 2021-02-09  Alan Modra  <amodra@gmail.com>
 
        PR 27382
index 45a34e3a6cf92841f962eb50caad2916f92294e4..3a91708b51c79c406166f308d71994a10798b298 100644 (file)
@@ -1308,7 +1308,7 @@ write_archive (bfd *iarch)
   /* We don't care if this fails; we might be creating the archive.  */
   bfd_close (iarch);
 
-  if (smart_rename (new_name, old_name, 0) != 0)
+  if (smart_rename (new_name, old_name, NULL) != 0)
     xexit (1);
   free (old_name);
   free (new_name);
index 5403a0c5d74ceec52469a4825b820c98164bdb85..0a1f63f645677a9b36fd0d89b174ff632d6206c8 100644 (file)
@@ -351,7 +351,7 @@ ar_save (void)
 
       bfd_close (obfd);
 
-      smart_rename (ofilename, real_name, 0);
+      smart_rename (ofilename, real_name, NULL);
       obfd = 0;
       free (ofilename);
     }
index 91f6a5b228fa35c2253121edb17264322e5d558d..aa7e33d8cd15e04ab12691bb29cb4365080998fc 100644 (file)
@@ -71,7 +71,8 @@ extern void print_version (const char *);
 /* In rename.c.  */
 extern void set_times (const char *, const struct stat *);
 
-extern int smart_rename (const char *, const char *, int);
+extern int smart_rename (const char *, const char *, struct stat *);
+
 
 /* In libiberty.  */
 void *xmalloc (size_t);
index eab3b6db5857731cec24dbf395d69ffba8b2590b..07a872b5a80354231697ae958d1d3a8229e1137a 100644 (file)
@@ -4861,12 +4861,10 @@ strip_main (int argc, char *argv[])
                 output_target, NULL);
       if (status == 0)
        {
-         if (preserve_dates)
-           set_times (tmpname, &statbuf);
          if (output_file != tmpname)
            status = (smart_rename (tmpname,
                                    output_file ? output_file : argv[i],
-                                   preserve_dates) != 0);
+                                   preserve_dates ? &statbuf : NULL) != 0);
          if (status == 0)
            status = hold_status;
        }
@@ -5931,11 +5929,9 @@ copy_main (int argc, char *argv[])
             output_target, input_arch);
   if (status == 0)
     {
-      if (preserve_dates)
-       set_times (tmpname, &statbuf);
       if (tmpname != output_filename)
        status = (smart_rename (tmpname, input_filename,
-                               preserve_dates) != 0);
+                               preserve_dates ? &statbuf : NULL) != 0);
     }
   else
     unlink_if_ordinary (tmpname);
index 65ad5bf52c457ccdf2c7ae5c3a24959fc4dc4ccc..f471b45fd3f23c89aa07d2fa3a42d43eef7e741b 100644 (file)
@@ -122,20 +122,13 @@ set_times (const char *destination, const struct stat *statbuf)
     non_fatal (_("%s: cannot set time: %s"), destination, strerror (errno));
 }
 
-#ifndef S_ISLNK
-#ifdef S_IFLNK
-#define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK)
-#else
-#define S_ISLNK(m) 0
-#define lstat stat
-#endif
-#endif
-
-/* Rename FROM to TO, copying if TO is a link.
-   Return 0 if ok, -1 if error.  */
+/* Rename FROM to TO, copying if TO exists.  TARGET_STAT has the file status
+   that, if non-NULL, is used to fix up timestamps after rename.  Return 0 if
+   ok, -1 if error.  */
 
 int
-smart_rename (const char *from, const char *to, int preserve_dates ATTRIBUTE_UNUSED)
+smart_rename (const char *from, const char *to,
+             struct stat *target_stat ATTRIBUTE_UNUSED)
 {
   bfd_boolean exists;
   struct stat s;
@@ -158,38 +151,10 @@ smart_rename (const char *from, const char *to, int preserve_dates ATTRIBUTE_UNU
       unlink (from);
     }
 #else
-  /* Use rename only if TO is not a symbolic link and has
-     only one hard link, and we have permission to write to it.  */
-  if (! exists
-      || (!S_ISLNK (s.st_mode)
-         && S_ISREG (s.st_mode)
-         && (s.st_mode & S_IWUSR)
-         && s.st_nlink == 1)
-      )
+  /* Avoid a full copy and use rename if TO does not exist.  */
+  if (!exists)
     {
-      ret = rename (from, to);
-      if (ret == 0)
-       {
-         if (exists)
-           {
-             /* Try to preserve the permission bits and ownership of
-                TO.  First get the mode right except for the setuid
-                bit.  Then change the ownership.  Then fix the setuid
-                bit.  We do the chmod before the chown because if the
-                chown succeeds, and we are a normal user, we won't be
-                able to do the chmod afterward.  We don't bother to
-                fix the setuid bit first because that might introduce
-                a fleeting security problem, and because the chown
-                will clear the setuid bit anyhow.  We only fix the
-                setuid bit if the chown succeeds, because we don't
-                want to introduce an unexpected setuid file owned by
-                the user running objcopy.  */
-             chmod (to, s.st_mode & 0777);
-             if (chown (to, s.st_uid, s.st_gid) >= 0)
-               chmod (to, s.st_mode & 07777);
-           }
-       }
-      else
+      if ((ret = rename (from, to)) != 0)
        {
          /* We have to clean up here.  */
          non_fatal (_("unable to rename '%s'; reason: %s"), to, strerror (errno));
@@ -202,8 +167,8 @@ smart_rename (const char *from, const char *to, int preserve_dates ATTRIBUTE_UNU
       if (ret != 0)
        non_fatal (_("unable to copy file '%s'; reason: %s"), to, strerror (errno));
 
-      if (preserve_dates)
-       set_times (to, &s);
+      if (target_stat != NULL)
+       set_times (to, target_stat);
       unlink (from);
     }
 #endif /* _WIN32 && !__CYGWIN32__ */