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.
This commit is contained in:
Antonio SJ Musumeci 2026-11-21 14:35:16 -05:00
parent 3735d7215d
commit e5a4f72c4d
2 changed files with 53 additions and 28 deletions

View file

@ -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.

View file

@ -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 <sched.h>
@ -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_);
}
}