mirror of
https://github.com/opencontainers/runc.git
synced 2026-07-11 06:03:57 +08:00
mount: add support for ridmap and idmap
ridmap indicates that the id mapping should be applied recursively (only
really relevant for rbind mount entries), and idmap indicates that it
should not be applied recursively (the default). If no mappings are
specified for the mount, we use the userns configuration of the
container. This matches the behaviour in the currently-unreleased
runtime-spec.
This includes a minor change to the state.json serialisation format, but
because there has been no released version of runc with commit
fbf183c6f8 ("Add uid and gid mappings to mounts"), we can safely make
this change without affecting running containers. Doing it this way
makes it much easier to handle m.IsIDMapped() and indicating that a
mapping has been specified.
Signed-off-by: Aleksa Sarai <cyphar@cyphar.com>
This commit is contained in:
@@ -229,16 +229,32 @@ func mountFd(nsHandles *userns.Handles, m *configs.Mount) (*mountSource, error)
|
||||
sourceType = mountSourceOpenTree
|
||||
|
||||
// Configure the id mapping.
|
||||
usernsFile, err := nsHandles.Get(userns.Mapping{
|
||||
UIDMappings: m.UIDMappings,
|
||||
GIDMappings: m.GIDMappings,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to create userns for %s id-mapping: %w", m.Source, err)
|
||||
var usernsFile *os.File
|
||||
if m.IDMapping.UserNSPath == "" {
|
||||
usernsFile, err = nsHandles.Get(userns.Mapping{
|
||||
UIDMappings: m.IDMapping.UIDMappings,
|
||||
GIDMappings: m.IDMapping.GIDMappings,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to create userns for %s id-mapping: %w", m.Source, err)
|
||||
}
|
||||
} else {
|
||||
usernsFile, err = os.Open(m.IDMapping.UserNSPath)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to open existing userns for %s id-mapping: %w", m.Source, err)
|
||||
}
|
||||
}
|
||||
defer usernsFile.Close()
|
||||
|
||||
if err := unix.MountSetattr(int(mountFile.Fd()), "", unix.AT_EMPTY_PATH, &unix.MountAttr{
|
||||
setAttrFlags := uint(unix.AT_EMPTY_PATH)
|
||||
// If the mount has "ridmap" set, we apply the configuration
|
||||
// recursively. This allows you to create "rbind" mounts where only
|
||||
// the top-level mount has an idmapping. I'm not sure why you'd
|
||||
// want that, but still...
|
||||
if m.IDMapping.Recursive {
|
||||
setAttrFlags |= unix.AT_RECURSIVE
|
||||
}
|
||||
if err := unix.MountSetattr(int(mountFile.Fd()), "", setAttrFlags, &unix.MountAttr{
|
||||
Attr_set: unix.MOUNT_ATTR_IDMAP,
|
||||
Userns_fd: uint64(usernsFile.Fd()),
|
||||
}); err != nil {
|
||||
|
||||
Reference in New Issue
Block a user