[PATCH] D96308: [llvm-objcopy] Avoid rename if input filename = output filename

Fangrui Song via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Feb 8 17:46:25 PST 2021


MaskRay created this revision.
MaskRay added reviewers: dxf, llozano, manojgupta, rupprecht, alexshap, jhenderson.
Herald added subscribers: abrachet, jfb, emaste.
MaskRay requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

In binutils, objcopy/strip perform `smart_rename` for "in-place" `strip exe`
and `objcopy exe`.  `smart_rename` calls `chown(2)` (vulnerable:
https://sourceware.org/bugzilla/show_bug.cgi?id=26945) which requires
additional care to restore file modes and owner/group.

binutils 2.37 is moving to use truncate+overwrite
https://sourceware.org/pipermail/binutils/2021-February/115282.html

It has nice properties that owner/group/mode/security context/xattr are all
preserved. The downside is `ETXTBSY` (while the executable is running) and
potentially corrupted output in the case of full disk situation and other
erroneous cases.

This patch emulates its behavior when input filename = output filename
(`llvm-strip exe`, `llvm-strip exe -o exe`)[1]. llvm-objcopy/llvm-strip
treats '-' as stdin/stdout so '-' needs to be excluded from this behavior.
In addition, the implementation of `--build-id-link-{input,out}` (not in binutils,
contributed by Fuchsia but not in-use) needs the on-disk file so they should be excluded as well.

Changing owner/group requires privilege to test. I merely strengthen the existing file mode tests.

[1]: I'd like to just check whether `-o` is present, but it would need more code, and it probably does not matter to additionally make `llvm-strip exe -o exe` special while `llvm-strip exe -o ./exe` follows our original behavior.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D96308

Files:
  llvm/test/tools/llvm-objcopy/ELF/mirror-permissions-unix.test
  llvm/tools/llvm-objcopy/llvm-objcopy.cpp

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D96308.322255.patch
Type: text/x-patch
Size: 5726 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210209/0f06ddf9/attachment.bin>


More information about the llvm-commits mailing list