-
Notifications
You must be signed in to change notification settings - Fork 2.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
rootless: optional support for generating config with subuid map #1692
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,8 +2,11 @@ package specconv | |
|
||
import ( | ||
"os" | ||
"sort" | ||
"strings" | ||
|
||
"github.com/opencontainers/runc/libcontainer/system" | ||
"github.com/opencontainers/runc/libcontainer/user" | ||
"github.com/opencontainers/runtime-spec/specs-go" | ||
) | ||
|
||
|
@@ -155,10 +158,66 @@ func Example() *specs.Spec { | |
} | ||
} | ||
|
||
// RootlessOpts is an optional spec for ToRootless | ||
type RootlessOpts struct { | ||
// Add all subuids/subgids to spec.Linux.{U,G}IDMappings. | ||
// Note that in many cases users shouldn't be mapping all of their | ||
// allocated subuids/subgids for each container. | ||
// They should be using independent sets of uids and gids if possible. | ||
// | ||
// MapAllSubIDs requires newuidmap(1) and newgidmap(1) with suid bit. | ||
// | ||
// When running in userns, MapAllSubIDs is ignored and | ||
// /proc/self/[ug]id_map entries are used. | ||
MapAllSubIDs bool | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm going to add more options such as |
||
} | ||
|
||
// Run-time context for ToRootless. | ||
type RootlessContext struct { | ||
EUID uint32 | ||
EGID uint32 | ||
SubUIDs []user.SubID | ||
SubGIDs []user.SubID | ||
UIDMap []user.IDMap | ||
GIDMap []user.IDMap | ||
InUserNS bool | ||
} | ||
|
||
// ToRootless converts the given spec file into one that should work with | ||
// rootless containers, by removing incompatible options and adding others that | ||
// are needed. | ||
func ToRootless(spec *specs.Spec) { | ||
func ToRootless(spec *specs.Spec, opts *RootlessOpts) error { | ||
var err error | ||
ctx := RootlessContext{} | ||
ctx.EUID = uint32(os.Geteuid()) | ||
ctx.EGID = uint32(os.Getegid()) | ||
ctx.SubUIDs, err = user.CurrentUserSubUIDs() | ||
if err != nil && !os.IsNotExist(err) { | ||
return err | ||
} | ||
ctx.SubGIDs, err = user.CurrentGroupSubGIDs() | ||
if err != nil && !os.IsNotExist(err) { | ||
return err | ||
} | ||
ctx.UIDMap, err = user.CurrentProcessUIDMap() | ||
if err != nil && !os.IsNotExist(err) { | ||
return err | ||
} | ||
uidMapExists := !os.IsNotExist(err) | ||
ctx.GIDMap, err = user.CurrentProcessUIDMap() | ||
if err != nil && !os.IsNotExist(err) { | ||
return err | ||
} | ||
ctx.InUserNS = uidMapExists && system.UIDMapInUserNS(ctx.UIDMap) | ||
return ToRootlessWithContext(ctx, spec, opts) | ||
} | ||
|
||
// ToRootlessWithContext converts the spec with the run-time context. | ||
// ctx can be internally modified for sorting. | ||
func ToRootlessWithContext(ctx RootlessContext, spec *specs.Spec, opts *RootlessOpts) error { | ||
if opts == nil { | ||
opts = &RootlessOpts{} | ||
} | ||
var namespaces []specs.LinuxNamespace | ||
|
||
// Remove networkns from the spec. | ||
|
@@ -177,16 +236,66 @@ func ToRootless(spec *specs.Spec) { | |
spec.Linux.Namespaces = namespaces | ||
|
||
// Add mappings for the current user. | ||
spec.Linux.UIDMappings = []specs.LinuxIDMapping{{ | ||
HostID: uint32(os.Geteuid()), | ||
ContainerID: 0, | ||
Size: 1, | ||
}} | ||
spec.Linux.GIDMappings = []specs.LinuxIDMapping{{ | ||
HostID: uint32(os.Getegid()), | ||
ContainerID: 0, | ||
Size: 1, | ||
}} | ||
if ctx.InUserNS { | ||
uNextContainerID := int64(0) | ||
sort.Sort(idmapSorter(ctx.UIDMap)) | ||
for _, uidmap := range ctx.UIDMap { | ||
spec.Linux.UIDMappings = append(spec.Linux.UIDMappings, | ||
specs.LinuxIDMapping{ | ||
HostID: uint32(uidmap.ID), | ||
ContainerID: uint32(uNextContainerID), | ||
Size: uint32(uidmap.Count), | ||
}) | ||
uNextContainerID += uidmap.Count | ||
} | ||
gNextContainerID := int64(0) | ||
sort.Sort(idmapSorter(ctx.GIDMap)) | ||
for _, gidmap := range ctx.GIDMap { | ||
spec.Linux.GIDMappings = append(spec.Linux.GIDMappings, | ||
specs.LinuxIDMapping{ | ||
HostID: uint32(gidmap.ID), | ||
ContainerID: uint32(gNextContainerID), | ||
Size: uint32(gidmap.Count), | ||
}) | ||
gNextContainerID += gidmap.Count | ||
} | ||
// opts.MapAllSubIDs is ignored in userns | ||
} else { | ||
spec.Linux.UIDMappings = []specs.LinuxIDMapping{{ | ||
HostID: ctx.EUID, | ||
ContainerID: 0, | ||
Size: 1, | ||
}} | ||
spec.Linux.GIDMappings = []specs.LinuxIDMapping{{ | ||
HostID: ctx.EGID, | ||
ContainerID: 0, | ||
Size: 1, | ||
}} | ||
if opts.MapAllSubIDs { | ||
uNextContainerID := int64(1) | ||
sort.Sort(subIDSorter(ctx.SubUIDs)) | ||
for _, subuid := range ctx.SubUIDs { | ||
spec.Linux.UIDMappings = append(spec.Linux.UIDMappings, | ||
specs.LinuxIDMapping{ | ||
HostID: uint32(subuid.SubID), | ||
ContainerID: uint32(uNextContainerID), | ||
Size: uint32(subuid.Count), | ||
}) | ||
uNextContainerID += subuid.Count | ||
} | ||
gNextContainerID := int64(1) | ||
sort.Sort(subIDSorter(ctx.SubGIDs)) | ||
for _, subgid := range ctx.SubGIDs { | ||
spec.Linux.GIDMappings = append(spec.Linux.GIDMappings, | ||
specs.LinuxIDMapping{ | ||
HostID: uint32(subgid.SubID), | ||
ContainerID: uint32(gNextContainerID), | ||
Size: uint32(subgid.Count), | ||
}) | ||
gNextContainerID += subgid.Count | ||
} | ||
} | ||
} | ||
|
||
// Fix up mounts. | ||
var mounts []specs.Mount | ||
|
@@ -218,4 +327,18 @@ func ToRootless(spec *specs.Spec) { | |
|
||
// Remove cgroup settings. | ||
spec.Linux.Resources = nil | ||
return nil | ||
} | ||
|
||
// subIDSorter is required for Go <= 1.7 | ||
type subIDSorter []user.SubID | ||
|
||
func (x subIDSorter) Len() int { return len(x) } | ||
func (x subIDSorter) Swap(i, j int) { x[i], x[j] = x[j], x[i] } | ||
func (x subIDSorter) Less(i, j int) bool { return x[i].SubID < x[j].SubID } | ||
|
||
type idmapSorter []user.IDMap | ||
|
||
func (x idmapSorter) Len() int { return len(x) } | ||
func (x idmapSorter) Swap(i, j int) { x[i], x[j] = x[j], x[i] } | ||
func (x idmapSorter) Less(i, j int) bool { return x[i].ID < x[j].ID } |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As this is very confusing, I'm temporary closing this PR.
Please also see #1837