2016-02-06 19:20:54 +01:00
|
|
|
package nametransform
|
2015-11-27 00:03:10 +01:00
|
|
|
|
|
|
|
import (
|
2017-05-25 14:21:55 +02:00
|
|
|
"bytes"
|
2016-11-06 14:09:34 +01:00
|
|
|
"io"
|
2016-01-25 00:51:28 +01:00
|
|
|
"os"
|
2015-11-27 22:18:36 +01:00
|
|
|
"path/filepath"
|
2015-11-27 00:03:10 +01:00
|
|
|
"strings"
|
2016-02-07 14:02:09 +01:00
|
|
|
"syscall"
|
2016-02-06 19:20:54 +01:00
|
|
|
|
|
|
|
"github.com/rfjakob/gocryptfs/internal/cryptocore"
|
2016-07-03 20:17:40 +02:00
|
|
|
"github.com/rfjakob/gocryptfs/internal/syscallcompat"
|
2016-06-15 23:30:44 +02:00
|
|
|
"github.com/rfjakob/gocryptfs/internal/tlog"
|
2016-02-06 19:20:54 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2016-10-02 06:14:18 +02:00
|
|
|
// DirIVLen is identical to AES block size
|
2016-09-02 23:39:50 +02:00
|
|
|
DirIVLen = 16
|
2016-10-02 06:14:18 +02:00
|
|
|
// DirIVFilename is the filename used to store directory IV.
|
|
|
|
// Exported because we have to ignore this name in directory listing.
|
2016-02-06 20:23:36 +01:00
|
|
|
DirIVFilename = "gocryptfs.diriv"
|
2015-11-27 00:03:10 +01:00
|
|
|
)
|
|
|
|
|
2016-02-06 22:54:14 +01:00
|
|
|
// ReadDirIV - read the "gocryptfs.diriv" file from "dir" (absolute ciphertext path)
|
2016-04-10 19:32:10 +02:00
|
|
|
// This function is exported because it allows for an efficient readdir implementation.
|
|
|
|
func ReadDirIV(dir string) (iv []byte, err error) {
|
2016-09-21 23:22:13 +02:00
|
|
|
fd, err := os.Open(filepath.Join(dir, DirIVFilename))
|
2016-04-10 19:32:10 +02:00
|
|
|
if err != nil {
|
2016-10-07 22:36:29 +02:00
|
|
|
// Note: getting errors here is normal because of concurrent deletes.
|
2016-04-10 19:32:10 +02:00
|
|
|
return nil, err
|
2015-11-27 22:20:01 +01:00
|
|
|
}
|
2016-09-21 23:22:13 +02:00
|
|
|
defer fd.Close()
|
|
|
|
return fdReadDirIV(fd)
|
2016-04-10 19:32:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// ReadDirIVAt reads "gocryptfs.diriv" from the directory that is opened as "dirfd".
|
|
|
|
// Using the dirfd makes it immune to concurrent renames of the directory.
|
|
|
|
func ReadDirIVAt(dirfd *os.File) (iv []byte, err error) {
|
2016-07-03 20:17:40 +02:00
|
|
|
fdRaw, err := syscallcompat.Openat(int(dirfd.Fd()), DirIVFilename, syscall.O_RDONLY, 0)
|
2016-04-10 19:32:10 +02:00
|
|
|
if err != nil {
|
2016-06-15 23:30:44 +02:00
|
|
|
tlog.Warn.Printf("ReadDirIVAt: opening %q in dir %q failed: %v",
|
2016-06-01 20:07:43 +02:00
|
|
|
DirIVFilename, dirfd.Name(), err)
|
2016-04-10 19:32:10 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
fd := os.NewFile(uintptr(fdRaw), DirIVFilename)
|
|
|
|
defer fd.Close()
|
2016-09-21 23:22:13 +02:00
|
|
|
return fdReadDirIV(fd)
|
|
|
|
}
|
2016-04-10 19:32:10 +02:00
|
|
|
|
2017-05-25 14:21:55 +02:00
|
|
|
// allZeroDirIV is preallocated to quickly check if the data read from disk is all zero
|
|
|
|
var allZeroDirIV = make([]byte, DirIVLen)
|
|
|
|
|
2016-09-21 23:22:13 +02:00
|
|
|
// fdReadDirIV reads and verifies the DirIV from an opened gocryptfs.diriv file.
|
|
|
|
func fdReadDirIV(fd *os.File) (iv []byte, err error) {
|
2016-09-02 23:39:50 +02:00
|
|
|
// We want to detect if the file is bigger than DirIVLen, so
|
2016-10-24 19:18:13 +02:00
|
|
|
// make the buffer 1 byte bigger than necessary.
|
2016-09-02 23:39:50 +02:00
|
|
|
iv = make([]byte, DirIVLen+1)
|
2016-04-10 19:32:10 +02:00
|
|
|
n, err := fd.Read(iv)
|
2016-11-06 14:09:34 +01:00
|
|
|
if err != nil && err != io.EOF {
|
2016-06-15 23:30:44 +02:00
|
|
|
tlog.Warn.Printf("ReadDirIVAt: Read failed: %v", err)
|
2016-04-10 19:32:10 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
iv = iv[0:n]
|
2016-09-02 23:39:50 +02:00
|
|
|
if len(iv) != DirIVLen {
|
2016-11-06 14:09:34 +01:00
|
|
|
tlog.Warn.Printf("ReadDirIVAt: wanted %d bytes, got %d. Returning EINVAL.", DirIVLen, len(iv))
|
2016-10-07 22:36:29 +02:00
|
|
|
return nil, syscall.EINVAL
|
2015-11-27 22:20:01 +01:00
|
|
|
}
|
2017-05-25 14:21:55 +02:00
|
|
|
if bytes.Equal(iv, allZeroDirIV) {
|
|
|
|
tlog.Warn.Printf("ReadDirIVAt: diriv is all-zero. Returning EINVAL.")
|
|
|
|
return nil, syscall.EINVAL
|
|
|
|
}
|
2015-11-27 22:20:01 +01:00
|
|
|
return iv, nil
|
2015-11-27 00:03:10 +01:00
|
|
|
}
|
|
|
|
|
2015-11-29 19:57:48 +01:00
|
|
|
// WriteDirIV - create diriv file inside "dir" (absolute ciphertext path)
|
2015-11-27 23:34:55 +01:00
|
|
|
// This function is exported because it is used from pathfs_frontend, main,
|
|
|
|
// and also the automated tests.
|
|
|
|
func WriteDirIV(dir string) error {
|
2016-09-02 23:39:50 +02:00
|
|
|
iv := cryptocore.RandBytes(DirIVLen)
|
2016-02-06 19:20:54 +01:00
|
|
|
file := filepath.Join(dir, DirIVFilename)
|
nametransform: WriteDirIV: replace ioutil.WriteFile
As reported at https://github.com/rfjakob/gocryptfs/issues/105 ,
the "ioutil.WriteFile(file, iv, 0400)" call causes "permissions denied"
errors on an NFSv4 setup.
"strace"ing diriv creation and gocryptfs.conf creation shows this:
conf (works on the user's NFSv4 mount):
openat(AT_FDCWD, "/tmp/a/gocryptfs.conf.tmp", O_WRONLY|O_CREAT|O_EXCL|O_CLOEXEC, 0400) = 3
diriv (fails):
openat(AT_FDCWD, "/tmp/a/gocryptfs.diriv", O_WRONLY|O_CREAT|O_TRUNC|O_CLOEXEC, 0400) = 3
This patch creates the diriv file with the same flags that are used for
creating the conf:
openat(AT_FDCWD, "/tmp/a/gocryptfs.diriv", O_WRONLY|O_CREAT|O_EXCL|O_CLOEXEC, 0400) = 3
Closes https://github.com/rfjakob/gocryptfs/issues/105
2017-04-29 14:15:13 +02:00
|
|
|
// 0400 permissions: gocryptfs.diriv should never be modified after creation.
|
|
|
|
// Don't use "ioutil.WriteFile", it causes trouble on NFS: https://github.com/rfjakob/gocryptfs/issues/105
|
|
|
|
fd, err := os.OpenFile(file, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0400)
|
2016-04-10 19:32:10 +02:00
|
|
|
if err != nil {
|
nametransform: WriteDirIV: replace ioutil.WriteFile
As reported at https://github.com/rfjakob/gocryptfs/issues/105 ,
the "ioutil.WriteFile(file, iv, 0400)" call causes "permissions denied"
errors on an NFSv4 setup.
"strace"ing diriv creation and gocryptfs.conf creation shows this:
conf (works on the user's NFSv4 mount):
openat(AT_FDCWD, "/tmp/a/gocryptfs.conf.tmp", O_WRONLY|O_CREAT|O_EXCL|O_CLOEXEC, 0400) = 3
diriv (fails):
openat(AT_FDCWD, "/tmp/a/gocryptfs.diriv", O_WRONLY|O_CREAT|O_TRUNC|O_CLOEXEC, 0400) = 3
This patch creates the diriv file with the same flags that are used for
creating the conf:
openat(AT_FDCWD, "/tmp/a/gocryptfs.diriv", O_WRONLY|O_CREAT|O_EXCL|O_CLOEXEC, 0400) = 3
Closes https://github.com/rfjakob/gocryptfs/issues/105
2017-04-29 14:15:13 +02:00
|
|
|
tlog.Warn.Printf("WriteDirIV: OpenFile: %v", err)
|
|
|
|
return err
|
2016-04-10 19:32:10 +02:00
|
|
|
}
|
nametransform: WriteDirIV: replace ioutil.WriteFile
As reported at https://github.com/rfjakob/gocryptfs/issues/105 ,
the "ioutil.WriteFile(file, iv, 0400)" call causes "permissions denied"
errors on an NFSv4 setup.
"strace"ing diriv creation and gocryptfs.conf creation shows this:
conf (works on the user's NFSv4 mount):
openat(AT_FDCWD, "/tmp/a/gocryptfs.conf.tmp", O_WRONLY|O_CREAT|O_EXCL|O_CLOEXEC, 0400) = 3
diriv (fails):
openat(AT_FDCWD, "/tmp/a/gocryptfs.diriv", O_WRONLY|O_CREAT|O_TRUNC|O_CLOEXEC, 0400) = 3
This patch creates the diriv file with the same flags that are used for
creating the conf:
openat(AT_FDCWD, "/tmp/a/gocryptfs.diriv", O_WRONLY|O_CREAT|O_EXCL|O_CLOEXEC, 0400) = 3
Closes https://github.com/rfjakob/gocryptfs/issues/105
2017-04-29 14:15:13 +02:00
|
|
|
_, err = fd.Write(iv)
|
|
|
|
if err != nil {
|
|
|
|
tlog.Warn.Printf("WriteDirIV: Write: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = fd.Close()
|
|
|
|
if err != nil {
|
|
|
|
tlog.Warn.Printf("WriteDirIV: Close: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2015-11-27 21:48:58 +01:00
|
|
|
}
|
|
|
|
|
2017-05-22 22:26:59 +02:00
|
|
|
// EncryptPathDirIV - encrypt relative plaintext path "plainPath" using EME with
|
|
|
|
// DirIV. "rootDir" is the backing storage root directory.
|
2016-04-10 19:32:10 +02:00
|
|
|
// Components that are longer than 255 bytes are hashed if be.longnames == true.
|
2016-02-06 19:20:54 +01:00
|
|
|
func (be *NameTransform) EncryptPathDirIV(plainPath string, rootDir string) (cipherPath string, err error) {
|
2015-11-27 00:03:10 +01:00
|
|
|
// Empty string means root directory
|
|
|
|
if plainPath == "" {
|
|
|
|
return plainPath, nil
|
|
|
|
}
|
2016-02-07 14:02:09 +01:00
|
|
|
// Reject names longer than 255 bytes already here. This relieves everybody
|
|
|
|
// who uses hashed long names from checking for that later.
|
|
|
|
baseName := filepath.Base(plainPath)
|
|
|
|
if len(baseName) > syscall.NAME_MAX {
|
|
|
|
return "", syscall.ENAMETOOLONG
|
|
|
|
}
|
2017-05-22 22:26:59 +02:00
|
|
|
// Check if the DirIV is cached. This catches the case of the user iterating
|
|
|
|
// over files in a directory pretty well.
|
2015-11-29 21:41:38 +01:00
|
|
|
parentDir := filepath.Dir(plainPath)
|
2016-11-01 10:34:41 +01:00
|
|
|
iv, cParentDir := be.DirIVCache.lookup(parentDir)
|
|
|
|
if iv != nil {
|
2016-02-06 22:54:14 +01:00
|
|
|
cBaseName := be.EncryptName(baseName, iv)
|
|
|
|
if be.longNames && len(cBaseName) > syscall.NAME_MAX {
|
2017-03-05 22:25:41 +01:00
|
|
|
cBaseName = be.HashLongName(cBaseName)
|
2016-02-06 22:54:14 +01:00
|
|
|
}
|
2016-11-09 23:41:47 +01:00
|
|
|
cipherPath = filepath.Join(cParentDir, cBaseName)
|
2015-12-08 16:13:29 +01:00
|
|
|
return cipherPath, nil
|
2015-11-29 21:41:38 +01:00
|
|
|
}
|
2017-05-22 22:26:59 +02:00
|
|
|
// We have to walk the directory tree, in the worst case starting at the root
|
|
|
|
// directory.
|
|
|
|
wd := rootDir
|
2015-11-27 00:03:10 +01:00
|
|
|
plainNames := strings.Split(plainPath, "/")
|
2017-05-22 22:26:59 +02:00
|
|
|
// So the DirIV we need is not cached. But maybe one level higher is
|
|
|
|
// cached. Then we can skip a few items in the directory walk.
|
|
|
|
// The catches the case of walking directories recursively.
|
|
|
|
parentDir2 := filepath.Dir(parentDir)
|
|
|
|
iv, cParentDir = be.DirIVCache.lookup(parentDir2)
|
|
|
|
if iv != nil {
|
|
|
|
parentDirBase := filepath.Base(parentDir)
|
|
|
|
cBaseName := be.EncryptName(parentDirBase, iv)
|
|
|
|
if be.longNames && len(cBaseName) > syscall.NAME_MAX {
|
|
|
|
cBaseName = be.HashLongName(cBaseName)
|
|
|
|
}
|
|
|
|
wd = filepath.Join(wd, cParentDir, cBaseName)
|
|
|
|
cipherPath = filepath.Join(cParentDir, cBaseName)
|
|
|
|
skip := len(strings.Split(cipherPath, "/"))
|
|
|
|
plainNames = plainNames[skip:]
|
|
|
|
}
|
|
|
|
// Walk the directory tree starting at "wd"
|
2015-11-27 00:03:10 +01:00
|
|
|
for _, plainName := range plainNames {
|
2016-02-06 22:54:14 +01:00
|
|
|
iv, err = ReadDirIV(wd)
|
2015-11-27 00:03:10 +01:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2016-02-06 22:54:14 +01:00
|
|
|
encryptedName := be.EncryptName(plainName, iv)
|
|
|
|
if be.longNames && len(encryptedName) > syscall.NAME_MAX {
|
2017-03-05 22:25:41 +01:00
|
|
|
encryptedName = be.HashLongName(encryptedName)
|
2016-02-06 22:54:14 +01:00
|
|
|
}
|
2017-05-22 22:26:59 +02:00
|
|
|
cipherPath = filepath.Join(cipherPath, encryptedName)
|
2015-11-27 00:03:10 +01:00
|
|
|
wd = filepath.Join(wd, encryptedName)
|
|
|
|
}
|
2016-02-06 22:54:14 +01:00
|
|
|
// Cache the final DirIV
|
2015-12-08 16:13:29 +01:00
|
|
|
cParentDir = filepath.Dir(cipherPath)
|
2016-02-06 12:27:09 +01:00
|
|
|
be.DirIVCache.store(parentDir, iv, cParentDir)
|
2015-12-08 16:13:29 +01:00
|
|
|
return cipherPath, nil
|
2015-11-27 00:03:10 +01:00
|
|
|
}
|