From 6ba465d40af63c98ec018eef06f9f44b37692664 Mon Sep 17 00:00:00 2001 From: Andrei Kvapil Date: Mon, 16 Nov 2026 11:46:30 -0500 Subject: [PATCH] fix(linstor): admit only blockstor's own Secret creates RBAC cannot limit create by name, and the blockstor controller and apiserver need create for the keyring and the generated unlock Secret. With the name-limited get they also hold, a compromised pod could create a service-account-token Secret for one of piraeus's ServiceAccounts in cozy-linstor under a covered name that does not exist yet, linstor-passphrase on a cluster that never set one, and read the token back. blockstor's design leaves closing this to the chart. The policy matches only Secret creates in the release namespace by the blockstor ServiceAccounts, so it cannot reach piraeus or cert-manager, and admits the keyring and the unlock Secret with the types blockstor gives them. The passphrase Secret is not admitted: blockstor never writes it. Assisted-by: LLM Signed-off-by: Andrei Kvapil --- .../templates/blockstor-secrets-policy.yaml | 61 +++++++++++++ .../tests/blockstor_secrets_policy_test.yaml | 90 +++++++++++++++++++ 2 files changed, 151 insertions(+) create mode 100644 packages/system/linstor/templates/blockstor-secrets-policy.yaml create mode 100644 packages/system/linstor/tests/blockstor_secrets_policy_test.yaml diff --git a/packages/system/linstor/templates/blockstor-secrets-policy.yaml b/packages/system/linstor/templates/blockstor-secrets-policy.yaml new file mode 100644 index 000000000..d1726aa90 --- /dev/null +++ b/packages/system/linstor/templates/blockstor-secrets-policy.yaml @@ -0,0 +1,61 @@ +{{- if and (eq .Values.backend "blockstor") (.Capabilities.APIVersions.Has "admissionregistration.k8s.io/v1/ValidatingAdmissionPolicy") }} +{{- $sa := printf "system:serviceaccount:%s:" .Release.Namespace }} +--- +{{- /* + RBAC cannot limit create by name, and the blockstor controller and + apiserver need it for the keyring and the generated unlock Secret. A + compromised pod could otherwise create a Secret under a name its + name-limited get already covers but that does not exist yet, such as + linstor-passphrase on a cluster that never set one, typed + kubernetes.io/service-account-token for one of piraeus's ServiceAccounts + in this namespace, and read the token back. This admits a Secret create + by a blockstor ServiceAccount only for the two Secrets blockstor creates, + each with the type blockstor gives it; the passphrase Secret is input + that blockstor never writes. + + It matches nothing but CREATE of Secrets in this namespace by these + ServiceAccounts, so piraeus, cert-manager and everyone else are outside + it, as are updates, which RBAC already limits by name. The satellite + holds no create; it is listed so a later grant does not reopen the gap. */}} +apiVersion: admissionregistration.k8s.io/v1 +kind: ValidatingAdmissionPolicy +metadata: + name: blockstor-secret-create +spec: + failurePolicy: Fail + matchConstraints: + namespaceSelector: + matchLabels: + kubernetes.io/metadata.name: {{ .Release.Namespace }} + resourceRules: + - apiGroups: [""] + apiVersions: [v1] + operations: [CREATE] + resources: [secrets] + matchConditions: + - name: blockstor-service-account + expression: >- + request.userInfo.username in [ + '{{ $sa }}blockstor-apiserver', + '{{ $sa }}blockstor-controller', + '{{ $sa }}blockstor-satellite' + ] + validations: + - expression: >- + (object.metadata.name == 'blockstor-master-key' && + has(object.type) && object.type == 'blockstor.org/master-key') || + (object.metadata.name == 'blockstor-unlock' && + (!has(object.type) || object.type == 'Opaque')) + message: >- + blockstor may create only the Secrets blockstor-master-key (type + blockstor.org/master-key) and blockstor-unlock (type Opaque) + reason: Forbidden +--- +apiVersion: admissionregistration.k8s.io/v1 +kind: ValidatingAdmissionPolicyBinding +metadata: + name: blockstor-secret-create +spec: + policyName: blockstor-secret-create + validationActions: [Deny] +{{- end }} diff --git a/packages/system/linstor/tests/blockstor_secrets_policy_test.yaml b/packages/system/linstor/tests/blockstor_secrets_policy_test.yaml new file mode 100644 index 000000000..2b77ec989 --- /dev/null +++ b/packages/system/linstor/tests/blockstor_secrets_policy_test.yaml @@ -0,0 +1,90 @@ +# RBAC cannot limit a Secret create by name, so this policy is what keeps a +# compromised blockstor pod from minting a token Secret for one of piraeus's +# ServiceAccounts under a name its get already covers. It must reach nothing +# but blockstor's own creates in its own namespace. +suite: blockstor Secret create policy +templates: + - templates/blockstor-secrets-policy.yaml +release: + name: linstor + namespace: cozy-linstor +# helm unittest's default API versions do not include it, and the template +# renders only where the cluster serves it. +capabilities: + apiVersions: + - admissionregistration.k8s.io/v1/ValidatingAdmissionPolicy +set: + backend: blockstor +tests: + - it: renders nothing under the LINSTOR backend + set: + backend: linstor + asserts: + - hasDocuments: + count: 0 + + - it: matches only Secret creates in the release namespace + documentSelector: + path: kind + value: ValidatingAdmissionPolicy + asserts: + - equal: + path: spec.matchConstraints + value: + namespaceSelector: + matchLabels: + kubernetes.io/metadata.name: cozy-linstor + resourceRules: + - apiGroups: [""] + apiVersions: [v1] + operations: [CREATE] + resources: [secrets] + - equal: + path: spec.failurePolicy + value: Fail + + - it: applies only to the blockstor ServiceAccounts of the release namespace + documentSelector: + path: kind + value: ValidatingAdmissionPolicy + asserts: + - lengthEqual: + path: spec.matchConditions + count: 1 + - equal: + path: spec.matchConditions[0].expression + value: >- + request.userInfo.username in [ + 'system:serviceaccount:cozy-linstor:blockstor-apiserver', + 'system:serviceaccount:cozy-linstor:blockstor-controller', + 'system:serviceaccount:cozy-linstor:blockstor-satellite' + ] + + - it: admits only the keyring and the unlock Secret, each with its type + documentSelector: + path: kind + value: ValidatingAdmissionPolicy + asserts: + - lengthEqual: + path: spec.validations + count: 1 + - matchRegex: + path: spec.validations[0].expression + pattern: "object.metadata.name == 'blockstor-master-key' &&\\s+has\\(object.type\\) && object.type == 'blockstor.org/master-key'" + - matchRegex: + path: spec.validations[0].expression + pattern: "object.metadata.name == 'blockstor-unlock' &&\\s+\\(!has\\(object.type\\) \\|\\| object.type == 'Opaque'\\)" + - notMatchRegex: + path: spec.validations[0].expression + pattern: linstor-passphrase + + - it: denies through a binding without its own selector + documentSelector: + path: kind + value: ValidatingAdmissionPolicyBinding + asserts: + - equal: + path: spec + value: + policyName: blockstor-secret-create + validationActions: [Deny]