Skip to content

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

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

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

Conversation

@arshsmith1

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.

  1. When the CR is the last byte of the retrieved datum (a truncated or tampered record), the lookahead reads one byte past the data and, if it is LF, advances *slider to buffer_len + 1.
  2. open_entity() then computes len = buffer_len - slider (apr_size_t), which underflows to ~SIZE_MAX and is handed to apr_bucket_pool_create() for the body bucket, so recalling the entry streams an out-of-bounds read to the client.

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 body-length subtraction cannot underflow. Output is unchanged for well-formed records.

Reachable when the socache provider hands back a malformed record (for example a shared or remote mod_socache_memcache / mod_socache_redis store). Verified with an AddressSanitizer reproducer of read_table plus the open_entity body handling: unpatched, read_table returns success with slider = buffer_len + 1 and the body read trips ASAN (heap-buffer-overflow READ); with the patch slider stays at buffer_len, len is 0, and there is no OOB. The whole file also compiles clean under -Wall with the change.

A pyhttpd test is not included: the defect needs a malformed entry in the socache store, which the test framework cannot inject against the in-process providers, so the ASAN reproducer above exercises it 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.

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.
@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."

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