From: Yuxuan Shui <yshui@codeweavers.com> parse_canonicalize will stop writing into the output buffer when the amount to write exceeded output_len, but it will still call remove_dot_segments and give it a path length that goes past the end of the output buffer, causing it to read out of bound. Additionally, we can't simply reject the input once we exceeded the capacity of the output buffer because dot segments removal might bring the length down within the limit. The current implementation is incorrect in this case since the output has already been cut off. This commit creates a temporary buffer and does dot segments removal first, and then everything else. There is further evidence that this is how native does it too, because if a path segment is "%2E." (%2E is "." when decoded), native will not remove it, which it would have done if dot segments removal happens after unescaping. This is included as an additional uri_parse_test. --- dlls/iertutil/uri.c | 53 +++++++++++++++++++---------------------- dlls/urlmon/tests/uri.c | 1 + 2 files changed, 25 insertions(+), 29 deletions(-) diff --git a/dlls/iertutil/uri.c b/dlls/iertutil/uri.c index 38408c4baf5..c2ad0915bf8 100644 --- a/dlls/iertutil/uri.c +++ b/dlls/iertutil/uri.c @@ -6332,8 +6332,8 @@ static HRESULT combine_uri(Uri *base, Uri *relative, DWORD flags, IUri **result, static HRESULT parse_canonicalize(const Uri *uri, DWORD flags, LPWSTR output, DWORD output_len, DWORD *result_len) { - const WCHAR *ptr = NULL; - WCHAR *path = NULL; + const WCHAR *ptr = NULL, *end_ptr = NULL; + WCHAR *reduced = NULL; const WCHAR **pptr; DWORD len = 0; BOOL reduce_path; @@ -6354,24 +6354,28 @@ static HRESULT parse_canonicalize(const Uri *uri, DWORD flags, LPWSTR output, reduce_path = !(flags & URL_DONT_SIMPLIFY) && ptr && check_hierarchical(pptr); - for(ptr = uri->canon_uri; ptr < uri->canon_uri+uri->canon_len; ++ptr) { - BOOL do_default_action = TRUE; - - /* Keep track of the path if we need to remove dot segments from - * it later. - */ - if(reduce_path && !path && ptr == uri->canon_uri+uri->path_start) - path = output+len; - - /* Check if it's time to reduce the path. */ - if(reduce_path && ptr == uri->canon_uri+uri->path_start+uri->path_len) { - DWORD current_path_len = (output+len) - path; - DWORD new_path_len = remove_dot_segments(path, current_path_len); + /* Reduce path first, otherwise it's difficult to check if the output will exceed + * output_len during escaping/unescaping. */ + if (reduce_path && uri->path_start > -1) + { + DWORD path_end, new_path_end; + path_end = uri->path_start+uri->path_len; + reduced = malloc(sizeof(WCHAR) * uri->canon_len); + memcpy(reduced, uri->canon_uri, sizeof(WCHAR) * path_end); + new_path_end = uri->path_start+remove_dot_segments(reduced+uri->path_start, uri->path_len); + memcpy(reduced+new_path_end, uri->canon_uri+path_end, + sizeof(WCHAR) * (uri->canon_len-path_end)); + ptr = reduced; + end_ptr = ptr+new_path_end+(uri->canon_len-path_end); + } + else + { + ptr = uri->canon_uri; + end_ptr = ptr+uri->canon_len; + } - /* Update the current length. */ - len -= (current_path_len-new_path_len); - reduce_path = FALSE; - } + for(; ptr < end_ptr; ++ptr) { + BOOL do_default_action = TRUE; if(*ptr == '%') { const WCHAR decoded = decode_pct_val(ptr); @@ -6416,16 +6420,7 @@ static HRESULT parse_canonicalize(const Uri *uri, DWORD flags, LPWSTR output, } } - /* Sometimes the path is the very last component of the IUri, so - * see if the dot segments need to be reduced now. - */ - if(reduce_path && path) { - DWORD current_path_len = (output+len) - path; - DWORD new_path_len = remove_dot_segments(path, current_path_len); - - /* Update the current length. */ - len -= (current_path_len-new_path_len); - } + if (reduced) free(reduced); if(len < output_len) output[len] = 0; diff --git a/dlls/urlmon/tests/uri.c b/dlls/urlmon/tests/uri.c index ec8a451aba1..6767f278579 100644 --- a/dlls/urlmon/tests/uri.c +++ b/dlls/urlmon/tests/uri.c @@ -8270,6 +8270,7 @@ static const uri_parse_test uri_parse_tests[] = { {"http://google.com/test/../",Uri_CREATE_NO_CANONICALIZE,PARSE_CANONICALIZE,URL_NO_META,"http://google.com/test/../",S_OK,FALSE}, {"http://google.com/test/../",Uri_CREATE_NO_CANONICALIZE,PARSE_CANONICALIZE,0,"http://google.com/",S_OK,FALSE}, {"zip://google.com/test/../",Uri_CREATE_NO_CANONICALIZE,PARSE_CANONICALIZE,0,"zip://google.com/",S_OK,FALSE}, + {"zip://google.com/test/%2E./",Uri_CREATE_NO_CANONICALIZE,PARSE_CANONICALIZE,URL_UNESCAPE,"zip://google.com/test/../",S_OK,FALSE,TRUE}, {"file:///c:/test/../test",Uri_CREATE_NO_CANONICALIZE,PARSE_CANONICALIZE,URL_DONT_SIMPLIFY,"file:///c:/test/../test",S_OK,FALSE}, /* PARSE_FRIENDLY tests. */ -- GitLab https://gitlab.winehq.org/wine/wine/-/merge_requests/8331