-
Notifications
You must be signed in to change notification settings - Fork 340
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fuse: enable id-mapped mount on supported kernels
The id-mapped mount feature for FUSE was introduced in Linux v6.12. This patch added a config option to enable the feature on supported kernels. Using id-mapped mount for FUSE requires the filesystem mounted with the "default_permissions" parameter and must trust the kernel to perform UID/GID-based checks correctly. For id-mapped mounts, FUSE will send mapped UID/GID in requests that create new inodes, and -1 for the others. More information in https://lwn.net/Articles/985803/ . A unit test was also written to validate the feature. The test requires root privilege to run due to the open_tree syscall. Change-Id: I6a93a0cd2109a03c5bb54946fba00c535d9d3d21
- Loading branch information
Showing
8 changed files
with
156 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
// Copyright 2025 the Go-FUSE Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package fs | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"os/exec" | ||
"path/filepath" | ||
"syscall" | ||
"testing" | ||
|
||
"github.com/hanwen/go-fuse/v2/fuse" | ||
"golang.org/x/sys/unix" | ||
) | ||
|
||
func TestIDMappedMount(t *testing.T) { | ||
// sys_open_tree requires CAP_SYS_ADMIN | ||
if os.Geteuid() != 0 { | ||
t.Skip("id-mapped mount requires CAP_SYS_ADMIN") | ||
} | ||
|
||
tc := newTestCase(t, &testOptions{idMappedMount: true}) | ||
tc.writeOrig("file", "hello", 0644) | ||
|
||
fi, err := os.Lstat(filepath.Join(tc.origDir, "file")) | ||
if err != nil { | ||
t.Fatalf("stat for path %s failed: %v", filepath.Join(tc.origDir, "file"), err) | ||
} | ||
st := fuse.ToStatT(fi) | ||
|
||
if tc.server.KernelSettings().Flags64()&fuse.CAP_ALLOW_IDMAP == 0 { | ||
t.Skip("Kernel does not support id-mapped mount") | ||
} | ||
|
||
const offset = 10000 | ||
fd, err := usernsFD(offset) | ||
if err != nil { | ||
t.Fatalf("failed to get user namespace FD: %v", err) | ||
} | ||
defer fd.Close() | ||
|
||
idDir := t.TempDir() | ||
if err = idMapMount(tc.mntDir, idDir, int(fd.Fd())); err != nil { | ||
t.Fatalf("id-mapped mount failed: %v", err) | ||
} | ||
defer unix.Unmount(idDir, 0) | ||
|
||
mfi, err := os.Lstat(filepath.Join(idDir, "file")) | ||
if err != nil { | ||
t.Fatalf("stat for path %s failed: %v", filepath.Join(idDir, "file"), err) | ||
} | ||
mst := fuse.ToStatT(mfi) | ||
|
||
if st.Uid+offset != mst.Uid { | ||
t.Errorf("Uid %v + offset %v != mapped Uid %v", st.Uid, offset, mst.Uid) | ||
} | ||
if st.Gid+offset != mst.Gid { | ||
t.Errorf("Gid %v + offset %v != mapped Gid %v", st.Gid, offset, mst.Gid) | ||
} | ||
} | ||
|
||
func idMapMount(source, target string, fd int) (err error) { | ||
const ignored = 0 | ||
dFd, err := unix.OpenTree(ignored, source, uint(unix.OPEN_TREE_CLONE|unix.OPEN_TREE_CLOEXEC|unix.AT_EMPTY_PATH)) | ||
if err != nil { | ||
return fmt.Errorf("open tree failed %s: %w", source, err) | ||
} | ||
defer unix.Close(dFd) | ||
if err = unix.MountSetattr(dFd, "", unix.AT_EMPTY_PATH, &unix.MountAttr{Attr_set: unix.MOUNT_ATTR_IDMAP, Userns_fd: uint64(fd)}); err != nil { | ||
return fmt.Errorf("set attr for %s failed: %w", source, err) | ||
} | ||
if err = unix.MoveMount(dFd, "", ignored, target, unix.MOVE_MOUNT_F_EMPTY_PATH); err != nil { | ||
return fmt.Errorf("move mount to %s failed: %w", target, err) | ||
} | ||
return nil | ||
} | ||
|
||
func usernsFD(offset int) (*os.File, error) { | ||
var err error | ||
args := []string{"sleep", "1h"} | ||
if args[0], err = exec.LookPath("sleep"); err != nil { | ||
return nil, fmt.Errorf("failed to find sleep binary: %w", err) | ||
} | ||
p, err := os.StartProcess(args[0], args, &os.ProcAttr{ | ||
Sys: &syscall.SysProcAttr{ | ||
Cloneflags: unix.CLONE_NEWUSER, | ||
UidMappings: []syscall.SysProcIDMap{ | ||
{ | ||
ContainerID: 0, | ||
HostID: offset, | ||
Size: offset, | ||
}, | ||
}, | ||
GidMappings: []syscall.SysProcIDMap{ | ||
{ | ||
ContainerID: 0, | ||
HostID: offset, | ||
Size: offset, | ||
}, | ||
}, | ||
Pdeathsig: syscall.SIGKILL, | ||
}, | ||
}) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to start process: %w", err) | ||
} | ||
defer func() { | ||
p.Kill() | ||
p.Wait() | ||
}() | ||
return os.Open(fmt.Sprintf("/proc/%d/ns/user", p.Pid)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters