Skip to content

feat(rbac): segmentation-violation rule — namespaced identity reaching cluster scope (closes #49) - #52

Merged
rrskris merged 2 commits into
kaaval:mainfrom
Maqbool61:feat/segmentation-violation-rule
Jul 14, 2026
Merged

rrskris merged 2 commits into
kaaval:mainfrom
Maqbool61:feat/segmentation-violation-rule

Conversation

@Maqbool61

Copy link
Copy Markdown
Contributor

What this implements

Adds a new RBAC rule (segmentation_violation) that flags identities breaking Kubernetes micro-segmentation — a namespaced ServiceAccount granted cluster-wide or cross-namespace reach. Implements the predicate described in issue #49 and satisfies the NIST SP 800-207A micro-segmentation tenet.


Problem

The existing 11 RBAC rules catch permission-content risks (wildcards, secrets access, exec grants, etc.) but none of them check whether the identity itself is scoped correctly. A ServiceAccount that lives in team-a but is bound cluster-wide via a ClusterRoleBinding — even to a narrow, low-risk role — violates namespace isolation regardless of what permissions that role grants. The namespace boundary is the fundamental unit of multi-tenancy in Kubernetes; crossing it is a segmentation failure, not just a permission failure.

Two patterns were undetected:

  1. A namespaced ServiceAccount bound at cluster scope via ClusterRoleBinding — the account reaches every namespace in the cluster
  2. A RoleBinding in namespace A that grants a ServiceAccount whose home namespace is B — lateral cross-namespace reach, a micro-segmentation violation even when the role is narrow

How it works

control-plane/app/rbac_service.py

Two new helpers and a predicate added:

_subject_namespace(subject) — extracts the namespace from a ServiceAccount subject, returns None for User/Group kinds (which are not namespace-scoped).

_is_segmentation_violation(binding, role_kind) — checks both patterns in order:

  • Case 1: ClusterRoleBinding whose subjects include any ServiceAccount with a non-None namespace → that account has been lifted from its namespace to cluster scope
  • Case 2: RoleBinding in namespace A that grants access to a ServiceAccount from namespace B (where A ≠ B) and the bound role is a ClusterRole → cross-namespace lateral reach
    Returns a human-readable description string when a violation is found, or None when clean.

The check is wired into evaluate_rbac_findings() right after the existing cluster_admin_binding check. It stacks on top of other risks — a wildcard ClusterRole bound to a namespaced ServiceAccount produces both wildcard_permissions AND segmentation_violation findings, since they represent two distinct problems (bad permissions + bad scoping). Severity is HIGH — cluster-scope reach is serious but not CRITICAL unless combined with cluster-admin-equivalent permissions (which the existing cluster_admin_binding rule already covers separately).

control-plane/app/remediation.py

Added segmentation_violation to both lookup tables:

  • _CIS_REFS: CIS Kubernetes Benchmark v1.12.0 §5.1.5, NIST SP 800-207A MS-3, and the Kubernetes RBAC Good Practices doc
  • _RBAC_ACTIONS: action template that tells the operator to replace the ClusterRoleBinding with a namespace-scoped RoleBinding and includes the exact kubectl delete command for the binding

Tests

control-plane/tests/test_rbac_service.py — 6 new tests + 1 updated pre-existing test:

Test What it verifies
test_namespaced_sa_cluster_role_binding_is_segmentation_violation Namespaced SA bound cluster-wide → segmentation_violation fires, severity HIGH, description names the namespace
test_cluster_sa_cluster_role_binding_is_not_segmentation_violation Non-namespaced User bound cluster-wide → rule does not fire (only SAs are namespace-scoped)
test_cross_namespace_role_binding_is_segmentation_violation RoleBinding in team-b granting SA from team-a → fires, both namespace names appear in description
test_same_namespace_role_binding_is_not_segmentation_violation RoleBinding in team-a granting SA from team-a → contained, does not fire
test_segmentation_violation_carries_remediation Finding carries action, why_it_matters, and populated benchmark_refs
test_segmentation_violation_stacks_with_other_risks Wildcard ClusterRole + namespaced SA → both wildcard_permissions AND segmentation_violation present
test_readonly_narrow_role_produces_no_rbac_risk_findings (updated) Pre-existing test used _single_rule_graph which binds via ClusterRoleBinding with a namespaced SA — now correctly expects segmentation_violation to fire, and asserts no content-risk findings appear (the original intent)

35 tests pass (26 RBAC + 9 remediation), 0 failed.


Acceptance criteria from issue #49

Criterion Status
New predicate over the effective access set ✅ _is_segmentation_violation() in rbac_service.py
Namespaced SA bound via ClusterRoleBinding to cluster-wide powers ✅ Case 1
Cross-namespace reach ✅ Case 2
Emits existing finding shape ✅ Same rule_type / severity / detail / remediation shape as all other rules
Remediation included ✅ _CIS_REFS + _RBAC_ACTIONS entries in remediation.py
NIST 800-207A micro-segmentation tenet ✅ Cited in benchmark_refs as MS-3

@Maqbool61

Copy link
Copy Markdown
Contributor Author

Fixing those issue.

@Maqbool61
Maqbool61 force-pushed the feat/segmentation-violation-rule branch from 0c1a733 to 03f864d Compare July 13, 2026 16:07

@rrskris rrskris left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really strong first contribution, @Maqbool61 — thank you for picking up the segmentation rule from the Zero-Trust design (#49). The rule logic is right (both the cluster-scope and cross-namespace cases), it reuses the pure-function core cleanly, emits the existing finding shape so scoring/remediation/SARIF/PolicyReport all just work, and the test coverage is genuinely thorough — positive, negative, remediation, and the stacking case. That's exactly the pattern.

Four things to fix before I can merge — the first is the important one:

1. Benchmark citations must be real (blocking). This is a hard rule for us because Kaaval is a compliance tool — a wrong or invented citation is worse than none (an auditor may act on it). Two problems in remediation.py:

  • CIS 5.1.5 is a mismatch. CIS Kubernetes 5.1.5 is "ensure default service accounts are not actively used" — it's about default SAs, not segmentation/cross-namespace reach. It doesn't cover this rule.
  • NIST SP 800-207A MS-3 is invented. 800-207A doesn't use a MS-n control-numbering scheme — that identifier doesn't exist in the document.

There is no direct CIS 5.1 control for segmentation (same situation as our exec_attach_grant rule, which cites RBAC Good Practices + OWASP K03 with no CIS ID). Please do the same here: drop the CIS 5.1.5 and the MS-3 entries, and cite NIST SP 800-207A by name with id: null (it's the right primary reference — just no fake control number) plus the Kubernetes RBAC Good Practices namespace-isolation guidance (the entry you already have is perfect). Look at how exec_attach_grant is cited in the same file for the shape.

2. Add a fixture. A new rule needs a case in hack/dev/rbac-fixtures.yaml (a namespaced ServiceAccount bound cluster-wide) so the rule fires in the live testbed too, not only in unit tests.

3. Add a docs/rbac-rules.md row. Every rule is documented in the catalog with its trigger, severity, and mapping — please add the row (this is also where point 4 gets addressed).

4. Let's talk about noise (not a hard block, but important). As written, the rule flags every namespaced ServiceAccount bound via ClusterRoleBinding as HIGH. That's a very common, often-legitimate pattern — monitoring agents (Prometheus), ingress controllers, cert-manager, etc. all bind a namespaced SA cluster-wide by design. Firing HIGH on all of them risks the alert-fatigue we explicitly exist to avoid. A couple of options to consider: keep it but document it in rbac-rules.md as "expected on cluster-agent SAs" (like we do for the kind local-path-provisioner cases), or lower the base severity and let the Contextual Risk Score lift the ones that matter, or scope the rule to when the bound ClusterRole is itself broad. I'd love your take — reply here and we'll settle it together; it may be worth a quick note on #49.

Also: your commits need DCO sign-off (git rebase --signoff main then force-push) — CI's DCO check is currently red on that. And two files lost their trailing newline (rbac_service.py, remediation.py) — please restore them.

None of this is a knock — it's a real rule that needed exactly this kind of review. Take your time; ping me here if any of it is unclear. 🙏

Comment thread control-plane/app/remediation.py Outdated
{"benchmark": CIS_BENCHMARK, "id": "5.1.5",
"title": "Ensure that default service accounts are not bound to active service account roles"},
{"benchmark": "NIST SP 800-207A", "id": "MS-3",
"title": "Enforce namespace isolation — identities should not cross namespace boundaries"},

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NIST SP 800-207A has no MS-3 control — this identifier is invented, and CIS 5.1.5 (line 62) is about default service accounts, not segmentation. Please drop both and cite NIST 800-207A with id: null + the Kubernetes RBAC Good Practices entry you already have (mirror how exec_attach_grant is cited above — no CIS ID, primary sources only). A wrong compliance citation in a compliance tool is worse than none.

…eaching cluster scope (closes kaaval#49)

Signed-off-by: Maqbool Ahmed <[email protected]>
…aval#52)

1. Fix benchmark citations in remediation.py
   - Drop CIS 5.1.5 (covers default SA token automount, not segmentation)
   - Drop NIST SP 800-207A MS-3 (invented control number — 800-207A has no
     MS-n numbering scheme)
   - Replace with: NIST SP 800-207A (id: null) + Kubernetes RBAC Good
     Practices, mirroring the exec_attach_grant pattern

2. Add rbac-fixtures.yaml entry (kaaval#12 cross-ns-reader)
   A namespaced ServiceAccount (monitoring/prometheus-sa) bound cluster-wide
   via ClusterRoleBinding so the live testbed can verify the rule fires.

3. Add docs/rbac-rules.md row for segmentation_violation
   Trigger, base severity (HIGH), and benchmark mapping. Includes a note on
   the expected cluster-agent SA pattern (Prometheus, ingress-controller,
   cert-manager) and guidance to use Contextual Risk Score rather than
   suppression for confirmed-intentional findings.

Signed-off-by: Maqbool Ahmed <[email protected]>
@Maqbool61
Maqbool61 force-pushed the feat/segmentation-violation-rule branch from 03f864d to faa906f Compare July 13, 2026 18:14
@Maqbool61
Maqbool61 requested a review from rrskris July 13, 2026 18:15
@Maqbool61

Copy link
Copy Markdown
Contributor Author

Really strong first contribution, @Maqbool61 — thank you for picking up the segmentation rule from the Zero-Trust design (#49). The rule logic is right (both the cluster-scope and cross-namespace cases), it reuses the pure-function core cleanly, emits the existing finding shape so scoring/remediation/SARIF/PolicyReport all just work, and the test coverage is genuinely thorough — positive, negative, remediation, and the stacking case. That's exactly the pattern.

Four things to fix before I can merge — the first is the important one:

1. Benchmark citations must be real (blocking). This is a hard rule for us because Kaaval is a compliance tool — a wrong or invented citation is worse than none (an auditor may act on it). Two problems in remediation.py:

* **`CIS 5.1.5` is a mismatch.** CIS Kubernetes 5.1.5 is _"ensure default service accounts are not actively used"_ — it's about default SAs, not segmentation/cross-namespace reach. It doesn't cover this rule.

* **`NIST SP 800-207A` `MS-3` is invented.** 800-207A doesn't use a `MS-n` control-numbering scheme — that identifier doesn't exist in the document.

There is no direct CIS 5.1 control for segmentation (same situation as our exec_attach_grant rule, which cites RBAC Good Practices + OWASP K03 with no CIS ID). Please do the same here: drop the CIS 5.1.5 and the MS-3 entries, and cite NIST SP 800-207A by name with id: null (it's the right primary reference — just no fake control number) plus the Kubernetes RBAC Good Practices namespace-isolation guidance (the entry you already have is perfect). Look at how exec_attach_grant is cited in the same file for the shape.

2. Add a fixture. A new rule needs a case in hack/dev/rbac-fixtures.yaml (a namespaced ServiceAccount bound cluster-wide) so the rule fires in the live testbed too, not only in unit tests.

3. Add a docs/rbac-rules.md row. Every rule is documented in the catalog with its trigger, severity, and mapping — please add the row (this is also where point 4 gets addressed).

4. Let's talk about noise (not a hard block, but important). As written, the rule flags every namespaced ServiceAccount bound via ClusterRoleBinding as HIGH. That's a very common, often-legitimate pattern — monitoring agents (Prometheus), ingress controllers, cert-manager, etc. all bind a namespaced SA cluster-wide by design. Firing HIGH on all of them risks the alert-fatigue we explicitly exist to avoid. A couple of options to consider: keep it but document it in rbac-rules.md as "expected on cluster-agent SAs" (like we do for the kind local-path-provisioner cases), or lower the base severity and let the Contextual Risk Score lift the ones that matter, or scope the rule to when the bound ClusterRole is itself broad. I'd love your take — reply here and we'll settle it together; it may be worth a quick note on #49.

Also: your commits need DCO sign-off (git rebase --signoff main then force-push) — CI's DCO check is currently red on that. And two files lost their trailing newline (rbac_service.py, remediation.py) — please restore them.

None of this is a knock — it's a real rule that needed exactly this kind of review. Take your time; ping me here if any of it is unclear. 🙏

All four points addressed in the latest commit:

  1. Citations fixed — dropped CIS 5.1.5 and the invented NIST MS-3 identifier. Replaced with NIST SP 800-207A (id: null) + Kubernetes RBAC Good Practices, exactly mirroring exec_attach_grant.
  2. Fixture added — hack/dev/rbac-fixtures.yaml now has entry chore: CNCF-standard governance pack #12 (cross-ns-reader): a namespaced ServiceAccount in monitoring bound cluster-wide via ClusterRoleBinding, so the rule fires in the live testbed.
  3. Docs row added — docs/rbac-rules.md has the new row with trigger, base severity (HIGH), and benchmark mapping.
  4. Noise — I've kept HIGH and documented the expected cluster-agent SA pattern (Prometheus, ingress-controller, cert-manager) in rbac-rules.md, with guidance to use the Contextual Risk Score rather than suppression for confirmed-intentional bindings. Happy to discuss lowering to MEDIUM on the issue if you'd prefer that direction.

@rrskris rrskris left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Everything's addressed, and verified against the files — the citations are honest now (NIST 800-207A + RBAC Good Practices, no invented IDs), the fixture and docs row are in, DCO's signed, CI's green. Really nice follow-through across two rounds, @Maqbool61. 🎉 Your call to keep HIGH and document the expected cluster-agent pattern (Prometheus/ingress/cert-manager) is consistent with how we already handle local-path-provisioner, so I'm good with it — if real-cluster usage shows it's too noisy we can revisit severity on #49. Merging. Thank you for the careful work!

@rrskris
rrskris merged commit 8eb4071 into kaaval:main Jul 14, 2026
6 checks passed
@Maqbool61
Maqbool61 deleted the feat/segmentation-violation-rule branch July 14, 2026 20:06
rrskris added a commit that referenced this pull request Jul 26, 2026
#119)

The token_automount rule added in #108 reads ServiceAccount automount
settings and pod specs. Both fetches are wrapped in their own try/except, so
a scanner without those permissions logs a warning and silently returns no
automount findings — meaning the rule shipped dark for anyone following the
documented in-cluster deployment.

Adds read-only get/list on core serviceaccounts and pods to both the
kaaval-scanner-reader ClusterRole in deploy/cronjob.yaml and the documented
kaaval-scanner role in docs/ci-integration.md.

Also corrects a stale claim next to that role. The docs asserted Kaaval
"would not flag it"; scanning deploy/cronjob.yaml shows it does, and has
since the segmentation_violation rule landed in #52 — a namespaced
ServiceAccount granted cluster scope via ClusterRoleBinding is exactly what
that rule looks for. Verified the two added grants introduce no new findings
(3 before, 3 after), so the least-privilege story holds; the parenthetical
was simply wrong. Replaced it with the actual result and why it is expected.

Signed-off-by: Vamshi Krishna Santhapuri <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants