What PHP's Session Handler Teaches Us About Atomicity
There is a comforting line of reasoning that goes like this: if a process locks a file before updating it, another process cannot interfere, so the update is safe.
That reasoning is only partly true.
A lock can answer who gets to write. It cannot answer what the file looks like if the writer stops halfway through.
That distinction sits at the centre of a recent PHP security vulnerability, CVE-2026-126714. It affects PHP’s default, file-based session handler and turns an interrupted session write into something much more serious than a lost login state.
The small assumption behind the big problem
PHP sessions are commonly stored as files. When a session changes, PHP serializes the session data and writes the result back to its session file.
The file-based handler also uses a lock. In simplified PHP code, that looks familiar enough:
$handle = fopen($path, 'c+');
flock($handle, LOCK_EX);
fwrite($handle, $serializedSession);
fflush($handle);
flock($handle, LOCK_UN);
The lock is useful. It prevents another session operation from entering the same critical section at the same time.
But fwrite() is still an operation that can be interrupted. A process can run out of memory, be terminated, or otherwise stop after only part of the new value has reached the file.
If the file was not cleared first, the result can look like so.
Before:
[old session data.........................]
After a partial overwrite:
[new session data........][old session suffix]
The lock has done its job. There were no competing writers. The file is still corrupted.
Locking is not atomicity
This is the useful distinction:
- Locking controls concurrent access. It says that one operation gets exclusive access while it is running.
- Atomicity controls visibility of an operation. It says that readers see either the old value or the new value, not an observable halfway state.
- Crash consistency asks what remains after the writer fails during the operation.
These guarantees overlap, but they are not interchangeable. A lock can provide mutual exclusion without making a multi-step update atomic.
That is why “the file is locked” is not the same as “the file can never be left in a partial state”.
Why a damaged session file can become code execution
The PHP advisory describes a particularly unpleasant consequence of this partial overwrite. PHP’s session decoder uses its unserialization machinery to restore session values. A carefully arranged combination of a new prefix and an old suffix can be interpreted differently from either session that produced it.
In other words, bytes that belonged to an ordinary string in the old session can become serialized object data once they are joined to the new prefix. When a later session_start() reads the file, PHP may create those objects and run their deserialization behaviour.
That does not mean every affected installation is instantly exploitable. The advisory lists several conditions: an attacker must be able to influence session data, interrupt a write at a useful point, and find a suitable object-gadget chain in the application. The result, when those conditions line up, can be remote code execution with the privileges of the PHP worker.
Session upload progress provides one way to influence session data through a multipart upload, although the underlying bug is not limited to that feature. An application that independently stores large, attacker-controlled values in a session can still encounter the same file-write defect.
What PHP changed
The fix is small, but the guarantee it restores is important. In the PHP commit, the session handler changed from truncating only when the new value was shorter than the existing file to truncating the file before every replacement write:
if (ftruncate(data->fd, 0) != 0) {
php_error_docref(NULL, E_WARNING,
"ftruncate() failed: %s (%d)", strerror(errno), errno);
return FAILURE;
}
The old code ignored a truncation failure and only truncated when the new serialized session was smaller. The new code always clears the file first and aborts if that cannot be done.
This means an interrupted write can still lose session data. It can still leave a truncated file. What it can no longer do is preserve the dangerous suffix of the previous session behind the new prefix.
That is a targeted fix for the actual failure mode—not a claim that file writes are magically durable under every kind of failure.
The versions to upgrade to
The PHP advisory lists these fixed releases:
- PHP 8.2.36
- PHP 8.3.37
- PHP 8.4.28
- PHP 8.5.13
The corresponding PHP 8 changelog records CVE-2026-126714 under the Session changes for the security releases published on November 19, 2026.
If you use PHP’s built-in files session handler, upgrading is the recommended response. The advisory also suggests disabling session.upload_progress.enabled as a temporary measure when it is not needed, but explicitly notes that this removes one input path; it does not repair applications that put attacker-controlled data into sessions themselves.
The lesson beyond PHP
This is not really a story about one missing ftruncate() call. It is a reminder to name the guarantee a piece of code actually provides.
When writing persistent state, ask separate questions:
- Can two writers enter at the same time?
- Can a reader observe a partial value?
- What happens if the writer exits after step one but before step two?
- What survives a process crash or power loss?
For some workloads, writing a temporary file and replacing the destination with a rename is a better model. For others, a database or a storage backend with transactional updates is the right answer. The details depend on the filesystem and the durability guarantees you need.
The important part is not to let a lock stand in for all of those decisions.
PHP’s session handler did use a lock. The vulnerability existed because the write was still not atomic when the process performing it disappeared.
That is the broader lesson here: a lock controls who can access an update, while atomicity determines what happens when the update is interrupted.
👋 Hi there! This is Amit, again. I write articles about all things web development. If you enjoy my work (the articles, the open-source projects, my general demeanour... anything really), consider leaving a tip & supporting the site. Your support is incredibly appreciated!