#7694·checkov

Bug: CKV2_K8S_5 misattributes RoleBinding→Role when Role names collide across namespaces (false positives + false negatives)

Author: KeyboardShapedFaceCreated Sep 22, 2026Updated Sep 22, 2026
Labelschecks

Checkov version

3.2.530 (also reproduced on 3.2.346)

Description

CKV2_K8S_5 ("No ServiceAccount/Node should be able to read all secrets") produces simultaneous false positives and false negatives in a repo where multiple Role objects share the same metadata.name but live in different namespaces (different files).

Checkov's graph check resolves the RoleBinding → Role edge by matching roleRef.name to metadata.name. When the same name exists in multiple files (one per namespace), the graph picks the wrong Role for some RoleBinding objects. The result is:

  • A fixed namespace (Role no longer grants secrets) still FAILS CKV2_K8S_5 — because Checkov matched its RoleBinding to a different, unfixed Role that happens to share the name.
  • An unfixed namespace (Role still grants resources: ["*"]) PASSES CKV2_K8S_5 — because its RoleBinding was matched to an already-fixed Role.

This is a security-relevant bug. The false negatives mean genuinely over-permissioned RBAC is silently marked clean.

Note: This is a separate bug from #7616. Issue #7616 (fixed by #7625/#7640) is a per-rule evaluation bug that causes false negatives when a single Role mixes scoped and unscoped rules. This bug is a graph-matching bug that causes wrong results whenever Role names are shared across namespaces.

Reproduction

Create the following minimal directory structure and run checkov -d . --check CKV2_K8S_5 --compact.

namespace-a-role.yaml (FIXED — no secrets access)

yaml
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  name: full-access
  namespace: namespace-a
rules:
  - apiGroups: [""]
    resources: ["configmaps", "pods", "services"]
    verbs: ["get", "list", "watch"]

namespace-a-rolebinding.yaml

yaml
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: full-access-binding
  namespace: namespace-a
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: Role
  name: full-access
subjects:
  - kind: ServiceAccount
    name: default
    namespace: namespace-a

namespace-b-role.yaml (UNFIXED — grants wildcard resources including secrets)

yaml
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  name: full-access
  namespace: namespace-b
rules:
  - apiGroups: [""]
    resources: ["*"]
    verbs: ["get", "list", "watch"]

namespace-b-rolebinding.yaml

yaml
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: full-access-binding
  namespace: namespace-b
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: Role
  name: full-access
subjects:
  - kind: ServiceAccount
    name: default
    namespace: namespace-b

Expected result

RoleBinding Expected
namespace-a-rolebinding.yaml PASSED — its Role does not grant secrets
namespace-b-rolebinding.yaml FAILED — its Role grants resources: ["*"] which includes secrets

Actual result (with duplicate names present)

Results are wrong and non-deterministic across runs. Checkov matches each RoleBinding to the wrong Role for some subset of namespaces. In testing across a real repo with 14 namespaces all sharing the name full-access, the following was observed consistently:

  • Two already-fixed namespaces: FAILED (false positive)
  • Six unfixed namespaces still granting resources: ["*"]: PASSED (false negative)
  • The remaining six: correct

Root cause hypothesis

The graph check builds edges from RoleBinding.roleRef.name to Role.metadata.name. When the same name appears in multiple files (different namespaces), the graph resolver does not factor in metadata.namespace to disambiguate — so it connects one RoleBinding to the wrong Role. The mismatch appears deterministic (not flaky), suggesting a consistent ordering artifact (e.g. file sort order) determines which Role "wins" the name during graph construction.

The namespace field on both the RoleBinding and the referenced Role should be used to scope the graph edge. A RoleBinding in namespace-b that references roleRef.name: full-access should only match a Role with metadata.name: full-access AND metadata.namespace: namespace-b.

Impact

  • Any Kubernetes repo that defines the same Role or ClusterRole name across multiple namespaces or files will produce wrong CKV2_K8S_5 results — and likely wrong results for all other graph-based CKV2_K8S_* checks.
  • The false negatives are the more serious consequence: over-permissioned RBAC passes the check silently.
  • This pattern (one Role per namespace, same name) is common in real-world multi-tenant Kubernetes manifests.