From e5a4f72c4d83000021262af01d293da74a15cbdb Mon Sep 17 00:00:00 2001 From: Antonio SJ Musumeci Date: Sat, 21 Nov 2026 14:35:16 -0500 Subject: [PATCH] fix: restore the mode when an nfsopenhack open fails - nfsopenhack adds S_IWUSR|S_IWGRP to a read-only file so the open can succeed, and resets the mode on the new fd. When the open still failed, the write bits stayed on the file. - Pin the inode before changing its permissions. Use that reference for the temporary mode, retry and restoration, so a concurrent rename cannot apply the original mode to an unrelated replacement. --- mkdocs/docs/config/nfsopenhack.md | 15 +++++-- src/fuse_open.cpp | 66 ++++++++++++++++++++----------- 2 files changed, 53 insertions(+), 28 deletions(-) diff --git a/mkdocs/docs/config/nfsopenhack.md b/mkdocs/docs/config/nfsopenhack.md index 4ddd999b..3452d386 100644 --- a/mkdocs/docs/config/nfsopenhack.md +++ b/mkdocs/docs/config/nfsopenhack.md @@ -2,8 +2,8 @@ * `nfsopenhack=off`: No hack applied. * `nfsopenhack=git`: Apply hack if path includes `/.git/`. -* `nfsopenhack=all`: Apply hack on all empty read-only files opened - for writing. +* `nfsopenhack=all`: Apply hack on all regular files with no write + permission bits when opened for writing. * Defaults to `off`. NFS is not fully POSIX compliant and historically certain behaviors, @@ -22,5 +22,12 @@ writable. Even though it's a more niche situation this hack breaks normal security and behavior and as such is `off` by default. If set to `git` it will only perform the hack when the path in question includes -`/.git/`. `all` will result in it applying anytime a read-only file -which is empty is opened for writing. +`/.git/`. `all` applies the same behavior regardless of file name or size. + +After a write open is denied, mergerfs pins the regular file without +following a final symlink. It temporarily adds owner and group write +permission and retries the open on that inode. It restores the original +mode after the retry, even if the retry fails. A failure to restore the +mode is returned as an error. If the file cannot be pinned, the open +stays denied; mergerfs never looks up the branch pathname again to +change permissions. diff --git a/src/fuse_open.cpp b/src/fuse_open.cpp index 1d247079..016447e1 100644 --- a/src/fuse_open.cpp +++ b/src/fuse_open.cpp @@ -93,16 +93,18 @@ #include "fs_close.hpp" #include "fs_cow.hpp" #include "fs_fchmod.hpp" -#include "fs_lchmod.hpp" -#include "fs_open.hpp" +#include "fs_fchmodat.hpp" +#include "fs_fstat.hpp" #include "fs_open_fd.hpp" #include "fs_openat.hpp" #include "fs_path.hpp" -#include "fs_stat.hpp" #include "fuse_passthrough.hpp" #include "procfs.hpp" #include "stat_util.hpp" +#include "fmt/format.h" +#include "scope_guard/scope_guard.hpp" + #include "fuse.h" #include @@ -123,30 +125,46 @@ _rdonly(const int flags_) static int -_lchmod_and_open_if_not_writable_and_empty(const fs::path &fullpath_, - const int flags_) +_open_if_not_writable(const fs::path &fullpath_, + const int flags_) { - int rv; struct stat st; - rv = fs::lstat(fullpath_,&st); + // Pin without following a final symlink or requiring read permission. + const int pin = fs::openat(AT_FDCWD,fullpath_,O_PATH|O_NOFOLLOW|O_CLOEXEC); + if(pin < 0) + return -EACCES; + DEFER { fs::close(pin); }; + + if((fs::fstat(pin,&st) < 0) || !S_ISREG(st.st_mode) || StatUtil::writable(st)) + return -EACCES; + +#if defined(__linux__) + // fchmod can't operate on O_PATH. Use its procfs link, which also + // works on kernels without fchmodat2(AT_EMPTY_PATH). + if(procfs::PROC_SELF_FD_FD <= 0) + return -EACCES; + const fmt::format_int name(pin); + const auto chmod_pin = [&name](const mode_t mode) + { return fs::fchmodat(procfs::PROC_SELF_FD_FD,name.c_str(),mode,0); }; +#elif defined(__FreeBSD__) + const auto chmod_pin = [pin](const mode_t mode) + { return fs::fchmodat(pin,"",mode,AT_EMPTY_PATH); }; +#endif + + if(chmod_pin(st.st_mode|S_IWUSR|S_IWGRP) < 0) + return -EACCES; + + const int fd = fs::open_fd(pin,flags_); + const int rv = ((fd < 0) ? chmod_pin(st.st_mode) : fs::fchmod(fd,st.st_mode)); if(rv < 0) - return -EACCES; + { + if(fd >= 0) + fs::close(fd); + return rv; + } - if(StatUtil::writable(st)) - return -EACCES; - - rv = fs::lchmod(fullpath_,(st.st_mode|S_IWUSR|S_IWGRP)); - if(rv < 0) - return -EACCES; - - rv = fs::open(fullpath_,flags_); - if(rv < 0) - return -EACCES; - - fs::fchmod(rv,st.st_mode); - - return rv; + return ((fd < 0) ? -EACCES : fd); } static @@ -165,11 +183,11 @@ _nfsopenhack(const fs::path &fullpath_, return -EACCES; if(fullpath_.string().find("/.git/") == std::string::npos) return -EACCES; - return ::_lchmod_and_open_if_not_writable_and_empty(fullpath_,flags_); + return ::_open_if_not_writable(fullpath_,flags_); case NFSOpenHack::ENUM::ALL: if(::_rdonly(flags_)) return -EACCES; - return ::_lchmod_and_open_if_not_writable_and_empty(fullpath_,flags_); + return ::_open_if_not_writable(fullpath_,flags_); } }