2016-02-06 19:20:54 +01:00
|
|
|
package nametransform
|
2015-11-27 00:03:10 +01:00
|
|
|
|
|
|
|
import (
|
2021-08-20 10:57:26 +02:00
|
|
|
"bytes"
|
2018-04-02 18:32:30 +02:00
|
|
|
"fmt"
|
2016-11-06 14:09:34 +01:00
|
|
|
"io"
|
2016-01-25 00:51:28 +01:00
|
|
|
"os"
|
2016-02-07 14:02:09 +01:00
|
|
|
"syscall"
|
2016-02-06 19:20:54 +01:00
|
|
|
|
2021-08-23 15:05:15 +02:00
|
|
|
"github.com/rfjakob/gocryptfs/v2/internal/cryptocore"
|
|
|
|
"github.com/rfjakob/gocryptfs/v2/internal/syscallcompat"
|
|
|
|
"github.com/rfjakob/gocryptfs/v2/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-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.
|
2020-10-14 00:35:16 +02:00
|
|
|
// Retries on EINTR.
|
2021-08-20 10:57:26 +02:00
|
|
|
// If deterministicNames is set it returns an all-zero slice.
|
|
|
|
func (n *NameTransform) ReadDirIVAt(dirfd int) (iv []byte, err error) {
|
|
|
|
if n.deterministicNames {
|
|
|
|
return make([]byte, DirIVLen), nil
|
|
|
|
}
|
2018-09-22 20:10:34 +02:00
|
|
|
fdRaw, err := syscallcompat.Openat(dirfd, DirIVFilename,
|
2017-11-30 19:40:53 +01:00
|
|
|
syscall.O_RDONLY|syscall.O_NOFOLLOW, 0)
|
2016-04-10 19:32:10 +02:00
|
|
|
if err != nil {
|
2019-01-04 23:21:27 +01:00
|
|
|
return nil, err
|
2016-04-10 19:32:10 +02:00
|
|
|
}
|
|
|
|
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
|
|
|
|
2021-08-20 10:57:26 +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 {
|
2018-04-02 18:32:30 +02:00
|
|
|
return nil, fmt.Errorf("read failed: %v", err)
|
2016-04-10 19:32:10 +02:00
|
|
|
}
|
|
|
|
iv = iv[0:n]
|
2016-09-02 23:39:50 +02:00
|
|
|
if len(iv) != DirIVLen {
|
2018-04-02 18:32:30 +02:00
|
|
|
return nil, fmt.Errorf("wanted %d bytes, got %d", DirIVLen, len(iv))
|
2015-11-27 22:20:01 +01:00
|
|
|
}
|
2021-08-20 10:57:26 +02:00
|
|
|
if bytes.Equal(iv, allZeroDirIV) {
|
|
|
|
return nil, fmt.Errorf("diriv is all-zero")
|
|
|
|
}
|
2015-11-27 22:20:01 +01:00
|
|
|
return iv, nil
|
2015-11-27 00:03:10 +01:00
|
|
|
}
|
|
|
|
|
2019-01-03 13:32:13 +01:00
|
|
|
// WriteDirIVAt - create a new gocryptfs.diriv file in the directory opened at
|
|
|
|
// "dirfd". On error we try to delete the incomplete file.
|
|
|
|
// This function is exported because it is used from fusefrontend, main,
|
|
|
|
// and also the automated tests.
|
2021-08-20 10:57:26 +02:00
|
|
|
func WriteDirIVAt(dirfd int) error {
|
|
|
|
iv := cryptocore.RandBytes(DirIVLen)
|
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.
|
2019-01-01 16:22:06 +01:00
|
|
|
// Don't use "ioutil.WriteFile", it causes trouble on NFS:
|
2021-08-23 15:05:15 +02:00
|
|
|
// https://github.com/rfjakob/gocryptfs/v2/commit/7d38f80a78644c8ec4900cc990bfb894387112ed
|
2019-03-30 20:06:40 +01:00
|
|
|
fd, err := syscallcompat.Openat(dirfd, DirIVFilename, os.O_WRONLY|os.O_CREATE|os.O_EXCL, dirivPerms)
|
2016-04-10 19:32:10 +02:00
|
|
|
if err != nil {
|
2017-11-29 13:21:28 +01:00
|
|
|
tlog.Warn.Printf("WriteDirIV: Openat: %v", err)
|
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
|
|
|
return err
|
2016-04-10 19:32:10 +02:00
|
|
|
}
|
2019-01-03 13:32:13 +01:00
|
|
|
// Wrap the fd in an os.File - we need the write retry logic.
|
|
|
|
f := os.NewFile(uintptr(fd), DirIVFilename)
|
|
|
|
_, err = f.Write(iv)
|
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
|
|
|
if err != nil {
|
2019-01-03 13:32:13 +01:00
|
|
|
f.Close()
|
2018-07-15 14:14:12 +02:00
|
|
|
// It is normal to get ENOSPC here
|
|
|
|
if !syscallcompat.IsENOSPC(err) {
|
|
|
|
tlog.Warn.Printf("WriteDirIV: Write: %v", err)
|
|
|
|
}
|
2018-07-15 12:00:08 +02:00
|
|
|
// Delete incomplete gocryptfs.diriv file
|
2019-01-03 13:32:13 +01:00
|
|
|
syscallcompat.Unlinkat(dirfd, DirIVFilename, 0)
|
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
|
|
|
return err
|
|
|
|
}
|
2019-01-03 13:32:13 +01:00
|
|
|
err = f.Close()
|
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
|
|
|
if err != nil {
|
|
|
|
tlog.Warn.Printf("WriteDirIV: Close: %v", err)
|
2018-07-15 12:00:08 +02:00
|
|
|
// Delete incomplete gocryptfs.diriv file
|
2019-01-03 13:32:13 +01:00
|
|
|
syscallcompat.Unlinkat(dirfd, DirIVFilename, 0)
|
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
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2015-11-27 21:48:58 +01:00
|
|
|
}
|