Skip to content

Commit 7832481

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 e17a161 commit 7832481

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
}
@@ -387,3 +396,26 @@ func (m orderedMounts) Swap(i, j int) {
387396
func (m orderedMounts) parts(i int) int {
388397
return strings.Count(filepath.Clean(m[i].Destination), string(os.PathSeparator))
389398
}
399+
400+
// specHasUserNamespace returns true if the OCI Spec has a Linux UserNamespace.
401+
func specHasUserNamespace(spec *oci.Spec) bool {
402+
if spec == nil || spec.Linux == nil {
403+
return false
404+
}
405+
for _, ns := range spec.Linux.Namespaces {
406+
if ns.Type == oci.UserNamespace {
407+
return true
408+
}
409+
}
410+
return false
411+
}
412+
413+
// cloneIDMappings clones a slice of OCI LinuxIDMappings.
414+
func cloneIDMappings(mappings []oci.LinuxIDMapping) []oci.LinuxIDMapping {
415+
if mappings == nil {
416+
return nil
417+
}
418+
clone := make([]oci.LinuxIDMapping, len(mappings))
419+
copy(clone, mappings)
420+
return clone
421+
}

pkg/cdi/container-edits_test.go

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