fusefrontend: convert ctlsock from openBackingDir to prepareAtSyscall

openBackingDir will be removed.

Also, remove leftover debug printfs.
This commit is contained in:
Jakob Unterwurzacher 2021-06-26 18:39:23 +02:00
parent ee59b5269b
commit f9f4bd214f
1 changed files with 35 additions and 20 deletions

View File

@ -1,7 +1,6 @@
package fusefrontend package fusefrontend
import ( import (
"fmt"
"path" "path"
"path/filepath" "path/filepath"
"strings" "strings"
@ -18,7 +17,7 @@ var _ ctlsocksrv.Interface = &RootNode{} // Verify that interface is implemented
// EncryptPath implements ctlsock.Backend // EncryptPath implements ctlsock.Backend
// //
// Symlink-safe through openBackingDir(). // Symlink-safe through openBackingDir().
func (rn *RootNode) EncryptPath(plainPath string) (string, error) { func (rn *RootNode) EncryptPath(plainPath string) (cipherPath string, err error) {
if plainPath == "" { if plainPath == "" {
// Empty string gets encrypted as empty string // Empty string gets encrypted as empty string
return plainPath, nil return plainPath, nil
@ -26,22 +25,42 @@ func (rn *RootNode) EncryptPath(plainPath string) (string, error) {
if rn.args.PlaintextNames { if rn.args.PlaintextNames {
return plainPath, nil return plainPath, nil
} }
// Encrypt path level by level using openBackingDir. Pretty inefficient,
// but does not matter here. dirfd, _, errno := rn.prepareAtSyscallMyself()
if errno != 0 {
return "", errno
}
defer syscall.Close(dirfd)
// Encrypt path level by level
parts := strings.Split(plainPath, "/") parts := strings.Split(plainPath, "/")
wd := "" wd := dirfd
cPath := "" for i, part := range parts {
for _, part := range parts { dirIV, err := nametransform.ReadDirIVAt(wd)
wd = filepath.Join(wd, part)
dirfd, cName, err := rn.openBackingDir(wd)
if err != nil { if err != nil {
return "", err return "", err
} }
syscall.Close(dirfd) cPart, err := rn.nameTransform.EncryptAndHashName(part, dirIV)
cPath = filepath.Join(cPath, cName) if err != nil {
return "", err
}
cipherPath = filepath.Join(cipherPath, cPart)
// Last path component? We are done.
if i == len(parts)-1 {
break
}
// Descend into next directory
wd, err = syscallcompat.Openat(wd, cPart, syscall.O_NOFOLLOW|syscall.O_DIRECTORY|syscallcompat.O_PATH, 0)
if err != nil {
return "", err
}
// Yes this is somewhat wasteful in terms of used file descriptors:
// we keep them all open until the function returns. But it is simple
// and reliable.
defer syscall.Close(wd)
} }
tlog.Debug.Printf("encryptPath '%s' -> '%s'", plainPath, cPath) tlog.Debug.Printf("EncryptPath %q -> %q", plainPath, cipherPath)
return cPath, nil return cipherPath, nil
} }
// DecryptPath implements ctlsock.Backend // DecryptPath implements ctlsock.Backend
@ -49,9 +68,9 @@ func (rn *RootNode) EncryptPath(plainPath string) (string, error) {
// DecryptPath is symlink-safe because openBackingDir() and decryptPathAt() // DecryptPath is symlink-safe because openBackingDir() and decryptPathAt()
// are symlink-safe. // are symlink-safe.
func (rn *RootNode) DecryptPath(cipherPath string) (plainPath string, err error) { func (rn *RootNode) DecryptPath(cipherPath string) (plainPath string, err error) {
dirfd, _, err := rn.openBackingDir("") dirfd, _, errno := rn.prepareAtSyscallMyself()
if err != nil { if errno != 0 {
return "", err return "", errno
} }
defer syscall.Close(dirfd) defer syscall.Close(dirfd)
return rn.decryptPathAt(dirfd, cipherPath) return rn.decryptPathAt(dirfd, cipherPath)
@ -69,20 +88,17 @@ func (rn *RootNode) decryptPathAt(dirfd int, cipherPath string) (plainPath strin
for i, part := range parts { for i, part := range parts {
dirIV, err := nametransform.ReadDirIVAt(wd) dirIV, err := nametransform.ReadDirIVAt(wd)
if err != nil { if err != nil {
fmt.Printf("ReadDirIV: %v\n", err)
return "", err return "", err
} }
longPart := part longPart := part
if nametransform.IsLongContent(part) { if nametransform.IsLongContent(part) {
longPart, err = nametransform.ReadLongNameAt(wd, part) longPart, err = nametransform.ReadLongNameAt(wd, part)
if err != nil { if err != nil {
fmt.Printf("ReadLongName: %v\n", err)
return "", err return "", err
} }
} }
name, err := rn.nameTransform.DecryptName(longPart, dirIV) name, err := rn.nameTransform.DecryptName(longPart, dirIV)
if err != nil { if err != nil {
fmt.Printf("DecryptName: %v\n", err)
return "", err return "", err
} }
plainPath = path.Join(plainPath, name) plainPath = path.Join(plainPath, name)
@ -100,6 +116,5 @@ func (rn *RootNode) decryptPathAt(dirfd int, cipherPath string) (plainPath strin
// and reliable. // and reliable.
defer syscall.Close(wd) defer syscall.Close(wd)
} }
return plainPath, nil return plainPath, nil
} }