|
| 1 | +package fusefrontend |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "syscall" |
| 6 | + |
| 7 | + "github.com/hanwen/go-fuse/v2/fs" |
| 8 | + "github.com/hanwen/go-fuse/v2/fuse" |
| 9 | + "github.com/rfjakob/gocryptfs/v2/internal/configfile" |
| 10 | + "github.com/rfjakob/gocryptfs/v2/internal/nametransform" |
| 11 | + "github.com/rfjakob/gocryptfs/v2/internal/syscallcompat" |
| 12 | + "github.com/rfjakob/gocryptfs/v2/internal/tlog" |
| 13 | +) |
| 14 | + |
| 15 | +func (n *Node) OpendirHandle(ctx context.Context, flags uint32) (fh fs.FileHandle, fuseFlags uint32, errno syscall.Errno) { |
| 16 | + var fd int = -1 |
| 17 | + var fdDup int = -1 |
| 18 | + var file *File |
| 19 | + var dirIV []byte |
| 20 | + var ds fs.DirStream |
| 21 | + rn := n.rootNode() |
| 22 | + |
| 23 | + dirfd, cName, errno := n.prepareAtSyscallMyself() |
| 24 | + if errno != 0 { |
| 25 | + return |
| 26 | + } |
| 27 | + defer syscall.Close(dirfd) |
| 28 | + |
| 29 | + // Open backing directory |
| 30 | + fd, err := syscallcompat.Openat(dirfd, cName, syscall.O_RDONLY|syscall.O_DIRECTORY|syscall.O_NOFOLLOW, 0) |
| 31 | + if err != nil { |
| 32 | + errno = fs.ToErrno(err) |
| 33 | + return |
| 34 | + } |
| 35 | + |
| 36 | + // NewLoopbackDirStreamFd gets its own fd to untangle Release vs Releasedir |
| 37 | + fdDup, err = syscall.Dup(fd) |
| 38 | + |
| 39 | + if err != nil { |
| 40 | + errno = fs.ToErrno(err) |
| 41 | + goto err_out |
| 42 | + } |
| 43 | + |
| 44 | + ds, errno = fs.NewLoopbackDirStreamFd(fdDup) |
| 45 | + if errno != 0 { |
| 46 | + goto err_out |
| 47 | + } |
| 48 | + |
| 49 | + if !rn.args.PlaintextNames { |
| 50 | + // Read the DirIV from disk |
| 51 | + dirIV, err = rn.nameTransform.ReadDirIVAt(fd) |
| 52 | + if err != nil { |
| 53 | + tlog.Warn.Printf("OpendirHandle: could not read %s: %v", nametransform.DirIVFilename, err) |
| 54 | + errno = syscall.EIO |
| 55 | + goto err_out |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + file, _, errno = NewFile(fd, cName, rn) |
| 60 | + if errno != 0 { |
| 61 | + goto err_out |
| 62 | + } |
| 63 | + |
| 64 | + file.dirHandle = &DirHandle{ |
| 65 | + ds: ds, |
| 66 | + dirIV: dirIV, |
| 67 | + isRootDir: n.IsRoot(), |
| 68 | + } |
| 69 | + |
| 70 | + return file, fuseFlags, errno |
| 71 | + |
| 72 | +err_out: |
| 73 | + if fd >= 0 { |
| 74 | + syscall.Close(fd) |
| 75 | + } |
| 76 | + if fdDup >= 0 { |
| 77 | + syscall.Close(fdDup) |
| 78 | + } |
| 79 | + if errno == 0 { |
| 80 | + tlog.Warn.Printf("BUG: OpendirHandle: err_out called with errno == 0") |
| 81 | + errno = syscall.EIO |
| 82 | + } |
| 83 | + return nil, 0, errno |
| 84 | +} |
| 85 | + |
| 86 | +type DirHandle struct { |
| 87 | + // Content of gocryptfs.diriv. nil if plaintextnames is used. |
| 88 | + dirIV []byte |
| 89 | + |
| 90 | + isRootDir bool |
| 91 | + |
| 92 | + // fs.loopbackDirStream with a private dup of the file descriptor |
| 93 | + ds fs.FileHandle |
| 94 | +} |
| 95 | + |
| 96 | +var _ = (fs.FileReleasedirer)((*File)(nil)) |
| 97 | + |
| 98 | +func (f *File) Releasedir(ctx context.Context, flags uint32) { |
| 99 | + // Does its own locking |
| 100 | + f.dirHandle.ds.(fs.FileReleasedirer).Releasedir(ctx, flags) |
| 101 | + // Does its own locking |
| 102 | + f.Release(ctx) |
| 103 | +} |
| 104 | + |
| 105 | +var _ = (fs.FileSeekdirer)((*File)(nil)) |
| 106 | + |
| 107 | +func (f *File) Seekdir(ctx context.Context, off uint64) syscall.Errno { |
| 108 | + return f.dirHandle.ds.(fs.FileSeekdirer).Seekdir(ctx, off) |
| 109 | +} |
| 110 | + |
| 111 | +var _ = (fs.FileFsyncdirer)((*File)(nil)) |
| 112 | + |
| 113 | +func (f *File) Fsyncdir(ctx context.Context, flags uint32) syscall.Errno { |
| 114 | + return f.dirHandle.ds.(fs.FileFsyncdirer).Fsyncdir(ctx, flags) |
| 115 | +} |
| 116 | + |
| 117 | +var _ = (fs.FileReaddirenter)((*File)(nil)) |
| 118 | + |
| 119 | +// This function is symlink-safe through use of openBackingDir() and |
| 120 | +// ReadDirIVAt(). |
| 121 | +func (f *File) Readdirent(ctx context.Context) (entry *fuse.DirEntry, errno syscall.Errno) { |
| 122 | + f.fdLock.RLock() |
| 123 | + defer f.fdLock.RUnlock() |
| 124 | + |
| 125 | + for { |
| 126 | + entry, errno = f.dirHandle.ds.(fs.FileReaddirenter).Readdirent(ctx) |
| 127 | + if errno != 0 || entry == nil { |
| 128 | + return |
| 129 | + } |
| 130 | + |
| 131 | + cName := entry.Name |
| 132 | + if cName == "." || cName == ".." { |
| 133 | + // We want these as-is |
| 134 | + return |
| 135 | + } |
| 136 | + if f.dirHandle.isRootDir && cName == configfile.ConfDefaultName { |
| 137 | + // silently ignore "gocryptfs.conf" in the top level dir |
| 138 | + continue |
| 139 | + } |
| 140 | + if f.rootNode.args.PlaintextNames { |
| 141 | + return |
| 142 | + } |
| 143 | + if !f.rootNode.args.DeterministicNames && cName == nametransform.DirIVFilename { |
| 144 | + // silently ignore "gocryptfs.diriv" everywhere if dirIV is enabled |
| 145 | + continue |
| 146 | + } |
| 147 | + // Handle long file name |
| 148 | + isLong := nametransform.LongNameNone |
| 149 | + if f.rootNode.args.LongNames { |
| 150 | + isLong = nametransform.NameType(cName) |
| 151 | + } |
| 152 | + if isLong == nametransform.LongNameContent { |
| 153 | + cNameLong, err := nametransform.ReadLongNameAt(f.intFd(), cName) |
| 154 | + if err != nil { |
| 155 | + tlog.Warn.Printf("Readdirent: incomplete entry %q: Could not read .name: %v", |
| 156 | + cName, err) |
| 157 | + f.rootNode.reportMitigatedCorruption(cName) |
| 158 | + continue |
| 159 | + } |
| 160 | + cName = cNameLong |
| 161 | + } else if isLong == nametransform.LongNameFilename { |
| 162 | + // ignore "gocryptfs.longname.*.name" |
| 163 | + continue |
| 164 | + } |
| 165 | + name, err := f.rootNode.nameTransform.DecryptName(cName, f.dirHandle.dirIV) |
| 166 | + if err != nil { |
| 167 | + tlog.Warn.Printf("Readdirent: could not decrypt entry %q: %v", |
| 168 | + cName, err) |
| 169 | + f.rootNode.reportMitigatedCorruption(cName) |
| 170 | + continue |
| 171 | + } |
| 172 | + // Override the ciphertext name with the plaintext name but reuse the rest |
| 173 | + // of the structure |
| 174 | + entry.Name = name |
| 175 | + return |
| 176 | + } |
| 177 | +} |
0 commit comments