Skip to content

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

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

arshsmith1 wants to merge 4 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.


  • 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.

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