Skip to content

Commit 8b59b76

Browse files
authored
Merge pull request opencontainers#3182 from cyphar/revert-3159
Revert "libct/devices: change devices.Type to be a string"
2 parents b4b7972 + 09b8081 commit 8b59b76

File tree

6 files changed

+44
-29
lines changed

6 files changed

+44
-29
lines changed

libcontainer/cgroups/devices/devices_emulator.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -89,22 +89,24 @@ func parseLine(line string) (*deviceRule, error) {
8989

9090
var (
9191
rule deviceRule
92-
node = devices.Type(fields[0])
92+
node = fields[0]
9393
major = fields[1]
9494
minor = fields[2]
9595
perms = fields[3]
9696
)
9797

9898
// Parse the node type.
9999
switch node {
100-
case devices.WildcardDevice:
100+
case "a":
101101
// Super-special case -- "a" always means every device with every
102102
// access mode. In fact, for devices.list this actually indicates that
103103
// the cgroup is in black-list mode.
104104
// TODO: Double-check that the entire file is "a *:* rwm".
105105
return nil, nil
106-
case devices.BlockDevice, devices.CharDevice:
107-
rule.meta.node = node
106+
case "b":
107+
rule.meta.node = devices.BlockDevice
108+
case "c":
109+
rule.meta.node = devices.CharDevice
108110
default:
109111
return nil, fmt.Errorf("unknown device type %q", node)
110112
}
@@ -136,7 +138,6 @@ func parseLine(line string) (*deviceRule, error) {
136138
if !rule.perms.IsValid() || rule.perms.IsEmpty() {
137139
return nil, fmt.Errorf("parse access mode: contained unknown modes or is empty: %q", perms)
138140
}
139-
140141
return &rule, nil
141142
}
142143

@@ -318,10 +319,10 @@ func (source *Emulator) Transition(target *Emulator) ([]*devices.Rule, error) {
318319
// black-list we also have to include a disruptive rule.
319320
if source.IsBlacklist() || source.defaultAllow != target.defaultAllow {
320321
transitionRules = append(transitionRules, &devices.Rule{
321-
Type: devices.WildcardDevice,
322+
Type: 'a',
322323
Major: -1,
323324
Minor: -1,
324-
Permissions: "rwm",
325+
Permissions: devices.Permissions("rwm"),
325326
Allow: target.defaultAllow,
326327
})
327328
// 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", rule.Type)
122+
return fmt.Errorf("invalid type %q", string(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: devices.WildcardDevice,
149+
Type: 'a',
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: devices.WildcardDevice,
176+
Type: 'a',
177177
Major: -1,
178178
Minor: -1,
179179
Permissions: "rwm",
180180
Allow: true,
181181
},
182182
{
183-
Type: devices.BlockDevice,
183+
Type: 'b',
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: devices.BlockDevice,
216+
Type: 'b',
217217
Major: 8,
218218
Minor: 1,
219219
Permissions: "rwm",
220220
Allow: false,
221221
},
222222
{
223-
Type: devices.WildcardDevice,
223+
Type: 'a',
224224
Major: -1,
225225
Minor: -1,
226226
Permissions: "rwm",
227227
Allow: true,
228228
},
229229
{
230-
Type: devices.BlockDevice,
230+
Type: 'b',
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 string
103+
type Type rune
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("%s %s:%s %s", d.Type, major, minor, d.Permissions)
169+
return fmt.Sprintf("%c %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("%s is not a valid device type for device %s", node.Type, node.Path)
713+
return fmt.Errorf("%c 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: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -479,15 +479,12 @@ func CreateCgroupConfig(opts *CreateOpts, defaultDevs []*devices.Device) (*confi
479479
if r != nil {
480480
for i, d := range spec.Linux.Resources.Devices {
481481
var (
482-
dt = devices.WildcardDevice
482+
t = "a"
483483
major = int64(-1)
484484
minor = int64(-1)
485485
)
486486
if 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-
}
487+
t = d.Type
491488
}
492489
if d.Major != nil {
493490
major = *d.Major
@@ -498,6 +495,10 @@ func CreateCgroupConfig(opts *CreateOpts, defaultDevs []*devices.Device) (*confi
498495
if d.Access == "" {
499496
return nil, fmt.Errorf("device access at %d field cannot be empty", i)
500497
}
498+
dt, err := stringToCgroupDeviceRune(t)
499+
if err != nil {
500+
return nil, err
501+
}
501502
c.Resources.Devices = append(c.Resources.Devices, &devices.Rule{
502503
Type: dt,
503504
Major: major,
@@ -634,7 +635,20 @@ func CreateCgroupConfig(opts *CreateOpts, defaultDevs []*devices.Device) (*confi
634635
return c, nil
635636
}
636637

637-
func stringToDeviceType(s string) (devices.Type, error) {
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) {
638652
switch s {
639653
case "p":
640654
return devices.FifoDevice, nil
@@ -643,7 +657,7 @@ func stringToDeviceType(s string) (devices.Type, error) {
643657
case "b":
644658
return devices.BlockDevice, nil
645659
default:
646-
return "", fmt.Errorf("invalid device type %q", s)
660+
return 0, fmt.Errorf("invalid device type %q", s)
647661
}
648662
}
649663

@@ -679,7 +693,7 @@ next:
679693
if d.GID != nil {
680694
gid = *d.GID
681695
}
682-
dt, err := stringToDeviceType(d.Type)
696+
dt, err := stringToDeviceRune(d.Type)
683697
if err != nil {
684698
return nil, err
685699
}

0 commit comments

Comments
 (0)