Repository navigation
mod_cache_socache: bound header parser to the cached entry length - #849
arshsmith1 wants to merge 5 commits into
Conversation
|
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 ( 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. |
|
Added 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. |
|
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 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." |
|
Good point on the MISS. The last request now also asserts |
|
Thanks @arshsmith1 — the fix itself looks right. The three A few things before this can go in:
|
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.
a48b596 to
9e9f886
Compare
|
Rebased onto trunk and force-pushed, so On CI: the last run that got as far as executing, 44369630003, did run the test rather than skip it. Description rewritten too: it now describes the lookahead as a stale byte read inside the |
|
Approved and ran the workflows on the rebased head. Results: Your new commit prints 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.
|
Found it. The pool-debug job isn't failing on the
Pushed a fix rather than a skip, since it's a real bug: |
read_array()andread_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 readbuffer[*slider]to test for the following LF without re-checking that bound.The retrieve buffer is sized to
CacheSocacheMaxSizeandbuffer_lenis 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,*sliderends up atbuffer_len + 1.open_entity()then computes the body length asbuffer_len - sliderin anapr_size_t, which underflows to ~SIZE_MAX, and hands it toapr_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 inread_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.pycaches a static file through adbm: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.pagfile directly.The
ASan, pool-debug, pytestjob turned up a separate, pre-existing bug the new test triggers:socache_dbm_destroy()builds the file names it unlinks fromctx->pool, a subpool of pconf, but it runs from a pconf cleanup, after pconf's subpools have already been destroyed. With--enable-pool-debugthat is a heap-use-after-free on the first config re-read, so httpd with adbm:socache never starts. The last commit makessocache_dbm_destroy()use a temporary pool instead.(see
SECURITY.mdandhttps://www.apache.org/security/#reporting-a-vulnerability).
APLOGNO()tag;numbers are assigned by a committer at merge time (see
docs/log-message-tags/README).changes-entries/*.txtfile is included,following the template in
README.CHANGES(not needed otherwise).test/modules/xxxdirectory matching themodules/xxxmodule source,or
test/modules/corefor core server changes. On Unix, verify thesepass locally with e.g.
make check-pytest PYTEST_DIRS=test/modules/xxx.