diff --git a/pkg/seccomp/precompiledseccomp/precompile_gen.go b/pkg/seccomp/precompiledseccomp/precompile_gen.go index 7331f6b17..55515bdc2 100644 --- a/pkg/seccomp/precompiledseccomp/precompile_gen.go +++ b/pkg/seccomp/precompiledseccomp/precompile_gen.go @@ -39,6 +39,7 @@ const ( packageNameStandin = "precompiled" precompiledseccompPackageName = "precompiledseccomp" registrationComment = "PROGRAM_REGISTRATION_GOES_HERE_THIS_IS_A_LOAD_BEARING_COMMENT" + disabledAtBuildtimeComment = "PRECOMPILATION_DISABLED_AT_BUILD_TIME_THIS_IS_A_LOAD_BEARING_COMMENT" programsMapVarName = "programs" ) @@ -57,7 +58,8 @@ func main() { // Get a sorted list of programs. var programs []precompiledseccomp.Program - if loadProgramsFn != nil { + disabledAtBuildTime := loadProgramsFn == nil + if !disabledAtBuildTime { var err error programs, err = loadProgramsFn() if err != nil { @@ -114,6 +116,8 @@ func main() { for _, program := range programs { fmt.Fprint(outFile, program.Registration(indent, precompiledseccompPackageName, programsMapVarName)) } + case strings.Contains(line, disabledAtBuildtimeComment): + fmt.Fprintf(outFile, "const PrecompilationDisabledAtBuildTime = %t\n", disabledAtBuildTime) default: fmt.Fprintf(outFile, "%s\n", line) } diff --git a/pkg/seccomp/precompiledseccomp/precompiled_lib.tmpl.go b/pkg/seccomp/precompiledseccomp/precompiled_lib.tmpl.go index 1984addb3..132796fe7 100644 --- a/pkg/seccomp/precompiledseccomp/precompiled_lib.tmpl.go +++ b/pkg/seccomp/precompiledseccomp/precompiled_lib.tmpl.go @@ -33,6 +33,10 @@ var ( registerPrecompiledProgramsOnce sync.Once ) +// PrecompilationDisabledAtBuildTime is a constant that is used to +// indicate that precompilation was disabled at build time. +const PrecompilationDisabledAtBuildTime = false // PRECOMPILATION_DISABLED_AT_BUILD_TIME_THIS_IS_A_LOAD_BEARING_COMMENT + // GetPrecompiled returns the precompiled program for the given name, // and whether that program name exists. func GetPrecompiled(programName string) (precompiledseccomp.Program, bool) { diff --git a/pkg/seccomp/precompiledseccomp/precompiledseccomp.go b/pkg/seccomp/precompiledseccomp/precompiledseccomp.go index e1dcc4cf1..67530ee4e 100644 --- a/pkg/seccomp/precompiledseccomp/precompiledseccomp.go +++ b/pkg/seccomp/precompiledseccomp/precompiledseccomp.go @@ -19,6 +19,7 @@ package precompiledseccomp import ( "encoding/binary" "fmt" + "maps" "sort" "strings" @@ -70,7 +71,42 @@ func (v Values) SetUint64(varName string, value uint64) { // GetUint64 retrieves the value of a 64-bit variable set using // `Values.SetUint64(varName)`. func (v Values) GetUint64(varName string) uint64 { - return uint64(v[varName+"_high32bits"])<<32 | uint64(v[varName+"_low32bits"]) + return uint64(v[varName+uint64VarSuffixHigh])<<32 | uint64(v[varName+uint64VarSuffixLow]) +} + +// PopUint64 retrieves the value of a 64-bit variable and removes it from `v`. +func (v Values) PopUint64(varName string) uint64 { + val := v.GetUint64(varName) + delete(v, varName+uint64VarSuffixHigh) + delete(v, varName+uint64VarSuffixLow) + return val +} + +// Uint64VarName turns a suffixed 32-bit variable name into a 64-bit variable +// name by stripping the underlying prefixes. +// Returns the empty string if the variable name is not a suffixed 32-bit +// variable name. +func (v Values) Uint64VarName(varName string) string { + if strings.HasSuffix(varName, uint64VarSuffixHigh) { + varName = strings.TrimSuffix(varName, uint64VarSuffixHigh) + } else if strings.HasSuffix(varName, uint64VarSuffixLow) { + varName = strings.TrimSuffix(varName, uint64VarSuffixLow) + } else { + return "" // Not a suffixed variable. + } + _, okHigh := v[varName+uint64VarSuffixHigh] + _, okLow := v[varName+uint64VarSuffixLow] + if !okHigh || !okLow { + return "" // We don't have values for this variable. + } + return varName +} + +// Copy returns a copy of `v`. +func (v Values) Copy() Values { + v2 := Values(make(map[string]uint32, len(v))) + maps.Copy(v2, v) + return v2 } // Precompile compiles a `ProgramDesc` with the given values. diff --git a/runsc/boot/platforms/BUILD b/pkg/sentry/platform/platforms/BUILD similarity index 96% rename from runsc/boot/platforms/BUILD rename to pkg/sentry/platform/platforms/BUILD index 34236738c..22ed12db4 100644 --- a/runsc/boot/platforms/BUILD +++ b/pkg/sentry/platform/platforms/BUILD @@ -18,7 +18,7 @@ exempt_go_library( # used to choose deps. stateify = False, visibility = [ - "//runsc:__subpackages__", + "//:sandbox", ], deps = select_system( darwin = [], diff --git a/runsc/boot/platforms/platforms.go b/pkg/sentry/platform/platforms/platforms.go similarity index 100% rename from runsc/boot/platforms/platforms.go rename to pkg/sentry/platform/platforms/platforms.go diff --git a/runsc/boot/platforms/platforms_darwin.go b/pkg/sentry/platform/platforms/platforms_darwin.go similarity index 100% rename from runsc/boot/platforms/platforms_darwin.go rename to pkg/sentry/platform/platforms/platforms_darwin.go diff --git a/runsc/boot/BUILD b/runsc/boot/BUILD index fef0763de..ebe27a137 100644 --- a/runsc/boot/BUILD +++ b/runsc/boot/BUILD @@ -84,6 +84,7 @@ go_library( "//pkg/sentry/loader", "//pkg/sentry/pgalloc", "//pkg/sentry/platform", + "//pkg/sentry/platform/platforms", "//pkg/sentry/seccheck", "//pkg/sentry/seccheck/points:points_go_proto", "//pkg/sentry/seccheck/sinks/null", @@ -124,7 +125,6 @@ go_library( "//pkg/tcpip/transport/udp", "//pkg/urpc", "//runsc/boot/filter", - "//runsc/boot/platforms", "//runsc/boot/portforward", "//runsc/boot/pprof", "//runsc/boot/procfs", diff --git a/runsc/boot/filter/config/BUILD b/runsc/boot/filter/config/BUILD index c9f3e205b..8bd8343b7 100644 --- a/runsc/boot/filter/config/BUILD +++ b/runsc/boot/filter/config/BUILD @@ -34,10 +34,10 @@ go_library( "//pkg/sentry/devices/nvproxy", "//pkg/sentry/devices/tpuproxy", "//pkg/sentry/platform", + "//pkg/sentry/platform/platforms", "//pkg/sentry/socket/hostinet", "//pkg/sentry/socket/plugin", "//pkg/tcpip/link/fdbased", - "//runsc/boot/platforms", "@org_golang_x_sync//errgroup:go_default_library", "@org_golang_x_sys//unix:go_default_library", ], diff --git a/runsc/boot/filter/config/config_precompiled.go b/runsc/boot/filter/config/config_precompiled.go index 5e2ed962d..273c0c9a7 100644 --- a/runsc/boot/filter/config/config_precompiled.go +++ b/runsc/boot/filter/config/config_precompiled.go @@ -24,7 +24,7 @@ import ( "gvisor.dev/gvisor/pkg/sentry/platform" // Import platforms that we need to precompile filters for. - _ "gvisor.dev/gvisor/runsc/boot/platforms" + _ "gvisor.dev/gvisor/pkg/sentry/platform/platforms" ) // Variable names used in precompiled filters. diff --git a/runsc/boot/loader.go b/runsc/boot/loader.go index f00810559..97ecbd4eb 100644 --- a/runsc/boot/loader.go +++ b/runsc/boot/loader.go @@ -51,6 +51,7 @@ import ( "gvisor.dev/gvisor/pkg/sentry/loader" "gvisor.dev/gvisor/pkg/sentry/pgalloc" "gvisor.dev/gvisor/pkg/sentry/platform" + _ "gvisor.dev/gvisor/pkg/sentry/platform/platforms" // register all platforms. "gvisor.dev/gvisor/pkg/sentry/seccheck" pb "gvisor.dev/gvisor/pkg/sentry/seccheck/points/points_go_proto" "gvisor.dev/gvisor/pkg/sentry/socket/netfilter" @@ -75,7 +76,6 @@ import ( "gvisor.dev/gvisor/pkg/tcpip/transport/tcp" "gvisor.dev/gvisor/pkg/tcpip/transport/udp" "gvisor.dev/gvisor/runsc/boot/filter" - _ "gvisor.dev/gvisor/runsc/boot/platforms" // register all platforms. pf "gvisor.dev/gvisor/runsc/boot/portforward" "gvisor.dev/gvisor/runsc/boot/pprof" "gvisor.dev/gvisor/runsc/config"