diff options
author | Fangrui Song <maskray@google.com> | 2019-07-11 10:17:59 +0000 |
---|---|---|
committer | Fangrui Song <maskray@google.com> | 2019-07-11 10:17:59 +0000 |
commit | 6dc59629570ae6fe1836cff3ff53912917d17af7 (patch) | |
tree | 163c4166284c232fd4a4b108e7c9a0dba5d32fa9 /llvm/tools/llvm-objcopy/llvm-objcopy.cpp | |
parent | f9ca13cb5f05f2a1acce223e794444078584ef0d (diff) | |
download | llvm-6dc59629570ae6fe1836cff3ff53912917d17af7.zip llvm-6dc59629570ae6fe1836cff3ff53912917d17af7.tar.gz llvm-6dc59629570ae6fe1836cff3ff53912917d17af7.tar.bz2 |
[llvm-objcopy] Don't change permissions of non-regular output files
There is currently an EPERM error when a regular user executes `llvm-objcopy a.o /dev/null`.
Worse, root can even change the mode bits of /dev/null.
Fix it by checking if the output file is special.
A new overload of llvm::sys::fs::setPermissions with FD as the parameter
is added. Users should provide `perm & ~umask` as the parameter if they
intend to respect umask.
The existing overload of llvm::sys::fs::setPermissions may be deleted if
we can find an implementation of fchmod() on Windows. fchmod() is
usually better than chmod() because it saves syscalls and can avoid race
condition.
Reviewed By: jakehehrlich, jhenderson
Differential Revision: https://reviews.llvm.org/D64236
llvm-svn: 365753
Diffstat (limited to 'llvm/tools/llvm-objcopy/llvm-objcopy.cpp')
-rw-r--r-- | llvm/tools/llvm-objcopy/llvm-objcopy.cpp | 15 |
1 files changed, 13 insertions, 2 deletions
diff --git a/llvm/tools/llvm-objcopy/llvm-objcopy.cpp b/llvm/tools/llvm-objcopy/llvm-objcopy.cpp index 2b0fef1..e937217 100644 --- a/llvm/tools/llvm-objcopy/llvm-objcopy.cpp +++ b/llvm/tools/llvm-objcopy/llvm-objcopy.cpp @@ -222,9 +222,20 @@ static Error restoreStatOnFile(StringRef Filename, FD, Stat.getLastAccessedTime(), Stat.getLastModificationTime())) return createFileError(Filename, EC); - if (auto EC = sys::fs::setPermissions(Filename, Stat.permissions(), - /*respectUmask=*/true)) + sys::fs::file_status OStat; + if (std::error_code EC = sys::fs::status(FD, OStat)) return createFileError(Filename, EC); + if (OStat.type() == sys::fs::file_type::regular_file) +#ifdef _WIN32 + if (auto EC = sys::fs::setPermissions( + Filename, static_cast<sys::fs::perms>(Stat.permissions() & + ~sys::fs::getUmask()))) +#else + if (auto EC = sys::fs::setPermissions( + FD, static_cast<sys::fs::perms>(Stat.permissions() & + ~sys::fs::getUmask()))) +#endif + return createFileError(Filename, EC); if (auto EC = sys::Process::SafelyCloseFileDescriptor(FD)) return createFileError(Filename, EC); |