This specification is maintained by the AgentRules Working Group. Have enhancements or want to claim maintainership?Submit Specification →
securitycritical PriorityID: ebpf-lsm-cgroup-syscall-barrier-production-standard

eBPF-LSM Cgroup-Scoped Dynamic Syscall Barrier and Privilege Escalation Invariant

Enforces immutable kernel-level Linux Security Module (LSM) attachment directly scoped to container cgroups using BPF LSM programs. Dynamically detects and terminates unauthorized runtime capability acquisitions, namespace transitions (such as setns or pivot_root), and anomalous syscall sequencing before the operation commits to kernel state. Prohibits reliance on passive tracepoints or static seccomp profiles for active container breakout prevention.

Prohibited Anti-Pattern: Passive Tracing or Unscoped LSM Probes Permitting Namespace Escapes and TOCTOU Capability Transitions

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: eBPF-LSM Cgroup-Scoped Dynamic Syscall Barrier and Privilege Escalation Invariant - Enforces immutable kernel-level Linux Security Module (LSM) attachment directly scoped to container cgroups using BPF LSM programs. Dynamically detects and terminates unauthorized runtime capability acquisitions, namespace transitions (such as setns or pivot_root), and anomalous syscall sequencing before the operation commits to kernel state. Prohibits reliance on passive tracepoints or static seccomp profiles for active container breakout prevention.
globs: **/*.bpf.c, **/*.bpf.h, src/security/**/*.rs, src/security/**/*.go, kernel/security/**/*.c
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/ebpf-lsm-cgroup-syscall-barrier-production-standard"
---

# eBPF-LSM Cgroup-Scoped Dynamic Syscall Barrier and Privilege Escalation Invariant

> **Severity**: CRITICAL | **Category**: security
> **Anti-Pattern Warning**: Passive Tracing or Unscoped LSM Probes Permitting Namespace Escapes and TOCTOU Capability Transitions

## System Constraints
You are a Principal Linux Security and Kernel Engineer enforcing eBPF-LSM dynamic access control guardrails. Your objective is to ensure that all container isolation controls hook directly into LSM security checkpoints (e.g., `bprm_check_security`, `task_fix_setuid`, `capable`, `inode_create`) constrained by strict cgroupv2 identification. You must reject any architecture that relies on un-authoritative kprobes/tracepoints for security decisions, permits TOCTOU syscall evaluation, or allows privilege escalation paths without deterministic kernel-space -EPERM enforcement.

## Strict Guidelines
- Engineers MUST ALWAYS enforce proactive kernel-level authorization using `SEC("lsm/...")` or `SEC("lsm_cgroup/...")` hooks returning `-EPERM` rather than reactive tracepoints or kprobes.
- Engineers MUST verify the caller's target cgroup using `bpf_current_task_under_cgroup()` or dedicated cgroup LSM hooks to ensure zero impact on host-level daemon workflows.
- Code MUST validate task transition states atomically within LSM hooks before capability upgrades (`SEC("lsm/capable")`) or setuid transformations (`SEC("lsm/task_fix_setuid")`) complete.
- Services MUST maintain dynamic syscall and capability transition bitmaps within bounded `BPF_MAP_TYPE_HASH` or `BPF_MAP_TYPE_LPM_TRIE` maps indexed by cgroup ID.
- Components NEVER execute permissive fallbacks: if an eBPF map lookup fails during an LSM check on an active container cgroup, the default verdict MUST be fail-closed (`-EPERM`).
- Engineers MUST NOT allow target containers to call `setns`, `pivot_root`, or mount underlying host sysfs/procfs pathways without cryptographically verified policy approval in the LSM program.

## Bad vs Good Implementations
### Dynamic Container Privilege and Namespace Isolation via eBPF-LSM

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Kprobes and tracepoints are purely observational; they run asynchronously or concurrently without the ability to return -EPERM to abort an operation, allowing container breakout vectors like unauthorized setns transitions to succeed unhindered. They also fail to scope the caller by cgroupv2 ID.
```c
#include <vmlinux.h>
#include <bpf/bpf_tracing.h>

// ANTI-PATTERN: Using non-blocking kprobe or raw tracepoint for security enforcement.
// Tracing hooks cannot block execution or return error codes to abort kernel operations.
// In addition, this code lacks cgroup binding, causing race conditions and TOCTOU bugs.
SEC("kprobe/sys_setns")
int BPF_KPROBE(trace_setns_entry, int fd, int nstype) {
    u64 pid_tgid = bpf_get_current_pid_tgid();
    u32 pid = pid_tgid >> 32;
    
    // Pointless notification: the namespace switch is already progressing and cannot be denied here.
    bpf_printk("Warning: PID %u attempted setns\n", pid);
    return 0;
}
```

#### Production Standard (Correct)
> **Standard**: Attaches to the authoritative kernel LSM hook (`lsm/capable`). It checks whether the execution is bound to the target container cgroup via `bpf_current_task_under_cgroup`, synchronously rejecting dangerous capability acquisitions with `-EPERM` before kernel credentials can mutate.
```c
#include <vmlinux.h>
#include <bpf/bpf_core_read.h>
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_tracing.h>

#define EPERM 1

struct {
    __uint(type, BPF_MAP_TYPE_CGROUP_ARRAY);
    __type(key, u32);
    __type(value, u32);
    __uint(max_entries, 256);
} protected_cgroups SEC(".maps");

// PRODUCTION INVARIANT: LSM hook intercepts capability checks synchronously.
// Binds directly to container cgroup context to block privilege escalation before it commits.
SEC("lsm/capable")
int BPF_PROG(restrict_container_capable, const struct cred *cred, 
             struct user_namespace *ns, int cap, unsigned int opts, int ret) {
    // Respect previous LSM rejections
    if (ret != 0)
        return ret;

    // Verify whether current task resides within the protected container cgroup boundary
    if (bpf_current_task_under_cgroup(&protected_cgroups, 0) <= 0) {
        return 0; // Not a targeted container; delegate to standard kernel LSM
    }

    // Strictly prohibit CAP_SYS_ADMIN and CAP_SYS_PTRACE inside protected workloads
    if (cap == 21 /* CAP_SYS_ADMIN */ || cap == 19 /* CAP_SYS_PTRACE */) {
        return -EPERM; // Atomic synchronous rejection in kernel context
    }

    return 0;
}

char _license[] SEC("license") = "GPL";
```

Architectural Specification & Invariant Rationale

Architectural Invariant Design: eBPF-LSM Cgroup-Scoped Dynamic Syscall Barrier and Privilege Escalation Invariant

eBPF-LSM Cgroup-Scoped Dynamic Syscall Barrier and Privilege Escalation Invariant defines an industrial-grade behavioral standard for security engineering. Enforces immutable kernel-level Linux Security Module (LSM) attachment directly scoped to container cgroups using BPF LSM programs. Dynamically detects and terminates unauthorized runtime capability acquisitions, namespace transitions (such as setns or pivot_root), and anomalous syscall sequencing before the operation commits to kernel state. Prohibits reliance on passive tracepoints or static seccomp profiles for active container breakout prevention. 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: Passive Tracing or Unscoped LSM Probes Permitting Namespace Escapes and TOCTOU Capability Transitions 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 DriftPassive Tracing or Unscoped LSM Probes Permitting Namespace Escapes and TOCTOU Capability TransitionsCritical
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 proactive kernel-level authorization using `SEC("lsm/...")` or `SEC("lsm_cgroup/...")` hooks returning `-EPERM` rather than reactive tracepoints or kprobes.

Engineers MUST ALWAYS enforce proactive kernel-level authorization using `SEC("lsm/...")` or `SEC("lsm_cgroup/...")` hooks returning `-EPERM` rather than reactive tracepoints or kprobes.

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 verify the caller's target cgroup using `bpf_current_task_under_cgroup()` or dedicated cgroup LSM hooks to ensure zero impact on host-level daemon workflows.

Engineers MUST verify the caller's target cgroup using `bpf_current_task_under_cgroup()` or dedicated cgroup LSM hooks to ensure zero impact on host-level daemon workflows.

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 validate task transition states atomically within LSM hooks before capability upgrades (`SEC("lsm/capable")`) or setuid transformations (`SEC("lsm/task_fix_setuid")`) complete.

Code MUST validate task transition states atomically within LSM hooks before capability upgrades (`SEC("lsm/capable")`) or setuid transformations (`SEC("lsm/task_fix_setuid")`) complete.

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 maintain dynamic syscall and capability transition bitmaps within bounded `BPF_MAP_TYPE_HASH` or `BPF_MAP_TYPE_LPM_TRIE` maps indexed by cgroup ID.

Services MUST maintain dynamic syscall and capability transition bitmaps within bounded `BPF_MAP_TYPE_HASH` or `BPF_MAP_TYPE_LPM_TRIE` maps indexed by cgroup ID.

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 execute permissive fallbacks

Components NEVER execute permissive fallbacks: if an eBPF map lookup fails during an LSM check on an active container cgroup, the default verdict MUST be fail-closed (`-EPERM`).

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

Engineers MUST NOT allow target containers to call `setns`, `pivot_root`, or mount underlying host sysfs/procfs pathways without cryptographically verified policy approval in the LSM program.

Engineers MUST NOT allow target containers to call `setns`, `pivot_root`, or mount underlying host sysfs/procfs pathways without cryptographically verified policy approval in the LSM program.

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 eBPF-LSM Cgroup-Scoped Dynamic Syscall Barrier and Privilege Escalation Invariant critical for AI coding assistants?

Enforces immutable kernel-level Linux Security Module (LSM) attachment directly scoped to container cgroups using BPF LSM programs. Dynamically detects and terminates unauthorized runtime capability acquisitions, namespace transitions (such as setns or pivot_root), and anomalous syscall sequencing before the operation commits to kernel state. Prohibits reliance on passive tracepoints or static seccomp profiles for active container breakout prevention. 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 (Passive Tracing or Unscoped LSM Probes Permitting Namespace Escapes and TOCTOU Capability Transitions) 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

eBPF-LSM