Skip to content

mod_cache_socache: bound header parser to the cached entry length - #849

Open
arshsmith1 wants to merge 5 commits into
apache:trunkfrom
arshsmith1:socache-header-parse-bounds
Open

arshsmith1 wants to merge 5 commits into
apache:trunkfrom
arshsmith1:socache-header-parse-bounds

Conversation

@arshsmith1

@arshsmith1 arshsmith1 commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor

read_array() and read_table() parse a cached entry retrieved from the socache store. Each loop is guarded by *slider < buffer_len, but after matching a CR they do (*slider)++ and then read buffer[*slider] to test for the following LF without re-checking that bound.

The retrieve buffer is sized to CacheSocacheMaxSize and buffer_len is always smaller than that, so the lookahead stays inside the allocation: it reads a stale byte left over from an earlier entry read into the same recycled buffer. When the CR is the last byte of the datum and that stale byte is LF, *slider ends up at buffer_len + 1.

open_entity() then computes the body length as buffer_len - slider in an apr_size_t, which underflows to ~SIZE_MAX, and hands it to apr_bucket_pool_create(). Recalling the entry streams from well past the end of the retrieved data.

Bounded each of the three post-CR LF lookaheads (once in read_array(), twice in read_table()) with *slider < buffer_len, so the parse position never moves past the end of the datum and the subtraction cannot underflow. Well-formed records parse exactly as before.

test/modules/cache/test_002_socache_truncated.py caches a static file through a dbm: socache store, rewrites the SDBM record so it ends on the CR of the request headers' final CRLF, and recalls it on the same keep-alive connection so the recycled buffer still holds the LF from the previous full hit. It skips where the default APR DBM is not SDBM, since it edits the .pag file directly.

The ASan, pool-debug, pytest job turned up a separate, pre-existing bug the new test triggers: socache_dbm_destroy() builds the file names it unlinks from ctx->pool, a subpool of pconf, but it runs from a pconf cleanup, after pconf's subpools have already been destroyed. With --enable-pool-debug that is a heap-use-after-free on the first config re-read, so httpd with a dbm: socache never starts. The last commit makes socache_dbm_destroy() use a temporary pool instead.


  • This PR does not report or address a security vulnerability
    (see SECURITY.md and
    https://www.apache.org/security/#reporting-a-vulnerability).
  • Code follows the httpd style guide.
  • New log messages (level debug or higher) use an empty APLOGNO() tag;
    numbers are assigned by a committer at merge time (see docs/log-message-tags/README).
  • If the change is user-visible, a changes-entries/*.txt file is included,
    following the template in README.CHANGES (not needed otherwise).
  • Test cases based on pyhttpd are included where appropriate, in the
    test/modules/xxx directory matching the modules/xxx module source,
    or test/modules/core for core server changes. On Unix, verify these
    pass locally with e.g. make check-pytest PYTEST_DIRS=test/modules/xxx.

@arturobernalg

Copy link
Copy Markdown
Member

The fix looks correct. I reproduced the body length underflow with a real httpd and a truncated socache entry: trunk fails to complete the response (writev() returns EINVAL), while the patched version no longer does.

Could we add a regression test using a deliberately truncated cache entry (e.g. with socache DBM)?

Separately, a truncated entry is still served as a complete 200 response with an empty body. That should probably be addressed independently.

@arshsmith1

Copy link
Copy Markdown
Contributor Author

Added test/modules/cache/test_002_socache_truncated.py. It caches a static file through socache DBM, rewrites the SDBM record so it ends on the CR of the request headers' final CRLF, then recalls it on the same keep-alive connection so the recycled buffer still holds the LF from the previous full hit. That's what makes trunk read past the datum. It skips when the default APR DBM isn't SDBM, since it has to edit the .pag file directly. I haven't run it against a built httpd locally yet, so CI is the first real run.

Agreed on the truncated entry being served as a 200 with an empty body. I'd rather keep this PR to the underflow and look at rejecting short entries in a separate one.

@arturobernalg

Copy link
Copy Markdown
Member

Thanks for adding the test. I verified it against a real httpd with SDBM: it fails consistently on trunk, including in an ASan build, and passes with the fix.

One small issue: the final assertions also pass on a cache MISS. Could you check that X-Cache reports a HIT, as you already do for the second request? Otherwise the test could silently stop exercising the corrupted entry.

The changes entry could also describe the user-visible fix rather than the implementation details, e.g. "mod_cache_socache: Prevent invalid response lengths when reading truncated cache entries."

@arshsmith1

Copy link
Copy Markdown
Contributor Author

Good point on the MISS. The last request now also asserts X-Cache starts with HIT, so the test fails if it stops actually serving the corrupted entry. I've also reworded the changes entry along the lines you suggested.

@arturobernalg

Copy link
Copy Markdown
Member

Thanks @arshsmith1 — the fix itself looks right. The three *slider < buffer_len guards keep slider <= buffer_len on every return, so the len = buffer_len - slider at the end can no longer underflow. The changes entry is correct too.

A few things before this can go in:

  1. Rebase onto trunk. The branch is well behind, and test/modules/cache/ (conftest/env, added in Add mod_cache_disk tests #841) isn't present here, so the new test can't run on the branch as-is.
  2. Let CI actually run and confirm the test executes. The workflows are still waiting for approval, so this has never been exercised. Please check the Linux log shows test_cache_002_01_truncated_entry running and passing, not skipped —
    if mod_socache_dbm isn't built there, the SDBM guard would skip it and it would pass silently.
  3. Update the PR description. As written it's a stale-byte lookahead read, not an out-of-bounds read of the allocation (the buffer is sized to dconf->max), and it no longer reflects that a pyhttpd test is included.

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.
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.
… 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.
@arshsmith1
arshsmith1 force-pushed the socache-header-parse-bounds branch from a48b596 to 9e9f886 Compare November 21, 2026 18:22
@arshsmith1

Copy link
Copy Markdown
Contributor Author

Rebased onto trunk and force-pushed, so test/modules/cache/ (conftest/env from #841) is in place and the new test runs on the branch as-is.

On CI: the last run that got as far as executing, 44369630003, did run the test rather than skip it. Python pytest test suites on x86 and on ARM, and ASan, pytest, all show modules/cache/test_002_socache_truncated.py ., so mod_socache_dbm was there, the .pag surgery happened and the corrupted entry was recalled. Two jobs didn't exercise it: UBSan skipped with "default APR DBM is not SDBM" (it builds against the distro apr-util), and ASan, pool-debug, pytest errored in setup because httpd exited 1 on the dbm: socache config. The logs don't say why, since that suite's error_log is overwritten by later suites before the artifact is collected, so the fixture now asserts with env.apachectl_stderr and the next run will report it. The runs for this branch are sitting in action_required and need your approval to start.

Description rewritten too: it now describes the lookahead as a stale byte read inside the CacheSocacheMaxSize-sized retrieve buffer rather than a read past the allocation, and it mentions the pyhttpd test.

@arturobernalg

Copy link
Copy Markdown
Member

Approved and ran the workflows on the rebased head. Results: ASan, pytest is green and the test runs there, and ASan, UBSan and the ASan, pool-debug (non-pytest) jobs all pass. The only red is ASan, pool-debug, pytest, which still fails — same dbm: startup problem.

Your new commit prints apachectl_stderr now, so the reason should be in that job's log. Could you check what httpd reports on startup there? Since plain ASan, pytest is fine, this looks specific to the pool-debug build rather than the fix. Either a real cause with a fix, or a justified skip for that build so the test doesn't leave that job red on trunk.

Separately: I built the rebased head locally and confirmed the test goes RED without the three guards (the server drops the connection on recall of the truncated entry) and GREEN with them — so the fix and the test both hold up.

…estroy

socache_dbm_create() creates ctx->pool as a subpool of the pool the
instance is created in (pconf), and every consumer calls the provider's
destroy() from a cleanup registered on that same pool.  apr_pool_clear()
destroys a pool's subpools before it runs its cleanups, so by the time
socache_dbm_destroy() runs, ctx->pool is already gone and the
apr_pstrcat() calls building the file names allocate from freed memory.

With a normal APR the freed pool node is merely recycled and this goes
unnoticed.  With --enable-pool-debug, where pools are backed by
malloc/free, the ASan pytest job fails to start httpd on any
configuration using a dbm: socache, as the first pconf clear after the
initial config pass trips a heap-use-after-free in pool_alloc() called
from socache_dbm_destroy().

Use a temporary pool for the file names instead.  ctx and ctx->data_file
live in the parent pool, which is still valid while its cleanups run.
@arshsmith1

Copy link
Copy Markdown
Contributor Author

Found it. The pool-debug job isn't failing on the dbm: config itself: apachectl_stderr came back empty and the error_log only has the AH00052 segfault notice, but the job's asan.log has the real report. It's a pre-existing use-after-free in mod_socache_dbm that the new test happens to trigger:

ERROR: AddressSanitizer: heap-use-after-free ... in pool_alloc
    #1 apr_pstrcat
    #2 socache_dbm_destroy mod_socache_dbm.c:203
    #3 destroy_cache mod_cache_socache.c:1348
    #4 run_cleanups
    #5 pool_clear_debug
    #7 reset_process_pconf server/main.c:332

socache_dbm_create() makes ctx->pool a subpool of pconf, and socache_dbm_destroy() builds the file names it unlinks from that pool. But destroy is called from a pconf cleanup (same in mod_ssl and mod_authn_socache), and apr_pool_clear() destroys subpools before it runs cleanups, so ctx->pool is already gone by then. With a normal APR the freed node is just recycled and nothing notices. With pool-debug it's a real free(), so the first pconf clear after the initial config pass blows up and httpd with a dbm: socache never gets past startup. That's also why only this job sees it: plain ASan without pool-debug can't.

Pushed a fix rather than a skip, since it's a real bug: socache_dbm_destroy() now uses a temporary pool for the names. It's a separate commit with its own changes entry, so it can be split out if you'd rather take it on its own. Verified locally against a pool-debug APR 1.7.x: before the fix httpd segfaults on startup with the socache test config under Guard Malloc, same stack as above; after it, it starts and test/modules/cache passes with the truncated-entry test running (SDBM, not skipped). ASan on macOS doesn't survive mod_so reloading the DSOs for the second config pass, so the ASan confirmation itself needs CI again. PR description updated to mention it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants