2016-02-06 19:27:59 +01:00
|
|
|
package fusefrontend
|
2015-11-27 00:03:10 +01:00
|
|
|
|
2015-11-27 23:34:55 +01:00
|
|
|
import (
|
2016-02-06 22:54:14 +01:00
|
|
|
"path/filepath"
|
2018-10-14 20:11:49 +02:00
|
|
|
"strings"
|
|
|
|
"syscall"
|
2016-02-06 22:54:14 +01:00
|
|
|
|
2018-10-14 20:11:49 +02:00
|
|
|
"github.com/rfjakob/gocryptfs/internal/nametransform"
|
fusefrontend: use OpenDirNofollow in openBackingDir
Rename openBackingPath to openBackingDir and use OpenDirNofollow
to be safe against symlink races. Note that openBackingDir is
not used in several important code paths like Create().
But it is used in Unlink, and the performance impact in the RM benchmark
to be acceptable:
Before
$ ./benchmark.bash
Testing gocryptfs at /tmp/benchmark.bash.bYO: gocryptfs v1.6-12-g930c37e-dirty; go-fuse v20170619-49-gb11e293; 2018-09-08 go1.10.3
WRITE: 262144000 bytes (262 MB, 250 MiB) copied, 1.07979 s, 243 MB/s
READ: 262144000 bytes (262 MB, 250 MiB) copied, 0.882413 s, 297 MB/s
UNTAR: 16.703
MD5: 7.606
LS: 1.349
RM: 3.237
After
$ ./benchmark.bash
Testing gocryptfs at /tmp/benchmark.bash.jK3: gocryptfs v1.6-13-g84d6faf-dirty; go-fuse v20170619-49-gb11e293; 2018-09-08 go1.10.3
WRITE: 262144000 bytes (262 MB, 250 MiB) copied, 1.06261 s, 247 MB/s
READ: 262144000 bytes (262 MB, 250 MiB) copied, 0.947228 s, 277 MB/s
UNTAR: 17.197
MD5: 7.540
LS: 1.364
RM: 3.410
2018-09-08 19:27:33 +02:00
|
|
|
"github.com/rfjakob/gocryptfs/internal/syscallcompat"
|
2015-11-27 23:34:55 +01:00
|
|
|
)
|
2015-11-27 00:03:10 +01:00
|
|
|
|
fusefrontend: use OpenDirNofollow in openBackingDir
Rename openBackingPath to openBackingDir and use OpenDirNofollow
to be safe against symlink races. Note that openBackingDir is
not used in several important code paths like Create().
But it is used in Unlink, and the performance impact in the RM benchmark
to be acceptable:
Before
$ ./benchmark.bash
Testing gocryptfs at /tmp/benchmark.bash.bYO: gocryptfs v1.6-12-g930c37e-dirty; go-fuse v20170619-49-gb11e293; 2018-09-08 go1.10.3
WRITE: 262144000 bytes (262 MB, 250 MiB) copied, 1.07979 s, 243 MB/s
READ: 262144000 bytes (262 MB, 250 MiB) copied, 0.882413 s, 297 MB/s
UNTAR: 16.703
MD5: 7.606
LS: 1.349
RM: 3.237
After
$ ./benchmark.bash
Testing gocryptfs at /tmp/benchmark.bash.jK3: gocryptfs v1.6-13-g84d6faf-dirty; go-fuse v20170619-49-gb11e293; 2018-09-08 go1.10.3
WRITE: 262144000 bytes (262 MB, 250 MiB) copied, 1.06261 s, 247 MB/s
READ: 262144000 bytes (262 MB, 250 MiB) copied, 0.947228 s, 277 MB/s
UNTAR: 17.197
MD5: 7.540
LS: 1.364
RM: 3.410
2018-09-08 19:27:33 +02:00
|
|
|
// openBackingDir opens the parent ciphertext directory of plaintext path
|
2019-01-02 22:32:21 +01:00
|
|
|
// "relPath". It returns the dirfd (opened with O_PATH) and the encrypted
|
|
|
|
// basename.
|
2018-10-14 20:11:49 +02:00
|
|
|
//
|
fusefrontend: use OpenDirNofollow in openBackingDir
Rename openBackingPath to openBackingDir and use OpenDirNofollow
to be safe against symlink races. Note that openBackingDir is
not used in several important code paths like Create().
But it is used in Unlink, and the performance impact in the RM benchmark
to be acceptable:
Before
$ ./benchmark.bash
Testing gocryptfs at /tmp/benchmark.bash.bYO: gocryptfs v1.6-12-g930c37e-dirty; go-fuse v20170619-49-gb11e293; 2018-09-08 go1.10.3
WRITE: 262144000 bytes (262 MB, 250 MiB) copied, 1.07979 s, 243 MB/s
READ: 262144000 bytes (262 MB, 250 MiB) copied, 0.882413 s, 297 MB/s
UNTAR: 16.703
MD5: 7.606
LS: 1.349
RM: 3.237
After
$ ./benchmark.bash
Testing gocryptfs at /tmp/benchmark.bash.jK3: gocryptfs v1.6-13-g84d6faf-dirty; go-fuse v20170619-49-gb11e293; 2018-09-08 go1.10.3
WRITE: 262144000 bytes (262 MB, 250 MiB) copied, 1.06261 s, 247 MB/s
READ: 262144000 bytes (262 MB, 250 MiB) copied, 0.947228 s, 277 MB/s
UNTAR: 17.197
MD5: 7.540
LS: 1.364
RM: 3.410
2018-09-08 19:27:33 +02:00
|
|
|
// The caller should then use Openat(dirfd, cName, ...) and friends.
|
2018-10-14 20:11:49 +02:00
|
|
|
// For convenience, if relPath is "", cName is going to be ".".
|
|
|
|
//
|
|
|
|
// openBackingDir is secure against symlink races by using Openat and
|
|
|
|
// ReadDirIVAt.
|
2018-09-22 20:25:47 +02:00
|
|
|
func (fs *FS) openBackingDir(relPath string) (dirfd int, cName string, err error) {
|
2019-01-02 22:32:21 +01:00
|
|
|
dirRelPath := nametransform.Dir(relPath)
|
2018-10-14 20:11:49 +02:00
|
|
|
// With PlaintextNames, we don't need to read DirIVs. Easy.
|
|
|
|
if fs.args.PlaintextNames {
|
2019-01-02 22:32:21 +01:00
|
|
|
dirfd, err = syscallcompat.OpenDirNofollow(fs.args.Cipherdir, dirRelPath)
|
2018-10-14 20:11:49 +02:00
|
|
|
if err != nil {
|
|
|
|
return -1, "", err
|
|
|
|
}
|
|
|
|
// If relPath is empty, cName is ".".
|
|
|
|
cName = filepath.Base(relPath)
|
|
|
|
return dirfd, cName, nil
|
2017-11-28 01:11:19 +01:00
|
|
|
}
|
2019-01-02 22:32:21 +01:00
|
|
|
// Cache lookup
|
|
|
|
dirfd, iv := fs.dirCache.Lookup(dirRelPath)
|
|
|
|
if dirfd > 0 {
|
2019-01-03 15:59:54 +01:00
|
|
|
// If relPath is empty, cName is ".".
|
|
|
|
if relPath == "" {
|
|
|
|
return dirfd, ".", nil
|
|
|
|
}
|
2019-01-02 22:32:21 +01:00
|
|
|
name := filepath.Base(relPath)
|
|
|
|
cName = fs.nameTransform.EncryptAndHashName(name, iv)
|
|
|
|
return dirfd, cName, nil
|
|
|
|
}
|
2018-10-14 20:11:49 +02:00
|
|
|
// Open cipherdir (following symlinks)
|
2019-01-03 18:11:07 +01:00
|
|
|
dirfd, err = syscall.Open(fs.args.Cipherdir, syscall.O_DIRECTORY|syscallcompat.O_PATH, 0)
|
2017-11-28 01:11:19 +01:00
|
|
|
if err != nil {
|
2018-09-22 20:10:34 +02:00
|
|
|
return -1, "", err
|
2017-11-28 01:11:19 +01:00
|
|
|
}
|
2018-10-14 20:11:49 +02:00
|
|
|
// If relPath is empty, cName is ".".
|
|
|
|
if relPath == "" {
|
|
|
|
return dirfd, ".", nil
|
|
|
|
}
|
|
|
|
// Walk the directory tree
|
|
|
|
parts := strings.Split(relPath, "/")
|
|
|
|
for i, name := range parts {
|
|
|
|
iv, err := nametransform.ReadDirIVAt(dirfd)
|
|
|
|
if err != nil {
|
2019-01-02 00:14:12 +01:00
|
|
|
syscall.Close(dirfd)
|
2018-10-14 20:11:49 +02:00
|
|
|
return -1, "", err
|
|
|
|
}
|
|
|
|
cName = fs.nameTransform.EncryptAndHashName(name, iv)
|
|
|
|
// Last part? We are done.
|
|
|
|
if i == len(parts)-1 {
|
2019-01-02 22:32:21 +01:00
|
|
|
fs.dirCache.Store(dirRelPath, dirfd, iv)
|
2018-10-14 20:11:49 +02:00
|
|
|
break
|
|
|
|
}
|
|
|
|
// Not the last part? Descend into next directory.
|
2019-01-03 18:11:07 +01:00
|
|
|
dirfd2, err := syscallcompat.Openat(dirfd, cName, syscall.O_NOFOLLOW|syscall.O_DIRECTORY|syscallcompat.O_PATH, 0)
|
2018-10-14 20:11:49 +02:00
|
|
|
syscall.Close(dirfd)
|
|
|
|
if err != nil {
|
|
|
|
return -1, "", err
|
|
|
|
}
|
|
|
|
dirfd = dirfd2
|
|
|
|
}
|
2018-09-22 20:25:47 +02:00
|
|
|
return dirfd, cName, nil
|
2017-11-28 01:11:19 +01:00
|
|
|
}
|