Skip to content

Commit 34df203

Browse files
authored
Merge pull request opencontainers#3159 from thaJeztah/norunes
libct/devices: change devices.Type to be a string
2 parents 3023e6c + 814f3ae commit 34df203

File tree

6 files changed

+29
-44
lines changed

6 files changed

+29
-44
lines changed

libcontainer/cgroups/devices/devices_emulator.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -88,24 +88,22 @@ func parseLine(line string) (*deviceRule, error) {
8888
}
8989
var (
9090
rule deviceRule
91-
node = matches[1]
91+
node = devices.Type(matches[1])
9292
major = matches[2]
9393
minor = matches[3]
9494
perms = matches[4]
9595
)
9696

9797
// Parse the node type.
9898
switch node {
99-
case "a":
99+
case devices.WildcardDevice:
100100
// Super-special case -- "a" always means every device with every
101101
// access mode. In fact, for devices.list this actually indicates that
102102
// the cgroup is in black-list mode.
103103
// TODO: Double-check that the entire file is "a *:* rwm".
104104
return nil, nil
105-
case "b":
106-
rule.meta.node = devices.BlockDevice
107-
case "c":
108-
rule.meta.node = devices.CharDevice
105+
case devices.BlockDevice, devices.CharDevice:
106+
rule.meta.node = node
109107
default:
110108
// Should never happen!
111109
return nil, fmt.Errorf("unknown device type %q", node)
@@ -139,6 +137,7 @@ func parseLine(line string) (*deviceRule, error) {
139137
// Should never happen!
140138
return nil, fmt.Errorf("parse access mode: contained unknown modes or is empty: %q", perms)
141139
}
140+
142141
return &rule, nil
143142
}
144143

@@ -320,10 +319,10 @@ func (source *Emulator) Transition(target *Emulator) ([]*devices.Rule, error) {
320319
// black-list we also have to include a disruptive rule.
321320
if source.IsBlacklist() || source.defaultAllow != target.defaultAllow {
322321
transitionRules = append(transitionRules, &devices.Rule{
323-
Type: 'a',
322+
Type: devices.WildcardDevice,
324323
Major: -1,
325324
Minor: -1,
326-
Permissions: devices.Permissions("rwm"),
325+
Permissions: "rwm",
327326
Allow: target.defaultAllow,
328327
})
329328
// The old rules are only relevant if we aren't starting out with a

libcontainer/cgroups/ebpf/devicefilter/devicefilter.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ func (p *program) appendRule(rule *devices.Rule) error {
119119
bpfType = int32(unix.BPF_DEVCG_DEV_BLOCK)
120120
default:
121121
// We do not permit 'a', nor any other types we don't know about.
122-
return fmt.Errorf("invalid type %q", string(rule.Type))
122+
return fmt.Errorf("invalid type %q", rule.Type)
123123
}
124124
if rule.Major > math.MaxUint32 {
125125
return fmt.Errorf("invalid major %d", rule.Major)

libcontainer/cgroups/ebpf/devicefilter/devicefilter_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ block-11:
146146
func TestDeviceFilter_Privileged(t *testing.T) {
147147
devices := []*devices.Rule{
148148
{
149-
Type: 'a',
149+
Type: devices.WildcardDevice,
150150
Major: -1,
151151
Minor: -1,
152152
Permissions: "rwm",
@@ -173,14 +173,14 @@ block-0:
173173
func TestDeviceFilter_PrivilegedExceptSingleDevice(t *testing.T) {
174174
devices := []*devices.Rule{
175175
{
176-
Type: 'a',
176+
Type: devices.WildcardDevice,
177177
Major: -1,
178178
Minor: -1,
179179
Permissions: "rwm",
180180
Allow: true,
181181
},
182182
{
183-
Type: 'b',
183+
Type: devices.BlockDevice,
184184
Major: 8,
185185
Minor: 0,
186186
Permissions: "rwm",
@@ -213,21 +213,21 @@ block-1:
213213
func TestDeviceFilter_Weird(t *testing.T) {
214214
devices := []*devices.Rule{
215215
{
216-
Type: 'b',
216+
Type: devices.BlockDevice,
217217
Major: 8,
218218
Minor: 1,
219219
Permissions: "rwm",
220220
Allow: false,
221221
},
222222
{
223-
Type: 'a',
223+
Type: devices.WildcardDevice,
224224
Major: -1,
225225
Minor: -1,
226226
Permissions: "rwm",
227227
Allow: true,
228228
},
229229
{
230-
Type: 'b',
230+
Type: devices.BlockDevice,
231231
Major: 8,
232232
Minor: 2,
233233
Permissions: "rwm",

libcontainer/devices/device.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -100,13 +100,13 @@ func (p Permissions) IsValid() bool {
100100
return p == fromSet(p.toSet())
101101
}
102102

103-
type Type rune
103+
type Type string
104104

105105
const (
106-
WildcardDevice Type = 'a'
107-
BlockDevice Type = 'b'
108-
CharDevice Type = 'c' // or 'u'
109-
FifoDevice Type = 'p'
106+
WildcardDevice Type = "a"
107+
BlockDevice Type = "b"
108+
CharDevice Type = "c" // or 'u'
109+
FifoDevice Type = "p"
110110
)
111111

112112
func (t Type) IsValid() bool {
@@ -166,7 +166,7 @@ func (d *Rule) CgroupString() string {
166166
if d.Minor == Wildcard {
167167
minor = "*"
168168
}
169-
return fmt.Sprintf("%c %s:%s %s", d.Type, major, minor, d.Permissions)
169+
return fmt.Sprintf("%s %s:%s %s", d.Type, major, minor, d.Permissions)
170170
}
171171

172172
func (d *Rule) Mkdev() (uint64, error) {

libcontainer/rootfs_linux.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -710,7 +710,7 @@ func mknodDevice(dest string, node *devices.Device) error {
710710
case devices.FifoDevice:
711711
fileMode |= unix.S_IFIFO
712712
default:
713-
return fmt.Errorf("%c is not a valid device type for device %s", node.Type, node.Path)
713+
return fmt.Errorf("%s is not a valid device type for device %s", node.Type, node.Path)
714714
}
715715
dev, err := node.Mkdev()
716716
if err != nil {

libcontainer/specconv/spec_linux.go

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -479,12 +479,15 @@ func CreateCgroupConfig(opts *CreateOpts, defaultDevs []*devices.Device) (*confi
479479
if r != nil {
480480
for i, d := range spec.Linux.Resources.Devices {
481481
var (
482-
t = "a"
482+
dt = devices.WildcardDevice
483483
major = int64(-1)
484484
minor = int64(-1)
485485
)
486486
if d.Type != "" {
487-
t = d.Type
487+
dt = devices.Type(d.Type)
488+
if !dt.CanCgroup() {
489+
return nil, fmt.Errorf("invalid cgroup device type %q", d.Type)
490+
}
488491
}
489492
if d.Major != nil {
490493
major = *d.Major
@@ -495,10 +498,6 @@ func CreateCgroupConfig(opts *CreateOpts, defaultDevs []*devices.Device) (*confi
495498
if d.Access == "" {
496499
return nil, fmt.Errorf("device access at %d field cannot be empty", i)
497500
}
498-
dt, err := stringToCgroupDeviceRune(t)
499-
if err != nil {
500-
return nil, err
501-
}
502501
c.Resources.Devices = append(c.Resources.Devices, &devices.Rule{
503502
Type: dt,
504503
Major: major,
@@ -635,20 +634,7 @@ func CreateCgroupConfig(opts *CreateOpts, defaultDevs []*devices.Device) (*confi
635634
return c, nil
636635
}
637636

638-
func stringToCgroupDeviceRune(s string) (devices.Type, error) {
639-
switch s {
640-
case "a":
641-
return devices.WildcardDevice, nil
642-
case "b":
643-
return devices.BlockDevice, nil
644-
case "c":
645-
return devices.CharDevice, nil
646-
default:
647-
return 0, fmt.Errorf("invalid cgroup device type %q", s)
648-
}
649-
}
650-
651-
func stringToDeviceRune(s string) (devices.Type, error) {
637+
func stringToDeviceType(s string) (devices.Type, error) {
652638
switch s {
653639
case "p":
654640
return devices.FifoDevice, nil
@@ -657,7 +643,7 @@ func stringToDeviceRune(s string) (devices.Type, error) {
657643
case "b":
658644
return devices.BlockDevice, nil
659645
default:
660-
return 0, fmt.Errorf("invalid device type %q", s)
646+
return "", fmt.Errorf("invalid device type %q", s)
661647
}
662648
}
663649

@@ -693,7 +679,7 @@ next:
693679
if d.GID != nil {
694680
gid = *d.GID
695681
}
696-
dt, err := stringToDeviceRune(d.Type)
682+
dt, err := stringToDeviceType(d.Type)
697683
if err != nil {
698684
return nil, err
699685
}

0 commit comments

Comments
 (0)