Mind the Gap: Time-of-Check to Time-of-Use Vulnerabilities in LLM-Enabled Agents

Derek Lilienthal, Sanghyun Hong · DOI

Abstract

As Large Language Model (LLM) agents are increasingly deployed in sensitive domains, they are granted the power to invoke external tools and APIs. This paper presents the first systematic study of Time-of-Check to Time-of-Use (TOCTOU) vulnerabilities in these agentic workflows. We introduce TOCTOU-Bench, a benchmark for evaluating these risks, and propose a multi-layered defense strategy—comprising prompt rewriting, state monitoring, and tool fusion—that can reduce the window of attack by up to 95%.

Introduction

While security research in LLMs has focused heavily on prompt injection and data exfiltration, it has largely overlooked a classic systems security flaw: the TOCTOU vulnerability. In traditional computing, a TOCTOU error occurs when a program checks a condition (e.g., "does this file exist?") but the condition changes before the program uses that resource.

In LLM agents, this "gap" occurs between successive tool calls. Because agents do not execute multi-step plans atomically, an attacker can modify the environment—such as swapping a file or changing a database entry—after the agent has "checked" the state but before it "uses" it. This temporal gap can lead to unauthorized data access, privilege escalation, or unsafe tool execution.

Proposed Defense Framework

To address these vulnerabilities, we propose three distinct defense mechanisms targeting different stages of the agentic workflow: prompt rewriting, State Integrity Monitoring (SIM), and Tool Fusion.

Overview of the proposed defense mechanisms: State Integrity Monitoring (SIM), Tool Fuser, and Prompt Rewriting.

1. Prompt Rewriting

This "sanitization" step modifies the user's original query to discourage vulnerable plan structures. For example, instead of a prompt that implies a sequence like "Check if file X exists, then open it," the prompt is rewritten to emphasize atomic logic: "Open file X only if it exists at the exact time of access." This pushes the agent toward more robust execution paths.

2. State Integrity Monitoring (SIM)

SIM acts as a runtime defense. We utilize GPT-4o to pre-identify pairs of tools that could form a "check-then-use" sequence, encoding these into a Finite State Automaton (FSA). At runtime, the monitor tracks the agent's plan; if it detects a sequence that violates security properties, it halts execution and alerts the user.

3. Tool Fuser

Inspired by atomic operations in operating systems, the Tool Fuser identifies vulnerable tool sequences and "fuses" them into a single, new tool. This new tool executes both the check and the use steps as one atomic operation, effectively eliminating the temporal gap an attacker would need to exploit.

TOCTOU-Bench

To evaluate these risks, we developed TOCTOU-Bench, built upon the AgentDojo framework. The benchmark consists of 66 realistic user tasks across environments like Slack, Banking, and Travel. Through manual inspection of ground-truth tool sequences, we identified that 56 of these 66 tasks (approximately 85%) contain potential TOCTOU vulnerabilities, highlighting the prevalence of this risk in current agent designs.

Results and Analysis

Our experiments demonstrate that the "temporal gap" is a significant threat. Unlike traditional software where race conditions occur in milliseconds, LLM agents often have gaps of several seconds between tool calls due to the time taken for reasoning. This makes them significantly easier to exploit.

Attack Window Reduction

The Tool Fuser proved highly effective, reducing the average time between vulnerable tool calls from 1.70 seconds to just 0.07 seconds—a 95% reduction. When combining all three defenses, the percentage of trajectories containing a TOCTOU vulnerability dropped from 12% to 8%.

Detection Performance

While the defenses showed promise, automated detection remains a challenge. The automated SIM achieved an AUC of 0.62 on baseline sequences, as shown in the ROC analysis below. The modest performance (including a 25% True Positive Rate) suggests that looking at tool descriptions alone is insufficient; monitors must understand the specific intent and parameters of a query to accurately flag risks.

ROC curve comparing the detection performance of the baseline and the proposed State Integrity Monitoring (SIM) approach.

Conclusion and Future Work

This study underscores that atomicity is the gold standard for securing LLM agents. While prompt rewriting and monitoring provide layers of defense, Tool Fusion physically removes the attack surface. Future work will focus on developing context-aware monitoring that better interprets tool parameters and user intent, as well as scaling Tool Fusion to automatically generate safe tools for diverse environments.

Summary updated

BibTeX
@inproceedings{lilienthal2025mind,
  author = {Derek Lilienthal and Sanghyun Hong},
  title = {{Mind the Gap: Time-of-Check to Time-of-Use Vulnerabilities in LLM-Enabled Agents}},
  booktitle = {MLForSys @ NeurIPS},
  year = {2025},
  url = {https://openreview.net/forum?id=RtwZmyXJS8},
}
Figure