v2api: fix Mkdir crash when using plaintextnames

This commit is contained in:
Jakob Unterwurzacher 2020-07-12 13:35:37 +02:00
parent b1d631d432
commit 735e2aa65b
2 changed files with 54 additions and 44 deletions

View File

@ -68,11 +68,21 @@ func (n *Node) Mkdir(ctx context.Context, name string, mode uint32, out *fuse.En
if rn.args.PreserveOwner { if rn.args.PreserveOwner {
caller, _ = fuse.FromContext(ctx) caller, _ = fuse.FromContext(ctx)
} }
var st syscall.Stat_t
if rn.args.PlaintextNames { if rn.args.PlaintextNames {
err = syscallcompat.MkdiratUser(dirfd, cName, mode, caller) err = syscallcompat.MkdiratUser(dirfd, cName, mode, caller)
if err != nil {
return nil, fs.ToErrno(err) return nil, fs.ToErrno(err)
} }
var ust unix.Stat_t
err = syscallcompat.Fstatat(dirfd, cName, &ust, unix.AT_SYMLINK_NOFOLLOW)
if err != nil {
return nil, fs.ToErrno(err)
}
st = syscallcompat.Unix2syscall(ust)
} else {
// We need write and execute permissions to create gocryptfs.diriv. // We need write and execute permissions to create gocryptfs.diriv.
// Also, we need read permissions to open the directory (to avoid // Also, we need read permissions to open the directory (to avoid
// race-conditions between getting and setting the mode). // race-conditions between getting and setting the mode).
@ -108,17 +118,13 @@ func (n *Node) Mkdir(ctx context.Context, name string, mode uint32, out *fuse.En
} }
defer syscall.Close(fd) defer syscall.Close(fd)
// Get unique inode number
var st syscall.Stat_t
err = syscall.Fstat(fd, &st) err = syscall.Fstat(fd, &st)
if err != nil { if err != nil {
tlog.Warn.Printf("Mkdir %q: Fstat failed: %v", cName, err) tlog.Warn.Printf("Mkdir %q: Fstat failed: %v", cName, err)
return nil, fs.ToErrno(err) return nil, fs.ToErrno(err)
} }
// Create child node
ch := n.newChild(ctx, &st, out)
// Set mode // Fix permissions
if origMode != mode { if origMode != mode {
// Preserve SGID bit if it was set due to inheritance. // Preserve SGID bit if it was set due to inheritance.
origMode = uint32(st.Mode&^0777) | origMode origMode = uint32(st.Mode&^0777) | origMode
@ -127,6 +133,10 @@ func (n *Node) Mkdir(ctx context.Context, name string, mode uint32, out *fuse.En
tlog.Warn.Printf("Mkdir %q: Fchmod %#o -> %#o failed: %v", cName, mode, origMode, err) tlog.Warn.Printf("Mkdir %q: Fchmod %#o -> %#o failed: %v", cName, mode, origMode, err)
} }
} }
}
// Create child node
ch := n.newChild(ctx, &st, out)
return ch, 0 return ch, 0
} }

View File

@ -280,7 +280,7 @@ func TestRename(t *testing.T, plainDir string) {
} }
err = syscall.Rename(file1, file2) err = syscall.Rename(file1, file2)
if err != nil { if err != nil {
t.Error(err) t.Errorf("Rename: %v", err)
return return
} }
syscall.Unlink(file2) syscall.Unlink(file2)