Skip to content

Commit 7116a1d

Browse files
committed
cdi: inject mount UID/GID mappings if user NS is in use.
When injecting mounts to a container with user namespaces in use, inject the mounts with UID and GID mappings taken from the OCI Spec. Signed-off-by: Krisztian Litkey <[email protected]>
1 parent ffd52dc commit 7116a1d

File tree

3 files changed

+151
-3
lines changed

3 files changed

+151
-3
lines changed

pkg/cdi/container-edits.go

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,18 @@ func (e *ContainerEdits) Apply(spec *oci.Spec) error {
114114
}
115115

116116
if len(e.Mounts) > 0 {
117+
var (
118+
uids []oci.LinuxIDMapping
119+
gids []oci.LinuxIDMapping
120+
)
121+
122+
if specHasUserNamespace(spec) {
123+
uids = cloneIDMappings(spec.Linux.UIDMappings)
124+
gids = cloneIDMappings(spec.Linux.GIDMappings)
125+
}
117126
for _, m := range e.Mounts {
118127
specgen.RemoveMount(m.ContainerPath)
119-
specgen.AddMount((&Mount{m}).toOCI())
128+
specgen.AddMount((&Mount{m}).toOCI(withMountIDMappings(uids, gids)))
120129
}
121130
sortMounts(&specgen)
122131
}
@@ -389,3 +398,26 @@ func (m orderedMounts) Swap(i, j int) {
389398
func (m orderedMounts) parts(i int) int {
390399
return strings.Count(filepath.Clean(m[i].Destination), string(os.PathSeparator))
391400
}
401+
402+
// specHasUserNamespace returns true if the OCI Spec has a Linux UserNamespace.
403+
func specHasUserNamespace(spec *oci.Spec) bool {
404+
if spec == nil || spec.Linux == nil {
405+
return false
406+
}
407+
for _, ns := range spec.Linux.Namespaces {
408+
if ns.Type == oci.UserNamespace {
409+
return true
410+
}
411+
}
412+
return false
413+
}
414+
415+
// cloneIDMappings clones a slice of OCI LinuxIDMappings.
416+
func cloneIDMappings(mappings []oci.LinuxIDMapping) []oci.LinuxIDMapping {
417+
if mappings == nil {
418+
return nil
419+
}
420+
clone := make([]oci.LinuxIDMapping, len(mappings))
421+
copy(clone, mappings)
422+
return clone
423+
}

pkg/cdi/container-edits_test.go

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -687,6 +687,103 @@ func TestApplyContainerEdits(t *testing.T) {
687687
},
688688
},
689689
},
690+
{
691+
name: "mount added to container with Linux user namespace and uid/gid mappings",
692+
spec: &oci.Spec{
693+
Linux: &oci.Linux{
694+
UIDMappings: []oci.LinuxIDMapping{
695+
{
696+
ContainerID: 0,
697+
HostID: 1000,
698+
Size: 999,
699+
},
700+
},
701+
GIDMappings: []oci.LinuxIDMapping{
702+
{
703+
ContainerID: 0,
704+
HostID: 2000,
705+
Size: 777,
706+
},
707+
},
708+
Namespaces: []oci.LinuxNamespace{
709+
{
710+
Type: oci.UserNamespace,
711+
Path: "/foo/bar",
712+
},
713+
},
714+
},
715+
Mounts: []oci.Mount{
716+
{
717+
Source: "/some/host/path1",
718+
Destination: "/dest/path/c",
719+
},
720+
{
721+
Source: "/some/host/path2",
722+
Destination: "/dest/path/b",
723+
},
724+
},
725+
},
726+
edits: &cdi.ContainerEdits{
727+
Mounts: []*cdi.Mount{
728+
{
729+
HostPath: "/some/host/path3",
730+
ContainerPath: "/dest/path/a",
731+
},
732+
},
733+
},
734+
result: &oci.Spec{
735+
Linux: &oci.Linux{
736+
UIDMappings: []oci.LinuxIDMapping{
737+
{
738+
ContainerID: 0,
739+
HostID: 1000,
740+
Size: 999,
741+
},
742+
},
743+
GIDMappings: []oci.LinuxIDMapping{
744+
{
745+
ContainerID: 0,
746+
HostID: 2000,
747+
Size: 777,
748+
},
749+
},
750+
Namespaces: []oci.LinuxNamespace{
751+
{
752+
Type: oci.UserNamespace,
753+
Path: "/foo/bar",
754+
},
755+
},
756+
},
757+
Mounts: []oci.Mount{
758+
{
759+
Source: "/some/host/path1",
760+
Destination: "/dest/path/c",
761+
},
762+
{
763+
Source: "/some/host/path2",
764+
Destination: "/dest/path/b",
765+
},
766+
{
767+
Source: "/some/host/path3",
768+
Destination: "/dest/path/a",
769+
UIDMappings: []oci.LinuxIDMapping{
770+
{
771+
ContainerID: 0,
772+
HostID: 1000,
773+
Size: 999,
774+
},
775+
},
776+
GIDMappings: []oci.LinuxIDMapping{
777+
{
778+
ContainerID: 0,
779+
HostID: 2000,
780+
Size: 777,
781+
},
782+
},
783+
},
784+
},
785+
},
786+
},
690787
} {
691788
t.Run(tc.name, func(t *testing.T) {
692789
edits := ContainerEdits{tc.edits}

pkg/cdi/oci.go

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,33 @@ func (h *Hook) toOCI() spec.Hook {
3030
}
3131
}
3232

33+
// Additional OCI mount option to apply to injected mounts.
34+
type ociMountOption func(*spec.Mount)
35+
36+
// withMountIDMappings adds UID and GID mappings for the given mount.
37+
func withMountIDMappings(uid, gid []spec.LinuxIDMapping) ociMountOption {
38+
return func(m *spec.Mount) {
39+
if uid != nil {
40+
m.UIDMappings = uid
41+
}
42+
if gid != nil {
43+
m.GIDMappings = gid
44+
}
45+
}
46+
}
47+
3348
// toOCI returns the opencontainers runtime Spec Mount for this Mount.
34-
func (m *Mount) toOCI() spec.Mount {
35-
return spec.Mount{
49+
func (m *Mount) toOCI(options ...ociMountOption) spec.Mount {
50+
om := spec.Mount{
3651
Source: m.HostPath,
3752
Destination: m.ContainerPath,
3853
Options: m.Options,
3954
Type: m.Type,
4055
}
56+
for _, o := range options {
57+
o(&om)
58+
}
59+
return om
4160
}
4261

4362
// toOCI returns the opencontainers runtime Spec LinuxDevice for this DeviceNode.

0 commit comments

Comments
 (0)