binutils: Make smart_rename safe too
authorSiddhesh Poyarekar <siddhesh@gotplt.org>
Mon, 7 Dec 2020 15:18:33 +0000 (20:48 +0530)
committerSiddhesh Poyarekar <siddhesh@gotplt.org>
Mon, 7 Dec 2020 15:18:33 +0000 (20:48 +0530)
commit014cc7f849e8209623fc99264814bce7b3b6faf2
tree890c586d1a37643dfa2aa394935748a5e6542f51
parent1a1c3b4cc17687091cff5a368bd6f13742bcfdf8
binutils: Make smart_rename safe too

smart_rename is capable of handling symlinks by copying and it also
tries to preserve ownership and permissions of files when they're
overwritten during the rename.  This is useful in objcopy where the
file properties need to be preserved.

However because smart_rename does this using file names, it leaves a
race window between renames and permission fixes.  This change removes
this race window by using file descriptors from the original BFDs that
were used to manipulate these files wherever possible.

The file that is to be renamed is also passed as a file descriptor so
that we use fchown/fchmod on the file descriptor, thus making sure
that we only modify the file we have opened to write.  Further, in
case the file is to be overwritten (as is the case in ar or objcopy),
the permissions that need to be restored are taken from the file
descriptor that was opened for input so that integrity of the file
status is maintained all the way through to the rename.

binutils/

* rename.c
* ar.c
(write_archive) [!defined (_WIN32) || defined (__CYGWIN32__)]:
Initialize TARGET_STAT and OFD to pass to SMART_RENAME.
* arsup.c
(ar_save) [defined (_WIN32) || defined (__CYGWIN32__)]:
Likewise.
* bucomm.h (smart_rename): Add new arguments to declaration.
* objcopy.c
(strip_main)[defined (_WIN32) || defined (__CYGWIN32__)]:
Initialize COPYFD and pass to SMART_RENAME.
(copy_main) [defined (_WIN32) || defined (__CYGWIN32__)]:
Likewise.
* rename.c (try_preserve_permissions): New function.
(smart_rename): Use it and add new arguments.
binutils/ChangeLog
binutils/ar.c
binutils/arsup.c
binutils/bucomm.h
binutils/objcopy.c
binutils/rename.c
This page took 0.025771 seconds and 4 git commands to generate.