← Back to Pull requests

fix(usm): key ssl_sock_by_ctx by process to stop leaking entries in multi-threaded TLS clients #59740

Closed
unknown closed this pull request without merging on Nov 09, 2026 16:36 UTC
View on GitHub
unknown unknown commented on Nov 05, 2026 16:04 UTC
Author

What does this PR do?

Builds every ssl_sock_by_ctx key through one helper, ssl_sock_by_ctx_key(), which clears the thread ID bits of pid_tgid and keeps the TGID in the upper 32 bits. All threads of a process now share one entry per SSL context.

ssl_ctx_by_pid_tgid is unchanged and stays per thread, because it is the per-thread handoff between SSL_read/SSL_write and tcp_sendmsg.

There are no Go changes. The key struct and its Go mirror are unchanged, and userspace keeps reading the TGID with key.Tgid >> 32.

Motivation

#43379 (7.74.0) added pid_tgid to the key to stop SSL context pointers from colliding across processes. The key stores the raw bpf_get_current_pid_tgid() value, so it also includes the thread ID.

In a client where several threads use the same TLS connection:

  • A thread with no entry for its own TID misses in tup_from_ssl_ctx. It goes through the tcp_sendmsg fallback and inserts a new {tid, ctx} entry, often with the tuple of another socket.

  • SSL_shutdown, gnutls_goodbye and tcp_close only delete the entry of the thread that closes the connection.

  • Thread exit and the dead-process cleaner don't remove the other entries.
  • As a result, entries pile up until the process exits. In production this filled ssl_sock_by_ctx (max_tracked_connections, 65,536 by default) within hours on nodes running a long-lived multi-threaded OpenSSL client. After that USM stopped seeing new native TLS connections. Restarting system-probe only helps until the map fills again.

    The same mechanism also hurts accuracy before the map is full. Requests issued from a thread that resolves the connection through the fallback get attributed to the wrong connection tuple.

    Nothing in #43379 or USMON-1491 relies on per-thread keys. The PR describes the problem as multi-process, and its userspace cleaner already treats the field as a TGID. A per-process key keeps that isolation.

    Escalation: APMS-20835.

    Describe how you validated your changes

    New test. TestSSLSockByCtxMultiThreadedLeak (pkg/network/usm/ebpf_ssl_test.go) runs a helper (testdata/ssl_threadpool_test) through docker-compose, following the pattern of TestFdBySSLBioMapLeak.

  • The helper opens 20 TLS connections, has 16 threads send 8,000 requests on them while connections rotate, closes everything with SSL_shutdown, and stays alive.

  • The test waits for the helper to be traced before it starts. It then checks that no ssl_sock_by_ctx entry is left for the helper's PID, and that every captured request sits on the connection whose client port is in its path.

  • I ran it as root on a 6.8 arm64 kernel with CO-RE objects built from this branch, and again with objects built from main:
  • | Objects | Entries left | Requests captured on the right / wrong connection |
    |---|---|---|
    | this branch | 0 | 8,000 / 0, PASS |
    | main | 741 | 63 / 7,003, FAIL |

    End-to-end with system-probe. I rebuilt the CO-RE usm.o and swapped it into the 7.83.1 agent image, forcing CO-RE and disabling all fallbacks. The four eBPF headers are byte-identical between 7.83.1 and main, with and without this patch.

  • The client was multi-threaded OpenSSL or GnuTLS against an untraced Go TLS server. Every connection was closed before counting, and the process stayed alive.

  • Accuracy was checked through /debug/http_monitoring, using the client port encoded in each request path.

  • Before every check I confirmed that the map has entries while traffic is live, so the 0s are real.
  • | Scenario | 7.73.0 (stock) | 7.83.1 | 7.83.1 + this PR |
    |---|---|---|---|
    | Entries while 20 connections are open (8 threads) | 20 | 180 | 20 |
    | Thread pool, 8 threads: leftover / right connection | 0 / 100% | 234 / 0.1% | 0 / 100% |
    | Thread pool, 32 threads | 0 / 100% | 1,085 / 0.3% | 0 / 100% |
    | One short-lived thread per request | 0 / 100% | 0 / 0% (nothing captured) | 0 / 100% |
    | Close without SSL_shutdown | 0 / 98.4% | 247 / 0.1% | 0 / 98.3% |
    | 4 forked workers, same SSL addresses (#43379's case) | 0 / 12.9–20.5% | 60–246 / 0.1–1.2% | 0 / 100% |
    | GnuTLS thread pool | 0 / 100% | 160 / 0.1% | 0 / 100% |
    | Soak: 32 threads, 640k requests, 5k rotations | 0 / 100% | 2,339 / 0.2% | 0 / 100% |
    | After killing all clients | 0 | 306 | 0 |

    Other checks:

  • CO-RE (release and debug) and prebuilt (release and debug, linux-headers 6.8.0-110) build with -Werror.

  • Only the 18 programs that touch the key change. Each one gains the mask: a 64-bit immediate plus an AND, inlined at every key build.

  • go vet and gofmt are clean.

  • I ran these with and without the patch, on the same kernel:

  • - in pkg/network/usm: TestSSLMapsCleaner, TestSSLMapsCleanup, TestFdBySSLBioMapLeak and TestTLSSuite (OpenSSL versions, curl, wget, musl, the OpenSSL container, Node.js);
    - in pkg/network/usm/tests: TestUSMSuite (218 subtests) and TestFullMonitorWithTracer.

    Everything passes in CO-RE mode, and the only difference between the two runs is the new test.

  • Locally failing in both runs, so not caused by this PR, and CI should cover both:

  • - every runtime_compiled subtest (no kernel headers in my VM);
    - TestNodeJSTLSWithLibnode (its image needs internal registry access).
  • Not covered locally, left to CI/KMT: x86_64, kernels older than 6.8, and the runtime-compiled build.
  • Additional Notes

  • Leak paths that this PR doesn't change, all already present in 7.73:

  • - A connection set up through SSL_set_fd and closed without SSL_shutdown keeps its entry until the SSL pointer is reused or the process exits.
    - The tcp_sendmsg fallback is still a best-effort guess.
  • Since threads now share the entry, the one-time tuple fill in tup_from_ssl_ctx (bpf_memcpy into the map value) can in theory run from two CPUs for the same SSL. OpenSSL already requires callers to serialise access to an SSL object, and this is the same sharing model as before 7.74.0 (key = ctx only).

  • The test helper uses pid: host so its PID matches the TGID in the key. The new compose file is picked up by _test_docker_image_list, and it uses the same image as bio_leak_test.

  • I can't set labels from a fork. Suggested labels: team/ebpf-platform (or team/universal-service-monitoring), component/system-probe, qa/done, and backport to the active 7.8x branch.
  • 🤖 Generated with Claude Code

    Diff: 650 lines (+487 / -163) across 8 files Reviewer: brycekahle Review duration: 5792m
    All Vouch review confidence checks passed
    Residual risk 0.30 = Change Risk (0.75) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.75 · 650 lines diff across 8 files
    0.75
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.388
    Shallow Review Depth shallow_review_depth
    0.289
    Sensitive Paths sensitive_paths
    0.075
    Real PR Conversation Timeline
    1 comments
    "Review active on #59740"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed sensitive_module
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 75.2% (HIGH) · Higher = larger blast radius
    75.2%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 60.5% · Lower = rushed or shallow review
    60.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 29.7% · Safe to merge
    29.7%