aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJakob Unterwurzacher2026-11-11 21:11:43 +0100
committerJakob Unterwurzacher2026-11-11 21:11:43 +0100
commitdfdf47d790fa758a9f443764ad296e86c9007ece (patch)
treedb488b0b7400742d5e7d42832fb8d246eb3ca4a7
parent8caa5dddeebaf7116c9fa12a64cac1aca567df58 (diff)
reverse: don't follow symlinks when serving gocryptfs.conf
.gocryptfs.reverse.conf is passed through to gocryptfs.conf in the reverse view unencrypted. lookupConf, VirtualConfNode.Getattr and VirtualConfNode.Open accessed it via syscall.Stat/Open on the full path, following symlinks. Anybody with write access to the backing directory could replace it with a symlink to, for example, /etc/shadow and read the target in plaintext through the mount when gocryptfs runs as root. Add RootNode.openConf(), which opens the file relative to the backing dir with O_NOFOLLOW|O_NONBLOCK (O_NONBLOCK so a FIFO cannot hang us) and rejects anything that is not a regular file. Use it for all three. Add TestConfigSymlink as a regression test. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
-rw-r--r--internal/fusefrontend_reverse/node_helpers.go8
-rw-r--r--internal/fusefrontend_reverse/virtualconf.go48
-rw-r--r--tests/reverse/config_symlink_test.go101
3 files changed, 147 insertions, 10 deletions
diff --git a/internal/fusefrontend_reverse/node_helpers.go b/internal/fusefrontend_reverse/node_helpers.go
index dc8d928..8a15689 100644
--- a/internal/fusefrontend_reverse/node_helpers.go
+++ b/internal/fusefrontend_reverse/node_helpers.go
@@ -11,7 +11,6 @@ import (
"github.com/hanwen/go-fuse/v2/fs"
"github.com/hanwen/go-fuse/v2/fuse"
- "github.com/rfjakob/gocryptfs/v2/internal/configfile"
"github.com/rfjakob/gocryptfs/v2/internal/pathiv"
"github.com/rfjakob/gocryptfs/v2/internal/syscallcompat"
)
@@ -190,13 +189,12 @@ func (n *Node) lookupDiriv(ctx context.Context, out *fuse.EntryOut) (ch *fs.Inod
// lookupConf returns a new Inode for the gocryptfs.conf file
func (n *Node) lookupConf(ctx context.Context, out *fuse.EntryOut) (ch *fs.Inode, errno syscall.Errno) {
rn := n.rootNode()
- p := filepath.Join(rn.args.Cipherdir, configfile.ConfReverseName)
- var st syscall.Stat_t
- err := syscall.Stat(p, &st)
+ fd, st, err := rn.openConf()
if err != nil {
errno = fs.ToErrno(err)
return
}
+ syscall.Close(fd)
// Get unique inode number
rn.inoMap.TranslateStat(&st)
out.Attr.FromStat(&st)
@@ -205,7 +203,7 @@ func (n *Node) lookupConf(ctx context.Context, out *fuse.EntryOut) (ch *fs.Inode
}
// Create child node
id := rn.uniqueStableAttr(uint32(st.Mode), st.Ino)
- node := &VirtualConfNode{path: p}
+ node := &VirtualConfNode{}
ch = n.NewInode(ctx, node, id)
return
}
diff --git a/internal/fusefrontend_reverse/virtualconf.go b/internal/fusefrontend_reverse/virtualconf.go
index ea358dd..684cc59 100644
--- a/internal/fusefrontend_reverse/virtualconf.go
+++ b/internal/fusefrontend_reverse/virtualconf.go
@@ -7,6 +7,10 @@ import (
"github.com/hanwen/go-fuse/v2/fs"
"github.com/hanwen/go-fuse/v2/fuse"
+
+ "github.com/rfjakob/gocryptfs/v2/internal/configfile"
+ "github.com/rfjakob/gocryptfs/v2/internal/syscallcompat"
+ "github.com/rfjakob/gocryptfs/v2/internal/tlog"
)
var _ = (fs.NodeOpener)((*VirtualConfNode)(nil))
@@ -14,8 +18,42 @@ var _ = (fs.NodeGetattrer)((*VirtualConfNode)(nil))
type VirtualConfNode struct {
fs.Inode
+}
- path string
+// openConf opens .gocryptfs.reverse.conf in the root of the backing
+// plaintext directory and returns the fd and its stat data.
+//
+// The file is passed through to the user unencrypted, so we must make sure
+// not to follow symlinks: otherwise, someone with write access to the
+// backing directory could replace it with a symlink to /etc/shadow and
+// read the target through the mount. O_NONBLOCK prevents hanging on a FIFO.
+// Anything that is not a regular file is rejected.
+func (rn *RootNode) openConf() (fd int, st syscall.Stat_t, err error) {
+ dirfd, err := syscallcompat.Open(rn.args.Cipherdir, syscall.O_DIRECTORY|syscallcompat.O_PATH, 0)
+ if err != nil {
+ return -1, st, err
+ }
+ defer syscall.Close(dirfd)
+ fd, err = syscallcompat.Openat(dirfd, configfile.ConfReverseName,
+ syscall.O_RDONLY|syscall.O_NOFOLLOW|syscall.O_NONBLOCK, 0)
+ if err != nil {
+ if err == syscall.ELOOP {
+ tlog.Warn.Printf("openConf: %s is a symlink, refusing to serve it", configfile.ConfReverseName)
+ }
+ return -1, st, err
+ }
+ err = syscall.Fstat(fd, &st)
+ if err != nil {
+ syscall.Close(fd)
+ return -1, st, err
+ }
+ if st.Mode&syscall.S_IFMT != syscall.S_IFREG {
+ tlog.Warn.Printf("openConf: %s is not a regular file (mode %#o), refusing to serve it",
+ configfile.ConfReverseName, st.Mode)
+ syscall.Close(fd)
+ return -1, st, syscall.EPERM
+ }
+ return fd, st, nil
}
// rootNode returns the Root Node of the filesystem.
@@ -24,7 +62,7 @@ func (n *VirtualConfNode) rootNode() *RootNode {
}
func (n *VirtualConfNode) Open(ctx context.Context, flags uint32) (fh fs.FileHandle, fuseFlags uint32, errno syscall.Errno) {
- fd, err := syscall.Open(n.path, syscall.O_RDONLY, 0)
+ fd, _, err := n.rootNode().openConf()
if err != nil {
errno = fs.ToErrno(err)
return
@@ -34,13 +72,13 @@ func (n *VirtualConfNode) Open(ctx context.Context, flags uint32) (fh fs.FileHan
}
func (n *VirtualConfNode) Getattr(ctx context.Context, fh fs.FileHandle, out *fuse.AttrOut) syscall.Errno {
- var st syscall.Stat_t
- err := syscall.Stat(n.path, &st)
+ rn := n.rootNode()
+ fd, st, err := rn.openConf()
if err != nil {
return fs.ToErrno(err)
}
+ syscall.Close(fd)
out.FromStat(&st)
- rn := n.rootNode()
if rn.args.ForceOwner != nil {
out.Owner = *rn.args.ForceOwner
}
diff --git a/tests/reverse/config_symlink_test.go b/tests/reverse/config_symlink_test.go
new file mode 100644
index 0000000..beb9d0d
--- /dev/null
+++ b/tests/reverse/config_symlink_test.go
@@ -0,0 +1,101 @@
+package reverse_test
+
+import (
+ "os"
+ "os/exec"
+ "strings"
+ "syscall"
+ "testing"
+ "time"
+
+ "github.com/rfjakob/gocryptfs/v2/tests/test_helpers"
+)
+
+// TestConfigSymlink verifies that gocryptfs.conf in the reverse view does
+// not follow a symlink at .gocryptfs.reverse.conf. The config file is passed
+// through unencrypted, so following the symlink would let anybody with write
+// access to the backing directory read arbitrary files (like /etc/shadow when
+// gocryptfs runs as root) through the mount.
+func TestConfigSymlink(t *testing.T) {
+ backingDir, err := os.MkdirTemp(test_helpers.TmpDir, t.Name()+".")
+ if err != nil {
+ t.Fatal(err)
+ }
+ cmd := exec.Command(test_helpers.GocryptfsBinary, "-q", "-init", "-reverse",
+ "-extpass", "echo test", "-scryptn=10", backingDir)
+ cmd.Stdout, cmd.Stderr = os.Stdout, os.Stderr
+ if err := cmd.Run(); err != nil {
+ t.Fatalf("init failed: %v", err)
+ }
+ mnt := backingDir + ".mnt"
+ // -wpanic=false because gocryptfs warns about the symlink and the FIFO
+ test_helpers.MountOrFatal(t, backingDir, mnt, "-reverse", "-extpass", "echo test", "-wpanic=false")
+ defer test_helpers.UnmountPanic(mnt)
+
+ conf := backingDir + "/.gocryptfs.reverse.conf"
+ mntConf := mnt + "/gocryptfs.conf"
+ // Sanity check: the regular config file is visible
+ if _, err := os.Stat(mntConf); err != nil {
+ t.Fatal(err)
+ }
+
+ // Replace .gocryptfs.reverse.conf with a symlink to a secret file
+ const secretContent = "SECRET-dd0eea2a"
+ secret := backingDir + ".secret"
+ if err := os.WriteFile(secret, []byte(secretContent), 0600); err != nil {
+ t.Fatal(err)
+ }
+ if err := os.Rename(conf, conf+".bak"); err != nil {
+ t.Fatal(err)
+ }
+ if err := os.Symlink(secret, conf); err != nil {
+ t.Fatal(err)
+ }
+ data, err := os.ReadFile(mntConf)
+ if strings.Contains(string(data), secretContent) {
+ t.Fatalf("symlink was followed, secret leaked: %q", data)
+ }
+ if err == nil {
+ t.Errorf("reading %s should have failed", mntConf)
+ }
+
+ // Replace it with a FIFO. Opening it must neither hang nor succeed.
+ if err := os.Remove(conf); err != nil {
+ t.Fatal(err)
+ }
+ if err := syscall.Mkfifo(conf, 0600); err != nil {
+ t.Fatal(err)
+ }
+ done := make(chan error, 1)
+ go func() {
+ _, err := os.ReadFile(mntConf)
+ done <- err
+ }()
+ select {
+ case err := <-done:
+ if err == nil {
+ t.Errorf("reading %s (FIFO) should have failed", mntConf)
+ }
+ case <-time.After(5 * time.Second):
+ // Unblock the hung open so that unmount can succeed
+ if f, err := os.OpenFile(conf, os.O_WRONLY|syscall.O_NONBLOCK, 0); err == nil {
+ f.Close()
+ }
+ t.Fatalf("reading %s (FIFO) hung", mntConf)
+ }
+
+ // Restore the regular file. It must be visible again.
+ if err := os.Remove(conf); err != nil {
+ t.Fatal(err)
+ }
+ if err := os.Rename(conf+".bak", conf); err != nil {
+ t.Fatal(err)
+ }
+ data, err = os.ReadFile(mntConf)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if len(data) == 0 {
+ t.Error("empty config file")
+ }
+}