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

未關閉 適合新手
#26,069 0 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

維護者通常 1 天內回覆

@Harp404 已經在處理了。

開始於 2026年12月5日。

  • #26080 來自 @Harp404 —— 未關閉

評估

難度
2/5
預估耗時
1-3 小時
新手友好度
72/100
Issue 類型
缺陷
描述清晰度
描述清楚
活躍度
活躍
技術堆疊
c, php
領域
backend

研究方向

閱讀main/fastcgi.h中的FCGI_HASH_FUNC定義,以及main/fastcgi.c中第1019行附近的FastCGI參數解析呼叫。檢查雜湊函式如何處理長度為三個字元的名稱,接著檢視現有的FastCGI測試,找出適合涵蓋此情況的位置。當雜湊不再讀取超出三個字元名稱的範圍,且相關測試通過時,工作即告完成。

由索引模型根據 Issue 內容生成。

描述

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

主要語言
C
星號
41.9k
分支
8.5k
平均合併
2 天 16 小時
30 天內合併 PR
117

環境準備

  • 沒有 Dockerfile 或 Docker Compose 檔案
  • 沒有 Pull Request 範本
  • 閱讀貢獻指南

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

php/php-src 的其他 Issue

查看 php/php-src 的全部 Issue

相似的 Issue

更多 C Issue

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。