From 6790ef5dce93d6eb55835bc4028e0334a9e39ab2 Mon Sep 17 00:00:00 2001 From: arshiya tabasum Date: Thu, 15 Oct 2026 14:08:53 +0530 Subject: [PATCH 1/4] mod_cache_socache: bound header parser to the cached entry length --- changes-entries/socache-header-parse-bounds.txt | 5 +++++ modules/cache/mod_cache_socache.c | 6 +++--- 2 files changed, 8 insertions(+), 3 deletions(-) create mode 100644 changes-entries/socache-header-parse-bounds.txt diff --git a/changes-entries/socache-header-parse-bounds.txt b/changes-entries/socache-header-parse-bounds.txt new file mode 100644 index 00000000000..b3e05b5ff57 --- /dev/null +++ b/changes-entries/socache-header-parse-bounds.txt @@ -0,0 +1,5 @@ + *) mod_cache_socache: Bound the cached-header parser to the retrieved entry + length so a truncated or corrupt socache record cannot advance the parse + position past the end of the data, which caused an apr_size_t underflow of + the body length and an out-of-bounds read when recalling the response. + [arshiya tabasum ] diff --git a/modules/cache/mod_cache_socache.c b/modules/cache/mod_cache_socache.c index 78bd3e7bb4c..a6e4f6a7985 100644 --- a/modules/cache/mod_cache_socache.c +++ b/modules/cache/mod_cache_socache.c @@ -150,7 +150,7 @@ static apr_status_t read_array(request_rec *r, apr_array_header_t *arr, *((const char **) apr_array_push(arr)) = apr_pstrndup(r->pool, (const char *) buffer + val, *slider - val); (*slider)++; - if (buffer[*slider] == '\n') { + if (*slider < buffer_len && buffer[*slider] == '\n') { (*slider)++; } val = *slider; @@ -209,7 +209,7 @@ static apr_status_t read_table(cache_handle_t *handle, request_rec *r, len = colon; if (key == *slider) { (*slider)++; - if (buffer[*slider] == '\n') { + if (*slider < buffer_len && buffer[*slider] == '\n') { (*slider)++; } return APR_SUCCESS; @@ -227,7 +227,7 @@ static apr_status_t read_table(cache_handle_t *handle, request_rec *r, + key, len - key), apr_pstrmemdup(r->pool, (const char *) buffer + colon, *slider - colon)); (*slider)++; - if (buffer[*slider] == '\n') { + if (*slider < buffer_len && buffer[*slider] == '\n') { (*slider)++; } key = *slider; From e4f7648b9078b7d3866cf4b42f476c6b68c44a23 Mon Sep 17 00:00:00 2001 From: arshiya tabasum Date: Wed, 18 Nov 2026 23:55:31 +0530 Subject: [PATCH 2/4] mod_cache_socache: add test for a truncated socache DBM entry Cache a static file through socache DBM, then cut the stored SDBM record short so it ends on the CR of the request headers' final CRLF, and check that recalling it on the same connection still completes. --- .../cache/test_002_socache_truncated.py | 142 ++++++++++++++++++ 1 file changed, 142 insertions(+) create mode 100644 test/modules/cache/test_002_socache_truncated.py diff --git a/test/modules/cache/test_002_socache_truncated.py b/test/modules/cache/test_002_socache_truncated.py new file mode 100644 index 00000000000..d2e23a7b6d4 --- /dev/null +++ b/test/modules/cache/test_002_socache_truncated.py @@ -0,0 +1,142 @@ +import http.client +import os +import shutil +import struct +import time + +import pytest + +from pyhttpd.conf import HttpdConf + +# SDBM page size, see apr-util dbm/sdbm/sdbm_private.h +SDBM_PBLKSIZ = 1024 + + +def _sdbm_read_page(page): + """Return the (key, value) pairs stored in an SDBM page.""" + n = struct.unpack_from("=h", page, 0)[0] + ino = struct.unpack_from(f"={n + 1}h", page, 0) + pairs = [] + end = SDBM_PBLKSIZ + for i in range(1, n, 2): + pairs.append((page[ino[i]:end], page[ino[i + 1]:ino[i]])) + end = ino[i + 1] + return pairs + + +def _sdbm_make_page(pairs): + """Pack (key, value) pairs into an SDBM page, as putpair() does.""" + page = bytearray(SDBM_PBLKSIZ) + ino = [0] + off = SDBM_PBLKSIZ + for key, val in pairs: + off -= len(key) + page[off:off + len(key)] = key + ino.append(off) + off -= len(val) + page[off:off + len(val)] = val + ino.append(off) + ino[0] = len(ino) - 1 + struct.pack_into(f"={len(ino)}h", page, 0, *ino) + return bytes(page) + + +def _sdbm_update(pag_file, match, update): + """Replace the value of the one pair whose key contains 'match'.""" + with open(pag_file, "rb") as fd: + data = bytearray(fd.read()) + found = 0 + for start in range(0, len(data), SDBM_PBLKSIZ): + page = bytes(data[start:start + SDBM_PBLKSIZ]) + if len(page) < SDBM_PBLKSIZ: + break + pairs = _sdbm_read_page(page) + if not any(match in key for key, _ in pairs): + continue + pairs = [(key, update(val) if match in key else val) + for key, val in pairs] + data[start:start + SDBM_PBLKSIZ] = _sdbm_make_page(pairs) + found += 1 + assert found == 1, f"expected one page with key {match!r}, found {found}" + with open(pag_file, "wb") as fd: + fd.write(data) + + +class TestSocacheTruncated: + + BODY = b"socache test body\n" + + @pytest.fixture(autouse=True, scope='class') + def _class_scope(self, env): + env.require(env.has_shared_module("socache_dbm"), + "mod_socache_dbm not built", dep="mod_socache_dbm") + cache_dir = os.path.join(env.gen_dir, "socache") + if os.path.exists(cache_dir): + shutil.rmtree(cache_dir) + os.makedirs(cache_dir) + TestSocacheTruncated.DBM = os.path.join(cache_dir, "cache") + docs = os.path.join(env.server_docs_dir, "test1", "socache") + os.makedirs(docs, exist_ok=True) + fpath = os.path.join(docs, "entry.bin") + with open(fpath, "wb") as fd: + fd.write(self.BODY) + # old enough for a heuristic freshness lifetime of a few hours + old = time.time() - 86400 + os.utime(fpath, (old, old)) + base = [] + mod_path = os.path.join(env.libexec_dir, "mod_socache_dbm.so") + if os.path.isfile(mod_path): + base.append(f'LoadModule socache_dbm_module "{mod_path}"') + base.extend([ + f'CacheSocache dbm:{TestSocacheTruncated.DBM}', + 'CacheHeader on', + ]) + conf = HttpdConf(env, extras={ + 'base': base, + f"test1.{env.http_tld}": [ + 'CacheEnable socache /socache/', + ], + }) + conf.add_vhost_test1() + conf.install() + assert env.apache_restart() == 0 + + @staticmethod + def _get(env, conn): + conn.request("GET", "/socache/entry.bin", headers={ + "Host": f"test1.{env.http_tld}:{env.http_port}", + }) + r = conn.getresponse() + return r.status, r.getheader("X-Cache", ""), r.read() + + def test_cache_002_01_truncated_entry(self, env): + """A socache entry cut short right after the CR that ends the + request headers must not make the body length underflow.""" + pag_file = f"{TestSocacheTruncated.DBM}.pag" + conn = http.client.HTTPConnection(env.http_addr, env.http_port, + timeout=10) + try: + status, xcache, body = self._get(env, conn) + assert status == 200 + assert body == self.BODY + if not os.path.isfile(pag_file): + pytest.skip("default APR DBM is not SDBM, cannot edit the " + "cache entry") + # served from the cache, which leaves the entire entry in + # the recycled buffer the next lookup on this connection uses + status, xcache, body = self._get(env, conn) + assert status == 200 + assert xcache.startswith("HIT"), xcache + assert body == self.BODY + + def truncate(val): + assert val.endswith(b"\r\n\r\n" + self.BODY), val + # keep only the CR of the request headers' final CRLF + return val[:-(len(self.BODY) + 1)] + _sdbm_update(pag_file, b"/socache/entry.bin", truncate) + + status, xcache, body = self._get(env, conn) + assert status == 200 + assert len(body) <= len(self.BODY) + finally: + conn.close() From 4f3a0c6ee7f43e35d639818c713e6ebdf782aa51 Mon Sep 17 00:00:00 2001 From: arshiya tabasum Date: Fri, 20 Nov 2026 20:46:32 +0530 Subject: [PATCH 3/4] mod_cache_socache: check the truncated entry is a cache hit in the test Assert X-Cache reports a HIT for the request that recalls the truncated entry too, so the test can't silently pass on a MISS without exercising it, and reword the CHANGES entry to describe the user-visible effect. --- changes-entries/socache-header-parse-bounds.txt | 7 ++----- test/modules/cache/test_002_socache_truncated.py | 1 + 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/changes-entries/socache-header-parse-bounds.txt b/changes-entries/socache-header-parse-bounds.txt index b3e05b5ff57..2777e5ccf3e 100644 --- a/changes-entries/socache-header-parse-bounds.txt +++ b/changes-entries/socache-header-parse-bounds.txt @@ -1,5 +1,2 @@ - *) mod_cache_socache: Bound the cached-header parser to the retrieved entry - length so a truncated or corrupt socache record cannot advance the parse - position past the end of the data, which caused an apr_size_t underflow of - the body length and an out-of-bounds read when recalling the response. - [arshiya tabasum ] + *) mod_cache_socache: Prevent invalid response lengths when reading + truncated cache entries. [arshiya tabasum ] diff --git a/test/modules/cache/test_002_socache_truncated.py b/test/modules/cache/test_002_socache_truncated.py index d2e23a7b6d4..b5c197ebe78 100644 --- a/test/modules/cache/test_002_socache_truncated.py +++ b/test/modules/cache/test_002_socache_truncated.py @@ -137,6 +137,7 @@ def truncate(val): status, xcache, body = self._get(env, conn) assert status == 200 + assert xcache.startswith("HIT"), xcache assert len(body) <= len(self.BODY) finally: conn.close() From 9e9f886e1c6921f682ea6701c7aee2c72b2725bb Mon Sep 17 00:00:00 2001 From: arshiya tabasum Date: Sat, 21 Nov 2026 23:52:41 +0530 Subject: [PATCH 4/4] mod_cache_socache: report httpd's stderr when the socache test server fails to start The bare assertion on apache_restart() gives no hint why httpd refused the configuration, and the suite's error_log is overwritten by later suites before the CI artifact is collected. --- test/modules/cache/test_002_socache_truncated.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/modules/cache/test_002_socache_truncated.py b/test/modules/cache/test_002_socache_truncated.py index b5c197ebe78..a68e066e7aa 100644 --- a/test/modules/cache/test_002_socache_truncated.py +++ b/test/modules/cache/test_002_socache_truncated.py @@ -99,7 +99,7 @@ def _class_scope(self, env): }) conf.add_vhost_test1() conf.install() - assert env.apache_restart() == 0 + assert env.apache_restart() == 0, env.apachectl_stderr @staticmethod def _get(env, conn):