Skip to content

Commit 368ead0

Browse files
committed
validation: check the device rules by trying them
The unified hierarchy applies the same device rules cgroup v1 did, but it publishes no list to read them back from, so linux_cgroups_devices has nothing to compare against and is skipped there. The rules are still in force; only this test's way of looking at them is gone. Look at them from inside the container instead. The three rules the test already configures name no device that the bundle puts in the container, so add a node the first of them allows and one that only the generator's leading deny-all covers. The rules themselves are untouched, which leaves the cgroup v1 path reading exactly what it read before. runtimetest works out what the list permits by applying the entries in the listed order, as a runtime is required to, and compares that with what opening the node does. Only EPERM means the device controller refused, and it is reported before the driver behind the node is consulted, so nothing has to be loaded for the answer to be meaningful. Devices a runtime must supply are left alone, since runtimes add rules of their own for those. Signed-off-by: Daniel Golle <daniel@makrotopia.org>
1 parent 5f035b2 commit 368ead0

3 files changed

Lines changed: 185 additions & 6 deletions

File tree

‎cmd/runtimetest/main.go‎

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -663,6 +663,90 @@ func (c *complianceTester) validateLinuxDevices(spec *rspec.Spec) error {
663663
return nil
664664
}
665665

666+
func isDefaultDevicePath(path string) bool {
667+
for _, device := range defaultDevices {
668+
if device.Path == path {
669+
return true
670+
}
671+
}
672+
673+
return false
674+
}
675+
676+
func deviceRuleMatches(rule rspec.LinuxDeviceCgroup, devType string, major int64, minor int64) bool {
677+
if rule.Type != "" && rule.Type != "a" && rule.Type != devType {
678+
return false
679+
}
680+
if rule.Major != nil && *rule.Major != major {
681+
return false
682+
}
683+
if rule.Minor != nil && *rule.Minor != minor {
684+
return false
685+
}
686+
687+
return rule.Access == "" || strings.Contains(rule.Access, "r")
688+
}
689+
690+
// deviceReadAllowed applies the entries in the listed order, as a runtime
691+
// is required to, and reports what the last one to match has to say. A
692+
// device that no entry matches carries no restriction.
693+
func deviceReadAllowed(rules []rspec.LinuxDeviceCgroup, devType string, major int64, minor int64) bool {
694+
allowed := true
695+
for _, rule := range rules {
696+
if deviceRuleMatches(rule, devType, major, minor) {
697+
allowed = rule.Allow
698+
}
699+
}
700+
701+
return allowed
702+
}
703+
704+
// deviceDeniedByCgroup opens a device node to find out whether the device
705+
// controller refuses it. EPERM comes back before the driver behind the
706+
// node is consulted, so a node with nothing behind it answers the question
707+
// just as well as one that could be read.
708+
func deviceDeniedByCgroup(path string) bool {
709+
f, err := os.OpenFile(path, os.O_RDONLY, 0)
710+
if err == nil {
711+
f.Close()
712+
return false
713+
}
714+
715+
return errors.Is(err, syscall.EPERM)
716+
}
717+
718+
func (c *complianceTester) validateLinuxResourcesDevices(spec *rspec.Spec) error {
719+
if spec.Linux == nil || spec.Linux.Resources == nil || len(spec.Linux.Resources.Devices) == 0 {
720+
c.harness.Skip(1, "linux.resources.devices is not set")
721+
return nil
722+
}
723+
724+
for _, device := range spec.Linux.Devices {
725+
// Runtimes add rules of their own for the devices they are
726+
// required to supply, so those say nothing about the list that
727+
// was configured.
728+
if isDefaultDevicePath(device.Path) {
729+
continue
730+
}
731+
expected := !deviceReadAllowed(spec.Linux.Resources.Devices, device.Type, device.Major, device.Minor)
732+
denied := deviceDeniedByCgroup(device.Path)
733+
rfcError, err := c.Ok(denied == expected, specerror.DevicesApplyInOrder, spec.Version,
734+
fmt.Sprintf("access to %s follows linux.resources.devices", device.Path))
735+
if err != nil {
736+
return err
737+
}
738+
_ = c.harness.YAML(map[string]string{
739+
"level": rfcError.Level.String(),
740+
"reference": rfcError.Reference,
741+
"device": fmt.Sprintf("%s %d:%d", device.Type, device.Major, device.Minor),
742+
"expected": fmt.Sprintf("denied: %t", expected),
743+
"actual": fmt.Sprintf("denied: %t", denied),
744+
})
745+
}
746+
747+
return nil
748+
}
749+
666750
func (c *complianceTester) validateDevice(device *rspec.LinuxDevice, condition specerror.Code, version string, description string) (err error) {
667751
var exists bool
668752
fi, err := os.Stat(device.Path)
@@ -1297,6 +1381,7 @@ func run(context *cli.Context) error {
12971381
c.validateDefaultFS,
12981382
c.validateDefaultDevices,
12991383
c.validateLinuxDevices,
1384+
c.validateLinuxResourcesDevices,
13001385
c.validateLinuxProcess,
13011386
c.validateMaskedPaths,
13021387
c.validateOOMScoreAdj,

‎cmd/runtimetest/main_test.go‎

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package main
2+
3+
import (
4+
"testing"
5+
6+
rspec "github.com/opencontainers/runtime-spec/specs-go"
7+
)
8+
9+
func devicePtr(v int64) *int64 {
10+
return &v
11+
}
12+
13+
func TestDeviceReadAllowed(t *testing.T) {
14+
// The list linux_cgroups_devices builds: the generator's leading
15+
// deny-all followed by the three rules the test adds.
16+
rules := []rspec.LinuxDeviceCgroup{
17+
{Allow: false, Access: "rwm"},
18+
{Allow: true, Type: "c", Major: devicePtr(10), Minor: devicePtr(229), Access: "rwm"},
19+
{Allow: true, Type: "b", Major: devicePtr(8), Minor: devicePtr(20), Access: "rw"},
20+
{Allow: true, Type: "b", Major: devicePtr(10), Minor: devicePtr(200), Access: "r"},
21+
}
22+
23+
for _, test := range []struct {
24+
description string
25+
devType string
26+
rules []rspec.LinuxDeviceCgroup
27+
major int64
28+
minor int64
29+
expected bool
30+
}{
31+
{description: "explicitly allowed", rules: rules, devType: "c", major: 10, minor: 229, expected: true},
32+
{description: "a minor the allow does not cover", rules: rules, devType: "c", major: 10, minor: 230, expected: false},
33+
{description: "right numbers, wrong type", rules: rules, devType: "b", major: 10, minor: 229, expected: false},
34+
{description: "allowed block device", rules: rules, devType: "b", major: 8, minor: 20, expected: true},
35+
{description: "read-only allow still permits reading", rules: rules, devType: "b", major: 10, minor: 200, expected: true},
36+
{description: "covered only by the deny-all", rules: rules, devType: "c", major: 1, minor: 3, expected: false},
37+
{description: "no rules at all", rules: nil, devType: "c", major: 1, minor: 3, expected: true},
38+
{
39+
description: "a deny that does not mention read",
40+
rules: []rspec.LinuxDeviceCgroup{{Allow: false, Access: "w"}},
41+
devType: "c", major: 1, minor: 3, expected: true,
42+
},
43+
{
44+
description: "a later rule overrides an earlier one",
45+
rules: []rspec.LinuxDeviceCgroup{
46+
{Allow: true, Access: "rwm"},
47+
{Allow: false, Type: "c", Major: devicePtr(10), Minor: devicePtr(229), Access: "rwm"},
48+
},
49+
devType: "c", major: 10, minor: 229, expected: false,
50+
},
51+
} {
52+
got := deviceReadAllowed(test.rules, test.devType, test.major, test.minor)
53+
if got != test.expected {
54+
t.Errorf("%s: %s %d:%d allowed == %t, expected %t",
55+
test.description, test.devType, test.major, test.minor, got, test.expected)
56+
}
57+
}
58+
}
59+
60+
func TestIsDefaultDevicePath(t *testing.T) {
61+
if !isDefaultDevicePath("/dev/null") {
62+
t.Error("/dev/null is a default device")
63+
}
64+
if isDefaultDevicePath("/dev/denied") {
65+
t.Error("/dev/denied is not a default device")
66+
}
67+
}

‎validation/linux_cgroups_devices/linux_cgroups_devices.go‎

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,28 @@ import (
44
"os"
55

66
"github.com/mndrix/tap-go"
7+
rspec "github.com/opencontainers/runtime-spec/specs-go"
78
"github.com/opencontainers/runtime-tools/cgroups"
89
"github.com/opencontainers/runtime-tools/validation/util"
910
)
1011

11-
func main() {
12-
var major1, minor1, major2, minor2, major3, minor3 int64 = 10, 229, 8, 20, 10, 200
12+
func deviceNode(path string, major int64, minor int64) rspec.LinuxDevice {
13+
mode := os.FileMode(0o666)
14+
owner := uint32(0)
1315

14-
if util.UnifiedCgroups() {
15-
util.Skip("cgroup v1 specific linux.resources.devices test", nil)
16-
os.Exit(0)
16+
return rspec.LinuxDevice{
17+
Path: path,
18+
Type: "c",
19+
Major: major,
20+
Minor: minor,
21+
FileMode: &mode,
22+
UID: &owner,
23+
GID: &owner,
1724
}
25+
}
26+
27+
func main() {
28+
var major1, minor1, major2, minor2, major3, minor3 int64 = 10, 229, 8, 20, 10, 200
1829

1930
t := tap.New()
2031
t.Header(0)
@@ -28,7 +39,23 @@ func main() {
2839
g.AddLinuxResourcesDevice(true, "c", &major1, &minor1, "rwm")
2940
g.AddLinuxResourcesDevice(true, "b", &major2, &minor2, "rw")
3041
g.AddLinuxResourcesDevice(true, "b", &major3, &minor3, "r")
31-
err = util.RuntimeOutsideValidate(g, t, util.ValidateLinuxResourcesDevices)
42+
43+
// The rules above name no device that the bundle puts in the
44+
// container, so nothing about them can be observed from the inside.
45+
// Add a node the first rule allows and one that only the leading
46+
// deny-all covers, which leaves the rules themselves untouched.
47+
g.AddDevice(deviceNode("/dev/allowed", major1, minor1))
48+
g.AddDevice(deviceNode("/dev/denied", major1, minor1+1))
49+
50+
// Reading the rules back is a cgroup v1 affair. The unified hierarchy
51+
// applies the same rules but publishes no list to compare against, so
52+
// there they are checked by trying the two nodes above.
53+
if util.UnifiedCgroups() {
54+
g.AddAnnotation("TestName", "check linux.resources.devices")
55+
err = util.RuntimeInsideValidate(g, t, nil)
56+
} else {
57+
err = util.RuntimeOutsideValidate(g, t, util.ValidateLinuxResourcesDevices)
58+
}
3259
if err != nil {
3360
t.Fail(err.Error())
3461
}

0 commit comments

Comments
 (0)