diff --git a/changes-entries/limit-xml-request-body.txt b/changes-entries/limit-xml-request-body.txt new file mode 100644 index 00000000000..0d71cec7daf --- /dev/null +++ b/changes-entries/limit-xml-request-body.txt @@ -0,0 +1,2 @@ + *) core: Reject invalid non-numeric values of LimitXMLRequestBody, as + LimitRequestBody does. PR 69975. [Arturo Bernal] diff --git a/changes-entries/size-parameter-parse.txt b/changes-entries/size-parameter-parse.txt new file mode 100644 index 00000000000..b8b128da654 --- /dev/null +++ b/changes-entries/size-parameter-parse.txt @@ -0,0 +1,4 @@ + *) core: Add ap_size_parameter_parse() and use it to reject malformed + ScriptLogLength, ScriptLogBuffer, BufferSize, AuthFormSize, and + mod_cache_disk and mod_cache_socache size and time values. + [Arturo Bernal] diff --git a/include/ap_mmn.h b/include/ap_mmn.h index 43bcaac0fb9..24d3d505a53 100644 --- a/include/ap_mmn.h +++ b/include/ap_mmn.h @@ -617,6 +617,7 @@ * options * 20120211.144 (2.4.72-dev) Add the optional function ap_authn_cache_store_env() * to mod_auth.h + * 20120211.145 (2.4.72-dev) Add ap_size_parameter_parse() to httpd.h */ #define MODULE_MAGIC_COOKIE 0x41503234UL /* "AP24" */ @@ -624,7 +625,7 @@ #ifndef MODULE_MAGIC_NUMBER_MAJOR #define MODULE_MAGIC_NUMBER_MAJOR 20120211 #endif -#define MODULE_MAGIC_NUMBER_MINOR 144 /* 0...n */ +#define MODULE_MAGIC_NUMBER_MINOR 145 /* 0...n */ /** * Determine if the server's current MODULE_MAGIC_NUMBER is at least a diff --git a/include/httpd.h b/include/httpd.h index eb24f820de0..759608075ac 100644 --- a/include/httpd.h +++ b/include/httpd.h @@ -2231,6 +2231,26 @@ AP_DECLARE(apr_status_t) ap_timeout_parameter_parse( apr_interval_time_t *timeout, const char *default_time_unit); +#ifndef APR_ERANGE +/* APR_ERANGE is not defined before APR 1.6. */ +#define APR_ERANGE ERANGE +#endif + +/** + * Parse a configuration parameter as a base 10 apr_off_t. The number may be + * followed by a single optional suffix, 'b'/'B' (bytes), 'k'/'K' (multiply + * by 1024), 'm'/'M' (multiply by 1024*1024) or 'g'/'G' (multiply by + * 1024*1024*1024); there must not be any other character after it. What + * values are acceptable is up to the caller. + * @param parameter The string containing the number. + * @param value The parsed value, only set on success. + * @return APR_SUCCESS, APR_EINVAL if the parameter is not a number with + * optional suffix, or APR_ERANGE if the value does not fit in an + * apr_off_t. + */ +AP_DECLARE(apr_status_t) ap_size_parameter_parse(const char *parameter, + apr_off_t *value); + /** * Determine if a request has a request body or not. * diff --git a/modules/aaa/mod_auth_digest.c b/modules/aaa/mod_auth_digest.c index 39733e41084..1650d207c89 100644 --- a/modules/aaa/mod_auth_digest.c +++ b/modules/aaa/mod_auth_digest.c @@ -574,35 +574,15 @@ static const char *set_uri_list(cmd_parms *cmd, void *config, const char *uri) static const char *set_shmem_size(cmd_parms *cmd, void *config, const char *size_str) { - char *endptr; apr_off_t size; apr_size_t min; + apr_status_t rv; - if (apr_strtoff(&size, size_str, &endptr, 10) != APR_SUCCESS || size < 0) { - return apr_pstrcat(cmd->pool, "Invalid size in AuthDigestShmemSize: ", - size_str, NULL); - } - if (*endptr == 'k' || *endptr == 'K') { - if (size > APR_INT64_MAX / 1024) { - return "AuthDigestShmemSize value is too large"; - } - size *= 1024; - endptr++; - } - else if (*endptr == 'm' || *endptr == 'M') { - if (size > APR_INT64_MAX / (1024 * 1024)) { - return "AuthDigestShmemSize value is too large"; - } - size *= 1024 * 1024; - endptr++; - } - else if (*endptr == 'b' || *endptr == 'B') { - endptr++; - } - while (apr_isspace(*endptr)) { - endptr++; + rv = ap_size_parameter_parse(size_str, &size); + if (rv == APR_ERANGE) { + return "AuthDigestShmemSize value is too large"; } - if (*endptr != '\0') { + if (rv != APR_SUCCESS || size < 0) { return apr_pstrcat(cmd->pool, "Invalid size in AuthDigestShmemSize: ", size_str, NULL); } diff --git a/modules/aaa/mod_auth_form.c b/modules/aaa/mod_auth_form.c index d443092aa73..38f30d1a3b4 100644 --- a/modules/aaa/mod_auth_form.c +++ b/modules/aaa/mod_auth_form.c @@ -256,7 +256,7 @@ static const char *set_cookie_form_size(cmd_parms * cmd, void *config, auth_form_config_rec *conf = config; apr_off_t size; - if (APR_SUCCESS != apr_strtoff(&size, arg, NULL, 10) + if (APR_SUCCESS != ap_size_parameter_parse(arg, &size) || size < 0 || size > APR_SIZE_MAX) { return "AuthCookieFormSize must be a size in bytes, or zero."; } diff --git a/modules/cache/mod_cache_disk.c b/modules/cache/mod_cache_disk.c index 8d17a195f3c..27ce4c3f2d8 100644 --- a/modules/cache/mod_cache_disk.c +++ b/modules/cache/mod_cache_disk.c @@ -1481,7 +1481,7 @@ static const char { disk_cache_dir_conf *dconf = (disk_cache_dir_conf *)in_struct_ptr; - if (apr_strtoff(&dconf->minfs, arg, NULL, 10) != APR_SUCCESS || + if (ap_size_parameter_parse(arg, &dconf->minfs) != APR_SUCCESS || dconf->minfs < 0) { return "CacheMinFileSize argument must be a non-negative integer representing the min size of a file to cache in bytes."; @@ -1495,7 +1495,7 @@ static const char { disk_cache_dir_conf *dconf = (disk_cache_dir_conf *)in_struct_ptr; - if (apr_strtoff(&dconf->maxfs, arg, NULL, 10) != APR_SUCCESS || + if (ap_size_parameter_parse(arg, &dconf->maxfs) != APR_SUCCESS || dconf->maxfs < 0) { return "CacheMaxFileSize argument must be a non-negative integer representing the max size of a file to cache in bytes."; @@ -1509,7 +1509,7 @@ static const char { disk_cache_dir_conf *dconf = (disk_cache_dir_conf *)in_struct_ptr; - if (apr_strtoff(&dconf->readsize, arg, NULL, 10) != APR_SUCCESS || + if (ap_size_parameter_parse(arg, &dconf->readsize) != APR_SUCCESS || dconf->readsize < 0) { return "CacheReadSize argument must be a non-negative integer representing the max amount of data to cache in go."; @@ -1524,7 +1524,7 @@ static const char disk_cache_dir_conf *dconf = (disk_cache_dir_conf *)in_struct_ptr; apr_off_t milliseconds; - if (apr_strtoff(&milliseconds, arg, NULL, 10) != APR_SUCCESS || + if (ap_size_parameter_parse(arg, &milliseconds) != APR_SUCCESS || milliseconds < 0) { return "CacheReadTime argument must be a non-negative integer representing the max amount of time taken to cache in go."; diff --git a/modules/cache/mod_cache_socache.c b/modules/cache/mod_cache_socache.c index 38f1bfb2793..78bd3e7bb4c 100644 --- a/modules/cache/mod_cache_socache.c +++ b/modules/cache/mod_cache_socache.c @@ -1264,7 +1264,7 @@ static const char *set_cache_max(cmd_parms *parms, void *in_struct_ptr, { cache_socache_dir_conf *dconf = (cache_socache_dir_conf *) in_struct_ptr; - if (apr_strtoff(&dconf->max, arg, NULL, 10) != APR_SUCCESS + if (ap_size_parameter_parse(arg, &dconf->max) != APR_SUCCESS || dconf->max < 1024 || dconf->max > APR_UINT32_MAX) { return "CacheSocacheMaxSize argument must be a integer representing " "the max size of a cached entry (headers and body), at least 1024 " @@ -1280,7 +1280,7 @@ static const char *set_cache_maxtime(cmd_parms *parms, void *in_struct_ptr, cache_socache_dir_conf *dconf = (cache_socache_dir_conf *) in_struct_ptr; apr_off_t seconds; - if (apr_strtoff(&seconds, arg, NULL, 10) != APR_SUCCESS || seconds < 0) { + if (ap_size_parameter_parse(arg, &seconds) != APR_SUCCESS || seconds < 0) { return "CacheSocacheMaxTime argument must be the maximum amount of time in seconds to cache an entry."; } dconf->maxtime = apr_time_from_sec(seconds); @@ -1294,7 +1294,7 @@ static const char *set_cache_mintime(cmd_parms *parms, void *in_struct_ptr, cache_socache_dir_conf *dconf = (cache_socache_dir_conf *) in_struct_ptr; apr_off_t seconds; - if (apr_strtoff(&seconds, arg, NULL, 10) != APR_SUCCESS || seconds < 0) { + if (ap_size_parameter_parse(arg, &seconds) != APR_SUCCESS || seconds < 0) { return "CacheSocacheMinTime argument must be the minimum amount of time in seconds to cache an entry."; } dconf->mintime = apr_time_from_sec(seconds); @@ -1307,7 +1307,7 @@ static const char *set_cache_readsize(cmd_parms *parms, void *in_struct_ptr, { cache_socache_dir_conf *dconf = (cache_socache_dir_conf *) in_struct_ptr; - if (apr_strtoff(&dconf->readsize, arg, NULL, 10) != APR_SUCCESS + if (ap_size_parameter_parse(arg, &dconf->readsize) != APR_SUCCESS || dconf->readsize < 0) { return "CacheSocacheReadSize argument must be a non-negative integer representing the max amount of data to cache in go."; } @@ -1321,7 +1321,7 @@ static const char *set_cache_readtime(cmd_parms *parms, void *in_struct_ptr, cache_socache_dir_conf *dconf = (cache_socache_dir_conf *) in_struct_ptr; apr_off_t milliseconds; - if (apr_strtoff(&milliseconds, arg, NULL, 10) != APR_SUCCESS + if (ap_size_parameter_parse(arg, &milliseconds) != APR_SUCCESS || milliseconds < 0) { return "CacheSocacheReadTime argument must be a non-negative integer representing the max amount of time taken to cache in go."; } diff --git a/modules/filters/mod_buffer.c b/modules/filters/mod_buffer.c index 203e6724502..9f6519b0e33 100644 --- a/modules/filters/mod_buffer.c +++ b/modules/filters/mod_buffer.c @@ -321,8 +321,8 @@ static const char *set_buffer_size(cmd_parms *cmd, void *dconf, const char *arg) { buffer_conf *conf = dconf; - if (APR_SUCCESS != apr_strtoff(&(conf->size), arg, NULL, 10) || conf->size - <= 0) { + if (APR_SUCCESS != ap_size_parameter_parse(arg, &(conf->size)) + || conf->size <= 0) { return "BufferSize must be a size in bytes, and greater than zero"; } conf->size_set = 1; diff --git a/modules/filters/mod_substitute.c b/modules/filters/mod_substitute.c index fdb5b7dc1f0..86f6f8954ec 100644 --- a/modules/filters/mod_substitute.c +++ b/modules/filters/mod_substitute.c @@ -776,40 +776,17 @@ static const char *set_pattern(cmd_parms *cmd, void *cfg, const char *line) return NULL; } -#define KBYTE 1024 -#define MBYTE 1048576 -#define GBYTE 1073741824 static const char *set_max_line_length(cmd_parms *cmd, void *cfg, const char *arg) { subst_dir_conf *dcfg = (subst_dir_conf *)cfg; apr_off_t max; - char *end; apr_status_t rv; - rv = apr_strtoff(&max, arg, &end, 10); - if (rv == APR_SUCCESS) { - if ((*end == 'K' || *end == 'k') && !end[1]) { - if (max > APR_INT64_MAX / KBYTE) - return "SubstituteMaxLineLength value too large"; - max *= KBYTE; - } - else if ((*end == 'M' || *end == 'm') && !end[1]) { - if (max > APR_INT64_MAX / MBYTE) - return "SubstituteMaxLineLength value too large"; - max *= MBYTE; - } - else if ((*end == 'G' || *end == 'g') && !end[1]) { - if (max > APR_INT64_MAX / GBYTE) - return "SubstituteMaxLineLength value too large"; - max *= GBYTE; - } - else if (*end && /* neither empty nor [Bb] */ - ((*end != 'B' && *end != 'b') || end[1])) { - rv = APR_EGENERAL; - } + rv = ap_size_parameter_parse(arg, &max); + if (rv == APR_ERANGE) { + return "SubstituteMaxLineLength value too large"; } - if (rv != APR_SUCCESS || max < 0) { return "SubstituteMaxLineLength must be a non-negative integer optionally " diff --git a/modules/generators/mod_cgi.c b/modules/generators/mod_cgi.c index f0413ae9382..e81bb806b21 100644 --- a/modules/generators/mod_cgi.c +++ b/modules/generators/mod_cgi.c @@ -146,7 +146,14 @@ static const char *set_scriptlog_length(cmd_parms *cmd, void *dummy, cgi_server_conf *conf = ap_get_module_config(s->module_config, &cgi_module); - conf->logbytes = atol(arg); + apr_off_t bytes; + + if (ap_size_parameter_parse(arg, &bytes) != APR_SUCCESS + || bytes < 0 || bytes > LONG_MAX) { + return "ScriptLogLength must be a non-negative integer."; + } + + conf->logbytes = (long)bytes; return NULL; } @@ -157,7 +164,14 @@ static const char *set_scriptlog_buffer(cmd_parms *cmd, void *dummy, cgi_server_conf *conf = ap_get_module_config(s->module_config, &cgi_module); - conf->bufbytes = atoi(arg); + apr_off_t bytes; + + if (ap_size_parameter_parse(arg, &bytes) != APR_SUCCESS + || bytes < 0 || (apr_uint64_t)bytes > APR_SIZE_MAX) { + return "ScriptLogBuffer must be a non-negative integer."; + } + + conf->bufbytes = (apr_size_t)bytes; return NULL; } diff --git a/modules/generators/mod_cgid.c b/modules/generators/mod_cgid.c index 0498658c065..2633eb9bcd2 100644 --- a/modules/generators/mod_cgid.c +++ b/modules/generators/mod_cgid.c @@ -1160,7 +1160,14 @@ static const char *set_scriptlog_length(cmd_parms *cmd, void *dummy, const char cgid_server_conf *conf = ap_get_module_config(s->module_config, &cgid_module); - conf->logbytes = atol(arg); + apr_off_t bytes; + + if (ap_size_parameter_parse(arg, &bytes) != APR_SUCCESS + || bytes < 0 || bytes > LONG_MAX) { + return "ScriptLogLength must be a non-negative integer."; + } + + conf->logbytes = (long)bytes; return NULL; } @@ -1170,7 +1177,14 @@ static const char *set_scriptlog_buffer(cmd_parms *cmd, void *dummy, const char cgid_server_conf *conf = ap_get_module_config(s->module_config, &cgid_module); - conf->bufbytes = atoi(arg); + apr_off_t bytes; + + if (ap_size_parameter_parse(arg, &bytes) != APR_SUCCESS + || bytes < 0 || bytes > INT_MAX) { + return "ScriptLogBuffer must be a non-negative integer."; + } + + conf->bufbytes = (int)bytes; return NULL; } diff --git a/server/core.c b/server/core.c index df270b019e4..e608d769956 100644 --- a/server/core.c +++ b/server/core.c @@ -58,6 +58,8 @@ #include "mod_so.h" /* for ap_find_loaded_module_symbol */ +#include /* for LONG_MAX */ + #if defined(RLIMIT_CPU) || defined (RLIMIT_DATA) || defined (RLIMIT_VMEM) || defined(RLIMIT_AS) || defined (RLIMIT_NPROC) #include "unixd.h" #endif @@ -3761,14 +3763,17 @@ static const char *set_limit_req_body(cmd_parms *cmd, void *conf_, const char *arg) { core_dir_config *conf = conf_; - char *errp; + apr_off_t limit; + apr_status_t rv; - if (APR_SUCCESS != apr_strtoff(&conf->limit_req_body, arg, &errp, 10)) { + rv = ap_size_parameter_parse(arg, &limit); + if (rv == APR_ERANGE) { return "LimitRequestBody argument is not parsable."; } - if (*errp || conf->limit_req_body < 0) { + if (rv != APR_SUCCESS || limit < 0) { return "LimitRequestBody requires a non-negative integer."; } + conf->limit_req_body = limit; return NULL; } @@ -3777,16 +3782,30 @@ static const char *set_limit_xml_req_body(cmd_parms *cmd, void *conf_, const char *arg) { core_dir_config *conf = conf_; + apr_uint64_t max = AP_MAX_LIMIT_XML_BODY; + apr_off_t limit; + apr_status_t rv; - conf->limit_xml_body = atol(arg); - if (conf->limit_xml_body < 0) + rv = ap_size_parameter_parse(arg, &limit); + if (rv == APR_ERANGE) { + return "LimitXMLRequestBody argument is not parsable."; + } + if (rv != APR_SUCCESS || limit < 0) { return "LimitXMLRequestBody requires a non-negative integer."; + } - /* zero is AP_MAX_LIMIT_XML_BODY (implicitly) */ - if ((apr_size_t)conf->limit_xml_body > AP_MAX_LIMIT_XML_BODY) + /* zero is AP_MAX_LIMIT_XML_BODY (implicitly), and limit_xml_body is + * a long, which is narrower than apr_size_t on some platforms. + */ + if (max > LONG_MAX) { + max = LONG_MAX; + } + if ((apr_uint64_t)limit > max) { return apr_psprintf(cmd->pool, "LimitXMLRequestBody must not exceed " - "%" APR_SIZE_T_FMT, AP_MAX_LIMIT_XML_BODY); + "%" APR_UINT64_T_FMT, max); + } + conf->limit_xml_body = (long)limit; return NULL; } diff --git a/server/util.c b/server/util.c index 5eff6ba4209..304722b11ba 100644 --- a/server/util.c +++ b/server/util.c @@ -2681,6 +2681,54 @@ AP_DECLARE(apr_status_t) ap_timeout_parameter_parse( *timeout = (apr_interval_time_t)check; return APR_SUCCESS; } + +AP_DECLARE(apr_status_t) ap_size_parameter_parse(const char *parameter, + apr_off_t *value) +{ + /* The maximum apr_off_t value. */ + const apr_off_t off_max = sizeof(apr_off_t) == 8 ? APR_INT64_MAX + : APR_INT32_MAX; + apr_off_t parsed, multiplier = 1; + apr_status_t rv; + char *end; + + /* Check for no conversion before the status: some libc implementations + * set EINVAL when no digits are present. */ + rv = apr_strtoff(&parsed, parameter, &end, 10); + if (end == parameter) { + return APR_EINVAL; + } + if (rv != APR_SUCCESS) { + return APR_ERANGE; + } + + if (*end == 'k' || *end == 'K') { + multiplier = 1024; + end++; + } + else if (*end == 'm' || *end == 'M') { + multiplier = 1024 * 1024; + end++; + } + else if (*end == 'g' || *end == 'G') { + multiplier = 1024 * 1024 * 1024; + end++; + } + else if (*end == 'b' || *end == 'B') { + end++; + } + if (*end != '\0') { + return APR_EINVAL; + } + + if (parsed > off_max / multiplier + || parsed < (-off_max - 1) / multiplier) { + return APR_ERANGE; + } + + *value = parsed * multiplier; + return APR_SUCCESS; +} #undef CHECK_OVERFLOW AP_DECLARE(int) ap_parse_strict_length(apr_off_t *len, const char *str) diff --git a/test/modules/core/test_010_limit_xml_request_body.py b/test/modules/core/test_010_limit_xml_request_body.py index 89052222df4..e9431d9739f 100644 --- a/test/modules/core/test_010_limit_xml_request_body.py +++ b/test/modules/core/test_010_limit_xml_request_body.py @@ -27,7 +27,8 @@ def test_core_010_001(self, env, value): # (without the change, only "-1" was rejected) @pytest.mark.parametrize("value", [ pytest.param(v, marks=not_backported( - "r1939121", "rejecting invalid LimitXMLRequestBody values")) + "r1939121", "rejecting invalid LimitXMLRequestBody values", + backported_in="2.4.72")) for v in ["abc123", "123abc", "123foo456", "1.5", "0x10", "+", "-"] ] + ["-1"]) def test_core_010_002(self, env, value): @@ -41,7 +42,7 @@ def test_core_010_002(self, env, value): ]) @not_backported("r1939121", "rejecting LimitXMLRequestBody values beyond a " "32-bit long, which atol() truncates on Windows", - platform="win32") + platform="win32", backported_in="2.4.72") def test_core_010_003(self, env, value): r = self.check_config(env, value) assert r.exit_code != 0, f"{value} was accepted" diff --git a/test/modules/core/test_012_config_off_t.py b/test/modules/core/test_012_config_off_t.py index 235487b2ae8..653f06674a0 100644 --- a/test/modules/core/test_012_config_off_t.py +++ b/test/modules/core/test_012_config_off_t.py @@ -15,9 +15,10 @@ "CacheSocacheReadTime", ] -OFF_T = not_backported("r1939129, r1939163", "strict size parsing for off_t directives") +OFF_T = not_backported("r1939129, r1939163", "strict size parsing for off_t directives", + backported_in="2.4.72") OFF_T_WIN = not_backported("r1939129, r1939163", "strict size parsing for off_t directives", - platform="win32") + platform="win32", backported_in="2.4.72") # Without the change (2.4.x), these directives take the number at the start of # a value and ignore the rest, and these also take a value without one; diff --git a/test/modules/filters/test_004_buffer_size.py b/test/modules/filters/test_004_buffer_size.py index d1b37b80d92..f0ef804f116 100644 --- a/test/modules/filters/test_004_buffer_size.py +++ b/test/modules/filters/test_004_buffer_size.py @@ -5,7 +5,7 @@ from pyhttpd.backport import not_backported from pyhttpd.conf import HttpdConf -STRICT = not_backported("r1939129", "strict BufferSize parsing") +STRICT = not_backported("r1939129", "strict BufferSize parsing", backported_in="2.4.72") class TestBufferSize: