Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions changes-entries/66168.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
*) mod_proxy_fcgi, mod_brotli: Fix a NULL pointer dereference (worker
crash) triggered by a FastCGI backend that sends a premature,
stream-ending empty FCGI_STDOUT record and keeps sending data
afterwards. PR 66168 [Arturo Bernal]
2 changes: 1 addition & 1 deletion docs/log-message-tags/next-number
Original file line number Diff line number Diff line change
@@ -1 +1 @@
10687
10703
8 changes: 8 additions & 0 deletions modules/filters/mod_brotli.c
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,14 @@ static apr_status_t compress_filter(ap_filter_t *f, apr_bucket_brigade *bb)
}

if (APR_BUCKET_IS_EOS(e)) {
/* The encoder state is about to be destroyed below. Remove
* ourselves from the filter chain now so that we can never be
* invoked again for this request, even if more data follows
* the EOS bucket (e.g. a backend forwarding data past the end
* of its own output stream through mod_proxy_fcgi; PR66168).
*/
ap_remove_output_filter(f);

rv = flush(ctx, BROTLI_OPERATION_FINISH, f);
if (rv != APR_SUCCESS) {
return rv;
Expand Down
26 changes: 26 additions & 0 deletions modules/proxy/mod_proxy_fcgi.c
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,7 @@ static apr_status_t dispatch(proxy_conn_rec *conn, proxy_dir_conf *conf,
{
apr_bucket_brigade *ib, *ob;
int seen_end_of_headers = 0, done = 0, ignore_body = 0;
int seen_stdout_eos = 0, warned_stdout_after_eos = 0;
apr_status_t rv = APR_SUCCESS;
int script_error_status = HTTP_OK;
conn_rec *c = r->connection;
Expand Down Expand Up @@ -815,6 +816,29 @@ static apr_status_t dispatch(proxy_conn_rec *conn, proxy_dir_conf *conf,

switch (type) {
case AP_FCGI_STDOUT:
if (seen_stdout_eos) {
/* The backend already signalled the end of its STDOUT
* stream with a zero-length record, and we already
* passed an EOS bucket down the output filter chain
* because of it. Some backends (e.g. PHP-FPM, see
* PHP bug #72185) can emit such a record too early and
* keep sending STDOUT data afterwards; discard it
* rather than forwarding more data past the EOS we
* already sent (PR66168). */
if (!warned_stdout_after_eos) {
ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r, APLOGNO(10702)
"Ignoring FastCGI STDOUT data received "
"after the backend already signalled "
"the end of its output stream");
warned_stdout_after_eos = 1;
}
if (clen > readbuflen) {
clen -= readbuflen;
goto recv_again;
}
break;
}

if (clen != 0) {
b = apr_bucket_transient_create(iobuf,
readbuflen,
Expand Down Expand Up @@ -942,6 +966,8 @@ static apr_status_t dispatch(proxy_conn_rec *conn, proxy_dir_conf *conf,
} else {
/* XXX what if we haven't seen end of the headers yet? */

seen_stdout_eos = 1;

if (script_error_status == HTTP_OK) {
b = apr_bucket_eos_create(c->bucket_alloc);
APR_BRIGADE_INSERT_TAIL(ob, b);
Expand Down
6 changes: 3 additions & 3 deletions test/modules/proxy/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,9 @@ def __init__(self, env: 'HttpdTestEnv'):
super().__init__(env=env)
self.add_source_dir(os.path.dirname(inspect.getfile(ProxyTestSetup)))
self.add_modules(["proxy", "proxy_http", "proxy_ajp", "proxy_balancer",
"proxy_connect", "proxy_uwsgi", "lbmethod_byrequests",
"remoteip"])
self.add_optional_modules(["proxy_hcheck"])
"proxy_connect", "proxy_uwsgi",
"lbmethod_byrequests", "remoteip"])
self.add_optional_modules(["proxy_hcheck", "proxy_fcgi", "brotli"])


class ProxyTestEnv(HttpdTestEnv):
Expand Down
140 changes: 140 additions & 0 deletions test/modules/proxy/fcgi_faker.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
"""
Minimal fake FastCGI responder backend for testing mod_proxy_fcgi.

Listens on a TCP port and speaks the FastCGI wire protocol (records only,
no multiplexing). It reads and discards FCGI_BEGIN_REQUEST, FCGI_PARAMS
and FCGI_STDIN, then writes back an intentionally malformed FCGI_STDOUT
stream: a normal chunk of body data, followed by a zero-length FCGI_STDOUT
record (which per the FastCGI spec marks the end of the stream), followed
by *more* FCGI_STDOUT data. Some backends (PHP-FPM hitting PHP bug #72185,
for one) really do this, and the extra data after the stream-ending empty
record must be discarded rather than forwarded down the output filter
chain (see PR66168).
"""
import socket
import struct
import threading


FCGI_BEGIN_REQUEST = 1
FCGI_END_REQUEST = 3
FCGI_PARAMS = 4
FCGI_STDIN = 5
FCGI_STDOUT = 6

HEADER_LEN = 8

BODY_BEFORE_EOS = b"before-eos-" * 200
BODY_AFTER_EOS = b"after-eos-" * 200


def _recv_exact(conn, n):
buf = b""
while len(buf) < n:
chunk = conn.recv(n - len(buf))
if not chunk:
return None
buf += chunk
return buf


def _recv_record_header(conn):
header = _recv_exact(conn, HEADER_LEN)
if header is None:
return None
_version, rtype, req_id, content_len, pad_len = struct.unpack(
">BBHHB", header[:7])
return rtype, req_id, content_len, pad_len


def _recv_record(conn):
"""Read one complete FastCGI record, returning (type, req_id, content)."""
hdr = _recv_record_header(conn)
if hdr is None:
return None
rtype, req_id, content_len, pad_len = hdr
content = _recv_exact(conn, content_len) if content_len else b""
if content is None:
return None
if pad_len:
if _recv_exact(conn, pad_len) is None:
return None
return rtype, req_id, content


def _send_record(conn, rtype, req_id, content=b""):
header = struct.pack(">BBHHBB", 1, rtype, req_id, len(content), 0, 0)
conn.sendall(header + content)


def _handle(conn):
try:
begin = _recv_record(conn)
if begin is None or begin[0] != FCGI_BEGIN_REQUEST:
return
req_id = begin[1]

# Drain FCGI_PARAMS until the stream-ending empty record.
while True:
rec = _recv_record(conn)
if rec is None:
return
if rec[0] == FCGI_PARAMS and len(rec[2]) == 0:
break

# Drain FCGI_STDIN until the stream-ending empty record.
while True:
rec = _recv_record(conn)
if rec is None:
return
if rec[0] == FCGI_STDIN and len(rec[2]) == 0:
break

headers = b"Content-Type: text/plain\r\n\r\n"
_send_record(conn, FCGI_STDOUT, req_id, headers + BODY_BEFORE_EOS)

# Premature stream-ending empty record (the PHP-FPM / PHP bug
# #72185 pattern), immediately followed by more STDOUT data that
# must never reach an output filter chain that already saw EOS.
_send_record(conn, FCGI_STDOUT, req_id, b"")
_send_record(conn, FCGI_STDOUT, req_id, BODY_AFTER_EOS)

_send_record(conn, FCGI_STDOUT, req_id, b"")
_send_record(conn, FCGI_END_REQUEST, req_id,
struct.pack(">LBxxx", 0, 0))
finally:
conn.close()


class FcgiFaker:
"""Fake FastCGI backend running in a daemon thread."""

def __init__(self, port: int):
self._port = port
self._done = False
self._sock = None
self._thread = None

def start(self):
self._sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self._sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self._sock.bind(("127.0.0.1", self._port))
self._sock.listen(16)
self._thread = threading.Thread(target=self._serve, daemon=True)
self._thread.start()

def stop(self):
self._done = True
try:
self._sock.close()
except OSError:
pass

def _serve(self):
while not self._done:
try:
conn, _ = self._sock.accept()
except OSError:
break
t = threading.Thread(target=_handle, args=(conn,), daemon=True)
t.start()
107 changes: 107 additions & 0 deletions test/modules/proxy/test_08_fcgi_eos.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
"""
Tests for PR66168 / BZ 66168: a FastCGI backend that sends a premature,
stream-ending zero-length FCGI_STDOUT record and then keeps sending more
STDOUT data afterwards (as PHP-FPM does when hitting PHP bug #72185) must
not crash the worker. The extra data has to be discarded, not forwarded
down an output filter chain that already received the EOS bucket.
"""
import subprocess

import pytest

from pyhttpd.conf import HttpdConf
from .fcgi_faker import FcgiFaker, BODY_BEFORE_EOS, BODY_AFTER_EOS


FCGI_PORT = 5210


def _curl_decodes_brotli(env):
"""Whether curl can decode a brotli response (--compressed), which
depends on how it was built: not all CI curls can."""
try:
out = subprocess.run([env.curl, "-V"], capture_output=True,
text=True, timeout=10).stdout
except (OSError, subprocess.SubprocessError):
return False
for line in out.splitlines():
if line.startswith("Features:"):
return "brotli" in line.split()
return False


@pytest.fixture(autouse=True, scope="class")
def _proxy_fcgi_eos_setup(env):
env.require(env.has_shared_module("proxy_fcgi"), "mod_proxy_fcgi not available",
dep="mod_proxy_fcgi",
detected=f"no mod_proxy_fcgi.so in {env.libexec_dir}")
env.require(env.has_shared_module("brotli"), "mod_brotli not available",
dep="mod_brotli",
detected=f"no mod_brotli.so in {env.libexec_dir}")

faker = FcgiFaker(port=FCGI_PORT)
faker.start()

# The backend deliberately sends data after its own stream-ending
# record; mod_proxy_fcgi logs AH10702 once per request when it
# discards that data instead of crashing on it.
env.httpd_error_log.add_ignored_lognos(["AH10702"])

conf = HttpdConf(env)
conf.start_vhost(
domains=[env.d_reverse],
port=env.http_port,
with_ssl=False,
)
conf.add([
"LogLevel proxy_fcgi:debug",
f"ProxyPass /fcgitest fcgi://127.0.0.1:{FCGI_PORT}/",
"AddOutputFilterByType BROTLI_COMPRESS text/plain",
])
conf.end_vhost()
conf.install()

assert env.apache_restart() == 0

yield

faker.stop()


class TestProxyFcgiEos:

def test_proxy_08_001(self, env):
"""A premature empty FCGI_STDOUT record followed by more backend
data must not crash the worker, even with a compressing output
filter (mod_brotli) in the chain."""
url = f"http://{env.d_reverse}:{env.http_port}/fcgitest"
r = env.curl_raw(url, options=["-H", "Accept-Encoding: br"])
assert r.exit_code == 0
assert r.response["status"] == 200
assert r.response["header"]["content-encoding"] == "br"
assert len(r.response["body"]) > 0

# Check the content too where curl can decode it: everything up
# to the premature EOS must have reached the client, but nothing
# the backend sent after it.
if _curl_decodes_brotli(env):
r = env.curl_raw(url, options=["--compressed",
"-H", "Accept-Encoding: br"])
assert r.exit_code == 0
assert r.response["status"] == 200
assert BODY_BEFORE_EOS.decode() in r.stdout
assert BODY_AFTER_EOS.decode() not in r.stdout

# No segfault (or any other unexpected error/warning, e.g. AH00051)
# was logged for this request: the package-scoped teardown asserts
# env.check_error_log() sees nothing unaccounted for.

def test_proxy_08_002(self, env):
"""The server (and this backend route) still answers normally
after handling the malformed backend response above."""
r = env.curl_raw(
f"http://{env.d_reverse}:{env.http_port}/fcgitest")
assert r.exit_code == 0
assert r.response["status"] == 200
assert BODY_BEFORE_EOS.decode() in r.stdout
assert BODY_AFTER_EOS.decode() not in r.stdout