aboutsummaryrefslogtreecommitdiff
path: root/internal
diff options
context:
space:
mode:
Diffstat (limited to 'internal')
-rw-r--r--internal/fusefrontend/root_node.go4
-rw-r--r--internal/syscallcompat/reopen_linux.go24
-rw-r--r--internal/syscallcompat/reopen_other.go37
3 files changed, 64 insertions, 1 deletions
diff --git a/internal/fusefrontend/root_node.go b/internal/fusefrontend/root_node.go
index 38d070d..fa8781b 100644
--- a/internal/fusefrontend/root_node.go
+++ b/internal/fusefrontend/root_node.go
@@ -206,7 +206,9 @@ func (rn *RootNode) openWriteOnlyFile(dirfd int, cName string, newFlags int) (rw
tlog.Warn.Printf("openWriteOnlyFile: reverting permissions failed: %v", err2)
}
}()
- return syscallcompat.Openat(dirfd, cName, newFlags, 0)
+ // cName may have been replaced by a different file since we opened woFd.
+ // ReopenFd makes sure we get the same file we just chmod'ed.
+ return syscallcompat.ReopenFd(woFd, dirfd, cName, newFlags)
}
// encryptSymlinkTarget: "data" is encrypted like file contents (GCM)
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)
+ })
+}
diff --git a/internal/syscallcompat/reopen_other.go b/internal/syscallcompat/reopen_other.go
new file mode 100644
index 0000000..15d868d
--- /dev/null
+++ b/internal/syscallcompat/reopen_other.go
@@ -0,0 +1,37 @@
+//go:build !linux
+
+package syscallcompat
+
+import (
+ "syscall"
+
+ "github.com/rfjakob/gocryptfs/v2/internal/tlog"
+)
+
+// ReopenFd opens the file referenced by "fd" again, with new "flags".
+//
+// There is no /proc/self/fd outside Linux, so we reopen "name" relative to
+// "dirfd" and verify that we got the same file as "fd". If "name" has been
+// replaced by a different file in the meantime, EAGAIN is returned.
+// Retries on EINTR.
+func ReopenFd(fd int, dirfd int, name string, flags int) (int, error) {
+ var st syscall.Stat_t
+ if err := syscall.Fstat(fd, &st); err != nil {
+ return -1, err
+ }
+ fd2, err := Openat(dirfd, name, flags, 0)
+ if err != nil {
+ return -1, err
+ }
+ var st2 syscall.Stat_t
+ if err := syscall.Fstat(fd2, &st2); err != nil {
+ syscall.Close(fd2)
+ return -1, err
+ }
+ if st2.Dev != st.Dev || st2.Ino != st.Ino {
+ tlog.Warn.Printf("ReopenFd: %q was replaced concurrently, returning EAGAIN", name)
+ syscall.Close(fd2)
+ return -1, syscall.EAGAIN
+ }
+ return fd2, nil
+}