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

オープン 初心者向け
#26,069 コメント 0 件 リアクション 0 件 担当者 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の定義と、1019行目付近のmain/fastcgi.cにあるFastCGIパラメーター解析の呼び出しを読んでください。ハッシュ関数が長さ3の名前をどのように扱うかを確認し、既存のFastCGIテストを調べて、このケースをカバーするのに適した場所を探してください。ハッシュが3文字の名前の範囲を超えて読み取らなくなり、関連するテストが通れば完了です。

索引モデルが 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時間
マージ済み PR(30日)
117

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

php/php-src のほかの issue

php/php-src の issue をすべて見る

似ている issue

C の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。