2020-08-01 21:13:00 +02:00
|
|
|
package fusefrontend_reverse
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-08-20 17:06:18 +02:00
|
|
|
"log"
|
2020-08-01 21:13:00 +02:00
|
|
|
"path/filepath"
|
|
|
|
"syscall"
|
|
|
|
|
2020-08-02 19:33:12 +02:00
|
|
|
"golang.org/x/sys/unix"
|
|
|
|
|
2020-08-01 21:13:00 +02:00
|
|
|
"github.com/hanwen/go-fuse/v2/fs"
|
|
|
|
"github.com/hanwen/go-fuse/v2/fuse"
|
2020-08-02 19:33:12 +02:00
|
|
|
|
2021-08-23 15:05:15 +02:00
|
|
|
"github.com/rfjakob/gocryptfs/v2/internal/configfile"
|
|
|
|
"github.com/rfjakob/gocryptfs/v2/internal/pathiv"
|
|
|
|
"github.com/rfjakob/gocryptfs/v2/internal/syscallcompat"
|
2020-08-02 19:33:12 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// File names are padded to 16-byte multiples, encrypted and
|
|
|
|
// base64-encoded. We can encode at most 176 bytes to stay below the 255
|
|
|
|
// bytes limit:
|
|
|
|
// * base64(176 bytes) = 235 bytes
|
|
|
|
// * base64(192 bytes) = 256 bytes (over 255!)
|
|
|
|
// But the PKCS#7 padding is at least one byte. This means we can only use
|
|
|
|
// 175 bytes for the file name.
|
|
|
|
shortNameMax = 175
|
2020-08-01 21:13:00 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// translateSize translates the ciphertext size in `out` into plaintext size.
|
2020-08-08 18:45:47 +02:00
|
|
|
func (n *Node) translateSize(dirfd int, cName string, pName string, out *fuse.Attr) {
|
2020-08-01 21:13:00 +02:00
|
|
|
if out.IsRegular() {
|
|
|
|
rn := n.rootNode()
|
|
|
|
out.Size = rn.contentEnc.PlainSizeToCipherSize(out.Size)
|
|
|
|
} else if out.IsSymlink() {
|
2020-08-08 18:45:47 +02:00
|
|
|
cLink, _ := n.readlink(dirfd, cName, pName)
|
|
|
|
out.Size = uint64(len(cLink))
|
2020-08-01 21:13:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Path returns the relative plaintext path of this node
|
|
|
|
func (n *Node) Path() string {
|
|
|
|
return n.Inode.Path(n.Root())
|
|
|
|
}
|
|
|
|
|
|
|
|
// rootNode returns the Root Node of the filesystem.
|
|
|
|
func (n *Node) rootNode() *RootNode {
|
|
|
|
return n.Root().Operations().(*RootNode)
|
|
|
|
}
|
|
|
|
|
2020-08-15 17:31:25 +02:00
|
|
|
// dirfdPlus gets filled out as we gather information about a node
|
|
|
|
type dirfdPlus struct {
|
|
|
|
// fd to the directory, opened with O_DIRECTORY|O_PATH
|
|
|
|
dirfd int
|
|
|
|
// Relative plaintext path
|
|
|
|
pPath string
|
|
|
|
// Plaintext basename: filepath.Base(pPath)
|
|
|
|
pName string
|
|
|
|
// Relative ciphertext path
|
|
|
|
cPath string
|
|
|
|
// Ciphertext basename: filepath.Base(cPath)
|
|
|
|
cName string
|
|
|
|
}
|
|
|
|
|
2020-08-01 21:13:00 +02:00
|
|
|
// prepareAtSyscall returns a (dirfd, cName) pair that can be used
|
|
|
|
// with the "___at" family of system calls (openat, fstatat, unlinkat...) to
|
|
|
|
// access the backing encrypted directory.
|
|
|
|
//
|
|
|
|
// If you pass a `child` file name, the (dirfd, cName) pair will refer to
|
|
|
|
// a child of this node.
|
|
|
|
// If `child` is empty, the (dirfd, cName) pair refers to this node itself.
|
2020-08-15 17:31:25 +02:00
|
|
|
func (n *Node) prepareAtSyscall(child string) (d *dirfdPlus, errno syscall.Errno) {
|
|
|
|
cPath := n.Path()
|
2020-08-01 21:13:00 +02:00
|
|
|
if child != "" {
|
2020-08-15 17:31:25 +02:00
|
|
|
cPath = filepath.Join(cPath, child)
|
2020-08-01 21:13:00 +02:00
|
|
|
}
|
|
|
|
rn := n.rootNode()
|
2020-08-15 17:31:25 +02:00
|
|
|
dirfd, pPath, err := rn.openBackingDir(cPath)
|
2020-08-01 21:13:00 +02:00
|
|
|
if err != nil {
|
|
|
|
errno = fs.ToErrno(err)
|
|
|
|
}
|
2020-08-15 17:31:25 +02:00
|
|
|
d = &dirfdPlus{
|
|
|
|
dirfd: dirfd,
|
|
|
|
pPath: pPath,
|
|
|
|
pName: filepath.Base(pPath),
|
|
|
|
cPath: cPath,
|
|
|
|
cName: filepath.Base(cPath),
|
|
|
|
}
|
2020-08-01 21:13:00 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// newChild attaches a new child inode to n.
|
|
|
|
// The passed-in `st` will be modified to get a unique inode number.
|
|
|
|
func (n *Node) newChild(ctx context.Context, st *syscall.Stat_t, out *fuse.EntryOut) *fs.Inode {
|
2021-08-16 18:40:48 +02:00
|
|
|
isOtherFilesystem := (uint64(st.Dev) != n.rootNode().rootDev)
|
2020-08-01 21:13:00 +02:00
|
|
|
// Get unique inode number
|
|
|
|
rn := n.rootNode()
|
|
|
|
rn.inoMap.TranslateStat(st)
|
|
|
|
out.Attr.FromStat(st)
|
|
|
|
// Create child node
|
|
|
|
id := fs.StableAttr{
|
|
|
|
Mode: uint32(st.Mode),
|
|
|
|
Gen: 1,
|
|
|
|
Ino: st.Ino,
|
|
|
|
}
|
2021-08-16 18:40:48 +02:00
|
|
|
node := &Node{
|
|
|
|
isOtherFilesystem: isOtherFilesystem,
|
|
|
|
}
|
2020-08-01 21:13:00 +02:00
|
|
|
return n.NewInode(ctx, node, id)
|
|
|
|
}
|
2020-08-01 22:28:25 +02:00
|
|
|
|
|
|
|
// isRoot returns true if this node is the root node
|
|
|
|
func (n *Node) isRoot() bool {
|
|
|
|
rn := n.rootNode()
|
|
|
|
return &rn.Node == n
|
|
|
|
}
|
2020-08-02 19:33:12 +02:00
|
|
|
|
|
|
|
func (n *Node) lookupLongnameName(ctx context.Context, nameFile string, out *fuse.EntryOut) (ch *fs.Inode, errno syscall.Errno) {
|
2020-08-15 17:31:25 +02:00
|
|
|
d, errno := n.prepareAtSyscall("")
|
2020-08-02 19:33:12 +02:00
|
|
|
if errno != 0 {
|
|
|
|
return
|
|
|
|
}
|
2020-08-15 17:31:25 +02:00
|
|
|
defer syscall.Close(d.dirfd)
|
2020-08-02 19:33:12 +02:00
|
|
|
|
|
|
|
// Find the file the gocryptfs.longname.XYZ.name file belongs to in the
|
|
|
|
// directory listing
|
2020-08-15 17:31:25 +02:00
|
|
|
fd, err := syscallcompat.Openat(d.dirfd, d.pName, syscall.O_RDONLY|syscall.O_DIRECTORY|syscall.O_NOFOLLOW, 0)
|
2020-08-02 19:33:12 +02:00
|
|
|
if err != nil {
|
|
|
|
errno = fs.ToErrno(err)
|
|
|
|
return
|
|
|
|
}
|
2020-08-15 15:28:32 +02:00
|
|
|
defer syscall.Close(fd)
|
2020-08-02 19:33:12 +02:00
|
|
|
rn := n.rootNode()
|
2021-08-20 17:06:18 +02:00
|
|
|
diriv := rn.deriveDirIV(d.cPath)
|
2020-08-02 19:33:12 +02:00
|
|
|
pName, cFullname, errno := rn.findLongnameParent(fd, diriv, nameFile)
|
|
|
|
if errno != 0 {
|
|
|
|
return
|
|
|
|
}
|
2020-08-15 17:31:25 +02:00
|
|
|
if rn.isExcludedPlain(filepath.Join(d.cPath, pName)) {
|
|
|
|
errno = syscall.EPERM
|
|
|
|
return
|
|
|
|
}
|
2020-08-02 19:33:12 +02:00
|
|
|
// Get attrs from parent file
|
2020-08-15 17:31:25 +02:00
|
|
|
st, err := syscallcompat.Fstatat2(fd, pName, unix.AT_SYMLINK_NOFOLLOW)
|
2020-08-02 19:33:12 +02:00
|
|
|
if err != nil {
|
|
|
|
errno = fs.ToErrno(err)
|
|
|
|
return
|
|
|
|
}
|
2020-08-09 22:11:46 +02:00
|
|
|
var vf *VirtualMemNode
|
|
|
|
vf, errno = n.newVirtualMemNode([]byte(cFullname), st, inoTagNameFile)
|
2020-08-02 19:33:12 +02:00
|
|
|
if errno != 0 {
|
|
|
|
return nil, errno
|
|
|
|
}
|
|
|
|
out.Attr = vf.attr
|
|
|
|
// Create child node
|
|
|
|
id := fs.StableAttr{Mode: uint32(vf.attr.Mode), Gen: 1, Ino: vf.attr.Ino}
|
|
|
|
ch = n.NewInode(ctx, vf, id)
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// lookupDiriv returns a new Inode for a gocryptfs.diriv file inside `n`.
|
|
|
|
func (n *Node) lookupDiriv(ctx context.Context, out *fuse.EntryOut) (ch *fs.Inode, errno syscall.Errno) {
|
2021-08-20 17:06:18 +02:00
|
|
|
if rn := n.rootNode(); rn.args.DeterministicNames {
|
|
|
|
log.Panic("BUG: lookupDiriv called but DeterministicNames is set")
|
|
|
|
}
|
|
|
|
|
2020-08-15 17:31:25 +02:00
|
|
|
d, errno := n.prepareAtSyscall("")
|
2020-08-02 19:33:12 +02:00
|
|
|
if errno != 0 {
|
|
|
|
return
|
|
|
|
}
|
2020-08-15 17:31:25 +02:00
|
|
|
defer syscall.Close(d.dirfd)
|
|
|
|
st, err := syscallcompat.Fstatat2(d.dirfd, d.pName, unix.AT_SYMLINK_NOFOLLOW)
|
2020-08-02 19:33:12 +02:00
|
|
|
if err != nil {
|
|
|
|
errno = fs.ToErrno(err)
|
|
|
|
return
|
|
|
|
}
|
2020-08-15 17:31:25 +02:00
|
|
|
content := pathiv.Derive(d.cPath, pathiv.PurposeDirIV)
|
2020-08-09 22:11:46 +02:00
|
|
|
var vf *VirtualMemNode
|
|
|
|
vf, errno = n.newVirtualMemNode(content, st, inoTagDirIV)
|
2020-08-02 19:33:12 +02:00
|
|
|
if errno != 0 {
|
|
|
|
return nil, errno
|
|
|
|
}
|
|
|
|
out.Attr = vf.attr
|
|
|
|
// Create child node
|
|
|
|
id := fs.StableAttr{Mode: uint32(vf.attr.Mode), Gen: 1, Ino: vf.attr.Ino}
|
|
|
|
ch = n.NewInode(ctx, vf, id)
|
|
|
|
return
|
|
|
|
}
|
2020-08-08 18:45:47 +02:00
|
|
|
|
2020-08-09 22:11:46 +02:00
|
|
|
// 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)
|
|
|
|
if err != nil {
|
|
|
|
errno = fs.ToErrno(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// Get unique inode number
|
|
|
|
rn.inoMap.TranslateStat(&st)
|
|
|
|
out.Attr.FromStat(&st)
|
|
|
|
// Create child node
|
|
|
|
id := fs.StableAttr{
|
|
|
|
Mode: uint32(st.Mode),
|
|
|
|
Gen: 1,
|
|
|
|
Ino: st.Ino,
|
|
|
|
}
|
|
|
|
node := &VirtualConfNode{path: p}
|
|
|
|
ch = n.NewInode(ctx, node, id)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-08-08 18:45:47 +02:00
|
|
|
// readlink reads and encrypts a symlink. Used by Readlink, Getattr, Lookup.
|
|
|
|
func (n *Node) readlink(dirfd int, cName string, pName string) (out []byte, errno syscall.Errno) {
|
|
|
|
plainTarget, err := syscallcompat.Readlinkat(dirfd, pName)
|
|
|
|
if err != nil {
|
|
|
|
errno = fs.ToErrno(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
rn := n.rootNode()
|
|
|
|
if rn.args.PlaintextNames {
|
|
|
|
return []byte(plainTarget), 0
|
|
|
|
}
|
|
|
|
// Nonce is derived from the relative *ciphertext* path
|
|
|
|
p := filepath.Join(n.Path(), cName)
|
|
|
|
nonce := pathiv.Derive(p, pathiv.PurposeSymlinkIV)
|
|
|
|
// Symlinks are encrypted like file contents and base64-encoded
|
|
|
|
cBinTarget := rn.contentEnc.EncryptBlockNonce([]byte(plainTarget), 0, nil, nonce)
|
|
|
|
cTarget := rn.nameTransform.B64EncodeToString(cBinTarget)
|
|
|
|
// The kernel will reject a symlink target above 4096 chars and return
|
|
|
|
// and I/O error to the user. Better emit the proper error ourselves.
|
|
|
|
if len(cTarget) > syscallcompat.PATH_MAX {
|
|
|
|
errno = syscall.ENAMETOOLONG
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return []byte(cTarget), 0
|
|
|
|
}
|