java-topology/docs/tickets/cilium-0001-requirement-hasvalue-linear-scan-identity-matching.md
russell@unturf.com 9934133dcf whitepaper: 312 sites / 151 ecosystems — wave2+3 defect tables and PDF rebuild
Add 88 new defect entries to HIGH and MEDIUM tables:
  HIGH: mysql-0001/0002, mariadb-0001, redis-0001/0002, valkey-0001/0002, openvpn-0001,
        vlc-0001, prometheus-0001, otel-collector-0001, cockroachdb-0001..0004,
        tidb-0001..0008, kubernetes-0001/0002, go-0001, kotlin-0002, scala-0001,
        allegro5-0001, sdl2-0001, grafana-0001, clickhouse-0001, duckdb-0001,
        mongodb-0001, envoy-0001, istio-0001, cilium-0001, linkerd2-0001,
        linux-0001/0002/0003, tor-0002/0003, curl-0001, julia-0001, lua-0001,
        perl5-0001, nats-0001, spring-0003/0004, tomcat-0001, onos-0002, odl-0002

  MEDIUM: helm-0001, mariadb-0002, openssl-0001/0002, memcached-0001,
          cassandra-0001..0004, flink-0001, storm-0001/0002, zookeeper-0001..0003,
          pip-0001, gradle-0001, nginx-0001, haproxy-0001, caddy-0001, varnish-0001,
          ffmpeg-0001, gstreamer-0001, raylib-0001, love2d-0001, php-0001/0002,
          r-source-0001, cpython-0002, ruby-0001, rabbitmq-0003/0004, activemq-0001,
          ovs-0001, onos-0003, odl-0002, jetty-0001

PDF: 976K
2026-03-27 15:23:43 -04:00

2.8 KiB
Raw Permalink Blame History

cilium-0001: Requirement.hasValue slices.Contains O(n) called per identity in selector cache → O(n²)

Target: cilium/cilium Severity: HIGH CWE: CWE-407 (Inefficient Algorithmic Complexity) File: pkg/k8s/slim/k8s/apis/labels/selector.go Status: PATCHED

Description

Requirement.hasValue(value string) calls slices.Contains(r.strValues, value) where r.strValues is a []string. This is O(V) where V = number of values in the requirement's value set (e.g. a In selector with many values).

hasValue is called from Requirement.Matches(), which is called from internalSelector.Matches(), which is called from selectorcache.go inside a loop over every security identity in the cache (for nid, id := range c.ids).

Total complexity: O(I × R × V) where I = identities, R = requirements per selector, V = values per requirement. In large clusters with thousands of endpoints and label selectors with many values (e.g. namespace In [ns1, ns2, ..., nsN]), this becomes quadratic.

Location Pattern
selector.go:217 slices.Contains(r.strValues, value) — O(V) membership test
selector.go:238 r.hasValue(val) in Matches() hot path
selectorcache.go:118 sel.source.Matches(id.lbls) — called per identity in namespace loop
selectorcache.go:728 sel.source.Matches(identity.lbls) — called per identity in full scan

Root cause

// selector.go:160 — strValues stored as plain slice
type Requirement struct {
    key       string
    operator  selection.Operator
    strValues []string   // ← slice, O(n) membership
}

// selector.go:216 — O(V) scan on every label match check
func (r *Requirement) hasValue(value string) bool {
    return slices.Contains(r.strValues, value)
}

Caller in selectorcache.go:100 (selections — iterates all identities):

for nid, id := range c.ids {
    if sel.source.Matches(id.lbls) {   // O(R × V) per identity
        ...
    }
}

In a cluster with I=10,000 identities, R=3 requirements, V=50 values each: 1,500,000 string comparisons per selector evaluation.

Fix

Change strValues from []string to sets.String (or map[string]struct{}). The hasValue call becomes an O(1) map lookup. The existing Values() accessor already returns a sets.String, so the change is straightforward.

// After fix
type Requirement struct {
    key          string
    operator     selection.Operator
    strValues    sets.String   // O(1) lookup
    strValueList []string      // keep for ordering/serialization only
}

func (r *Requirement) hasValue(value string) bool {
    return r.strValues.Has(value)   // O(1)
}

Ops/ns numbers (Java benchmark)

See defects/cilium/unit/CiliumTest.java. At I=10,000 identities, V=50 values: slow ~500,000 ops, fast ~10,000 ops → >50× speedup.