From f6dad8d0fae25b5d88ad036b841fea10b7296ccb Mon Sep 17 00:00:00 2001 From: Jakob Unterwurzacher Date: Thu, 3 Jan 2019 13:32:13 +0100 Subject: nametransform: simplify WriteDirIV to WriteDirIVAt Un-spaghettify the function and let the callers open the directory. --- init_dir.go | 9 ++++++++- internal/fusefrontend/fs_dir.go | 9 +++++++-- internal/nametransform/diriv.go | 33 +++++++++++++-------------------- tests/test_helpers/helpers.go | 8 +++++++- 4 files changed, 35 insertions(+), 24 deletions(-) diff --git a/init_dir.go b/init_dir.go index 5d5cbc9..1f5a7fc 100644 --- a/init_dir.go +++ b/init_dir.go @@ -6,12 +6,14 @@ import ( "os" "path/filepath" "strings" + "syscall" "github.com/rfjakob/gocryptfs/internal/configfile" "github.com/rfjakob/gocryptfs/internal/cryptocore" "github.com/rfjakob/gocryptfs/internal/exitcodes" "github.com/rfjakob/gocryptfs/internal/nametransform" "github.com/rfjakob/gocryptfs/internal/readpassword" + "github.com/rfjakob/gocryptfs/internal/syscallcompat" "github.com/rfjakob/gocryptfs/internal/tlog" ) @@ -96,7 +98,12 @@ func initDir(args *argContainer) { // Forward mode with filename encryption enabled needs a gocryptfs.diriv file // in the root dir if !args.plaintextnames && !args.reverse { - err = nametransform.WriteDirIV(-1, args.cipherdir) + // Open cipherdir (following symlinks) + dirfd, err := syscall.Open(args.cipherdir, syscall.O_RDONLY|syscall.O_DIRECTORY|syscallcompat.O_PATH, 0) + if err == nil { + err = nametransform.WriteDirIVAt(dirfd) + syscall.Close(dirfd) + } if err != nil { tlog.Fatal.Println(err) os.Exit(exitcodes.Init) diff --git a/internal/fusefrontend/fs_dir.go b/internal/fusefrontend/fs_dir.go index 181ccb5..302fe38 100644 --- a/internal/fusefrontend/fs_dir.go +++ b/internal/fusefrontend/fs_dir.go @@ -37,9 +37,14 @@ func (fs *FS) mkdirWithIv(dirfd int, cName string, mode uint32) error { if err != nil { return err } - // Create gocryptfs.diriv - err = nametransform.WriteDirIV(dirfd, cName) + dirfd2, err := syscallcompat.Openat(dirfd, cName, syscall.O_DIRECTORY|syscall.O_NOFOLLOW|syscallcompat.O_PATH, 0) + if err == nil { + // Create gocryptfs.diriv + err = nametransform.WriteDirIVAt(dirfd2) + syscall.Close(dirfd2) + } if err != nil { + // Delete inconsistent directory (missing gocryptfs.diriv!) err2 := syscallcompat.Unlinkat(dirfd, cName, unix.AT_REMOVEDIR) if err2 != nil { tlog.Warn.Printf("mkdirWithIv: rollback failed: %v", err2) diff --git a/internal/nametransform/diriv.go b/internal/nametransform/diriv.go index 93c4c68..1e49ba1 100644 --- a/internal/nametransform/diriv.go +++ b/internal/nametransform/diriv.go @@ -4,10 +4,8 @@ import ( "bytes" "fmt" "io" - "log" "os" "path/filepath" - "strings" "syscall" "golang.org/x/sys/unix" @@ -60,43 +58,38 @@ func fdReadDirIV(fd *os.File) (iv []byte, err error) { return iv, nil } -// WriteDirIV - create diriv file inside of the specified directory. If dirfd -// is nil "dir" should be the absolute path to the directory. If dirfd != nil -// "dir" should be a path (without slashes) relative to the directory -// described by "dirfd". This function is exported because it is used from -// pathfs_frontend, main, and also the automated tests. -func WriteDirIV(dirfd int, dir string) error { - // For relative paths we do not expect that "dir" contains slashes - if dirfd >= 0 && strings.Contains(dir, "/") { - log.Panicf("WriteDirIV: Relative path should not contain slashes: %v", dir) - } +// 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. +func WriteDirIVAt(dirfd int) error { iv := cryptocore.RandBytes(DirIVLen) - file := filepath.Join(dir, DirIVFilename) // 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/commit/7d38f80a78644c8ec4900cc990bfb894387112ed - fdRaw, err := syscallcompat.Openat(dirfd, file, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0400) + fd, err := syscallcompat.Openat(dirfd, DirIVFilename, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0400) if err != nil { tlog.Warn.Printf("WriteDirIV: Openat: %v", err) return err } - fd := os.NewFile(uintptr(fdRaw), file) - _, err = fd.Write(iv) + // Wrap the fd in an os.File - we need the write retry logic. + f := os.NewFile(uintptr(fd), DirIVFilename) + _, err = f.Write(iv) if err != nil { - fd.Close() + f.Close() // It is normal to get ENOSPC here if !syscallcompat.IsENOSPC(err) { tlog.Warn.Printf("WriteDirIV: Write: %v", err) } // Delete incomplete gocryptfs.diriv file - syscallcompat.Unlinkat(dirfd, file, 0) + syscallcompat.Unlinkat(dirfd, DirIVFilename, 0) return err } - err = fd.Close() + err = f.Close() if err != nil { tlog.Warn.Printf("WriteDirIV: Close: %v", err) // Delete incomplete gocryptfs.diriv file - syscallcompat.Unlinkat(dirfd, file, 0) + syscallcompat.Unlinkat(dirfd, DirIVFilename, 0) return err } return nil diff --git a/tests/test_helpers/helpers.go b/tests/test_helpers/helpers.go index c6a4d6e..9ff29a8 100644 --- a/tests/test_helpers/helpers.go +++ b/tests/test_helpers/helpers.go @@ -18,6 +18,7 @@ import ( "github.com/rfjakob/gocryptfs/internal/ctlsock" "github.com/rfjakob/gocryptfs/internal/nametransform" + "github.com/rfjakob/gocryptfs/internal/syscallcompat" ) // TmpDir will be created inside this directory, set in init() to @@ -112,7 +113,12 @@ func ResetTmpDir(createDirIV bool) { panic(err) } if createDirIV { - err = nametransform.WriteDirIV(-1, DefaultCipherDir) + // Open cipherdir (following symlinks) + dirfd, err := syscall.Open(DefaultCipherDir, syscall.O_RDONLY|syscall.O_DIRECTORY|syscallcompat.O_PATH, 0) + if err == nil { + err = nametransform.WriteDirIVAt(dirfd) + syscall.Close(dirfd) + } if err != nil { panic(err) } -- cgit v1.2.3