Skip to content

fix out-of-bounds read in mod_proxy_html metafix - #783

Open
arshsmith1 wants to merge 4 commits into
apache:trunkfrom
arshsmith1:proxy-html-metafix-bounds
Open

arshsmith1 wants to merge 4 commits into
apache:trunkfrom
arshsmith1:proxy-html-metafix-bounds

Conversation

@arshsmith1

Copy link
Copy Markdown
Contributor

metafix scans the backend response buffer for a header name and content value, but buf comes straight from apr_bucket_read and is not NUL terminated while the scans have no bound against buf+len. A backend body whose tag lands at the end of the bucket (with ProxyHTMLMeta on) walks past the buffer, reading adjacent heap and copying it into a response header name. Bound every scan in metafix to buf+len so a tag at the buffer boundary stops cleanly.

@arturobernalg

Copy link
Copy Markdown
Member

The fix looks correct. I reproduced both out-of-bounds reads with a real httpd (ProxyHTMLMeta On, PROXY_HTML_FORCE) serving a 4096-byte file that ends with the meta tag, with a guard page mapped right after the file. On trunk, <meta http-equiv> and <meta content='0;url=/x http-equiv=Refresh> at the end of the body crash the child with SIGSEGV. With this PR applied they no longer do, and well-formed meta tags are handled as before.

Could you add a regression test? There are no mod_proxy_html tests yet. A case with the meta tag at the very end of the body would do, although without ASan or a guard page it may also pass on trunk.

Serve documents through mod_proxy_html with ProxyHTMLMeta on: a
well-formed http-equiv tag is lifted into a response header, and a
tag at the very end of the body is handled without reading past it.
@arshsmith1

Copy link
Copy Markdown
Contributor Author

Added test/modules/filters/test_004_proxy_html_meta.py. It serves 4096-byte documents ending in each of your two tags through mod_proxy_html (ProxyHTMLMeta On, PROXY_HTML_FORCE) and checks they come back 200 with no header lifted, plus a well-formed tag that should still turn into a response header. It skips when mod_proxy_html isn't built. As you said, without ASan or a guard page the end-of-body cases may pass on trunk too. I haven't been able to run it on a Unix build yet, so CI will be the first run.

@arturobernalg

Copy link
Copy Markdown
Member

Thanks for the test. I checked it locally: with a guard page after the mapped file, both cases crash on trunk and pass with the fix.
One small thing: test_004 is already taken in test/modules/filters. Could you rename the new test to test_010_proxy_html_meta.py?

test_004 is already used in test/modules/filters on trunk.
@arshsmith1

Copy link
Copy Markdown
Contributor Author

Renamed it to test_010_proxy_html_meta.py and the test functions to test_filters_010_* to match.

@arturobernalg

Copy link
Copy Markdown
Member

Thanks @arshsmith1. The out-of-bounds read is real — the hand-written scans after the regex match run against the raw, non-NUL-terminated bucket buffer, and the bounds you added close both the http-equiv name scan and the
unterminated-quote content scan. No off-by-one that I can see.

Two things block it as-is, plus a nit:

  1. The fix makes an empty header name reachable — it needs a guard. With at the end of the buffer, p ends at end, so header is "" while content is non-empty. header && content is true, so
    apr_table_setn(r->headers_out, "", "x") runs, and check_header() then rejects it with AH02428 and turns the request into a 500. metafix() should skip when the name is empty (q == p).
  2. The test isn't a reliable RED/GREEN. The cases use 4096-byte static documents, and as the test comment itself notes, the over-read can go undetected on unfixed trunk without a guard page, so it can pass without the fix. Could you
    make it fail on unfixed trunk (guard page / EnableMMAP Off under ASan) and add a case for the empty-name input above? Also the head still has test_filters_004_01/02 rather than the renamed 010, and if CI doesn't build mod_proxy_html
    the test skips silently — worth confirming in the Linux log that it actually ran.

…l without the fix

metafix() could still reach apr_table_setn(r->headers_out, "", content)
once the scans were bounded: with <meta content=x http-equiv> at the end
of the buffer the header-name scan stops at end with q == p, leaving an
empty name while content is non-empty.  check_header() then rejects it
and the request turns into a 500.  Skip the match when the name is empty.

The test now sets EnableMMAP Off so the body is read into an allocated
buffer, where the over-read is caught under ASan, and adds the empty-name
case above.  Rename the test functions to test_filters_010_* to match the
file.
@arshsmith1

Copy link
Copy Markdown
Contributor Author

Good catches, both. Pushed:

  1. metafix() now skips the match when the name is empty (q == p), so the <meta content=x http-equiv> case no longer reaches apr_table_setn(headers_out, "", ...) and the 500 is gone.

  2. The test sets EnableMMAP Off on the Location, so the body is read into an allocated buffer instead of being mmap'd, and the over-read runs off the end of it. Under the ASan, pytest job that aborts the child on unfixed trunk, so the end-of-body cases fail without the fix. I also added the empty-name input from point 1, which gives a reliable RED even without ASan: unguarded it's a 500, with the fix it's a 200 and no header.

  3. Renamed the functions to test_filters_010_01/02. reallyall builds proxy_html (and env.py pulls in xml2enc/proxy_html as optional modules), so the ASan pytest job runs the suite rather than skipping it. I still don't have a Unix ASan build here, so CI is the first real run; I'll watch that job and confirm it isn't skipped.

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