Skip to content

Commit 4d1f56c

Browse files
committed
cdi,SPECS.md: allow empty cgroup permissions.
Allow injecting devices with empty cgroup permissions, requested by the "none" permission string, also defined as pkg/cdi.NoPermissions. Signed-off-by: Krisztian Litkey <krisztian.litkey@intel.com>
1 parent 98a7d73 commit 4d1f56c

File tree

3 files changed

+64
-5
lines changed

3 files changed

+64
-5
lines changed

SPEC.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ The keywords "must", "must not", "required", "shall", "shall not", "should", "sh
139139
// * r - allows container to read from the specified device.
140140
// * w - allows container to write to the specified device.
141141
// * m - allows container to create device files that do not yet exist.
142+
// Omitted or empty permissions default to 'rwm'. 'none' requests empty permissions.
142143
"permissions": "<permissions>" (optional),
143144
"uid": <int> (optional),
144145
"gid": <int> (optional)

pkg/cdi/container-edits.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ const (
4242
PoststartHook = "poststart"
4343
// PoststopHook is the name of the OCI "poststop" hook.
4444
PoststopHook = "poststop"
45+
46+
// NoPermissions requests empty cgroup permissions for a device.
47+
NoPermissions = "none"
4548
)
4649

4750
var (
@@ -106,8 +109,11 @@ func (e *ContainerEdits) Apply(spec *oci.Spec) error {
106109

107110
if dev.Type == "b" || dev.Type == "c" {
108111
access := d.Permissions
109-
if access == "" {
112+
switch access {
113+
case "":
110114
access = "rwm"
115+
case NoPermissions:
116+
access = ""
111117
}
112118
specgen.AddLinuxResourcesDevice(true, dev.Type, &dev.Major, &dev.Minor, access)
113119
}
@@ -354,10 +360,12 @@ func (d *DeviceNode) Validate() error {
354360
if _, ok := validTypes[d.Type]; !ok {
355361
return fmt.Errorf("device %q: invalid type %q", d.Path, d.Type)
356362
}
357-
for _, bit := range d.Permissions {
358-
if bit != 'r' && bit != 'w' && bit != 'm' {
359-
return fmt.Errorf("device %q: invalid permissions %q",
360-
d.Path, d.Permissions)
363+
if d.Permissions != NoPermissions {
364+
for _, bit := range d.Permissions {
365+
if bit != 'r' && bit != 'w' && bit != 'm' {
366+
return fmt.Errorf("device %q: invalid permissions %q",
367+
d.Path, d.Permissions)
368+
}
361369
}
362370
}
363371
return nil

pkg/cdi/container-edits_test.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,18 @@ func TestValidateContainerEdits(t *testing.T) {
133133
},
134134
invalid: true,
135135
},
136+
{
137+
name: "valid device, with NoPermissions",
138+
edits: &cdi.ContainerEdits{
139+
DeviceNodes: []*cdi.DeviceNode{
140+
{
141+
Path: "/dev/vendorctl",
142+
Type: "b",
143+
Permissions: NoPermissions,
144+
},
145+
},
146+
},
147+
},
136148
{
137149
name: "valid mount",
138150
edits: &cdi.ContainerEdits{
@@ -420,6 +432,44 @@ func TestApplyContainerEdits(t *testing.T) {
420432
},
421433
},
422434
},
435+
{
436+
name: "empty spec, device with explicitly empty permissions",
437+
spec: &oci.Spec{},
438+
edits: &cdi.ContainerEdits{
439+
DeviceNodes: []*cdi.DeviceNode{
440+
{
441+
Path: "/dev/nil",
442+
Type: "c",
443+
Major: 1,
444+
Minor: 3,
445+
Permissions: NoPermissions,
446+
},
447+
},
448+
},
449+
result: &oci.Spec{
450+
Linux: &oci.Linux{
451+
Devices: []oci.LinuxDevice{
452+
{
453+
Path: "/dev/nil",
454+
Type: "c",
455+
Major: nullDeviceMajor,
456+
Minor: nullDeviceMinor,
457+
},
458+
},
459+
Resources: &oci.LinuxResources{
460+
Devices: []oci.LinuxDeviceCgroup{
461+
{
462+
Allow: true,
463+
Type: "c",
464+
Major: &nullDeviceMajor,
465+
Minor: &nullDeviceMinor,
466+
Access: "",
467+
},
468+
},
469+
},
470+
},
471+
},
472+
},
423473
{
424474
name: "empty spec, device, env var",
425475
spec: &oci.Spec{},

0 commit comments

Comments
 (0)