2020-06-21 12:42:18 +02:00
|
|
|
package fusefrontend
|
|
|
|
|
|
|
|
import (
|
2020-06-21 13:25:12 +02:00
|
|
|
"os"
|
2020-07-04 21:42:04 +02:00
|
|
|
"strings"
|
2020-06-21 13:46:08 +02:00
|
|
|
"sync"
|
2020-06-21 13:25:12 +02:00
|
|
|
"syscall"
|
2020-06-21 12:42:18 +02:00
|
|
|
"time"
|
|
|
|
|
2020-06-21 13:25:12 +02:00
|
|
|
"github.com/rfjakob/gocryptfs/internal/configfile"
|
2020-06-21 12:44:32 +02:00
|
|
|
"github.com/rfjakob/gocryptfs/internal/contentenc"
|
|
|
|
"github.com/rfjakob/gocryptfs/internal/inomap"
|
|
|
|
"github.com/rfjakob/gocryptfs/internal/nametransform"
|
2020-07-12 15:02:53 +02:00
|
|
|
"github.com/rfjakob/gocryptfs/internal/serialize_reads"
|
2020-06-21 13:25:12 +02:00
|
|
|
"github.com/rfjakob/gocryptfs/internal/syscallcompat"
|
2020-06-21 12:42:18 +02:00
|
|
|
"github.com/rfjakob/gocryptfs/internal/tlog"
|
|
|
|
)
|
|
|
|
|
2020-06-21 12:44:32 +02:00
|
|
|
// RootNode is the root of the filesystem tree of Nodes.
|
|
|
|
type RootNode struct {
|
|
|
|
Node
|
|
|
|
// args stores configuration arguments
|
|
|
|
args Args
|
2020-06-21 13:46:08 +02:00
|
|
|
// dirIVLock: Lock()ed if any "gocryptfs.diriv" file is modified
|
|
|
|
// Readers must RLock() it to prevent them from seeing intermediate
|
|
|
|
// states
|
|
|
|
dirIVLock sync.RWMutex
|
2020-06-21 12:44:32 +02:00
|
|
|
// Filename encryption helper
|
2021-06-21 11:53:33 +02:00
|
|
|
nameTransform *nametransform.NameTransform
|
2020-06-21 12:44:32 +02:00
|
|
|
// Content encryption helper
|
|
|
|
contentEnc *contentenc.ContentEnc
|
2020-07-04 21:37:44 +02:00
|
|
|
// This lock is used by openWriteOnlyFile() to block concurrent opens while
|
|
|
|
// it relaxes the permissions on a file.
|
|
|
|
openWriteOnlyLock sync.RWMutex
|
2020-06-21 12:44:32 +02:00
|
|
|
// MitigatedCorruptions is used to report data corruption that is internally
|
|
|
|
// mitigated by ignoring the corrupt item. For example, when OpenDir() finds
|
|
|
|
// a corrupt filename, we still return the other valid filenames.
|
|
|
|
// The corruption is logged to syslog to inform the user, and in addition,
|
|
|
|
// the corrupt filename is logged to this channel via
|
|
|
|
// reportMitigatedCorruption().
|
|
|
|
// "gocryptfs -fsck" reads from the channel to also catch these transparently-
|
|
|
|
// mitigated corruptions.
|
|
|
|
MitigatedCorruptions chan string
|
|
|
|
// IsIdle flag is set to zero each time fs.isFiltered() is called
|
|
|
|
// (uint32 so that it can be reset with CompareAndSwapUint32).
|
|
|
|
// When -idle was used when mounting, idleMonitor() sets it to 1
|
|
|
|
// periodically.
|
|
|
|
IsIdle uint32
|
2021-04-03 13:08:28 +02:00
|
|
|
// dirCache caches directory fds
|
2021-04-04 13:05:47 +02:00
|
|
|
dirCache dirCache
|
2020-06-21 12:44:32 +02:00
|
|
|
// inoMap translates inode numbers from different devices to unique inode
|
|
|
|
// numbers.
|
2021-07-31 13:24:25 +02:00
|
|
|
inoMap *inomap.InoMap
|
|
|
|
// gen is the node generation numbers. Normally, it is always set to 1,
|
|
|
|
// but -sharestorage uses an incrementing counter for new nodes.
|
|
|
|
// This makes each directory entry unique (even hard links),
|
|
|
|
// makes go-fuse hand out separate FUSE Node IDs for each, and prevents
|
|
|
|
// bizarre problems when inode numbers are reused behind our back.
|
|
|
|
gen uint64
|
2021-08-02 20:01:26 +02:00
|
|
|
// quirks is a bitmap that enables workaround for quirks in the filesystem
|
|
|
|
// backing the cipherdir
|
|
|
|
quirks uint64
|
2020-06-21 12:44:32 +02:00
|
|
|
}
|
|
|
|
|
2021-06-21 11:53:33 +02:00
|
|
|
func NewRootNode(args Args, c *contentenc.ContentEnc, n *nametransform.NameTransform) *RootNode {
|
2020-07-12 15:02:53 +02:00
|
|
|
if args.SerializeReads {
|
|
|
|
serialize_reads.InitSerializer()
|
|
|
|
}
|
|
|
|
if len(args.Exclude) > 0 {
|
|
|
|
tlog.Warn.Printf("Forward mode does not support -exclude")
|
|
|
|
}
|
fusefrontend: implement recursive diriv caching
The new contrib/maxlen.bash showed that we have exponential
runtime with respect to directory depth.
The new recursive diriv caching is a lot smarter as it caches
intermediate lookups. maxlen.bash now completes in a few seconds.
xfstests results same as
https://github.com/rfjakob/fuse-xfstests/blob/2d158e4c82be85c15269af77498e353f928f4fab/screenlog.0 :
Failures: generic/035 generic/062 generic/080 generic/093 generic/099 generic/215 generic/285 generic/319 generic/426 generic/444 generic/467 generic/477 generic/523
Failed 13 of 580 tests
benchmark.bash results are identical:
$ ./benchmark.bash
Testing gocryptfs at /tmp/benchmark.bash.BdQ: gocryptfs v2.0.1-17-g6b09bc0; go-fuse v2.1.1-0.20210611132105-24a1dfe6b4f8; 2021-06-25 go1.16.5 linux/amd64
/tmp/benchmark.bash.BdQ.mnt is a mountpoint
WRITE: 262144000 bytes (262 MB, 250 MiB) copied, 0,4821 s, 544 MB/s
READ: 262144000 bytes (262 MB, 250 MiB) copied, 0,266061 s, 985 MB/s
UNTAR: 8,280
MD5: 4,564
LS: 1,745
RM: 2,244
2021-06-25 11:33:18 +02:00
|
|
|
ivLen := nametransform.DirIVLen
|
|
|
|
if args.PlaintextNames {
|
|
|
|
ivLen = 0
|
|
|
|
}
|
2021-03-07 17:22:29 +01:00
|
|
|
rn := &RootNode{
|
2020-06-21 12:44:32 +02:00
|
|
|
args: args,
|
|
|
|
nameTransform: n,
|
|
|
|
contentEnc: c,
|
|
|
|
inoMap: inomap.New(),
|
fusefrontend: implement recursive diriv caching
The new contrib/maxlen.bash showed that we have exponential
runtime with respect to directory depth.
The new recursive diriv caching is a lot smarter as it caches
intermediate lookups. maxlen.bash now completes in a few seconds.
xfstests results same as
https://github.com/rfjakob/fuse-xfstests/blob/2d158e4c82be85c15269af77498e353f928f4fab/screenlog.0 :
Failures: generic/035 generic/062 generic/080 generic/093 generic/099 generic/215 generic/285 generic/319 generic/426 generic/444 generic/467 generic/477 generic/523
Failed 13 of 580 tests
benchmark.bash results are identical:
$ ./benchmark.bash
Testing gocryptfs at /tmp/benchmark.bash.BdQ: gocryptfs v2.0.1-17-g6b09bc0; go-fuse v2.1.1-0.20210611132105-24a1dfe6b4f8; 2021-06-25 go1.16.5 linux/amd64
/tmp/benchmark.bash.BdQ.mnt is a mountpoint
WRITE: 262144000 bytes (262 MB, 250 MiB) copied, 0,4821 s, 544 MB/s
READ: 262144000 bytes (262 MB, 250 MiB) copied, 0,266061 s, 985 MB/s
UNTAR: 8,280
MD5: 4,564
LS: 1,745
RM: 2,244
2021-06-25 11:33:18 +02:00
|
|
|
dirCache: dirCache{ivLen: ivLen},
|
2021-08-02 20:01:26 +02:00
|
|
|
quirks: detectQuirks(args.Cipherdir),
|
2020-06-21 12:44:32 +02:00
|
|
|
}
|
2021-03-07 17:22:29 +01:00
|
|
|
return rn
|
2020-06-21 12:44:32 +02:00
|
|
|
}
|
|
|
|
|
2021-04-05 18:20:17 +02:00
|
|
|
// main.doMount() calls this after unmount
|
|
|
|
func (rn *RootNode) AfterUnmount() {
|
|
|
|
// print stats before we exit
|
|
|
|
rn.dirCache.stats()
|
|
|
|
}
|
|
|
|
|
2020-06-21 13:25:12 +02:00
|
|
|
// mangleOpenFlags is used by Create() and Open() to convert the open flags the user
|
|
|
|
// wants to the flags we internally use to open the backing file.
|
|
|
|
// The returned flags always contain O_NOFOLLOW.
|
|
|
|
func (rn *RootNode) mangleOpenFlags(flags uint32) (newFlags int) {
|
|
|
|
newFlags = int(flags)
|
|
|
|
// Convert WRONLY to RDWR. We always need read access to do read-modify-write cycles.
|
|
|
|
if (newFlags & syscall.O_ACCMODE) == syscall.O_WRONLY {
|
|
|
|
newFlags = newFlags ^ os.O_WRONLY | os.O_RDWR
|
|
|
|
}
|
|
|
|
// We also cannot open the file in append mode, we need to seek back for RMW
|
|
|
|
newFlags = newFlags &^ os.O_APPEND
|
|
|
|
// O_DIRECT accesses must be aligned in both offset and length. Due to our
|
|
|
|
// crypto header, alignment will be off, even if userspace makes aligned
|
|
|
|
// accesses. Running xfstests generic/013 on ext4 used to trigger lots of
|
|
|
|
// EINVAL errors due to missing alignment. Just fall back to buffered IO.
|
|
|
|
newFlags = newFlags &^ syscallcompat.O_DIRECT
|
|
|
|
// Create and Open are two separate FUSE operations, so O_CREAT should not
|
|
|
|
// be part of the open flags.
|
|
|
|
newFlags = newFlags &^ syscall.O_CREAT
|
|
|
|
// We always want O_NOFOLLOW to be safe against symlink races
|
|
|
|
newFlags |= syscall.O_NOFOLLOW
|
|
|
|
return newFlags
|
|
|
|
}
|
|
|
|
|
2020-06-21 12:42:18 +02:00
|
|
|
// reportMitigatedCorruption is used to report a corruption that was transparently
|
|
|
|
// mitigated and did not return an error to the user. Pass the name of the corrupt
|
|
|
|
// item (filename for OpenDir(), xattr name for ListXAttr() etc).
|
|
|
|
// See the MitigatedCorruptions channel for more info.
|
|
|
|
func (rn *RootNode) reportMitigatedCorruption(item string) {
|
|
|
|
if rn.MitigatedCorruptions == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
case rn.MitigatedCorruptions <- item:
|
|
|
|
case <-time.After(1 * time.Second):
|
|
|
|
tlog.Warn.Printf("BUG: reportCorruptItem: timeout")
|
|
|
|
//debug.PrintStack()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2020-06-21 13:25:12 +02:00
|
|
|
|
fusefrontend: implement recursive diriv caching
The new contrib/maxlen.bash showed that we have exponential
runtime with respect to directory depth.
The new recursive diriv caching is a lot smarter as it caches
intermediate lookups. maxlen.bash now completes in a few seconds.
xfstests results same as
https://github.com/rfjakob/fuse-xfstests/blob/2d158e4c82be85c15269af77498e353f928f4fab/screenlog.0 :
Failures: generic/035 generic/062 generic/080 generic/093 generic/099 generic/215 generic/285 generic/319 generic/426 generic/444 generic/467 generic/477 generic/523
Failed 13 of 580 tests
benchmark.bash results are identical:
$ ./benchmark.bash
Testing gocryptfs at /tmp/benchmark.bash.BdQ: gocryptfs v2.0.1-17-g6b09bc0; go-fuse v2.1.1-0.20210611132105-24a1dfe6b4f8; 2021-06-25 go1.16.5 linux/amd64
/tmp/benchmark.bash.BdQ.mnt is a mountpoint
WRITE: 262144000 bytes (262 MB, 250 MiB) copied, 0,4821 s, 544 MB/s
READ: 262144000 bytes (262 MB, 250 MiB) copied, 0,266061 s, 985 MB/s
UNTAR: 8,280
MD5: 4,564
LS: 1,745
RM: 2,244
2021-06-25 11:33:18 +02:00
|
|
|
// isFiltered - check if plaintext file "child" should be forbidden
|
2020-06-21 13:25:12 +02:00
|
|
|
//
|
|
|
|
// Prevents name clashes with internal files when file names are not encrypted
|
fusefrontend: implement recursive diriv caching
The new contrib/maxlen.bash showed that we have exponential
runtime with respect to directory depth.
The new recursive diriv caching is a lot smarter as it caches
intermediate lookups. maxlen.bash now completes in a few seconds.
xfstests results same as
https://github.com/rfjakob/fuse-xfstests/blob/2d158e4c82be85c15269af77498e353f928f4fab/screenlog.0 :
Failures: generic/035 generic/062 generic/080 generic/093 generic/099 generic/215 generic/285 generic/319 generic/426 generic/444 generic/467 generic/477 generic/523
Failed 13 of 580 tests
benchmark.bash results are identical:
$ ./benchmark.bash
Testing gocryptfs at /tmp/benchmark.bash.BdQ: gocryptfs v2.0.1-17-g6b09bc0; go-fuse v2.1.1-0.20210611132105-24a1dfe6b4f8; 2021-06-25 go1.16.5 linux/amd64
/tmp/benchmark.bash.BdQ.mnt is a mountpoint
WRITE: 262144000 bytes (262 MB, 250 MiB) copied, 0,4821 s, 544 MB/s
READ: 262144000 bytes (262 MB, 250 MiB) copied, 0,266061 s, 985 MB/s
UNTAR: 8,280
MD5: 4,564
LS: 1,745
RM: 2,244
2021-06-25 11:33:18 +02:00
|
|
|
func (rn *RootNode) isFiltered(child string) bool {
|
2020-06-21 13:25:12 +02:00
|
|
|
if !rn.args.PlaintextNames {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
// gocryptfs.conf in the root directory is forbidden
|
fusefrontend: implement recursive diriv caching
The new contrib/maxlen.bash showed that we have exponential
runtime with respect to directory depth.
The new recursive diriv caching is a lot smarter as it caches
intermediate lookups. maxlen.bash now completes in a few seconds.
xfstests results same as
https://github.com/rfjakob/fuse-xfstests/blob/2d158e4c82be85c15269af77498e353f928f4fab/screenlog.0 :
Failures: generic/035 generic/062 generic/080 generic/093 generic/099 generic/215 generic/285 generic/319 generic/426 generic/444 generic/467 generic/477 generic/523
Failed 13 of 580 tests
benchmark.bash results are identical:
$ ./benchmark.bash
Testing gocryptfs at /tmp/benchmark.bash.BdQ: gocryptfs v2.0.1-17-g6b09bc0; go-fuse v2.1.1-0.20210611132105-24a1dfe6b4f8; 2021-06-25 go1.16.5 linux/amd64
/tmp/benchmark.bash.BdQ.mnt is a mountpoint
WRITE: 262144000 bytes (262 MB, 250 MiB) copied, 0,4821 s, 544 MB/s
READ: 262144000 bytes (262 MB, 250 MiB) copied, 0,266061 s, 985 MB/s
UNTAR: 8,280
MD5: 4,564
LS: 1,745
RM: 2,244
2021-06-25 11:33:18 +02:00
|
|
|
if child == configfile.ConfDefaultName {
|
2020-06-21 13:25:12 +02:00
|
|
|
tlog.Info.Printf("The name /%s is reserved when -plaintextnames is used\n",
|
|
|
|
configfile.ConfDefaultName)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
// Note: gocryptfs.diriv is NOT forbidden because diriv and plaintextnames
|
|
|
|
// are exclusive
|
|
|
|
return false
|
|
|
|
}
|
2020-07-04 20:32:02 +02:00
|
|
|
|
|
|
|
// decryptSymlinkTarget: "cData64" is base64-decoded and decrypted
|
|
|
|
// like file contents (GCM).
|
|
|
|
// The empty string decrypts to the empty string.
|
|
|
|
//
|
|
|
|
// This function does not do any I/O and is hence symlink-safe.
|
|
|
|
func (rn *RootNode) decryptSymlinkTarget(cData64 string) (string, error) {
|
|
|
|
if cData64 == "" {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
cData, err := rn.nameTransform.B64DecodeString(cData64)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
data, err := rn.contentEnc.DecryptBlock([]byte(cData), 0, nil)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return string(data), nil
|
|
|
|
}
|
2020-07-04 21:37:44 +02:00
|
|
|
|
|
|
|
// Due to RMW, we always need read permissions on the backing file. This is a
|
|
|
|
// problem if the file permissions do not allow reading (i.e. 0200 permissions).
|
|
|
|
// This function works around that problem by chmod'ing the file, obtaining a fd,
|
|
|
|
// and chmod'ing it back.
|
|
|
|
func (rn *RootNode) openWriteOnlyFile(dirfd int, cName string, newFlags int) (rwFd int, err error) {
|
|
|
|
woFd, err := syscallcompat.Openat(dirfd, cName, syscall.O_WRONLY|syscall.O_NOFOLLOW, 0)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer syscall.Close(woFd)
|
|
|
|
var st syscall.Stat_t
|
|
|
|
err = syscall.Fstat(woFd, &st)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// The cast to uint32 fixes a build failure on Darwin, where st.Mode is uint16.
|
|
|
|
perms := uint32(st.Mode)
|
|
|
|
// Verify that we don't have read permissions
|
|
|
|
if perms&0400 != 0 {
|
|
|
|
tlog.Warn.Printf("openWriteOnlyFile: unexpected permissions %#o, returning EPERM", perms)
|
|
|
|
err = syscall.EPERM
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// Upgrade the lock to block other Open()s and downgrade again on return
|
|
|
|
rn.openWriteOnlyLock.RUnlock()
|
|
|
|
rn.openWriteOnlyLock.Lock()
|
|
|
|
defer func() {
|
|
|
|
rn.openWriteOnlyLock.Unlock()
|
|
|
|
rn.openWriteOnlyLock.RLock()
|
|
|
|
}()
|
|
|
|
// Relax permissions and revert on return
|
|
|
|
err = syscall.Fchmod(woFd, perms|0400)
|
|
|
|
if err != nil {
|
|
|
|
tlog.Warn.Printf("openWriteOnlyFile: changing permissions failed: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
err2 := syscall.Fchmod(woFd, perms)
|
|
|
|
if err2 != nil {
|
|
|
|
tlog.Warn.Printf("openWriteOnlyFile: reverting permissions failed: %v", err2)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
return syscallcompat.Openat(dirfd, cName, newFlags, 0)
|
|
|
|
}
|
2020-07-04 21:42:04 +02:00
|
|
|
|
2020-07-11 19:43:07 +02:00
|
|
|
// encryptSymlinkTarget: "data" is encrypted like file contents (GCM)
|
|
|
|
// and base64-encoded.
|
|
|
|
// The empty string encrypts to the empty string.
|
|
|
|
//
|
|
|
|
// Symlink-safe because it does not do any I/O.
|
|
|
|
func (rn *RootNode) encryptSymlinkTarget(data string) (cData64 string) {
|
|
|
|
if data == "" {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
cData := rn.contentEnc.EncryptBlock([]byte(data), 0, nil)
|
|
|
|
cData64 = rn.nameTransform.B64EncodeToString(cData)
|
|
|
|
return cData64
|
|
|
|
}
|
2020-07-14 19:55:20 +02:00
|
|
|
|
|
|
|
// encryptXattrValue encrypts the xattr value "data".
|
|
|
|
// The data is encrypted like a file content block, but without binding it to
|
|
|
|
// a file location (block number and file id are set to zero).
|
|
|
|
// Special case: an empty value is encrypted to an empty value.
|
|
|
|
func (rn *RootNode) encryptXattrValue(data []byte) (cData []byte) {
|
|
|
|
if len(data) == 0 {
|
|
|
|
return []byte{}
|
|
|
|
}
|
|
|
|
return rn.contentEnc.EncryptBlock(data, 0, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// decryptXattrValue decrypts the xattr value "cData".
|
|
|
|
func (rn *RootNode) decryptXattrValue(cData []byte) (data []byte, err error) {
|
|
|
|
if len(cData) == 0 {
|
|
|
|
return []byte{}, nil
|
|
|
|
}
|
|
|
|
data, err1 := rn.contentEnc.DecryptBlock([]byte(cData), 0, nil)
|
|
|
|
if err1 == nil {
|
|
|
|
return data, nil
|
|
|
|
}
|
|
|
|
// This backward compatibility is needed to support old
|
|
|
|
// file systems having xattr values base64-encoded.
|
|
|
|
cData, err2 := rn.nameTransform.B64DecodeString(string(cData))
|
|
|
|
if err2 != nil {
|
|
|
|
// Looks like the value was not base64-encoded, but just corrupt.
|
|
|
|
// Return the original decryption error: err1
|
|
|
|
return nil, err1
|
|
|
|
}
|
|
|
|
return rn.contentEnc.DecryptBlock([]byte(cData), 0, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// encryptXattrName transforms "user.foo" to "user.gocryptfs.a5sAd4XAa47f5as6dAf"
|
2021-06-02 14:21:30 +02:00
|
|
|
func (rn *RootNode) encryptXattrName(attr string) (string, error) {
|
2020-07-14 19:55:20 +02:00
|
|
|
// xattr names are encrypted like file names, but with a fixed IV.
|
2021-06-02 14:21:30 +02:00
|
|
|
cAttr, err := rn.nameTransform.EncryptName(attr, xattrNameIV)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return xattrStorePrefix + cAttr, nil
|
2020-07-14 19:55:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (rn *RootNode) decryptXattrName(cAttr string) (attr string, err error) {
|
|
|
|
// Reject anything that does not start with "user.gocryptfs."
|
|
|
|
if !strings.HasPrefix(cAttr, xattrStorePrefix) {
|
|
|
|
return "", syscall.EINVAL
|
|
|
|
}
|
|
|
|
// Strip "user.gocryptfs." prefix
|
|
|
|
cAttr = cAttr[len(xattrStorePrefix):]
|
|
|
|
attr, err = rn.nameTransform.DecryptName(cAttr, xattrNameIV)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return attr, nil
|
|
|
|
}
|