This specification is maintained by the AgentRules Working Group. Have enhancements or want to claim maintainership?Submit Specification →
securitycritical PriorityID: ebpf-sockops-zero-trust-microsegmentation

Kernel-Enforced Sockops L7 Cryptographic Identity Verification

Enforces cryptographic service identity attestation directly within kernel socket operations (BPF_PROG_TYPE_SOCK_OPS and BPF_PROG_TYPE_SK_MSG) before payload traversal to userspace. Prohibits routing packets through sockhash or sockmap based solely on mutable L3/L4 5-tuples without verifying mutual kernel-attested SPIFFE IDs or pinned cryptographic session tokens.

Prohibited Anti-Pattern: L3/L4 Socket Map Injection Bypassing Kernel-Level Cryptographic L7 Identity Attestation

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: Kernel-Enforced Sockops L7 Cryptographic Identity Verification - Enforces cryptographic service identity attestation directly within kernel socket operations (BPF_PROG_TYPE_SOCK_OPS and BPF_PROG_TYPE_SK_MSG) before payload traversal to userspace. Prohibits routing packets through sockhash or sockmap based solely on mutable L3/L4 5-tuples without verifying mutual kernel-attested SPIFFE IDs or pinned cryptographic session tokens.
globs: bpf/**/*.bpf.c, bpf/**/*.h, pkg/ebpf/**/*.go, src/security/ebpf/**/*.c
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/ebpf-sockops-zero-trust-microsegmentation"
---

# Kernel-Enforced Sockops L7 Cryptographic Identity Verification

> **Severity**: CRITICAL | **Category**: security
> **Anti-Pattern Warning**: L3/L4 Socket Map Injection Bypassing Kernel-Level Cryptographic L7 Identity Attestation

## System Constraints
You are a Principal Systems and Security Architect specializing in eBPF kernel programming and zero-trust microsegmentation. Mandate strict cryptographic attestation in sockops and sk_msg programs to halt lateral traversal before bytes reach userspace.

## Strict Guidelines
- Engineers MUST ALWAYS validate SPIFFE identity digests and cryptographic session state in BPF maps before updating BPF_MAP_TYPE_SOCKHASH or BPF_MAP_TYPE_SOCKMAP.
- Engineers MUST NOT establish socket bypass links via bpf_sock_hash_update or bpf_msg_redirect_hash relying strictly on IP/Port 5-tuples.
- Code MUST explicitly drop connections and return SK_DROP or BPF_SOCK_OPS_RST when peer cryptographic identity tokens are missing, expired, or non-attested.
- Services MUST enforce mutual socket state machine verification at both BPF_SOCK_OPS_ACTIVE_ESTABLISHED_CB and BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB events.
- Components NEVER bridge raw TCP streams into userspace application memory buffers without verifying that the socket's security context was validated by the host zero-trust daemon.

## Bad vs Good Implementations
### eBPF Sockops Identity Validation vs 5-Tuple Blind Redirect

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: The sockops hook inserts sockets directly into sock_map based strictly on mutable L3/L4 5-tuples. An unauthorized local workload or compromised container with network namespace access can reuse or spoof these IPs/ports, completely bypassing L7 policy enforcement before userspace inspection.
```c
#include <linux/bpf.h>
#include <bpf/bpf_helpers.h>

struct {
    __uint(type, BPF_MAP_TYPE_SOCKHASH);
    __type(key, struct sock_key);
    __type(value, __u64);
    __uint(max_entries, 65535);
} sock_map SEC(".maps");

SEC("sockops")
int bpf_sockmap_intercept(struct bpf_sock_ops *skops) {
    if (skops->op == BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB ||
        skops->op == BPF_SOCK_OPS_ACTIVE_ESTABLISHED_CB) {
        struct sock_key key = {
            .sip4 = skops->local_ip4,
            .dip4 = skops->remote_ip4,
            .sport = skops->local_port,
            .dport = bpf_ntohl(skops->remote_port),
        };
        // VULNERABILITY: Blind socket forwarding based solely on IP/Port.
        // Attacker with IP spoofing or pod IP churn can bypass L7 policy.
        bpf_sock_hash_update(skops, &sock_map, &key, BPF_ANY);
    }
    return 0;
}
```

#### Production Standard (Correct)
> **Standard**: The eBPF program verifies the attested cryptographically derived security identity pair before registering the socket in the fast-path sockhash. Unauthorized or unauthenticated lateral connections are prevented from entering the kernel redirection table.
```c
#include <linux/bpf.h>
#include <bpf/bpf_helpers.h>

struct identity_key {
    __u32 security_id;
    __u32 peer_security_id;
};

struct {
    __uint(type, BPF_MAP_TYPE_HASH);
    __type(key, struct identity_key);
    __type(value, __u8); // 1 = authorized, 0 = revoked
    __uint(max_entries, 16384);
} identity_policy_map SEC(".maps");

struct {
    __uint(type, BPF_MAP_TYPE_SOCKHASH);
    __type(key, struct sock_key);
    __type(value, __u64);
    __uint(max_entries, 65535);
} authorized_sock_map SEC(".maps");

SEC("sockops")
int bpf_sockmap_intercept(struct bpf_sock_ops *skops) {
    if (skops->op != BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB &&
        skops->op != BPF_SOCK_OPS_ACTIVE_ESTABLISHED_CB) {
        return 0;
    }

    struct identity_key id = {
        .security_id = skops->sk ? skops->sk->sk_priority : 0,
        .peer_security_id = skops->sk ? skops->sk->sk_mark : 0,
    };

    __u8 *is_authorized = bpf_map_lookup_elem(&identity_policy_map, &id);
    if (!is_authorized || *is_authorized != 1) {
        // Enforce kernel-level drop before userspace protocol parse
        return 1; // Terminate / suppress sockmap fast-path
    }

    struct sock_key key = {
        .sip4 = skops->local_ip4,
        .dip4 = skops->remote_ip4,
        .sport = skops->local_port,
        .dport = bpf_ntohl(skops->remote_port),
    };

    bpf_sock_hash_update(skops, &authorized_sock_map, &key, BPF_NOEXIST);
    return 0;
}
```

Recommended MCP Servers (mcp-serverlist.com)

Equip agent with system tools to verify code for this stack.

Recommended Agent Skills (askills.dev)

Ready-to-use executable skill functions tailored to this stack.

Architectural Specification & Invariant Rationale

Architectural Invariant Design: Kernel-Enforced Sockops L7 Cryptographic Identity Verification

Kernel-Enforced Sockops L7 Cryptographic Identity Verification defines an industrial-grade behavioral standard for security engineering. Enforces cryptographic service identity attestation directly within kernel socket operations (BPF_PROG_TYPE_SOCK_OPS and BPF_PROG_TYPE_SK_MSG) before payload traversal to userspace. Prohibits routing packets through sockhash or sockmap based solely on mutable L3/L4 5-tuples without verifying mutual kernel-attested SPIFFE IDs or pinned cryptographic session tokens. 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: L3/L4 Socket Map Injection Bypassing Kernel-Level Cryptographic L7 Identity Attestation 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 DriftL3/L4 Socket Map Injection Bypassing Kernel-Level Cryptographic L7 Identity AttestationCritical
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 validate SPIFFE identity digests and cryptographic session state in BPF maps before updating BPF_MAP_TYPE_SOCKHASH or BPF_MAP_TYPE_SOCKMAP.

Engineers MUST ALWAYS validate SPIFFE identity digests and cryptographic session state in BPF maps before updating BPF_MAP_TYPE_SOCKHASH or BPF_MAP_TYPE_SOCKMAP.

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 establish socket bypass links via bpf_sock_hash_update or bpf_msg_redirect_hash relying strictly on IP/Port 5-tuples.

Engineers MUST NOT establish socket bypass links via bpf_sock_hash_update or bpf_msg_redirect_hash relying strictly on IP/Port 5-tuples.

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 explicitly drop connections and return SK_DROP or BPF_SOCK_OPS_RST when peer cryptographic identity tokens are missing, expired, or non-attested.

Code MUST explicitly drop connections and return SK_DROP or BPF_SOCK_OPS_RST when peer cryptographic identity tokens are missing, expired, or non-attested.

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 enforce mutual socket state machine verification at both BPF_SOCK_OPS_ACTIVE_ESTABLISHED_CB and BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB events.

Services MUST enforce mutual socket state machine verification at both BPF_SOCK_OPS_ACTIVE_ESTABLISHED_CB and BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB events.

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 bridge raw TCP streams into userspace application memory buffers without verifying that the socket's security context was validated by the host zero-trust daemon.

Components NEVER bridge raw TCP streams into userspace application memory buffers without verifying that the socket's security context was validated by the host zero-trust daemon.

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 Kernel-Enforced Sockops L7 Cryptographic Identity Verification critical for AI coding assistants?

Enforces cryptographic service identity attestation directly within kernel socket operations (BPF_PROG_TYPE_SOCK_OPS and BPF_PROG_TYPE_SK_MSG) before payload traversal to userspace. Prohibits routing packets through sockhash or sockmap based solely on mutable L3/L4 5-tuples without verifying mutual kernel-attested SPIFFE IDs or pinned cryptographic session tokens. 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 (L3/L4 Socket Map Injection Bypassing Kernel-Level Cryptographic L7 Identity Attestation) 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

Kernel-Enforced