From 8caa5dddeebaf7116c9fa12a64cac1aca567df58 Mon Sep 17 00:00:00 2001 From: Jakob Unterwurzacher Date: Wed, 11 Nov 2026 21:05:14 +0100 Subject: fusefrontend: openWriteOnlyFile: reopen the same file, not just the same name openWriteOnlyFile chmods the file via woFd and then reopens it by name. O_NOFOLLOW prevents following symlinks, but cName could still have been replaced by a different regular file in between, and we would return an fd to that file. Add syscallcompat.ReopenFd(): * Linux: open /proc/self/fd/N, which always resolves to the file woFd refers to. * Other platforms: reopen by name and compare dev/ino against woFd, returning EAGAIN on mismatch. Co-Authored-By: Claude Opus 5.5 --- internal/syscallcompat/reopen_linux.go | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 internal/syscallcompat/reopen_linux.go (limited to 'internal/syscallcompat/reopen_linux.go') diff --git a/internal/syscallcompat/reopen_linux.go b/internal/syscallcompat/reopen_linux.go new file mode 100644 index 0000000..3b0766b --- /dev/null +++ b/internal/syscallcompat/reopen_linux.go @@ -0,0 +1,24 @@ +package syscallcompat + +import ( + "fmt" + "syscall" +) + +// ReopenFd opens the file referenced by "fd" again, with new "flags". +// +// On Linux, this goes through /proc/self/fd, which always resolves to +// exactly the file "fd" refers to, even if it has been renamed or replaced. +// "dirfd" and "name" are not used. They are needed for the fallback +// implementation on other platforms. +// Retries on EINTR. +func ReopenFd(fd int, dirfd int, name string, flags int) (int, error) { + procPath := fmt.Sprintf("/proc/self/fd/%d", fd) + // /proc/self/fd/N is a symlink, so O_NOFOLLOW would make the open fail + // with ELOOP. Following it is safe because it is a "magic link" that + // points to the open file description, not to a path. + flags = flags&^syscall.O_NOFOLLOW | syscall.O_CLOEXEC + return retryEINTR2(func() (int, error) { + return syscall.Open(procPath, flags, 0) + }) +} -- cgit v1.2.3