Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions kernelguard.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,9 @@
RE_CPP_MARKERS = re.compile(r'#include|__global__|__device__|__host__|extern\s+"C"|asm\s+volatile')
RE_TRIPLE_QUOTED = re.compile(r'(?:r)?(?:"""|\'\'\')(.*?)(?:"""|\'\'\')', re.DOTALL)

# Pattern 16: Generator-based replay attack
RE_GEN_REPLAY = re.compile(r'def\s+\w+\s*\([^)]*\)\s*:[^}]*yield[^}]*next\s*\([^)]*\)\s*[^=]*=\s*\w+\.send')


# ---------------------------------------------------------------------------
# Utility: strip C++/CUDA inline source from Python code
Expand Down Expand Up @@ -1397,6 +1400,26 @@ def detect_token_paste_cuda_api(code: str | SubmissionFacts) -> list[dict]:
}]


def detect_generator_replay(code: str | SubmissionFacts) -> list[dict]:
"""Pattern 16: Generator-based replay attack.

Detects generators whose .send() method is exposed as custom_kernel.
The attack: create generator, prime with next(), assign g.send to custom_kernel.
"""
facts = ensure_submission_facts(code)
raw = facts.raw_code
matches = []

if RE_GEN_REPLAY.search(raw):
matches.append({
"pattern": "GENERATOR_REPLAY",
"severity": "high",
"evidence": "Generator-based replay attack: generator.send() exposed as callable",
})

return matches


# Sequence-aware batch graph exploit: learns repeated call sequences
# and builds a batch CUDA graph that launches all calls at once.
RE_CALL_SEQUENCE = re.compile(r'\b_?call_sequence\b|\b_?known_seq\b|\bbatch_graph\b|\bbatch_handle\b')
Expand Down Expand Up @@ -3773,6 +3796,7 @@ def support_only_patterns(matched_patterns: list[dict]) -> bool:
detect_unsync_multistream,
detect_cuda_event_disable_timing,
detect_token_paste_cuda_api,
detect_generator_replay,
detect_sequence_batch_graph,
detect_runtime_package_install,
# AST-based detectors (Layer 2)
Expand Down Expand Up @@ -3811,6 +3835,7 @@ def support_only_patterns(matched_patterns: list[dict]) -> bool:
("unsync_multistream", detect_unsync_multistream),
("cuda_event_disable_timing", detect_cuda_event_disable_timing),
("token_paste_cuda_api", detect_token_paste_cuda_api),
("generator_replay", detect_generator_replay),
("sequence_batch_graph", detect_sequence_batch_graph),
("runtime_package_install", detect_runtime_package_install),
("trusted_module_import", detect_trusted_module_import),
Expand Down