FPM/FastCGI: FCGI_HASH_FUNC reads var[3] for 3-character names (guarded only by var_len < 3)

Open Beginner friendly
#26,069 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

@Harp404 is already working on this.

Since Dec 5, 2026.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
72/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
c, php
Domain
backend

Research direction

Read the FCGI_HASH_FUNC definition in main/fastcgi.h and the FastCGI parameter parsing call in main/fastcgi.c near line 1019. Check how the hash function handles names of length three, then review the existing FastCGI tests for a suitable place to cover that case. Done means the hash no longer reads beyond a three-character name and the relevant tests pass.

Written by the indexing model from the issue text.

Description

Description

main/fastcgi.h:

#define FCGI_HASH_FUNC(var, var_len) \
    (UNEXPECTED(var_len < 3) ? (unsigned int)var_len : \
        (((unsigned int)var[3]) << 2) + \
        (((unsigned int)var[var_len-2]) << 4) + \
        (((unsigned int)var[var_len-1]) << 2) + \
        var_len)

The guard is var_len < 3, but the body reads var[3]. For a name of length exactly 3, the valid indices are 0..2, so var[3] is one byte past the name.

In FastCGI param parsing (main/fastcgi.c:1019) name and value are contiguous in the param buffer:

fcgi_hash_set(&req->env, FCGI_HASH_FUNC(p, name_len), (char*)p, name_len, (char*)p + name_len, val_len);

so for a 3-char name var[3] reads the first byte of the value (or the record terminator) — inside the allocation. A true 1-byte out-of-bounds read is only conceivable for a crafted record whose 3-char name sits exactly at the end of the content buffer with an empty value; even then the byte is only fed into a hash and never returned, and the whole surface is behind the FastCGI socket.

So this is a minor correctness nit / optional hardening, not a security issue. Confirmed by source on PHP-8.6 and PHP-8.3 (main/fastcgi.h).

Suggested fix

Guard the var[3] access — e.g. change the condition to var_len < 4 so that 3-char names take the (unsigned int)var_len fast path. This only changes internal hash values (recomputed each run) and removes the past-the-name read.

PHP Version

PHP-8.6 and PHP-8.3 branch heads (confirmed by source).

Operating System

Linux

Dominant language
C
Stars
41.9k
Forks
8.5k
Avg merge
2d 16h
Merged PRs (30d)
117

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from php/php-src

All issues in php/php-src

Similar issues

More C issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.