FPM/FastCGI: FCGI_HASH_FUNC reads var[3] for 3-character names (guarded only by var_len < 3)
Maintainers usually reply within 1 day
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 72/100
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
- No Dockerfile or Docker Compose file
- No pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from php/php-src
-
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
php/php-src#26067 · 4 comments ·
Maintainers usually reply within 1 day
-
FPM: fpm_request_end() releases the scoreboard writer lock without acquiring it (8.6 regression)Open
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
Maintainers usually reply within 1 day
-
Bug Status: Verified
Difficulty 1/5 Under an hour Newbie friendliness 74/100
Maintainers usually reply within 1 day
-
getimagesize(): WebP VP8 dimensions are accepted with an invalid start codePossibly taken @lazerg claimed this 5 days ago. OpenBug Status: Needs Triage
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Maintainers usually reply within 1 day
Similar issues
-
CF_CFDP_S_SendFileData computes the file checksum from the SB buffer after CFE_SB_TransmitBufferOpen
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
-
needs triage
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
389ds/389-ds-base#8251 ·
Maintainers usually reply within 1 day
-
bug data-types
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
ClickHouse/clickhouse-cpp#645 ·
Maintainers usually reply within 1 day
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 85/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
ROCm/hrx-system#3628 ·
Maintainers usually reply within 1 day