Skip to content

Commit 8b6cb85

Browse files
author
Haiyan Meng
committed
Check CAP_LAST_CAP while setting privileged
Signed-off-by: Haiyan Meng <hmeng@redhat.com>
1 parent 2928edb commit 8b6cb85

6 files changed

Lines changed: 58 additions & 22 deletions

File tree

cmd/ocitools/generate.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ var generateCommand = cli.Command{
7878
}
7979
}
8080

81-
err := setupSpec(specgen, context)
81+
err := setupSpec(&specgen, context)
8282
if err != nil {
8383
return err
8484
}
@@ -96,7 +96,12 @@ var generateCommand = cli.Command{
9696
},
9797
}
9898

99-
func setupSpec(g generate.Generator, context *cli.Context) error {
99+
func setupSpec(gp *generate.Generator, context *cli.Context) error {
100+
if context.GlobalBool("host-specific") {
101+
gp.EnableHostSpecific()
102+
}
103+
104+
g := *gp
100105
spec := g.GetSpec()
101106

102107
if len(spec.Version) == 0 {

cmd/ocitools/main.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ func main() {
1818
Value: "error",
1919
Usage: "Log level (panic, fatal, error, warn, info, or debug)",
2020
},
21+
cli.BoolFlag{
22+
Name: "host-specific",
23+
Usage: "generate host-specific configs or do host-specific validations",
24+
},
2125
}
2226

2327
app.Commands = []cli.Command{

cmd/ocitools/validate.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ type configCheck func(rspec.Spec, string, bool) []string
2525

2626
var bundleValidateFlags = []cli.Flag{
2727
cli.StringFlag{Name: "path", Value: ".", Usage: "path to a bundle"},
28-
cli.BoolFlag{Name: "host-specific", Usage: "Check host specific configs."},
2928
}
3029

3130
var (
@@ -84,7 +83,7 @@ var bundleValidateCommand = cli.Command{
8483
return fmt.Errorf("The root path %q is not a directory.", rootfsPath)
8584
}
8685

87-
hostCheck := context.Bool("host-specific")
86+
hostCheck := context.GlobalBool("host-specific")
8887

8988
checks := []configCheck{
9089
checkMandatoryFields,

generate/generate.go

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ var (
2121

2222
// Generator represents a generator for a container spec.
2323
type Generator struct {
24-
spec *rspec.Spec
24+
spec *rspec.Spec
25+
hostSpecific bool
2526
}
2627

2728
// New creates a spec Generator with the default spec.
@@ -139,12 +140,16 @@ func New() Generator {
139140
Devices: []rspec.Device{},
140141
},
141142
}
142-
return Generator{&spec}
143+
return Generator{
144+
spec: &spec,
145+
}
143146
}
144147

145148
// NewFromSpec creates a spec Generator from a given spec.
146149
func NewFromSpec(spec *rspec.Spec) Generator {
147-
return Generator{spec}
150+
return Generator{
151+
spec: spec,
152+
}
148153
}
149154

150155
// NewFromFile loads the template specifed in a file into a spec Generator.
@@ -166,7 +171,19 @@ func NewFromTemplate(r io.Reader) (Generator, error) {
166171
if err := json.NewDecoder(r).Decode(&spec); err != nil {
167172
return Generator{}, err
168173
}
169-
return Generator{&spec}, nil
174+
return Generator{
175+
spec: &spec,
176+
}, nil
177+
}
178+
179+
// EnableHostSpecific enables g.hostSpecific.
180+
func (g *Generator) EnableHostSpecific() {
181+
g.hostSpecific = true
182+
}
183+
184+
// GetHostSpecific gets g.hostSpecific.
185+
func (g *Generator) GetHostSpecific() bool {
186+
return g.hostSpecific
170187
}
171188

172189
// SetSpec sets the spec in the Generator g.
@@ -909,6 +926,9 @@ func (g *Generator) SetupPrivileged(privileged bool) {
909926
// Add all capabilities in privileged mode.
910927
var finalCapList []string
911928
for _, cap := range capability.List() {
929+
if g.hostSpecific && cap > capability.CAP_LAST_CAP {
930+
continue
931+
}
912932
finalCapList = append(finalCapList, fmt.Sprintf("CAP_%s", strings.ToUpper(cap.String())))
913933
}
914934
g.spec.Process.Capabilities = finalCapList
@@ -918,12 +938,15 @@ func (g *Generator) SetupPrivileged(privileged bool) {
918938
}
919939
}
920940

921-
func checkCap(c string) error {
941+
func checkCap(c string, hostSpecific bool) error {
922942
isValid := false
923943
cp := strings.ToUpper(c)
924944

925945
for _, cap := range capability.List() {
926946
if cp == strings.ToUpper(cap.String()) {
947+
if hostSpecific && cap > capability.CAP_LAST_CAP {
948+
return fmt.Errorf("CAP_%s is not supported on the current host", cp)
949+
}
927950
isValid = true
928951
break
929952
}
@@ -942,7 +965,7 @@ func (g *Generator) ClearProcessCapabilities() {
942965

943966
// AddProcessCapability adds a process capability into g.spec.Process.Capabilities.
944967
func (g *Generator) AddProcessCapability(c string) error {
945-
if err := checkCap(c); err != nil {
968+
if err := checkCap(c, g.hostSpecific); err != nil {
946969
return err
947970
}
948971

@@ -960,7 +983,7 @@ func (g *Generator) AddProcessCapability(c string) error {
960983

961984
// DropProcessCapability drops a process capability from g.spec.Process.Capabilities.
962985
func (g *Generator) DropProcessCapability(c string) error {
963-
if err := checkCap(c); err != nil {
986+
if err := checkCap(c, g.hostSpecific); err != nil {
964987
return err
965988
}
966989

man/ocitools-validate.1.md

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,6 @@ Validate an OCI bundle
1818
**--path=PATH
1919
Path to bundle
2020

21-
**--host-specific**
22-
Check host specific configs.
23-
By default, validation only tests for compatibility with a hypothetical host.
24-
With this flag, validation will also run more specific tests to see whether
25-
the current host is capable of launching a container from the configuration.
26-
For example, validating a compliant Windows configuration on a Linux machine
27-
will pass without this flag ("there may be a Windows host capable of
28-
launching this container"), but will fail with it ("this host is not capable
29-
of launching this container").
30-
3121
# SEE ALSO
3222
**ocitools**(1)
3323

man/ocitools.1.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,26 @@ ocitools is a collection of tools for working with the [OCI runtime specificatio
1515

1616
# OPTIONS
1717
**--help**
18-
Print usage statement
18+
Print usage statement.
1919

2020
**-v**, **--version**
2121
Print version information.
2222

23+
**--log-level**
24+
Log level (panic, fatal, error, warn, info, or debug) (default: "error").
25+
26+
**--host-specific**
27+
Generate host-specific configs or do host-specific validations.
28+
29+
By default, generator generates configs without checking whether they are
30+
supported on the current host. With this flag, generator will first check
31+
whether each config is supported on the current host, and only add it into
32+
the config file if it passes the checking.
33+
34+
By default, validation only tests for compatibility with a hypothetical host.
35+
With this flag, validation will also run more specific tests to see whether
36+
the current host is capable of launching a container from the configuration.
37+
2338
# COMMANDS
2439
**validate**
2540
Validating OCI bundle

0 commit comments

Comments
 (0)