Skip to content

Commit 5c1e0ab

Browse files
committed
generate: remove validate dependency
The two modules {validate,generate} should be mutually exclusive and should not depend on each other. In addition, it is not the job of generate to carry out validation of any arguments provided (especially system-specific arguments). lastCap needs two copies because of the RHEL6 hack, which is a shame but does not justify the import dependency (because that dependency pulls in logrus and a few other libraries for no good reason). Fixes: 1a899a6 ("validate: optimize capabilites check") Signed-off-by: Aleksa Sarai <asarai@suse.de>
1 parent f2ae88b commit 5c1e0ab

2 files changed

Lines changed: 22 additions & 18 deletions

File tree

generate/generate.go

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111

1212
rspec "github.com/opencontainers/runtime-spec/specs-go"
1313
"github.com/opencontainers/runtime-tools/generate/seccomp"
14-
"github.com/opencontainers/runtime-tools/validate"
1514
"github.com/syndtr/gocapability/capability"
1615
)
1716

@@ -819,12 +818,24 @@ func (g *Generator) AddBindMount(source, dest string, options []string) {
819818
g.spec.Mounts = append(g.spec.Mounts, mnt)
820819
}
821820

821+
// lastCap return last cap of system, and is required to hack around RHEL6.
822+
// This is an exact copy of "validate/validate.go".lastCap.
823+
func lastCap() capability.Cap {
824+
last := capability.CAP_LAST_CAP
825+
// hack for RHEL6 which has no /proc/sys/kernel/cap_last_cap
826+
if last == capability.Cap(63) {
827+
last = capability.CAP_BLOCK_SUSPEND
828+
}
829+
830+
return last
831+
}
832+
822833
// SetupPrivileged sets up the privilege-related fields inside g.spec.
823834
func (g *Generator) SetupPrivileged(privileged bool) {
824835
if privileged { // Add all capabilities in privileged mode.
825836
var finalCapList []string
826837
for _, cap := range capability.List() {
827-
if g.HostSpecific && cap > validate.LastCap() {
838+
if g.HostSpecific && cap > lastCap() {
828839
continue
829840
}
830841
finalCapList = append(finalCapList, fmt.Sprintf("CAP_%s", strings.ToUpper(cap.String())))
@@ -855,12 +866,8 @@ func (g *Generator) ClearProcessCapabilities() {
855866

856867
// AddProcessCapability adds a process capability into g.spec.Process.Capabilities.
857868
func (g *Generator) AddProcessCapability(c string) error {
858-
cp := strings.ToUpper(c)
859-
if err := validate.CapValid(cp, g.HostSpecific); err != nil {
860-
return err
861-
}
862-
863869
g.initSpec()
870+
cp := strings.ToUpper(c)
864871

865872
for _, cap := range g.spec.Process.Capabilities.Bounding {
866873
if strings.ToUpper(cap) == cp {
@@ -902,12 +909,8 @@ func (g *Generator) AddProcessCapability(c string) error {
902909

903910
// DropProcessCapability drops a process capability from g.spec.Process.Capabilities.
904911
func (g *Generator) DropProcessCapability(c string) error {
905-
cp := strings.ToUpper(c)
906-
if err := validate.CapValid(cp, g.HostSpecific); err != nil {
907-
return err
908-
}
909-
910912
g.initSpec()
913+
cp := strings.ToUpper(c)
911914

912915
for i, cap := range g.spec.Process.Capabilities.Bounding {
913916
if strings.ToUpper(cap) == cp {

validate/validate.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ func (v *Validator) CheckProcess() (msgs []string) {
292292
}
293293

294294
for _, capability := range caps {
295-
if err := CapValid(capability, v.HostSpecific); err != nil {
295+
if err := capValid(capability, v.HostSpecific); err != nil {
296296
msgs = append(msgs, fmt.Sprintf("capability %q is not valid, man capabilities(7)", capability))
297297
}
298298
}
@@ -589,16 +589,16 @@ func (v *Validator) CheckSeccomp() (msgs []string) {
589589
return
590590
}
591591

592-
// CapValid checks whether a capability is valid
593-
func CapValid(c string, hostSpecific bool) error {
592+
// capValid checks whether a capability is valid
593+
func capValid(c string, hostSpecific bool) error {
594594
isValid := false
595595

596596
if !strings.HasPrefix(c, "CAP_") {
597597
return fmt.Errorf("capability %s must start with CAP_", c)
598598
}
599599
for _, cap := range capability.List() {
600600
if c == fmt.Sprintf("CAP_%s", strings.ToUpper(cap.String())) {
601-
if hostSpecific && cap > LastCap() {
601+
if hostSpecific && cap > lastCap() {
602602
return fmt.Errorf("CAP_%s is not supported on the current host", c)
603603
}
604604
isValid = true
@@ -612,8 +612,9 @@ func CapValid(c string, hostSpecific bool) error {
612612
return nil
613613
}
614614

615-
// LastCap return last cap of system
616-
func LastCap() capability.Cap {
615+
// lastCap return last cap of system, and is required to hack around RHEL6.
616+
// This is an exact copy of "generate/generate.go".lastCap.
617+
func lastCap() capability.Cap {
617618
last := capability.CAP_LAST_CAP
618619
// hack for RHEL6 which has no /proc/sys/kernel/cap_last_cap
619620
if last == capability.Cap(63) {

0 commit comments

Comments
 (0)