Bug 206572 - KeyedDecoderGeneric crashes when it accesses a data with empty string key.
Summary: KeyedDecoderGeneric crashes when it accesses a data with empty string key.
Status: RESOLVED FIXED
Alias: None
Product: WebKit
Classification: Unclassified
Component: Platform (show other bugs)
Version: WebKit Nightly Build
Hardware: Unspecified Unspecified
: P2 Normal
Assignee: Nobody
URL:
Keywords: InRadar
Depends on:
Blocks:
 
Reported: 2020-01-21 23:28 PST by Takashi Komori
Modified: 2020-01-23 02:59 PST (History)
11 users (show)

See Also:


Attachments
Patch (3.09 KB, patch)
2020-01-21 23:46 PST, Takashi Komori
no flags Details | Formatted Diff | Diff
Patch (3.04 KB, patch)
2020-01-23 00:57 PST, Takashi Komori
no flags Details | Formatted Diff | Diff
Patch (3.21 KB, patch)
2020-01-23 01:41 PST, Takashi Komori
no flags Details | Formatted Diff | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Takashi Komori 2020-01-21 23:28:42 PST
When KeyedDecoderGeneric decodes a record associated with empty string key, it tries to add a record to a HashMap with null string (not empty string) but adding HashMap fails because HashMap can't treat null string as a key.

While adding a record with null string key, StringHash::hash() crashes by accessing null pointer.
Comment 1 Takashi Komori 2020-01-21 23:46:26 PST
Created attachment 388399 [details]
Patch
Comment 2 Takashi Komori 2020-01-21 23:51:05 PST
Added test just encodes/decodes a boolean data with empty string key but I only tested on wincairo port.
Comment 3 Fujii Hironori 2020-01-22 00:52:06 PST
How did you find this bug? Is this a real use case? empty string key.
Comment 4 Takashi Komori 2020-01-22 02:37:21 PST
(In reply to Fujii Hironori from comment #3)
> How did you find this bug? Is this a real use case? empty string key.

This crash occurred and found when I tried to decode broken encoded data (zero filled from middle) by chance.
So I think it doesn't occur in normal scenarios.

To avoid decoding collapsed data, I tried to verify encoded data by using Decoder::verifyChecksum (PersistentDecoder.cpp)
But it didn't work because we can't verify before all decoding is done.
Persistence::Decoder calculates checksum while decoding data.
Comment 5 Fujii Hironori 2020-01-22 03:25:17 PST
String::fromUTF8 will return a null string for invalid UTF-8 sequence.
You should check null-string after caling String::fromUTF8.

  result = String::fromUTF8(buffer.data(), size);
Comment 6 Takashi Komori 2020-01-23 00:57:41 PST
Created attachment 388524 [details]
Patch
Comment 7 Takashi Komori 2020-01-23 00:59:01 PST
(In reply to Fujii Hironori from comment #5)
> String::fromUTF8 will return a null string for invalid UTF-8 sequence.
> You should check null-string after caling String::fromUTF8.
> 
>   result = String::fromUTF8(buffer.data(), size);

Fixed.
Comment 8 Fujii Hironori 2020-01-23 01:02:11 PST
Comment on attachment 388524 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=388524&action=review

> Source/WebCore/platform/generic/KeyedDecoderGeneric.cpp:61
> +        result = emptyString();

I think if String::fromUTF8 is failed, a null string and false should be returned.

Of course, an empty string should be return for size == 0 case.

WDTY?
Comment 9 Takashi Komori 2020-01-23 01:41:51 PST
Created attachment 388525 [details]
Patch
Comment 10 Takashi Komori 2020-01-23 01:44:31 PST
(In reply to Fujii Hironori from comment #8)
> Comment on attachment 388524 [details]
> Patch
> 
> View in context:
> https://bugs.webkit.org/attachment.cgi?id=388524&action=review
> 
> > Source/WebCore/platform/generic/KeyedDecoderGeneric.cpp:61
> > +        result = emptyString();
> 
> I think if String::fromUTF8 is failed, a null string and false should be
> returned.
> 
> Of course, an empty string should be return for size == 0 case.
> 
> WDTY?

I agree. Failing fromUTF8 means decoded string is collapsed.
Fixed.
Comment 11 WebKit Commit Bot 2020-01-23 02:58:21 PST
The commit-queue encountered the following flaky tests while processing attachment 388525 [details]:

editing/spelling/spellcheck-attribute.html bug 206178 (authors: g.czajkowski@samsung.com, mark.lam@apple.com, and rniwa@webkit.org)
The commit-queue is continuing to process your patch.
Comment 12 WebKit Commit Bot 2020-01-23 02:58:53 PST
Comment on attachment 388525 [details]
Patch

Clearing flags on attachment: 388525

Committed r254971: <https://trac.webkit.org/changeset/254971>
Comment 13 WebKit Commit Bot 2020-01-23 02:58:54 PST
All reviewed patches have been landed.  Closing bug.
Comment 14 Radar WebKit Bug Importer 2020-01-23 02:59:14 PST
<rdar://problem/58829480>