Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Add integer overflow checks to URL escape allocation functions
  • Loading branch information
kodareef5 committed Apr 8, 2026
commit dbd148598f811d93d31bc55b6ebdb161dc76c700
24 changes: 18 additions & 6 deletions server/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -1816,8 +1816,10 @@ AP_DECLARE(char *) ap_escape_shell_cmd(apr_pool_t *p, const char *str)
char *cmd;
unsigned char *d;
const unsigned char *s;
apr_size_t len = strlen(str);

cmd = apr_palloc(p, 2 * strlen(str) + 1); /* Be safe */
ap_assert(len <= (APR_SIZE_MAX - 1) / 2);
cmd = apr_palloc(p, 2 * len + 1);
d = (unsigned char *)cmd;
s = (const unsigned char *)str;
for (; *s; ++s) {
Expand Down Expand Up @@ -2073,7 +2075,9 @@ AP_DECLARE(char *) ap_escape_path_segment_buffer(char *copy, const char *segment

AP_DECLARE(char *) ap_escape_path_segment(apr_pool_t *p, const char *segment)
{
return ap_escape_path_segment_buffer(apr_palloc(p, 3 * strlen(segment) + 1), segment);
apr_size_t len = strlen(segment);
ap_assert(len <= (APR_SIZE_MAX - 1) / 3);
return ap_escape_path_segment_buffer(apr_palloc(p, 3 * len + 1), segment);
}

AP_DECLARE(char *) ap_os_escape_path(apr_pool_t *p, const char *path, int partial)
Expand All @@ -2082,11 +2086,17 @@ AP_DECLARE(char *) ap_os_escape_path(apr_pool_t *p, const char *path, int partia
* Allocate another +1 to allow the caller to add a trailing '/' (see
* comment in 'ap_sub_req_lookup_dirent')
*/
char *copy = apr_palloc(p, 3 * strlen(path) + 3 + 1);
const unsigned char *s = (const unsigned char *)path;
unsigned char *d = (unsigned char *)copy;
apr_size_t len = strlen(path);
char *copy;
const unsigned char *s;
unsigned char *d;
unsigned c;

ap_assert(len <= (APR_SIZE_MAX - 4) / 3);
copy = apr_palloc(p, 3 * len + 3 + 1);
s = (const unsigned char *)path;
d = (unsigned char *)copy;

if (!partial) {
const char *colon = ap_strchr_c(path, ':');
const char *slash = ap_strchr_c(path, '/');
Expand Down Expand Up @@ -2133,7 +2143,9 @@ AP_DECLARE(char *) ap_escape_urlencoded_buffer(char *copy, const char *buffer)

AP_DECLARE(char *) ap_escape_urlencoded(apr_pool_t *p, const char *buffer)
{
return ap_escape_urlencoded_buffer(apr_palloc(p, 3 * strlen(buffer) + 1), buffer);
apr_size_t len = strlen(buffer);
ap_assert(len <= (APR_SIZE_MAX - 1) / 3);
return ap_escape_urlencoded_buffer(apr_palloc(p, 3 * len + 1), buffer);
}

/* ap_escape_uri is now a macro for os_escape_path */
Expand Down