libgocryptfs/internal/fusefrontend/node.go

285 lines
7.4 KiB
Go
Raw Normal View History

package fusefrontend
import (
"context"
2020-06-21 13:25:12 +02:00
"os"
"path/filepath"
"syscall"
"golang.org/x/sys/unix"
"github.com/hanwen/go-fuse/v2/fs"
"github.com/hanwen/go-fuse/v2/fuse"
2020-06-21 13:25:12 +02:00
"github.com/rfjakob/gocryptfs/internal/nametransform"
"github.com/rfjakob/gocryptfs/internal/syscallcompat"
2020-06-21 13:25:12 +02:00
"github.com/rfjakob/gocryptfs/internal/tlog"
)
// Node is a file or directory in the filesystem tree
// in a gocryptfs mount.
type Node struct {
fs.Inode
}
// path returns the relative plaintext path of this node
func (n *Node) path() string {
return n.Path(n.Root())
}
2020-07-04 21:16:20 +02:00
// rootNode returns the Root Node of the filesystem.
func (n *Node) rootNode() *RootNode {
return n.Root().Operations().(*RootNode)
}
2020-07-04 21:16:20 +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.
func (n *Node) prepareAtSyscall(child string) (dirfd int, cName string, errno syscall.Errno) {
p := n.path()
if child != "" {
p = filepath.Join(p, child)
}
rn := n.rootNode()
2020-06-21 13:25:12 +02:00
if rn.isFiltered(p) {
2020-07-04 21:16:20 +02:00
errno = syscall.EPERM
return
2020-06-21 13:25:12 +02:00
}
dirfd, cName, err := rn.openBackingDir(p)
if err != nil {
2020-07-04 21:16:20 +02:00
errno = fs.ToErrno(err)
}
return
}
// Lookup - FUSE call for discovering a file.
func (n *Node) Lookup(ctx context.Context, name string, out *fuse.EntryOut) (ch *fs.Inode, errno syscall.Errno) {
dirfd, cName, errno := n.prepareAtSyscall(name)
if errno != 0 {
return
}
2020-06-21 12:42:18 +02:00
defer syscall.Close(dirfd)
2020-07-04 21:16:20 +02:00
// Get device number and inode number into `st`
st, err := syscallcompat.Fstatat2(dirfd, cName, unix.AT_SYMLINK_NOFOLLOW)
if err != nil {
return nil, fs.ToErrno(err)
}
// Get unique inode number
2020-07-04 21:16:20 +02:00
n.rootNode().inoMap.TranslateStat(st)
out.Attr.FromStat(st)
// Create child node
id := fs.StableAttr{
Mode: uint32(st.Mode),
Gen: 1,
Ino: st.Ino,
}
node := &Node{}
2020-07-04 21:16:20 +02:00
ch = n.NewInode(ctx, node, id)
return ch, 0
}
2020-06-21 14:08:53 +02:00
// GetAttr - FUSE call for stat()ing a file.
//
// GetAttr is symlink-safe through use of openBackingDir() and Fstatat().
2020-07-04 21:16:20 +02:00
func (n *Node) Getattr(ctx context.Context, f fs.FileHandle, out *fuse.AttrOut) (errno syscall.Errno) {
dirfd, cName, errno := n.prepareAtSyscall("")
if errno != 0 {
return
2020-06-21 12:42:18 +02:00
}
defer syscall.Close(dirfd)
2020-06-21 12:42:18 +02:00
st, err := syscallcompat.Fstatat2(dirfd, cName, unix.AT_SYMLINK_NOFOLLOW)
if err != nil {
return fs.ToErrno(err)
}
2020-07-04 21:16:20 +02:00
n.rootNode().inoMap.TranslateStat(st)
2020-06-21 12:42:18 +02:00
out.Attr.FromStat(st)
return 0
}
2020-06-21 13:25:12 +02:00
2020-06-21 14:08:53 +02:00
// Create - FUSE call. Creates a new file.
//
// Symlink-safe through the use of Openat().
2020-06-21 13:25:12 +02:00
func (n *Node) Create(ctx context.Context, name string, flags uint32, mode uint32, out *fuse.EntryOut) (inode *fs.Inode, fh fs.FileHandle, fuseFlags uint32, errno syscall.Errno) {
2020-07-04 21:16:20 +02:00
dirfd, cName, errno := n.prepareAtSyscall(name)
if errno != 0 {
return
2020-06-21 13:25:12 +02:00
}
defer syscall.Close(dirfd)
2020-07-04 21:16:20 +02:00
var err error
2020-06-21 13:25:12 +02:00
fd := -1
// Make sure context is nil if we don't want to preserve the owner
2020-07-04 21:16:20 +02:00
rn := n.rootNode()
2020-06-21 13:25:12 +02:00
if !rn.args.PreserveOwner {
ctx = nil
}
newFlags := rn.mangleOpenFlags(flags)
// Handle long file name
if !rn.args.PlaintextNames && nametransform.IsLongContent(cName) {
// Create ".name"
2020-07-04 21:16:20 +02:00
err = rn.nameTransform.WriteLongNameAt(dirfd, cName, name)
2020-06-21 13:25:12 +02:00
if err != nil {
return nil, nil, 0, fs.ToErrno(err)
}
// Create content
fd, err = syscallcompat.OpenatUserCtx(dirfd, cName, newFlags|syscall.O_CREAT|syscall.O_EXCL, mode, ctx)
if err != nil {
nametransform.DeleteLongNameAt(dirfd, cName)
}
} else {
// Create content, normal (short) file name
fd, err = syscallcompat.OpenatUserCtx(dirfd, cName, newFlags|syscall.O_CREAT|syscall.O_EXCL, mode, ctx)
}
if err != nil {
// xfstests generic/488 triggers this
if err == syscall.EMFILE {
var lim syscall.Rlimit
syscall.Getrlimit(syscall.RLIMIT_NOFILE, &lim)
tlog.Warn.Printf("Create %q: too many open files. Current \"ulimit -n\": %d", cName, lim.Cur)
}
return nil, nil, 0, fs.ToErrno(err)
}
// Get device number and inode number into `st`
2020-07-04 21:37:44 +02:00
var st syscall.Stat_t
err = syscall.Fstat(fd, &st)
2020-06-21 13:25:12 +02:00
if err != nil {
2020-07-04 21:37:44 +02:00
errno = fs.ToErrno(err)
return
2020-06-21 13:25:12 +02:00
}
// Get unique inode number
2020-07-04 21:37:44 +02:00
rn.inoMap.TranslateStat(&st)
out.Attr.FromStat(&st)
2020-06-21 13:25:12 +02:00
// Create child node
id := fs.StableAttr{
Mode: uint32(st.Mode),
Gen: 1,
Ino: st.Ino,
}
node := &Node{}
ch := n.NewInode(ctx, node, id)
f := os.NewFile(uintptr(fd), cName)
2020-07-04 21:37:44 +02:00
return ch, NewFile2(f, rn, &st), 0, 0
2020-06-21 13:25:12 +02:00
}
2020-06-21 14:08:53 +02:00
// Unlink - FUSE call. Delete a file.
//
// Symlink-safe through use of Unlinkat().
2020-07-04 21:16:20 +02:00
func (n *Node) Unlink(ctx context.Context, name string) (errno syscall.Errno) {
dirfd, cName, errno := n.prepareAtSyscall(name)
if errno != 0 {
return
2020-06-21 14:08:53 +02:00
}
defer syscall.Close(dirfd)
2020-07-04 21:16:20 +02:00
2020-06-21 14:08:53 +02:00
// Delete content
2020-07-04 21:16:20 +02:00
err := syscallcompat.Unlinkat(dirfd, cName, 0)
2020-06-21 14:08:53 +02:00
if err != nil {
return fs.ToErrno(err)
}
// Delete ".name" file
2020-07-04 21:16:20 +02:00
if !n.rootNode().args.PlaintextNames && nametransform.IsLongContent(cName) {
2020-06-21 14:08:53 +02:00
err = nametransform.DeleteLongNameAt(dirfd, cName)
if err != nil {
tlog.Warn.Printf("Unlink: could not delete .name file: %v", err)
}
}
return fs.ToErrno(err)
}
2020-07-04 20:32:02 +02:00
// Readlink - FUSE call.
//
// Symlink-safe through openBackingDir() + Readlinkat().
2020-07-04 21:16:20 +02:00
func (n *Node) Readlink(ctx context.Context) (out []byte, errno syscall.Errno) {
dirfd, cName, errno := n.prepareAtSyscall("")
if errno != 0 {
return
2020-07-04 20:32:02 +02:00
}
defer syscall.Close(dirfd)
cTarget, err := syscallcompat.Readlinkat(dirfd, cName)
if err != nil {
return nil, fs.ToErrno(err)
}
2020-07-04 21:16:20 +02:00
rn := n.rootNode()
2020-07-04 20:32:02 +02:00
if rn.args.PlaintextNames {
return []byte(cTarget), 0
}
// Symlinks are encrypted like file contents (GCM) and base64-encoded
target, err := rn.decryptSymlinkTarget(cTarget)
if err != nil {
tlog.Warn.Printf("Readlink %q: decrypting target failed: %v", cName, err)
return nil, syscall.EIO
}
return []byte(target), 0
}
2020-07-04 21:37:44 +02:00
// Open - FUSE call. Open already-existing file.
//
// Symlink-safe through Openat().
func (n *Node) Open(ctx context.Context, flags uint32) (fh fs.FileHandle, fuseFlags uint32, errno syscall.Errno) {
dirfd, cName, errno := n.prepareAtSyscall("")
if errno != 0 {
return
}
defer syscall.Close(dirfd)
rn := n.rootNode()
newFlags := rn.mangleOpenFlags(flags)
// Taking this lock makes sure we don't race openWriteOnlyFile()
rn.openWriteOnlyLock.RLock()
defer rn.openWriteOnlyLock.RUnlock()
// Open backing file
fd, err := syscallcompat.Openat(dirfd, cName, newFlags, 0)
// Handle a few specific errors
if err != nil {
if err == syscall.EMFILE {
var lim syscall.Rlimit
syscall.Getrlimit(syscall.RLIMIT_NOFILE, &lim)
tlog.Warn.Printf("Open %q: too many open files. Current \"ulimit -n\": %d", cName, lim.Cur)
}
if err == syscall.EACCES && (int(flags)&syscall.O_ACCMODE) == syscall.O_WRONLY {
fd, err = rn.openWriteOnlyFile(dirfd, cName, newFlags)
}
}
// Could not handle the error? Bail out
if err != nil {
errno = fs.ToErrno(err)
return
}
var st syscall.Stat_t
err = syscall.Fstat(fd, &st)
if err != nil {
errno = fs.ToErrno(err)
return
}
f := os.NewFile(uintptr(fd), cName)
fh = NewFile2(f, rn, &st)
return
}
2020-07-05 20:05:07 +02:00
// Setattr - FUSE call. Called for chmod, truncate, utimens, ...
func (n *Node) Setattr(ctx context.Context, f fs.FileHandle, in *fuse.SetAttrIn, out *fuse.AttrOut) (errno syscall.Errno) {
var f2 *File2
if f != nil {
f2 = f.(*File2)
} else {
f, _, errno := n.Open(ctx, syscall.O_RDWR)
if errno != 0 {
return errno
}
f2 = f.(*File2)
defer f2.Release()
}
return f2.Setattr(ctx, in, out)
}