Skip to content

Commit aeae2d8

Browse files
committed
cdi: inject mount UID/GID mappings if user NS is in use.
When injecting mounts to a container with user namespaces, if the mount is a bind mount inject it with mappings taken from the OCI Spec (since currently the CDI Spec does not provide a way to add ID mappings to a mount). Signed-off-by: Krisztian Litkey <krisztian.litkey@intel.com>
1 parent ffd52dc commit aeae2d8

File tree

3 files changed

+166
-3
lines changed

3 files changed

+166
-3
lines changed

pkg/cdi/container-edits.go

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,23 @@ 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+
mnt := &Mount{m}
129+
if !mnt.isBindMount() {
130+
specgen.AddMount(mnt.toOCI())
131+
} else {
132+
specgen.AddMount(mnt.toOCI(withMountIDMappings(uids, gids)))
133+
}
120134
}
121135
sortMounts(&specgen)
122136
}
@@ -322,6 +336,18 @@ func (m *Mount) Validate() error {
322336
return nil
323337
}
324338

339+
func (m *Mount) isBindMount() bool {
340+
if m.Type == "bind" {
341+
return true
342+
}
343+
for _, o := range m.Options {
344+
if o == "bind" || o == "rbind" {
345+
return true
346+
}
347+
}
348+
return false
349+
}
350+
325351
// IntelRdt is a CDI IntelRdt wrapper.
326352
// This is used for validation and conversion to OCI specifications.
327353
type IntelRdt struct {
@@ -389,3 +415,26 @@ func (m orderedMounts) Swap(i, j int) {
389415
func (m orderedMounts) parts(i int) int {
390416
return strings.Count(filepath.Clean(m[i].Destination), string(os.PathSeparator))
391417
}
418+
419+
// specHasUserNamespace returns true if the OCI Spec has a Linux UserNamespace.
420+
func specHasUserNamespace(spec *oci.Spec) bool {
421+
if spec == nil || spec.Linux == nil {
422+
return false
423+
}
424+
for _, ns := range spec.Linux.Namespaces {
425+
if ns.Type == oci.UserNamespace {
426+
return true
427+
}
428+
}
429+
return false
430+
}
431+
432+
// cloneIDMappings clones a slice of OCI LinuxIDMappings.
433+
func cloneIDMappings(mappings []oci.LinuxIDMapping) []oci.LinuxIDMapping {
434+
if mappings == nil {
435+
return nil
436+
}
437+
clone := make([]oci.LinuxIDMapping, len(mappings))
438+
copy(clone, mappings)
439+
return clone
440+
}

pkg/cdi/container-edits_test.go

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -687,6 +687,105 @@ 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+
Type: "bind",
732+
},
733+
},
734+
},
735+
result: &oci.Spec{
736+
Linux: &oci.Linux{
737+
UIDMappings: []oci.LinuxIDMapping{
738+
{
739+
ContainerID: 0,
740+
HostID: 1000,
741+
Size: 999,
742+
},
743+
},
744+
GIDMappings: []oci.LinuxIDMapping{
745+
{
746+
ContainerID: 0,
747+
HostID: 2000,
748+
Size: 777,
749+
},
750+
},
751+
Namespaces: []oci.LinuxNamespace{
752+
{
753+
Type: oci.UserNamespace,
754+
Path: "/foo/bar",
755+
},
756+
},
757+
},
758+
Mounts: []oci.Mount{
759+
{
760+
Source: "/some/host/path1",
761+
Destination: "/dest/path/c",
762+
},
763+
{
764+
Source: "/some/host/path2",
765+
Destination: "/dest/path/b",
766+
},
767+
{
768+
Source: "/some/host/path3",
769+
Destination: "/dest/path/a",
770+
Type: "bind",
771+
UIDMappings: []oci.LinuxIDMapping{
772+
{
773+
ContainerID: 0,
774+
HostID: 1000,
775+
Size: 999,
776+
},
777+
},
778+
GIDMappings: []oci.LinuxIDMapping{
779+
{
780+
ContainerID: 0,
781+
HostID: 2000,
782+
Size: 777,
783+
},
784+
},
785+
},
786+
},
787+
},
788+
},
690789
} {
691790
t.Run(tc.name, func(t *testing.T) {
692791
edits := ContainerEdits{tc.edits}

pkg/cdi/oci.go

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,29 @@ 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+
m.UIDMappings = uid
40+
m.GIDMappings = gid
41+
}
42+
}
43+
3344
// toOCI returns the opencontainers runtime Spec Mount for this Mount.
34-
func (m *Mount) toOCI() spec.Mount {
35-
return spec.Mount{
45+
func (m *Mount) toOCI(options ...ociMountOption) spec.Mount {
46+
om := spec.Mount{
3647
Source: m.HostPath,
3748
Destination: m.ContainerPath,
3849
Options: m.Options,
3950
Type: m.Type,
4051
}
52+
for _, o := range options {
53+
o(&om)
54+
}
55+
return om
4156
}
4257

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

0 commit comments

Comments
 (0)