diff --git a/changes-entries/proxy-html-metafix-bounds.txt b/changes-entries/proxy-html-metafix-bounds.txt new file mode 100644 index 00000000000..3f55db8d7ef --- /dev/null +++ b/changes-entries/proxy-html-metafix-bounds.txt @@ -0,0 +1,3 @@ + *) mod_proxy_html: Fix out-of-bounds reads in metafix() when a + tag appears at the end of the response buffer with + ProxyHTMLMeta enabled. [Arshiya Tabasum] diff --git a/modules/filters/mod_proxy_html.c b/modules/filters/mod_proxy_html.c index a8c29db862a..afab76e8427 100644 --- a/modules/filters/mod_proxy_html.c +++ b/modules/filters/mod_proxy_html.c @@ -689,6 +689,7 @@ static meta *metafix(request_rec *r, const char *buf, apr_size_t len) size_t offs = 0; const char *p; const char *q; + const char *end = buf + len; char *header; char *content; ap_regmatch_t pmatch[2]; @@ -699,8 +700,8 @@ static meta *metafix(request_rec *r, const char *buf, apr_size_t len) header = NULL; content = NULL; p = buf+offs+pmatch[1].rm_eo; - while (!apr_isalpha(*++p)); - for (q = p; apr_isalnum(*q) || (*q == '-'); ++q); + while (++p < end && !apr_isalpha(*p)); + for (q = p; q < end && (apr_isalnum(*q) || (*q == '-')); ++q); header = apr_pstrmemdup(r->pool, p, q-p); if (!ap_cstr_casecmpn(header, "Content-Type", 12)) { ret = apr_palloc(r->pool, sizeof(meta)); @@ -713,22 +714,22 @@ static meta *metafix(request_rec *r, const char *buf, apr_size_t len) pmatch[0].rm_eo - pmatch[0].rm_so); /* if it doesn't contain "content", ignore, don't crash! */ if (p != NULL) { - while (*p) { + while (p < end && *p) { p += 7; - while (apr_isspace(*p)) + while (p < end && apr_isspace(*p)) ++p; /* XXX Should we search for another content= pattern? */ - if (*p != '=') + if (p >= end || *p != '=') break; - while (*p && apr_isspace(*++p)); - if ((*p == '\'') || (*p == '"')) { + while (++p < end && apr_isspace(*p)); + if (p < end && ((*p == '\'') || (*p == '"'))) { delim = *p++; - for (q = p; *q && *q != delim; ++q); + for (q = p; q < end && *q != delim; ++q); /* No terminating delimiter found? Skip the bogus directive */ - if (*q != delim) + if (q >= end || *q != delim) break; } else { - for (q = p; *q && !apr_isspace(*q) && (*q != '>'); ++q); + for (q = p; q < end && !apr_isspace(*q) && (*q != '>'); ++q); } content = apr_pstrmemdup(r->pool, p, q-p); break; diff --git a/test/modules/filters/env.py b/test/modules/filters/env.py index c0aba5f25c3..77996f93c2a 100644 --- a/test/modules/filters/env.py +++ b/test/modules/filters/env.py @@ -14,6 +14,7 @@ def __init__(self, env: 'HttpdTestEnv'): self.add_source_dir(os.path.dirname(inspect.getfile(FiltersTestSetup))) self.add_modules(["substitute", "sed"]) self.add_cgi_module() + self.add_optional_modules(["xml2enc", "proxy_html"]) class FiltersTestEnv(HttpdTestEnv): diff --git a/test/modules/filters/test_004_proxy_html_meta.py b/test/modules/filters/test_004_proxy_html_meta.py new file mode 100644 index 00000000000..21aa0ab63a4 --- /dev/null +++ b/test/modules/filters/test_004_proxy_html_meta.py @@ -0,0 +1,81 @@ +import os + +import pytest + +from pyhttpd.conf import HttpdConf + +from .env import FiltersTestEnv + +# ProxyHTMLMeta lifts tags into response headers. The +# scan for the header name and the content value must stop at the end of +# the data read from the backend, which is not NUL terminated, so a tag +# right at the end of the body must not be read past. +# +# The documents are 4096 bytes, ending in the tag, so that an over-read on +# an unfixed server is more likely to run off the end of the mapped file. +# Without a guard page or ASan it may still go unnoticed. +DOC_SIZE = 4096 +HEAD = "t" + +WELL_FORMED = (HEAD + '' + + "ok") +END_TAGS = { + "no_name": "", + "open_content": " + SetEnv PROXY_HTML_FORCE 1 + ProxyHTMLEnable On + ProxyHTMLMeta On + ProxyHTMLLinks a href + + """, + }) + conf.add_vhost_test1() + conf.install() + assert env.apache_restart() == 0 + + def get(self, env, name): + return env.curl_get(env.mkurl("http", "test1", f"/phtml/{name}.html")) + + # A well-formed tag is still lifted into a response header. + def test_filters_004_01(self, env): + r = self.get(env, "well_formed") + assert r.response, "no response" + assert r.response["status"] == 200 + assert r.response["header"].get("x-meta-test") == "lifted" + + # A tag at the very end of the body is served without a crash and, + # having no usable header name or content value, lifts no header. + @pytest.mark.parametrize("name", list(END_TAGS)) + def test_filters_004_02(self, env, name): + r = self.get(env, name) + assert r.response, f"{name}: no response" + assert r.response["status"] == 200, \ + f"{name}: status {r.response['status']}" + assert "refresh" not in r.response["header"]