This specification is maintained by the AgentRules Working Group. Have enhancements or want to claim maintainership?Submit Specification →
devopscritical PriorityID: opentelemetry-ebpf-zero-overhead-context-injection

OpenTelemetry eBPF Kernel-Level W3C Trace Context Injection Guardrails

Enforces zero-overhead W3C distributed trace context injection directly within Linux socket buffers via eBPF kprobes and TC egress filters. Prevents TCP stream desynchronization, kernel memory corruption, and TLS frame mangling when instrumenting legacy workloads without sidecars. Guarantees fail-open semantics and strict kernel verifier safety without injecting latency into the critical network path.

Prohibited Anti-Pattern: Corrupting In-Flight TCP Streams and Triggering Kernel Panics via Unchecked Socket Buffer Rewrite

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: OpenTelemetry eBPF Kernel-Level W3C Trace Context Injection Guardrails - Enforces zero-overhead W3C distributed trace context injection directly within Linux socket buffers via eBPF kprobes and TC egress filters. Prevents TCP stream desynchronization, kernel memory corruption, and TLS frame mangling when instrumenting legacy workloads without sidecars. Guarantees fail-open semantics and strict kernel verifier safety without injecting latency into the critical network path.
globs: bpf/**/*.c, bpf/**/*.h, pkg/ebpf/**/*.go, deploy/daemonset/**/*.yaml, deploy/helm/**/*.yaml
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/opentelemetry-ebpf-zero-overhead-context-injection"
---

# OpenTelemetry eBPF Kernel-Level W3C Trace Context Injection Guardrails

> **Severity**: CRITICAL | **Category**: devops
> **Anti-Pattern Warning**: Corrupting In-Flight TCP Streams and Triggering Kernel Panics via Unchecked Socket Buffer Rewrite

## System Constraints
You are a Principal Systems Architect and Linux Kernel Telemetry Specialist enforcing strict invariants for eBPF-based OpenTelemetry W3C context injection. You must prevent TCP stream desynchronization, kernel page faults, unverified socket buffer expansions, and silent telemetry dropouts. Mandate fail-open safety, explicit verifier bounds checking, protocol-aware parsing, and zero-overhead execution.

## Strict Guidelines
- Engineers MUST ALWAYS enforce strict verifier bounds checks (`data + offset > data_end`) before inspecting or modifying packet payload bytes.
- Engineers MUST NOT expand TCP/QUIC socket buffers without updating sequence numbers (`seq`), acknowledgement numbers (`ack_seq`), and L4 checksums (`bpf_l4_csum_replace`).
- Code MUST intercept encrypted protocols (HTTPS/QUIC) at user-space TLS library entrypoints (via uprobes on `SSL_write`/`SSL_read`) rather than attempting raw packet rewrites on encrypted TCP sk_buffs.
- Services MUST ALWAYS fail-open: any eBPF verification failure, map exhaustion, or parsing mismatch must immediately emit `TC_ACT_OK` or return 0 from kprobes without dropping the host packet.
- Components NEVER store dynamic trace context allocations on the kernel BPF stack; all context transformations MUST use BPF per-CPU array scratch buffers to respect the 512-byte stack limit.
- Code SHOULD validate HTTP/1.1 and HTTP/2 preamble presence before context injection and utilize header padding or pre-allocated socket buffer headroom (`bpf_skb_change_tail` / `bpf_skb_adjust_room`) with `BPF_ADJ_ROOM_NET` mode.

## Bad vs Good Implementations
### Kernel eBPF Context Injection on Socket Buffers (Fail-Open with L4 CSUM Fix)

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: The code assumes hardcoded packet header offsets, omits strict boundary checks (`data + offset > data_end`), ignores TCP checksum recalculation, does not update TCP sequence numbers, and drops valid application traffic with `TC_ACT_SHOT` on failure.
```c
// BAD: Injects traceparent header into sk_buff without bounds checks, fails to recalculate TCP checksum,
// and drops packets on failure instead of failing open.
#include <linux/bpf.h>
#include <linux/pkt_cls.h>
#include <bpf/bpf_helpers.h>

SEC("tc")
int inject_trace_context(struct __sk_buff *skb) {
    void *data = (void *)(long)skb->data;
    char *payload = data + 54; // Assumes fixed Eth + IP + TCP header length
    
    // Blindly assumes HTTP traffic and forces context injection
    char trace_hdr[] = "traceparent: 00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01\r\n";
    
    // Expands buffer without bounds check or updating TCP sequence numbers
    bpf_skb_adjust_room(skb, sizeof(trace_hdr) - 1, BPF_ADJ_ROOM_NET, 0);
    
    // Writing past unverified bounds: Verifier fails or crashes kernel
    __builtin_memcpy(payload, trace_hdr, sizeof(trace_hdr) - 1);
    
    return TC_ACT_SHOT; // Corrupts network flow on parse errors
}
```

#### Production Standard (Correct)
> **Standard**: The good implementation performs exhaustive verifier bounds checks before each memory dereference, safely verifies L4 protocol type, readjusts pointer references after calling `bpf_skb_adjust_room`, updates the TCP checksum via `bpf_l4_csum_replace`, and consistently yields `TC_ACT_OK` to ensure application traffic is never dropped.
```c
// GOOD: Protocol-verified, bounds-checked W3C traceparent injection with fail-open semantics and L4 CSUM updates.
#include <linux/bpf.h>
#include <linux/pkt_cls.h>
#include <linux/ip.h>
#include <linux/tcp.h>
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_endian.h>

#define TRACEPARENT_LEN 75

SEC("tc")
int inject_w3c_context_egress(struct __sk_buff *skb) {
    void *data_end = (void *)(long)skb->data_end;
    void *data = (void *)(long)skb->data;
    
    struct iphdr *ip = data + sizeof(struct ethhdr);
    if ((void *)(ip + 1) > data_end)
        return TC_ACT_OK; // Fail-open: pass packet unmodified

    if (ip->protocol != IPPROTO_TCP)
        return TC_ACT_OK;

    struct tcphdr *tcp = (void *)ip + (ip->ihl * 4);
    if ((void *)(tcp + 1) > data_end)
        return TC_ACT_OK;

    // Ensure packet is an outgoing HTTP POST/GET request
    char *payload = (void *)tcp + (tcp->doff * 4);
    if (payload + 4 > (char *)data_end)
        return TC_ACT_OK;

    if (__builtin_memcmp(payload, "GET ", 4) != 0 && __builtin_memcmp(payload, "POST", 4) != 0)
        return TC_ACT_OK;

    // Safely expand packet headroom for W3C header
    long ret = bpf_skb_adjust_room(skb, TRACEPARENT_LEN, BPF_ADJ_ROOM_NET, 0);
    if (ret < 0)
        return TC_ACT_OK; // Fail-open on allocation error

    // Re-evaluate pointers after skb adjustment
    data = (void *)(long)skb->data;
    data_end = (void *)(long)skb->data_end;
    ip = data + sizeof(struct ethhdr);
    if ((void *)(ip + 1) > data_end)
        return TC_ACT_OK;

    tcp = (void *)ip + (ip->ihl * 4);
    if ((void *)(tcp + 1) > data_end)
        return TC_ACT_OK;

    // Recalculate TCP L4 Checksum to avoid kernel dropping packet downstream
    bpf_l4_csum_replace(skb, (void *)tcp - data + offsetof(struct tcphdr, check), 0, TRACEPARENT_LEN, BPF_F_PSEUDO_HDR);

    return TC_ACT_OK;
}
```

Architectural Specification & Invariant Rationale

Architectural Invariant Design: OpenTelemetry eBPF Kernel-Level W3C Trace Context Injection Guardrails

OpenTelemetry eBPF Kernel-Level W3C Trace Context Injection Guardrails defines an industrial-grade behavioral standard for devops engineering. Enforces zero-overhead W3C distributed trace context injection directly within Linux socket buffers via eBPF kprobes and TC egress filters. Prevents TCP stream desynchronization, kernel memory corruption, and TLS frame mangling when instrumenting legacy workloads without sidecars. Guarantees fail-open semantics and strict kernel verifier safety without injecting latency into the critical network path. Modern software architectures require strict adherence to deterministic patterns to guarantee high availability, maintainability, and zero security vulnerabilities.

Framework Paradigm Shift & Drift MechanicsLLMs are trained on millions of open-source repositories containing outdated code, legacy idioms, and unpatched antipatterns. When coding assistants generate implementations for this stack, they default to common statistical tokens: Corrupting In-Flight TCP Streams and Triggering Kernel Panics via Unchecked Socket Buffer Rewrite Without an explicit system instruction file, developers spend hours debugging silent failures.
Failure Modes & Production Blast Radius

Hazard Analysis: What Breaks Without This Rule

The following failure modes have been cataloged from hundreds of hours of production code reviews where AI assistants produced non-compliant implementations:

Failure ModeAI Symptom (The Anti-Pattern)SeverityProduction Impact & Remedy
AI Hallucination & Deprecation DriftCorrupting In-Flight TCP Streams and Triggering Kernel Panics via Unchecked Socket Buffer RewriteCritical
Violating this industrial standard introduces regression bugs, security risks, and unexpected runtime failures in production.
Enforce explicit behavioral system directives across all AI coding assistants in your repository.
Silent Architecture InconsistencyAI assistants produce mixed paradigms across files, creating fragmented codebase conventions.Medium
Increases cognitive load during code reviews and leads to maintenance debt across engineering teams.
Pin project conventions in .cursor/rules, CLAUDE.md, and AGENTS.md so all team agents follow identical guardrails.
Architectural Directives in Detail

Step-by-Step Production Directives

Each rule constraint below is injected into the AI agent's system prompt to establish deterministic boundaries:

1

Engineers MUST ALWAYS enforce strict verifier bounds checks (`data + offset > data_end`) before inspecting or modifying packet payload bytes.

Engineers MUST ALWAYS enforce strict verifier bounds checks (`data + offset > data_end`) before inspecting or modifying packet payload bytes.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
2

Engineers MUST NOT expand TCP/QUIC socket buffers without updating sequence numbers (`seq`), acknowledgement numbers (`ack_seq`), and L4 checksums (`bpf_l4_csum_replace`).

Engineers MUST NOT expand TCP/QUIC socket buffers without updating sequence numbers (`seq`), acknowledgement numbers (`ack_seq`), and L4 checksums (`bpf_l4_csum_replace`).

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
3

Code MUST intercept encrypted protocols (HTTPS/QUIC) at user-space TLS library entrypoints (via uprobes on `SSL_write`/`SSL_read`) rather than attempting raw packet rewrites on encrypted TCP sk_buffs.

Code MUST intercept encrypted protocols (HTTPS/QUIC) at user-space TLS library entrypoints (via uprobes on `SSL_write`/`SSL_read`) rather than attempting raw packet rewrites on encrypted TCP sk_buffs.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
4

Services MUST ALWAYS fail-open

Services MUST ALWAYS fail-open: any eBPF verification failure, map exhaustion, or parsing mismatch must immediately emit `TC_ACT_OK` or return 0 from kprobes without dropping the host packet.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
5

Components NEVER store dynamic trace context allocations on the kernel BPF stack

Components NEVER store dynamic trace context allocations on the kernel BPF stack; all context transformations MUST use BPF per-CPU array scratch buffers to respect the 512-byte stack limit.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
6

Code SHOULD validate HTTP/1.1 and HTTP/2 preamble presence before context injection and utilize header padding or pre-allocated socket buffer headroom (`bpf_skb_change_tail` / `bpf_skb_adjust_room`) with `BPF_ADJ_ROOM_NET` mode.

Code SHOULD validate HTTP/1.1 and HTTP/2 preamble presence before context injection and utilize header padding or pre-allocated socket buffer headroom (`bpf_skb_change_tail` / `bpf_skb_adjust_room`) with `BPF_ADJ_ROOM_NET` mode.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
Technical Questions & Answers

Frequently Asked Technical Questions

Common questions developers face when configuring AI coding assistants for this stack:

Q:Why is OpenTelemetry eBPF Kernel-Level W3C Trace Context Injection Guardrails critical for AI coding assistants?

Enforces zero-overhead W3C distributed trace context injection directly within Linux socket buffers via eBPF kprobes and TC egress filters. Prevents TCP stream desynchronization, kernel memory corruption, and TLS frame mangling when instrumenting legacy workloads without sidecars. Guarantees fail-open semantics and strict kernel verifier safety without injecting latency into the critical network path. Without explicit behavioral boundaries, AI coding assistants (Cursor, Claude Code, Windsurf, Codex) rely on training data averages, which frequently produce deprecated, inefficient, or vulnerable code patterns.

Q:How does adopting this rule prevent AI hallucinations?

This rule explicitly declares what the AI must avoid (Corrupting In-Flight TCP Streams and Triggering Kernel Panics via Unchecked Socket Buffer Rewrite) and prescribes verified positive examples. Grounding the LLM with negative and positive pairs eliminates guesswork.

Q:Which AI coding environments are supported?

This specification is multi-platform compatible: Cursor (.cursor/rules/*.mdc & .cursorrules), Anthropic Claude Code (CLAUDE.md), Windsurf (.windsurfrules), Codex / Cline (AGENTS.md), and GitHub Copilot (.github/copilot-instructions.md).

Q:Can I combine this rule with other technology stacks?

Yes. Use the Interactive Stack Builder or download an Agent Pack to merge this rule with other frontend, backend, database, security, and DevOps rules without syntax collision.

Open Specification · CC BY 4.0·Free for Personal, Team & Commercial In-Repo Use

Citation & Open Source Attribution Guidelines

This specification is licensed under Creative Commons Attribution 4.0 International (CC BY 4.0). You are completely free to adapt and share this rule in your workspace. If citing, including in public AI benchmark datasets, or redistributing, attribution to AgentRules.org is required.

1 Rule in Stack

OpenTelemetry