Skip to content

CFURL: fix out-of-bounds read when unescaping a trailing percent#39

Open
DTW-Thalion wants to merge 1 commit into
gnustep:masterfrom
DTW-Thalion:fix/cfurl-percent-decode-oob
Open

CFURL: fix out-of-bounds read when unescaping a trailing percent#39
DTW-Thalion wants to merge 1 commit into
gnustep:masterfrom
DTW-Thalion:fix/cfurl-percent-decode-oob

Conversation

@DTW-Thalion

Copy link
Copy Markdown
Contributor

CFURLCreateStringByReplacingPercentEscapesUsingEncoding() read the two
hex digits (and the following look-ahead character) after a '%' with
"CFStringGetCharacterAtIndex(origString, ++i)" and never checked i
against the string length. A string ending in '%' (or '%' followed by a
single hex digit) therefore read one or more characters past the end.
CFStringGetCharacterAtIndex indexes the backing store directly, so for a
string whose buffer is exactly its length (e.g. a no-copy string) this is
a genuine out-of-bounds read. Under valgrind, for "abc%":

Invalid read of size 1 ... CFURLCreateStringByReplacingPercentEscapesUsingEncoding (CFURL.c:1594)

It is reachable from the public CFURLCreateStringByReplacingPercentEscapes.

Bound each index against origLen: a '%' not followed by two hex digits is
rejected (success = false), and the look-ahead for a further '%' yields 0
at the end of the string so the loop terminates normally. Adds a
regression test using a no-copy string ending in '%'.

CFURLCreateStringByReplacingPercentEscapesUsingEncoding() read the two
hex digits (and the following look-ahead character) after a '%' with
"CFStringGetCharacterAtIndex(origString, ++i)" and never checked i
against the string length.  A string ending in '%' (or '%' followed by a
single hex digit) therefore read one or more characters past the end.
CFStringGetCharacterAtIndex indexes the backing store directly, so for a
string whose buffer is exactly its length (e.g. a no-copy string) this is
a genuine out-of-bounds read.  Under valgrind, for "abc%":

  Invalid read of size 1 ... CFURLCreateStringByReplacingPercentEscapesUsingEncoding (CFURL.c:1594)

It is reachable from the public CFURLCreateStringByReplacingPercentEscapes.

Bound each index against origLen: a '%' not followed by two hex digits is
rejected (success = false), and the look-ahead for a further '%' yields 0
at the end of the string so the loop terminates normally.  Adds a
regression test using a no-copy string ending in '%'.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant