aboutsummaryrefslogtreecommitdiff
path: root/internal/fusefrontend/file_dir_ops.go
blob: b69e7bc4e153d9594cde45c7274d02a01d039b6c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package fusefrontend

import (
	"context"
	"syscall"

	"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/nametransform"
	"github.com/rfjakob/gocryptfs/v2/internal/syscallcompat"
	"github.com/rfjakob/gocryptfs/v2/internal/tlog"
)

func (n *Node) OpendirHandle(ctx context.Context, flags uint32) (fh fs.FileHandle, fuseFlags uint32, errno syscall.Errno) {
	var fd int = -1
	var fdDup int = -1
	var file *File
	var dirIV []byte
	var ds fs.DirStream
	rn := n.rootNode()

	dirfd, cName, errno := n.prepareAtSyscallMyself()
	if errno != 0 {
		return
	}
	defer syscall.Close(dirfd)

	// Open backing directory
	fd, err := syscallcompat.Openat(dirfd, cName, syscall.O_RDONLY|syscall.O_DIRECTORY|syscall.O_NOFOLLOW, 0)
	if err != nil {
		errno = fs.ToErrno(err)
		return
	}

	// NewLoopbackDirStreamFd gets its own fd to untangle Release vs Releasedir
	fdDup, err = syscall.Dup(fd)

	if err != nil {
		errno = fs.ToErrno(err)
		goto err_out
	}

	ds, errno = fs.NewLoopbackDirStreamFd(fdDup)
	if errno != 0 {
		goto err_out
	}

	if !rn.args.PlaintextNames {
		// Read the DirIV from disk
		dirIV, err = rn.nameTransform.ReadDirIVAt(fd)
		if err != nil {
			tlog.Warn.Printf("OpendirHandle: could not read %s: %v", nametransform.DirIVFilename, err)
			errno = syscall.EIO
			goto err_out
		}
	}

	file, _, errno = NewFile(fd, cName, rn)
	if errno != 0 {
		goto err_out
	}

	file.dirHandle = &DirHandle{
		ds:        ds,
		dirIV:     dirIV,
		isRootDir: n.IsRoot(),
	}

	return file, fuseFlags, errno

err_out:
	if fd >= 0 {
		syscall.Close(fd)
	}
	if fdDup >= 0 {
		syscall.Close(fdDup)
	}
	if errno == 0 {
		tlog.Warn.Printf("BUG: OpendirHandle: err_out called with errno == 0")
		errno = syscall.EIO
	}
	return nil, 0, errno
}

type DirHandle struct {
	// Content of gocryptfs.diriv. nil if plaintextnames is used.
	dirIV []byte

	isRootDir bool

	// fs.loopbackDirStream with a private dup of the file descriptor
	ds fs.FileHandle
}

var _ = (fs.FileReleasedirer)((*File)(nil))

func (f *File) Releasedir(ctx context.Context, flags uint32) {
	// Does its own locking
	f.dirHandle.ds.(fs.FileReleasedirer).Releasedir(ctx, flags)
	// Does its own locking
	f.Release(ctx)
}

var _ = (fs.FileSeekdirer)((*File)(nil))

func (f *File) Seekdir(ctx context.Context, off uint64) syscall.Errno {
	return f.dirHandle.ds.(fs.FileSeekdirer).Seekdir(ctx, off)
}

var _ = (fs.FileFsyncdirer)((*File)(nil))

func (f *File) Fsyncdir(ctx context.Context, flags uint32) syscall.Errno {
	return f.dirHandle.ds.(fs.FileFsyncdirer).Fsyncdir(ctx, flags)
}

var _ = (fs.FileReaddirenter)((*File)(nil))

// This function is symlink-safe through use of openBackingDir() and
// ReadDirIVAt().
func (f *File) Readdirent(ctx context.Context) (entry *fuse.DirEntry, errno syscall.Errno) {
	f.fdLock.RLock()
	defer f.fdLock.RUnlock()

	for {
		entry, errno = f.dirHandle.ds.(fs.FileReaddirenter).Readdirent(ctx)
		if errno != 0 || entry == nil {
			return
		}

		cName := entry.Name
		if cName == "." || cName == ".." {
			// We want these as-is
			return
		}
		if f.dirHandle.isRootDir && cName == configfile.ConfDefaultName {
			// silently ignore "gocryptfs.conf" in the top level dir
			continue
		}
		if f.rootNode.args.PlaintextNames {
			return
		}
		if !f.rootNode.args.DeterministicNames && cName == nametransform.DirIVFilename {
			// silently ignore "gocryptfs.diriv" everywhere if dirIV is enabled
			continue
		}
		// Handle long file name
		isLong := nametransform.LongNameNone
		if f.rootNode.args.LongNames {
			isLong = nametransform.NameType(cName)
		}
		if isLong == nametransform.LongNameContent {
			cNameLong, err := nametransform.ReadLongNameAt(f.intFd(), cName)
			if err != nil {
				tlog.Warn.Printf("Readdirent: incomplete entry %q: Could not read .name: %v",
					cName, err)
				f.rootNode.reportMitigatedCorruption(cName)
				continue
			}
			cName = cNameLong
		} else if isLong == nametransform.LongNameFilename {
			// ignore "gocryptfs.longname.*.name"
			continue
		}
		name, err := f.rootNode.nameTransform.DecryptName(cName, f.dirHandle.dirIV)
		if err != nil {
			tlog.Warn.Printf("Readdirent: could not decrypt entry %q: %v",
				cName, err)
			f.rootNode.reportMitigatedCorruption(cName)
			continue
		}
		// Override the ciphertext name with the plaintext name but reuse the rest
		// of the structure
		entry.Name = name
		return
	}
}