Skip to content

Commit 60c7e77

Browse files
Zhai Zhaoxuanzhouhao
Zhai Zhaoxuan
authored and
zhouhao
committed
generate: add --device-access-add and --device-access-remove option
These options allow user to configures the device whitelist. Signed-off-by: Zhai Zhaoxuan <[email protected]>
1 parent 9e0e42d commit 60c7e77

File tree

3 files changed

+150
-0
lines changed

3 files changed

+150
-0
lines changed

cmd/oci-runtime-tool/.generate.go.swp

56 KB
Binary file not shown.

cmd/oci-runtime-tool/generate.go

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,18 @@ import (
1818

1919
var generateFlags = []cli.Flag{
2020
cli.StringSliceFlag{Name: "args", Usage: "command to run in the container"},
21+
<<<<<<< 9e0e42dbf918070406a2a4a2e1476e7350ba9129
22+
=======
23+
cli.StringSliceFlag{Name: "bind", Usage: "bind mount directories src:dest[:options...]"},
24+
cli.StringSliceFlag{Name: "cap-add", Usage: "add Linux capabilities"},
25+
cli.StringSliceFlag{Name: "cap-drop", Usage: "drop Linux capabilities"},
26+
cli.StringFlag{Name: "cgroup", Usage: "cgroup namespace"},
27+
cli.StringFlag{Name: "cgroups-path", Usage: "specify the path to the cgroups"},
28+
cli.StringFlag{Name: "cwd", Value: "/", Usage: "current working directory for the process"},
29+
cli.StringSliceFlag{Name: "device-access-add", Usage: "add a device access rule"},
30+
cli.StringSliceFlag{Name: "device-access-remove", Usage: "remove a device access rule"},
31+
cli.BoolFlag{Name: "disable-oom-kill", Usage: "disable OOM Killer"},
32+
>>>>>>> generate: add --device-access-add and --device-access-remove option
2133
cli.StringSliceFlag{Name: "env", Usage: "add environment variable e.g. key=value"},
2234
cli.StringSliceFlag{Name: "env-file", Usage: "read in a file of environment variables"},
2335
cli.StringSliceFlag{Name: "hooks-poststart", Usage: "set command to run in poststart hooks"},
@@ -234,8 +246,35 @@ func setupSpec(g *generate.Generator, context *cli.Context) error {
234246
g.SetLinuxCgroupsPath(context.String("linux-cgroups-path"))
235247
}
236248

249+
<<<<<<< 9e0e42dbf918070406a2a4a2e1476e7350ba9129
237250
if context.IsSet("linux-masked-paths") {
238251
paths := context.StringSlice("linux-masked-paths")
252+
=======
253+
if context.IsSet("device-access-add") {
254+
devices := context.StringSlice("device-access-add")
255+
for _, device := range devices {
256+
dev, err := parseLinuxResourcesDeviceAccess(device, g)
257+
if err != nil {
258+
return err
259+
}
260+
g.AddLinuxResourcesDevice(dev.Allow, dev.Type, dev.Major, dev.Minor, dev.Access)
261+
}
262+
}
263+
264+
if context.IsSet("device-access-remove") {
265+
devices := context.StringSlice("device-access-remove")
266+
for _, device := range devices {
267+
dev, err := parseLinuxResourcesDeviceAccess(device, g)
268+
if err != nil {
269+
return err
270+
}
271+
g.RemoveLinuxResourcesDevice(dev.Allow, dev.Type, dev.Major, dev.Minor, dev.Access)
272+
}
273+
}
274+
275+
if context.IsSet("masked-paths") {
276+
paths := context.StringSlice("masked-paths")
277+
>>>>>>> generate: add --device-access-add and --device-access-remove option
239278
for _, path := range paths {
240279
g.AddLinuxMaskedPaths(path)
241280
}
@@ -811,6 +850,7 @@ func parseRlimit(rlimit string) (string, uint64, uint64, error) {
811850
return parts[0], uint64(hard), uint64(soft), nil
812851
}
813852

853+
<<<<<<< 9e0e42dbf918070406a2a4a2e1476e7350ba9129
814854
func parseNamespace(ns string) (string, string, error) {
815855
parts := strings.SplitN(ns, ":", 2)
816856
if len(parts) == 0 || parts[0] == "" {
@@ -904,6 +944,82 @@ func parseDevice(device string, g *generate.Generator) (rspec.LinuxDevice, error
904944
}
905945

906946
return dev, nil
947+
=======
948+
var cgroupDeviceType = map[string]bool{
949+
"a": true, // all
950+
"b": true, // block device
951+
"c": true, // character device
952+
}
953+
var cgroupDeviceAccess = map[string]bool{
954+
"r": true, //read
955+
"w": true, //write
956+
"m": true, //mknod
957+
}
958+
959+
// parseLinuxResourcesDeviceAccess parses the raw string passed with the --device-access-add flag
960+
func parseLinuxResourcesDeviceAccess(device string, g *generate.Generator) (rspec.DeviceCgroup, error) {
961+
var allow bool
962+
var devType, access *string
963+
var major, minor *int64
964+
965+
argsParts := strings.Split(device, ",")
966+
967+
switch argsParts[0] {
968+
case "allow":
969+
allow = true
970+
case "deny":
971+
allow = false
972+
default:
973+
return rspec.DeviceCgroup{},
974+
fmt.Errorf("Only 'allow' and 'deny' are allowed in the first field of device-access-add: %s", device)
975+
}
976+
977+
for _, s := range argsParts[1:] {
978+
s = strings.TrimSpace(s)
979+
if s == "" {
980+
continue
981+
}
982+
parts := strings.SplitN(s, "=", 2)
983+
if len(parts) != 2 {
984+
return rspec.DeviceCgroup{}, fmt.Errorf("Incomplete device-access-add arguments: %s", s)
985+
}
986+
name, value := parts[0], parts[1]
987+
988+
switch name {
989+
case "type":
990+
if !cgroupDeviceType[value] {
991+
return rspec.DeviceCgroup{}, fmt.Errorf("Invalid device type in device-access-add: %s", value)
992+
}
993+
devType = &value
994+
case "major":
995+
i, err := strconv.ParseInt(value, 10, 64)
996+
if err != nil {
997+
return rspec.DeviceCgroup{}, err
998+
}
999+
major = &i
1000+
case "minor":
1001+
i, err := strconv.ParseInt(value, 10, 64)
1002+
if err != nil {
1003+
return rspec.DeviceCgroup{}, err
1004+
}
1005+
minor = &i
1006+
case "access":
1007+
for _, c := range strings.Split(value, "") {
1008+
if !cgroupDeviceAccess[c] {
1009+
return rspec.DeviceCgroup{}, fmt.Errorf("Invalid device access in device-access-add: %s", c)
1010+
}
1011+
}
1012+
access = &value
1013+
}
1014+
}
1015+
return rspec.DeviceCgroup{
1016+
Allow: allow,
1017+
Type: devType,
1018+
Major: major,
1019+
Minor: minor,
1020+
Access: access,
1021+
}, nil
1022+
>>>>>>> generate: add --device-access-add and --device-access-remove option
9071023
}
9081024

9091025
func addSeccomp(context *cli.Context, g *generate.Generator) error {

generate/generate.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1135,6 +1135,7 @@ func (g *Generator) RemoveLinuxNamespace(ns string) error {
11351135
return nil
11361136
}
11371137

1138+
<<<<<<< 9e0e42dbf918070406a2a4a2e1476e7350ba9129
11381139
// AddDevice - add a device into g.spec.Linux.Devices
11391140
func (g *Generator) AddDevice(device rspec.LinuxDevice) {
11401141
g.initSpecLinux()
@@ -1174,6 +1175,39 @@ func (g *Generator) ClearLinuxDevices() {
11741175
}
11751176

11761177
g.spec.Linux.Devices = []rspec.LinuxDevice{}
1178+
=======
1179+
// AddLinuxResourcesDevice - add a device into g.spec.Linux.Resources.Devices
1180+
func (g *Generator) AddLinuxResourcesDevice(allow bool, devType *string, major, minor *int64, access *string) {
1181+
g.initSpecLinuxResources()
1182+
1183+
device := rspec.DeviceCgroup{
1184+
Allow: allow,
1185+
Type: devType,
1186+
Access: access,
1187+
Major: major,
1188+
Minor: minor,
1189+
}
1190+
g.spec.Linux.Resources.Devices = append(g.spec.Linux.Resources.Devices, device)
1191+
}
1192+
1193+
// RemoveLinuxResourcesDevice - remove a device from g.spec.Linux.Resources.Devices
1194+
func (g *Generator) RemoveLinuxResourcesDevice(allow bool, devType *string, major, minor *int64, access *string) {
1195+
if g.spec == nil || g.spec.Linux == nil || g.spec.Linux.Resources == nil {
1196+
return
1197+
}
1198+
for i, device := range g.spec.Linux.Resources.Devices {
1199+
if device.Allow == allow &&
1200+
(devType == device.Type || (devType != nil && device.Type != nil && *devType == *device.Type)) &&
1201+
(access == device.Access || (access != nil && device.Access != nil && *access == *device.Access)) &&
1202+
(major == device.Major || (major != nil && device.Major != nil && *major == *device.Major)) &&
1203+
(minor == device.Minor || (minor != nil && device.Minor != nil && *minor == *device.Minor)) {
1204+
1205+
g.spec.Linux.Resources.Devices = append(g.spec.Linux.Resources.Devices[:i], g.spec.Linux.Resources.Devices[i+1:]...)
1206+
return
1207+
}
1208+
}
1209+
return
1210+
>>>>>>> generate: add --device-access-add and --device-access-remove option
11771211
}
11781212

11791213
// strPtr returns the pointer pointing to the string s.

0 commit comments

Comments
 (0)