|
| 1 | +package fusefrontend |
| 2 | + |
| 3 | +import ( |
| 4 | + "sync/atomic" |
| 5 | + "syscall" |
| 6 | + |
| 7 | + "github.com/rfjakob/gocryptfs/internal/tlog" |
| 8 | + |
| 9 | + "github.com/hanwen/go-fuse/v2/fs" |
| 10 | + |
| 11 | + "github.com/rfjakob/gocryptfs/internal/nametransform" |
| 12 | + "github.com/rfjakob/gocryptfs/internal/syscallcompat" |
| 13 | +) |
| 14 | + |
| 15 | +// prepareAtSyscall returns a (dirfd, cName) pair that can be used |
| 16 | +// with the "___at" family of system calls (openat, fstatat, unlinkat...) to |
| 17 | +// access the backing encrypted child file. |
| 18 | +func (n *Node) prepareAtSyscall(child string) (dirfd int, cName string, errno syscall.Errno) { |
| 19 | + if child == "" { |
| 20 | + tlog.Warn.Printf("BUG: prepareAtSyscall: child=%q, should have called prepareAtSyscallMyself", child) |
| 21 | + return n.prepareAtSyscallMyself() |
| 22 | + } |
| 23 | + |
| 24 | + rn := n.rootNode() |
| 25 | + |
| 26 | + // All filesystem operations go through here, so this is a good place |
| 27 | + // to reset the idle marker. |
| 28 | + atomic.StoreUint32(&rn.IsIdle, 0) |
| 29 | + |
| 30 | + if n.IsRoot() && rn.isFiltered(child) { |
| 31 | + return -1, "", syscall.EPERM |
| 32 | + } |
| 33 | + |
| 34 | + var encryptName func(int, string, []byte) (string, error) |
| 35 | + if !rn.args.PlaintextNames { |
| 36 | + encryptName = func(dirfd int, child string, iv []byte) (cName string, err error) { |
| 37 | + // Badname allowed, try to determine filenames |
| 38 | + if rn.nameTransform.HaveBadnamePatterns() { |
| 39 | + return rn.nameTransform.EncryptAndHashBadName(child, iv, dirfd) |
| 40 | + } |
| 41 | + return rn.nameTransform.EncryptAndHashName(child, iv) |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + // Cache lookup |
| 46 | + var iv []byte |
| 47 | + dirfd, iv = rn.dirCache.Lookup(n) |
| 48 | + if dirfd > 0 { |
| 49 | + if rn.args.PlaintextNames { |
| 50 | + return dirfd, child, 0 |
| 51 | + } |
| 52 | + var err error |
| 53 | + cName, err = encryptName(dirfd, child, iv) |
| 54 | + if err != nil { |
| 55 | + syscall.Close(dirfd) |
| 56 | + return -1, "", fs.ToErrno(err) |
| 57 | + } |
| 58 | + return |
| 59 | + } |
| 60 | + |
| 61 | + // Slowpath: Open ourselves & read diriv |
| 62 | + parentDirfd, myCName, errno := n.prepareAtSyscallMyself() |
| 63 | + if errno != 0 { |
| 64 | + return |
| 65 | + } |
| 66 | + defer syscall.Close(parentDirfd) |
| 67 | + |
| 68 | + dirfd, err := syscallcompat.Openat(parentDirfd, myCName, syscall.O_NOFOLLOW|syscall.O_DIRECTORY|syscallcompat.O_PATH, 0) |
| 69 | + |
| 70 | + // Cache store |
| 71 | + if !rn.args.PlaintextNames { |
| 72 | + var err error |
| 73 | + iv, err = nametransform.ReadDirIVAt(dirfd) |
| 74 | + if err != nil { |
| 75 | + syscall.Close(dirfd) |
| 76 | + return -1, "", fs.ToErrno(err) |
| 77 | + } |
| 78 | + } |
| 79 | + rn.dirCache.Store(n, dirfd, iv) |
| 80 | + |
| 81 | + if rn.args.PlaintextNames { |
| 82 | + return dirfd, child, 0 |
| 83 | + } |
| 84 | + |
| 85 | + cName, err = encryptName(dirfd, child, iv) |
| 86 | + if err != nil { |
| 87 | + syscall.Close(dirfd) |
| 88 | + return -1, "", fs.ToErrno(err) |
| 89 | + } |
| 90 | + |
| 91 | + return |
| 92 | +} |
| 93 | + |
| 94 | +func (n *Node) prepareAtSyscallMyself() (dirfd int, cName string, errno syscall.Errno) { |
| 95 | + dirfd = -1 |
| 96 | + |
| 97 | + // Handle root node |
| 98 | + if n.IsRoot() { |
| 99 | + var err error |
| 100 | + rn := n.rootNode() |
| 101 | + dirfd, cName, err = rn.openBackingDir("") |
| 102 | + if err != nil { |
| 103 | + errno = fs.ToErrno(err) |
| 104 | + } |
| 105 | + return |
| 106 | + } |
| 107 | + |
| 108 | + // Otherwise convert to prepareAtSyscall of parent node |
| 109 | + myName, p1 := n.Parent() |
| 110 | + if p1 == nil || myName == "" { |
| 111 | + errno = syscall.ENOENT |
| 112 | + return |
| 113 | + } |
| 114 | + parent := toNode(p1.Operations()) |
| 115 | + return parent.prepareAtSyscall(myName) |
| 116 | +} |
0 commit comments