Skip to content

Commit 119b0a1

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. The open does not block, and only character and block devices are compared, since the device controller governs nothing else; a FIFO in linux.devices would otherwise hold the test up waiting for a writer. 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 a828440 commit 119b0a1

3 files changed

Lines changed: 236 additions & 6 deletions

File tree

‎cmd/runtimetest/main.go‎

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -663,6 +663,108 @@ 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+
// deviceCgroupType maps a linux.devices type onto the type the device
705+
// controller knows, which covers character and block devices only.
706+
func deviceCgroupType(devType string) (string, bool) {
707+
switch devType {
708+
case "c", "u":
709+
return "c", true
710+
case "b":
711+
return "b", true
712+
}
713+
714+
return "", false
715+
}
716+
717+
// deviceDeniedByCgroup opens a device node to find out whether the device
718+
// controller refuses it. EPERM comes back before the driver behind the
719+
// node is consulted, so a node with nothing behind it answers the question
720+
// just as well as one that could be read. The open does not block, so a
721+
// driver that waits for a peer cannot hold the test up.
722+
func deviceDeniedByCgroup(path string) bool {
723+
f, err := os.OpenFile(path, os.O_RDONLY|syscall.O_NONBLOCK, 0)
724+
if err == nil {
725+
f.Close()
726+
return false
727+
}
728+
729+
return errors.Is(err, syscall.EPERM)
730+
}
731+
732+
func (c *complianceTester) validateLinuxResourcesDevices(spec *rspec.Spec) error {
733+
if spec.Linux == nil || spec.Linux.Resources == nil || len(spec.Linux.Resources.Devices) == 0 {
734+
c.harness.Skip(1, "linux.resources.devices is not set")
735+
return nil
736+
}
737+
738+
for _, device := range spec.Linux.Devices {
739+
// Runtimes add rules of their own for the devices they are
740+
// required to supply, so those say nothing about the list that
741+
// was configured.
742+
if isDefaultDevicePath(device.Path) {
743+
continue
744+
}
745+
devType, ok := deviceCgroupType(device.Type)
746+
if !ok {
747+
continue
748+
}
749+
expected := !deviceReadAllowed(spec.Linux.Resources.Devices, devType, device.Major, device.Minor)
750+
denied := deviceDeniedByCgroup(device.Path)
751+
rfcError, err := c.Ok(denied == expected, specerror.DevicesApplyInOrder, spec.Version,
752+
fmt.Sprintf("access to %s follows linux.resources.devices", device.Path))
753+
if err != nil {
754+
return err
755+
}
756+
_ = c.harness.YAML(map[string]string{
757+
"level": rfcError.Level.String(),
758+
"reference": rfcError.Reference,
759+
"device": fmt.Sprintf("%s %d:%d", device.Type, device.Major, device.Minor),
760+
"expected": fmt.Sprintf("denied: %t", expected),
761+
"actual": fmt.Sprintf("denied: %t", denied),
762+
})
763+
}
764+
765+
return nil
766+
}
767+
666768
func (c *complianceTester) validateDevice(device *rspec.LinuxDevice, condition specerror.Code, version string, description string) (err error) {
667769
var exists bool
668770
fi, err := os.Stat(device.Path)
@@ -1297,6 +1399,7 @@ func run(context *cli.Context) error {
12971399
c.validateDefaultFS,
12981400
c.validateDefaultDevices,
12991401
c.validateLinuxDevices,
1402+
c.validateLinuxResourcesDevices,
13001403
c.validateLinuxProcess,
13011404
c.validateMaskedPaths,
13021405
c.validateOOMScoreAdj,

‎cmd/runtimetest/main_test.go‎

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
package main
2+
3+
import (
4+
"path/filepath"
5+
"syscall"
6+
"testing"
7+
8+
rspec "github.com/opencontainers/runtime-spec/specs-go"
9+
)
10+
11+
func devicePtr(v int64) *int64 {
12+
return &v
13+
}
14+
15+
func TestDeviceReadAllowed(t *testing.T) {
16+
// The list linux_cgroups_devices builds: the generator's leading
17+
// deny-all followed by the three rules the test adds.
18+
rules := []rspec.LinuxDeviceCgroup{
19+
{Allow: false, Access: "rwm"},
20+
{Allow: true, Type: "c", Major: devicePtr(10), Minor: devicePtr(229), Access: "rwm"},
21+
{Allow: true, Type: "b", Major: devicePtr(8), Minor: devicePtr(20), Access: "rw"},
22+
{Allow: true, Type: "b", Major: devicePtr(10), Minor: devicePtr(200), Access: "r"},
23+
}
24+
25+
for _, test := range []struct {
26+
description string
27+
devType string
28+
rules []rspec.LinuxDeviceCgroup
29+
major int64
30+
minor int64
31+
expected bool
32+
}{
33+
{description: "explicitly allowed", rules: rules, devType: "c", major: 10, minor: 229, expected: true},
34+
{description: "a minor the allow does not cover", rules: rules, devType: "c", major: 10, minor: 230, expected: false},
35+
{description: "right numbers, wrong type", rules: rules, devType: "b", major: 10, minor: 229, expected: false},
36+
{description: "allowed block device", rules: rules, devType: "b", major: 8, minor: 20, expected: true},
37+
{description: "read-only allow still permits reading", rules: rules, devType: "b", major: 10, minor: 200, expected: true},
38+
{description: "covered only by the deny-all", rules: rules, devType: "c", major: 1, minor: 3, expected: false},
39+
{description: "no rules at all", rules: nil, devType: "c", major: 1, minor: 3, expected: true},
40+
{
41+
description: "a deny that does not mention read",
42+
rules: []rspec.LinuxDeviceCgroup{{Allow: false, Access: "w"}},
43+
devType: "c", major: 1, minor: 3, expected: true,
44+
},
45+
{
46+
description: "a later rule overrides an earlier one",
47+
rules: []rspec.LinuxDeviceCgroup{
48+
{Allow: true, Access: "rwm"},
49+
{Allow: false, Type: "c", Major: devicePtr(10), Minor: devicePtr(229), Access: "rwm"},
50+
},
51+
devType: "c", major: 10, minor: 229, expected: false,
52+
},
53+
} {
54+
got := deviceReadAllowed(test.rules, test.devType, test.major, test.minor)
55+
if got != test.expected {
56+
t.Errorf("%s: %s %d:%d allowed == %t, expected %t",
57+
test.description, test.devType, test.major, test.minor, got, test.expected)
58+
}
59+
}
60+
}
61+
62+
func TestIsDefaultDevicePath(t *testing.T) {
63+
if !isDefaultDevicePath("/dev/null") {
64+
t.Error("/dev/null is a default device")
65+
}
66+
if isDefaultDevicePath("/dev/denied") {
67+
t.Error("/dev/denied is not a default device")
68+
}
69+
}
70+
71+
func TestDeviceCgroupType(t *testing.T) {
72+
for _, test := range []struct {
73+
devType string
74+
expected string
75+
governed bool
76+
}{
77+
{devType: "c", expected: "c", governed: true},
78+
{devType: "u", expected: "c", governed: true},
79+
{devType: "b", expected: "b", governed: true},
80+
{devType: "p", governed: false},
81+
} {
82+
got, governed := deviceCgroupType(test.devType)
83+
if got != test.expected || governed != test.governed {
84+
t.Errorf("%s: type == %q governed == %t, expected %q %t",
85+
test.devType, got, governed, test.expected, test.governed)
86+
}
87+
}
88+
}
89+
90+
func TestDeviceDeniedByCgroupFifo(t *testing.T) {
91+
fifo := filepath.Join(t.TempDir(), "fifo")
92+
if err := syscall.Mkfifo(fifo, 0o600); err != nil {
93+
t.Fatal(err)
94+
}
95+
// Opening a FIFO without a writer blocks unless the open is
96+
// non-blocking.
97+
if deviceDeniedByCgroup(fifo) {
98+
t.Error("a FIFO outside any device rule was reported as denied")
99+
}
100+
}

‎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)