From 50dc2e7afe7449c5b7df588419228ab59bdcb2c4 Mon Sep 17 00:00:00 2001 From: Tingmao Wang Date: Tue, 24 Nov 2026 11:59:09 +0000 Subject: [PATCH] rego: Reject policies with api_version < 0.6.0 Policies before 0.6.0 does not handle the plan9_mount enforcement point, and possibly more with older versions, and the default behavior in api.rego is too lax and will lead to host gaining control in basically all scenarios. Signed-off-by: Tingmao Wang --- pkg/securitypolicy/api_test.rego | 8 +-- pkg/securitypolicy/framework.rego | 7 ++ pkg/securitypolicy/regopolicy_linux_test.go | 65 +++++++++++++++++-- .../securitypolicyenforcer_rego.go | 15 +++++ 4 files changed, 86 insertions(+), 9 deletions(-) diff --git a/pkg/securitypolicy/api_test.rego b/pkg/securitypolicy/api_test.rego index 767c506e58..e616c7fa76 100644 --- a/pkg/securitypolicy/api_test.rego +++ b/pkg/securitypolicy/api_test.rego @@ -1,10 +1,10 @@ package api -version := "0.0.2" +version := "0.7.0" enforcement_points := { "__fixture_for_future_test__": {"introducedVersion": "100.0.0", "default_results": {"allowed": true}, "use_framework": false}, - "__fixture_for_allowed_test_true__": {"introducedVersion": "0.0.2", "default_results": {"allowed": true}, "use_framework": false}, - "__fixture_for_allowed_test_false__": {"introducedVersion": "0.0.2", "default_results": {"allowed": false}, "use_framework": false}, - "__fixture_for_allowed_extra__": {"introducedVersion": "0.0.1", "default_results": {"allowed": false, "__test__": "test"}, "use_framework": false} + "__fixture_for_allowed_test_true__": {"introducedVersion": "0.7.0", "default_results": {"allowed": true}, "use_framework": false}, + "__fixture_for_allowed_test_false__": {"introducedVersion": "0.7.0", "default_results": {"allowed": false}, "use_framework": false}, + "__fixture_for_allowed_extra__": {"introducedVersion": "0.6.0", "default_results": {"allowed": false, "__test__": "test"}, "use_framework": false} } diff --git a/pkg/securitypolicy/framework.rego b/pkg/securitypolicy/framework.rego index 5d762ffa05..70dfdef310 100644 --- a/pkg/securitypolicy/framework.rego +++ b/pkg/securitypolicy/framework.rego @@ -3311,6 +3311,13 @@ default policy_api_version := null policy_framework_version := data.policy.framework_version policy_api_version := data.policy.api_version +default is_unsupported_api_version := false + +is_unsupported_api_version if { + policy_api_version != null + semver.compare(policy_api_version, "0.6.0") < 0 +} + # deprecated policy_framework_version := data.policy.framework_svn policy_api_version := data.policy.api_svn diff --git a/pkg/securitypolicy/regopolicy_linux_test.go b/pkg/securitypolicy/regopolicy_linux_test.go index 59d702909b..da4fd80510 100644 --- a/pkg/securitypolicy/regopolicy_linux_test.go +++ b/pkg/securitypolicy/regopolicy_linux_test.go @@ -2472,12 +2472,67 @@ func Test_Rego_Version_Future_Enforcement_Point(t *testing.T) { } } +func Test_Rego_Unsupported_APIVersion(t *testing.T) { + tests := []struct { + name string + versionKey string + version string + supported bool + }{ + { + name: "below minimum", + versionKey: "api_version", + version: "0.5.9", + }, + { + name: "minimum", + versionKey: "api_version", + version: "0.6.0", + supported: true, + }, + { + name: "above minimum", + versionKey: "api_version", + version: "0.7.0", + supported: true, + }, + { + name: "deprecated key below minimum", + versionKey: "api_svn", + version: "0.5.9", + }, + { + name: "deprecated key at minimum", + versionKey: "api_svn", + version: "0.6.0", + supported: true, + }, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + code := fmt.Sprintf("package policy\n\n%s := %q", test.versionKey, test.version) + _, err := newRegoPolicy(code, []oci.Mount{}, []oci.Mount{}, testOSType) + if test.supported { + if err != nil { + t.Fatalf("supported policy version %s was rejected: %v", test.version, err) + } + return + } + + if err == nil { + t.Fatalf("unsupported policy version %s was accepted", test.version) + } + }) + } +} + // Tests whether the enforcement point logic returns the default behavior // and "unsupported" when the enforcement point was added in a version of the // framework that was released after the policy was authored as indicated // by their respective version information. func Test_Rego_Version_Unavailable_Enforcement_Point(t *testing.T) { - code := "package policy\n\napi_version := \"0.0.1\"" + code := "package policy\n\napi_version := \"0.6.0\"" policy, err := newRegoPolicy(code, []oci.Mount{}, []oci.Mount{}, testOSType) if err != nil { @@ -2511,7 +2566,7 @@ func Test_Rego_Version_Unavailable_Enforcement_Point(t *testing.T) { } func Test_Rego_Enforcement_Point_Allowed(t *testing.T) { - code := "package policy\n\napi_version := \"0.0.1\"" + code := "package policy\n\napi_version := \"0.6.0\"" policy, err := newRegoPolicy(code, []oci.Mount{}, []oci.Mount{}, testOSType) if err != nil { @@ -2560,7 +2615,7 @@ func Test_Rego_Enforcement_Point_Extra(t *testing.T) { ctx := context.Background() code := `package policy -api_version := "0.0.1" +api_version := "0.6.0" __fixture_for_allowed_extra__ := {"allowed": true} ` @@ -8921,7 +8976,7 @@ func Test_Rego_APISVN(t *testing.T) { func Test_Rego_NoReason(t *testing.T) { code := `package policy - api_version := "0.0.1" + api_version := "0.6.0" mount_device := {"allowed": false} ` @@ -9013,7 +9068,7 @@ func Test_Rego_ErrorTruncation_Unable(t *testing.T) { func Test_Rego_ErrorTruncation_CustomPolicy(t *testing.T) { code := fmt.Sprintf(`package policy - api_version := "0.1.0" + api_version := "0.6.0" mount_device := {"allowed": false} diff --git a/pkg/securitypolicy/securitypolicyenforcer_rego.go b/pkg/securitypolicy/securitypolicyenforcer_rego.go index d3485bd110..af82570f60 100644 --- a/pkg/securitypolicy/securitypolicyenforcer_rego.go +++ b/pkg/securitypolicy/securitypolicyenforcer_rego.go @@ -204,6 +204,21 @@ func newRegoPolicy(code string, defaultMounts []oci.Mount, privilegedMounts []oc return nil, fmt.Errorf("rego compilation failed: %w", err) } + result, err := policy.rego.RawQuery("data.framework.is_unsupported_api_version", map[string]interface{}{}) + if err != nil { + return nil, fmt.Errorf("error checking policy api_version: %w", err) + } + if len(result) != 1 || len(result[0].Expressions) != 1 { + return nil, errors.New("error checking policy api_version: unexpected result") + } + unsupported, ok := result[0].Expressions[0].Value.(bool) + if !ok { + return nil, errors.New("error checking policy api_version: result is not a boolean") + } + if unsupported { + return nil, errors.New("policy api_version is unsupported: minimum supported version is 0.6.0") + } + // by default we do not perform message truncation policy.maxErrorMessageLength = 0