This specification is maintained by the AgentRules Working Group. Have enhancements or want to claim maintainership?Submit Specification →
securitycritical PriorityID: bpf-lsm-inode-execution-gatekeeper-standard

BPF LSM Inode Execution Gatekeeper & VFS Boundary Standard

Mandates authoritative VFS kernel-space mediation using BPF LSM hooks and immutable inode metadata matching rather than mutable string-based dentry heuristics. Enforces zero-trust execution boundaries by validating container mount namespace IDs, filesystem device numbers, and immutable inode identifiers prior to binary interpreter and ELF payload dispatch.

Prohibited Anti-Pattern: TOCTOU Path Filtering and Deferred Execution Checks in BPF LSM Gatekeepers

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: BPF LSM Inode Execution Gatekeeper & VFS Boundary Standard - Mandates authoritative VFS kernel-space mediation using BPF LSM hooks and immutable inode metadata matching rather than mutable string-based dentry heuristics. Enforces zero-trust execution boundaries by validating container mount namespace IDs, filesystem device numbers, and immutable inode identifiers prior to binary interpreter and ELF payload dispatch.
globs: **/*.bpf.c, **/*.bpf.h, pkg/bpf/**/*.go, src/bpf/**/*.rs
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/bpf-lsm-inode-execution-gatekeeper-standard"
---

# BPF LSM Inode Execution Gatekeeper & VFS Boundary Standard

> **Severity**: CRITICAL | **Category**: security
> **Anti-Pattern Warning**: TOCTOU Path Filtering and Deferred Execution Checks in BPF LSM Gatekeepers

## System Constraints
You are a Principal Security Engineer enforcing kernel-level execution boundaries via BPF LSM. Mandate that all container boundary enforcement and binary execution checks resolve immutable inode and mount namespace primitives directly inside the LSM hook before dispatch, returning deterministic POSIX error codes upon boundary violations.

## Strict Guidelines
- Engineers MUST ALWAYS hook authoritative LSM operations such as `lsm/bprm_check_security` and `lsm/file_open` to enforce fail-closed barriers prior to binary execution or file evaluation.
- Engineers MUST NOT rely on mutable path strings, raw dentry names, or userspace-supplied file paths subject to rename races, symlink pivoting, and TOCTOU vulnerabilities.
- Engineers MUST validate binary execution by cross-referencing task mount namespace identifiers (`task->nsproxy->mnt_ns->ns.inum`) with underlying filesystem superblock devices (`inode->i_sb->s_dev`) and inode numbers (`inode->i_ino`).
- Code SHOULD utilize BPF inner maps (such as `BPF_MAP_TYPE_HASH` or `BPF_MAP_TYPE_LPM_TRIE`) keyed by namespace and inode tuples to achieve constant-time policy lookups inside the hot path.
- Services MUST return negative standard POSIX error codes (e.g., `-EPERM` or `-EACCES`) directly from the BPF LSM hook to terminate execution before payload invocation without relying on userspace intervention.
- Components NEVER permit execution based on process names (`comm`) or unverified thread metadata which can be modified by unprivileged child tasks.

## Bad vs Good Implementations
### Kernel LSM Hook: Inode & Namespace Boundary Enforcement

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Evaluating mutable path strings (`bprm->filename`) and process names (`comm`) introduces Time-of-Check to Time-of-Use (TOCTOU) race conditions through symlinks or mount pivots, and fails to check the container mount namespace boundary.
```c
#include "vmlinux.h"
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_core_read.h>

SEC("lsm/bprm_check_security")
int BPF_PROG(restrict_execution, struct linux_binprm *bprm)
{
    // Flawed: Inspecting task comm or raw path string is vulnerable to TOCTOU and spoofing
    char comm[16];
    bpf_get_current_comm(&comm, sizeof(comm));

    char filename[128];
    BPF_CORE_READ_STR_INTO(&filename, bprm, filename);

    // Vulnerable string check: Path can be manipulated via mount binds or symlinks
    if (filename[0] == '/' && filename[1] == 't' && filename[2] == 'm' && filename[3] == 'p') {
        return -1; // Non-standard return code
    }

    return 0; // Deferred checks allow unvetted binaries to invoke interpreters
}
```

#### Production Standard (Correct)
> **Standard**: Hooks `lsm/bprm_check_security` directly at the VFS layer, querying immutable inode numbers, superblock device identifiers, and container mount namespace IDs. Execution is rejected immediately with `-EPERM` if the tuple does not exist in the policy map.
```c
#include "vmlinux.h"
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_core_read.h>
#include <bpf/bpf_tracing.h>

#define EPERM 1

struct exec_key {
    __u32 mnt_ns_id;
    __u64 dev;
    __u64 ino;
};

struct {
    __uint(type, BPF_MAP_TYPE_HASH);
    __uint(max_entries, 10240);
    __type(key, struct exec_key);
    __type(value, __u8);
} allowed_binaries SEC(".maps");

SEC("lsm/bprm_check_security")
int BPF_PROG(enforce_binary_gatekeeper, struct linux_binprm *bprm)
{
    struct task_struct *current_task = (struct task_struct *)bpf_get_current_task();
    if (!current_task || !bprm || !bprm->file) {
        return -EPERM;
    }

    // Authoritative namespace resolution from task proxy
    __u32 mnt_ns = BPF_CORE_READ(current_task, nsproxy, mnt_ns, ns.inum);
    
    // Resolve immutable inode and superblock device primitives
    struct inode *inode = BPF_CORE_READ(bprm, file, f_inode);
    if (!inode) {
        return -EPERM;
    }

    struct exec_key key = {
        .mnt_ns_id = mnt_ns,
        .dev = (BPF_CORE_READ(inode, i_sb, s_dev)),
        .ino = (BPF_CORE_READ(inode, i_ino)),
    };

    __u8 *allowed = bpf_map_lookup_elem(&allowed_binaries, &key);
    if (!allowed || *allowed != 1) {
        // Deterministically deny execution before payload setup
        return -EPERM;
    }

    return 0;
}

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

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: BPF LSM Inode Execution Gatekeeper & VFS Boundary Standard

BPF LSM Inode Execution Gatekeeper & VFS Boundary Standard defines an industrial-grade behavioral standard for security engineering. Mandates authoritative VFS kernel-space mediation using BPF LSM hooks and immutable inode metadata matching rather than mutable string-based dentry heuristics. Enforces zero-trust execution boundaries by validating container mount namespace IDs, filesystem device numbers, and immutable inode identifiers prior to binary interpreter and ELF payload dispatch. 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: TOCTOU Path Filtering and Deferred Execution Checks in BPF LSM Gatekeepers 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 DriftTOCTOU Path Filtering and Deferred Execution Checks in BPF LSM GatekeepersCritical
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 hook authoritative LSM operations such as `lsm/bprm_check_security` and `lsm/file_open` to enforce fail-closed barriers prior to binary execution or file evaluation.

Engineers MUST ALWAYS hook authoritative LSM operations such as `lsm/bprm_check_security` and `lsm/file_open` to enforce fail-closed barriers prior to binary execution or file evaluation.

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 rely on mutable path strings, raw dentry names, or userspace-supplied file paths subject to rename races, symlink pivoting, and TOCTOU vulnerabilities.

Engineers MUST NOT rely on mutable path strings, raw dentry names, or userspace-supplied file paths subject to rename races, symlink pivoting, and TOCTOU vulnerabilities.

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

Engineers MUST validate binary execution by cross-referencing task mount namespace identifiers (`task->nsproxy->mnt_ns->ns.inum`) with underlying filesystem superblock devices (`inode->i_sb->s_dev`) and inode numbers (`inode->i_ino`).

Engineers MUST validate binary execution by cross-referencing task mount namespace identifiers (`task->nsproxy->mnt_ns->ns.inum`) with underlying filesystem superblock devices (`inode->i_sb->s_dev`) and inode numbers (`inode->i_ino`).

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

Code SHOULD utilize BPF inner maps (such as `BPF_MAP_TYPE_HASH` or `BPF_MAP_TYPE_LPM_TRIE`) keyed by namespace and inode tuples to achieve constant-time policy lookups inside the hot path.

Code SHOULD utilize BPF inner maps (such as `BPF_MAP_TYPE_HASH` or `BPF_MAP_TYPE_LPM_TRIE`) keyed by namespace and inode tuples to achieve constant-time policy lookups inside the hot path.

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

Services MUST return negative standard POSIX error codes (e.g., `-EPERM` or `-EACCES`) directly from the BPF LSM hook to terminate execution before payload invocation without relying on userspace intervention.

Services MUST return negative standard POSIX error codes (e.g., `-EPERM` or `-EACCES`) directly from the BPF LSM hook to terminate execution before payload invocation without relying on userspace intervention.

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

Components NEVER permit execution based on process names (`comm`) or unverified thread metadata which can be modified by unprivileged child tasks.

Components NEVER permit execution based on process names (`comm`) or unverified thread metadata which can be modified by unprivileged child tasks.

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 BPF LSM Inode Execution Gatekeeper & VFS Boundary Standard critical for AI coding assistants?

Mandates authoritative VFS kernel-space mediation using BPF LSM hooks and immutable inode metadata matching rather than mutable string-based dentry heuristics. Enforces zero-trust execution boundaries by validating container mount namespace IDs, filesystem device numbers, and immutable inode identifiers prior to binary interpreter and ELF payload dispatch. 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 (TOCTOU Path Filtering and Deferred Execution Checks in BPF LSM Gatekeepers) 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

BPF