Skip to content

Commit 192a8eb

Browse files
author
Zhou Hao
committed
generate: add process-cap-add option
Add process-cap-add option to add Linux capabilities to all 5 capability sets. ``` oci-runtime-tool generate --process-cap-add CAP_FOWNER,CAP_FSETID ``` Signed-off-by: Zhou Hao <[email protected]>
1 parent f81d4a8 commit 192a8eb

File tree

4 files changed

+80
-0
lines changed

4 files changed

+80
-0
lines changed

cmd/oci-runtime-tool/generate.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ var generateFlags = []cli.Flag{
9494
cli.StringFlag{Name: "os", Value: runtime.GOOS, Usage: "operating system the container is created for"},
9595
cli.StringFlag{Name: "output", Usage: "output file (defaults to stdout)"},
9696
cli.BoolFlag{Name: "privileged", Usage: "enable privileged container settings"},
97+
cli.StringFlag{Name: "process-cap-add", Usage: "add Linux capabilities to all 5 capability sets"},
9798
cli.StringFlag{Name: "process-cap-add-ambient", Usage: "add Linux ambient capabilities"},
9899
cli.StringFlag{Name: "process-cap-add-bounding", Usage: "add Linux bounding capabilities"},
99100
cli.StringFlag{Name: "process-cap-add-effective", Usage: "add Linux effective capabilities"},
@@ -339,6 +340,16 @@ func setupSpec(g *generate.Generator, context *cli.Context) error {
339340
g.ClearProcessCapabilities()
340341
}
341342

343+
if context.IsSet("process-cap-add") {
344+
addCaps := context.String("process-cap-add")
345+
parts := strings.Split(addCaps, ",")
346+
for _, part := range parts {
347+
if err := g.AddProcessCapability(part); err != nil {
348+
return err
349+
}
350+
}
351+
}
352+
342353
if context.IsSet("process-cap-add-ambient") {
343354
addCaps := context.String("process-cap-add-ambient")
344355
parts := strings.Split(addCaps, ",")

completions/bash/oci-runtime-tool

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,7 @@ _oci-runtime-tool_generate() {
371371
--mounts-remove
372372
--os
373373
--output
374+
--process-cap-add
374375
--process-cap-add-ambient
375376
--process-cap-add-bounding
376377
--process-cap-add-effective

generate/generate.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1074,6 +1074,69 @@ func (g *Generator) ClearProcessCapabilities() {
10741074
g.Config.Process.Capabilities.Ambient = []string{}
10751075
}
10761076

1077+
// AddProcessCapability adds a process capability into all 5 capability sets.
1078+
func (g *Generator) AddProcessCapability(c string) error {
1079+
cp := strings.ToUpper(c)
1080+
if err := validate.CapValid(cp, g.HostSpecific); err != nil {
1081+
return err
1082+
}
1083+
1084+
g.initConfigProcessCapabilities()
1085+
1086+
var foundAmbient, foundBounding, foundEffective, foundInheritable, foundPermitted bool
1087+
for _, cap := range g.Config.Process.Capabilities.Ambient {
1088+
if strings.ToUpper(cap) == cp {
1089+
foundAmbient = true
1090+
break
1091+
}
1092+
}
1093+
if !foundAmbient {
1094+
g.Config.Process.Capabilities.Ambient = append(g.Config.Process.Capabilities.Ambient, cp)
1095+
}
1096+
1097+
for _, cap := range g.Config.Process.Capabilities.Bounding {
1098+
if strings.ToUpper(cap) == cp {
1099+
foundBounding = true
1100+
break
1101+
}
1102+
}
1103+
if !foundBounding {
1104+
g.Config.Process.Capabilities.Bounding = append(g.Config.Process.Capabilities.Bounding, cp)
1105+
}
1106+
1107+
for _, cap := range g.Config.Process.Capabilities.Effective {
1108+
if strings.ToUpper(cap) == cp {
1109+
foundEffective = true
1110+
break
1111+
}
1112+
}
1113+
if !foundEffective {
1114+
g.Config.Process.Capabilities.Effective = append(g.Config.Process.Capabilities.Effective, cp)
1115+
}
1116+
1117+
for _, cap := range g.Config.Process.Capabilities.Inheritable {
1118+
if strings.ToUpper(cap) == cp {
1119+
foundInheritable = true
1120+
break
1121+
}
1122+
}
1123+
if !foundInheritable {
1124+
g.Config.Process.Capabilities.Inheritable = append(g.Config.Process.Capabilities.Inheritable, cp)
1125+
}
1126+
1127+
for _, cap := range g.Config.Process.Capabilities.Permitted {
1128+
if strings.ToUpper(cap) == cp {
1129+
foundPermitted = true
1130+
break
1131+
}
1132+
}
1133+
if !foundPermitted {
1134+
g.Config.Process.Capabilities.Permitted = append(g.Config.Process.Capabilities.Permitted, cp)
1135+
}
1136+
1137+
return nil
1138+
}
1139+
10771140
// AddProcessCapabilityAmbient adds a process capability into g.Config.Process.Capabilities.Ambient.
10781141
func (g *Generator) AddProcessCapabilityAmbient(c string) error {
10791142
cp := strings.ToUpper(c)

man/oci-runtime-tool-generate.1.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,11 @@ read the configuration from `config.json`.
372372

373373
When the operator executes **oci-runtime-tool generate --privileged**, OCI will enable access to all devices on the host as well as disable some of the confinement mechanisms like AppArmor, SELinux, and seccomp from blocking access to privileged processes. This gives the container processes nearly all the same access to the host as processes generating outside of a container on the host.
374374

375+
**--process-cap-add**=[]
376+
Add Linux capabilities to all 5 capability sets.
377+
You can use this command to add multiple capabilities. Each value should be used ',' separated.
378+
e.g. --process-cap-add CAP_FOWNER,CAP_FSETID
379+
375380
**--process-cap-add-ambient**=[]
376381
Add Linux ambient capabilities.
377382
You can use this command to add multiple capabilities. Each value should be used ',' separated.

0 commit comments

Comments
 (0)