Skip to content

Commit dbd1485

Browse files
committed
Add integer overflow checks to URL escape allocation functions
1 parent 3b3af44 commit dbd1485

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

server/util.c

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1816,8 +1816,10 @@ AP_DECLARE(char *) ap_escape_shell_cmd(apr_pool_t *p, const char *str)
18161816
char *cmd;
18171817
unsigned char *d;
18181818
const unsigned char *s;
1819+
apr_size_t len = strlen(str);
18191820

1820-
cmd = apr_palloc(p, 2 * strlen(str) + 1); /* Be safe */
1821+
ap_assert(len <= (APR_SIZE_MAX - 1) / 2);
1822+
cmd = apr_palloc(p, 2 * len + 1);
18211823
d = (unsigned char *)cmd;
18221824
s = (const unsigned char *)str;
18231825
for (; *s; ++s) {
@@ -2073,7 +2075,9 @@ AP_DECLARE(char *) ap_escape_path_segment_buffer(char *copy, const char *segment
20732075

20742076
AP_DECLARE(char *) ap_escape_path_segment(apr_pool_t *p, const char *segment)
20752077
{
2076-
return ap_escape_path_segment_buffer(apr_palloc(p, 3 * strlen(segment) + 1), segment);
2078+
apr_size_t len = strlen(segment);
2079+
ap_assert(len <= (APR_SIZE_MAX - 1) / 3);
2080+
return ap_escape_path_segment_buffer(apr_palloc(p, 3 * len + 1), segment);
20772081
}
20782082

20792083
AP_DECLARE(char *) ap_os_escape_path(apr_pool_t *p, const char *path, int partial)
@@ -2082,11 +2086,17 @@ AP_DECLARE(char *) ap_os_escape_path(apr_pool_t *p, const char *path, int partia
20822086
* Allocate another +1 to allow the caller to add a trailing '/' (see
20832087
* comment in 'ap_sub_req_lookup_dirent')
20842088
*/
2085-
char *copy = apr_palloc(p, 3 * strlen(path) + 3 + 1);
2086-
const unsigned char *s = (const unsigned char *)path;
2087-
unsigned char *d = (unsigned char *)copy;
2089+
apr_size_t len = strlen(path);
2090+
char *copy;
2091+
const unsigned char *s;
2092+
unsigned char *d;
20882093
unsigned c;
20892094

2095+
ap_assert(len <= (APR_SIZE_MAX - 4) / 3);
2096+
copy = apr_palloc(p, 3 * len + 3 + 1);
2097+
s = (const unsigned char *)path;
2098+
d = (unsigned char *)copy;
2099+
20902100
if (!partial) {
20912101
const char *colon = ap_strchr_c(path, ':');
20922102
const char *slash = ap_strchr_c(path, '/');
@@ -2133,7 +2143,9 @@ AP_DECLARE(char *) ap_escape_urlencoded_buffer(char *copy, const char *buffer)
21332143

21342144
AP_DECLARE(char *) ap_escape_urlencoded(apr_pool_t *p, const char *buffer)
21352145
{
2136-
return ap_escape_urlencoded_buffer(apr_palloc(p, 3 * strlen(buffer) + 1), buffer);
2146+
apr_size_t len = strlen(buffer);
2147+
ap_assert(len <= (APR_SIZE_MAX - 1) / 3);
2148+
return ap_escape_urlencoded_buffer(apr_palloc(p, 3 * len + 1), buffer);
21372149
}
21382150

21392151
/* ap_escape_uri is now a macro for os_escape_path */

0 commit comments

Comments
 (0)