Closed Bug 1871623 Opened 2 years ago Closed 8 months ago

Double-free in icu_73::Locale::Locale

Categories

(Core :: JavaScript: Internationalization API, defect, P2)

Firefox 121
defect

Tracking

()

RESOLVED FIXED
Tracking Status
firefox-esr115 --- unaffected
firefox123 --- unaffected

People

(Reporter: d4ni31, Unassigned)

References

(Blocks 1 open bug)

Details

(Keywords: csectype-uaf, reporter-external, sec-other)

User Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36

Steps to reproduce:

Details

https://searchfox.org/mozilla-central/source/intl/icu/source/common/uloc_tag.cpp#2100

in ultag_parse()
    t->buf = tagBuf;
....

            if (tagLen < newTagLength) {
                uprv_free(tagBuf);
                tagBuf = (char*)uprv_malloc(newTagLength + 1);
                if (tagBuf == nullptr) {
                    *status = U_MEMORY_ALLOCATION_ERROR;
                    return nullptr;
                }
                t->buf = tagBuf;
                tagLen = newTagLength;
            }

The problem is in case of the U_MEMORY_ALLOCATION_ERROR, we freed tagBuf but that value is still in t->buf. We should move the line t->buf = tagBuf; up before the if block so the freed old value of tagBuf will no longer in t->buf

patch diff

diff --git a/source/common/uloc_tag.cpp b/source/common/uloc_tag.cpp
index 43d597549..8f3778be5 100644
--- a/source/common/uloc_tag.cpp
+++ b/source/common/uloc_tag.cpp
@@ -2092,12 +2092,13 @@ ultag_parse(const char* tag, int32_t tagLen, int32_t* parsedLen, UErrorCode* sta
             int32_t oldTagLength = tagLen;
             if (tagLen < newTagLength) {
                 uprv_free(tagBuf);
-                tagBuf = (char*)uprv_malloc(newTagLength + 1);
+                // Change t->buf after the free and before return to avoid the second double free in
+                // the destructor of t when t is out of scope.
+                t->buf = tagBuf = (char*)uprv_malloc(newTagLength + 1);
                 if (tagBuf == nullptr) {
                     *status = U_MEMORY_ALLOCATION_ERROR;
                     return nullptr;
                 }
-                t->buf = tagBuf;
                 tagLen = newTagLength;
             }
             parsedLenDelta = checkLegacyLen - replacementLen;
diff --git a/source/test/cintltst/cloctst.c b/source/test/cintltst/cloctst.c
index 3edd87851..f11efc192 100644
--- a/source/test/cintltst/cloctst.c
+++ b/source/test/cintltst/cloctst.c
@@ -2508,6 +2508,10 @@ static void TestCanonicalization21749StackUseAfterScope(void)
                 input, u_errorName(status));
         return;
     }
+
+    // ICU-22475 test that we don't free an internal buffer twice.
+    status = U_ZERO_ERROR;
+    uloc_canonicalize("ti-defaultgR-lS-z-UK-0P", buffer, UPRV_LENGTHOF(buffer), &status);
 }
 
 static void TestDisplayKeywords(void)

AddressSanitizer

ASan 1

==13==ERROR: AddressSanitizer: attempting double-free on 0x60b00000c590 in thread T0:
SCARINESS: 42 (double-free)
    #0 0x4a0c12 in free /src/llvm-project/compiler-rt/lib/asan/asan_malloc_linux.cpp:52:3
    #1 0x5f761d in uprv_free_73 /src/icu/icu4c/source/common/cmemory.cpp:99:13
    #2 0x5a5a65 in ultag_close /src/icu/icu4c/source/common/uloc_tag.cpp:2447:5
    #3 0x5a5a65 in icu_73::LocalULanguageTagPointer::~LocalULanguageTagPointer() /src/icu/icu4c/source/common/uloc_tag.cpp:364:1
    #4 0x59ff04 in ultag_parse /src/icu/icu4c/source/common/uloc_tag.cpp:2433:1
    #5 0x59ff04 in ulocimp_forLanguageTag_73 /src/icu/icu4c/source/common/uloc_tag.cpp:2791:38
    #6 0x59d5d6 in uloc_forLanguageTag_73 /src/icu/icu4c/source/common/uloc_tag.cpp:2761:5
    #7 0x589f22 in _ConvertBCP47 /src/icu/icu4c/source/common/uloc.cpp:485:28
    #8 0x589f22 in _canonicalize(char const*, icu_73::ByteSink&, unsigned int, UErrorCode*) /src/icu/icu4c/source/common/uloc.cpp:1518:27
    #9 0x58aedf in ulocimp_canonicalize_73 /src/icu/icu4c/source/common/uloc.cpp:1974:5
    #10 0x58aedf in uloc_canonicalize_73 /src/icu/icu4c/source/common/uloc.cpp:1952:5
    #11 0x4de4b2 in LLVMFuzzerTestOneInput /src/icu/icu4c/source/test/fuzzer/uloc_canonicalize_fuzzer.cpp:17:3

DEDUP_TOKEN: free--uprv_free_73--ultag_close
0x60b00000c590 is located 0 bytes inside of 101-byte region [0x60b00000c590,0x60b00000c5f5)
freed by thread T0 here:
    #0 0x4a0c12 in free /src/llvm-project/compiler-rt/lib/asan/asan_malloc_linux.cpp:52:3
    #1 0x5f761d in uprv_free_73 /src/icu/icu4c/source/common/cmemory.cpp:99:13
    #2 0x59ddec in ultag_parse /src/icu/icu4c/source/common/uloc_tag.cpp:2094:17
    #3 0x59ddec in ulocimp_forLanguageTag_73 /src/icu/icu4c/source/common/uloc_tag.cpp:2791:38
    #4 0x59d5d6 in uloc_forLanguageTag_73 /src/icu/icu4c/source/common/uloc_tag.cpp:2761:5
    #5 0x589f22 in _ConvertBCP47 /src/icu/icu4c/source/common/uloc.cpp:485:28
    #6 0x589f22 in _canonicalize(char const*, icu_73::ByteSink&, unsigned int, UErrorCode*) /src/icu/icu4c/source/common/uloc.cpp:1518:27
    #7 0x58aedf in ulocimp_canonicalize_73 /src/icu/icu4c/source/common/uloc.cpp:1974:5
    #8 0x58aedf in uloc_canonicalize_73 /src/icu/icu4c/source/common/uloc.cpp:1952:5
    #9 0x4de4b2 in LLVMFuzzerTestOneInput /src/icu/icu4c/source/test/fuzzer/uloc_canonicalize_fuzzer.cpp:17:3

DEDUP_TOKEN: free--uprv_free_73--ultag_parse
previously allocated by thread T0 here:
    #0 0x4a0eb6 in __interceptor_malloc /src/llvm-project/compiler-rt/lib/asan/asan_malloc_linux.cpp:69:3
    #1 0x4debd9 in malloc (/out/uloc_canonicalize_fuzzer+0x4debd9)
    #2 0x5f7479 in uprv_malloc_73 /src/icu/icu4c/source/common/cmemory.cpp:58:20
    #3 0x59da1e in ultag_parse /src/icu/icu4c/source/common/uloc_tag.cpp:2045:21
    #4 0x59da1e in ulocimp_forLanguageTag_73 /src/icu/icu4c/source/common/uloc_tag.cpp:2791:38
    #5 0x59d5d6 in uloc_forLanguageTag_73 /src/icu/icu4c/source/common/uloc_tag.cpp:2761:5
    #6 0x589f22 in _ConvertBCP47 /src/icu/icu4c/source/common/uloc.cpp:485:28
    #7 0x589f22 in _canonicalize(char const*, icu_73::ByteSink&, unsigned int, UErrorCode*) /src/icu/icu4c/source/common/uloc.cpp:1518:27
    #8 0x58aedf in ulocimp_canonicalize_73 /src/icu/icu4c/source/common/uloc.cpp:1974:5
    #9 0x58aedf in uloc_canonicalize_73 /src/icu/icu4c/source/common/uloc.cpp:1952:5
    #10 0x4de4b2 in LLVMFuzzerTestOneInput /src/icu/icu4c/source/test/fuzzer/uloc_canonicalize_fuzzer.cpp:17:3

NULL alloc : malloc(107) 
#4 0x5f7479 in (null) /work/icu/common//src/icu/icu4c/source/common/cmemory.cpp:58
#5 0x59ddfe in ultag_parse /work/icu/common//src/icu/icu4c/source/common/uloc_tag.cpp:2095
#6 0x59ddfe in ulocimp_forLanguageTag_73 /work/icu/common//src/icu/icu4c/source/common/uloc_tag.cpp:2791
#7 0x59d5d6 in uloc_forLanguageTag_73 /work/icu/common//src/icu/icu4c/source/common/uloc_tag.cpp:2761
#8 0x589f22 in _ConvertBCP47 /work/icu/common//src/icu/icu4c/source/common/uloc.cpp:485
#9 0x589f22 in _canonicalize /work/icu/common//src/icu/icu4c/source/common/uloc.cpp:1518
#10 0x58aedf in ulocimp_canonicalize_73 /work/icu/common//src/icu/icu4c/source/common/uloc.cpp:1974
#11 0x58aedf in uloc_canonicalize_73 /work/icu/common//src/icu/icu4c/source/common/uloc.cpp:1952
#12 0x4de4b2 in LLVMFuzzerTestOneInput /work/icu//src/icu/icu4c/source/test/fuzzer/uloc_canonicalize_fuzzer.cpp:17

ASan 2

Failing allocation is malloc(15) 
#4 0x63e929 in (null) /work/icu/common//src/icu/icu4c/source/common/cmemory.cpp:58
#5 0x5d059e in ultag_parse /work/icu/common//src/icu/icu4c/source/common/uloc_tag.cpp:2095
#6 0x5d059e in ulocimp_forLanguageTag_73 /work/icu/common//src/icu/icu4c/source/common/uloc_tag.cpp:2791
#7 0x5cfd76 in uloc_forLanguageTag_73 /work/icu/common//src/icu/icu4c/source/common/uloc_tag.cpp:2761
#8 0x5bc6c2 in _ConvertBCP47 /work/icu/common//src/icu/icu4c/source/common/uloc.cpp:485
#9 0x5bc6c2 in _canonicalize /work/icu/common//src/icu/icu4c/source/common/uloc.cpp:1518
#10 0x5ba77c in ulocimp_getName_73 /work/icu/common//src/icu/icu4c/source/common/uloc.cpp:1902
#11 0x5ba77c in uloc_getName_73 /work/icu/common//src/icu/icu4c/source/common/uloc.cpp:1880
#12 0x581533 in init /work/icu/common//src/icu/icu4c/source/common/locid.cpp:1855
#13 0x583ee0 in Locale /work/icu/common//src/icu/icu4c/source/common/locid.cpp:400
#14 0x4dec6c in LLVMFuzzerTestOneInput /work/icu//src/icu/icu4c/source/test/fuzzer/locale_fuzzer.cpp:46

Stack trace is 
==14==ERROR: AddressSanitizer: attempting double-free on 0x6020000000f0 in thread T0:
SCARINESS: 42 (double-free)
    #0 0x4a0c12 in free /src/llvm-project/compiler-rt/lib/asan/asan_malloc_linux.cpp:52:3
    #1 0x63eacd in uprv_free_73 /src/icu/icu4c/source/common/cmemory.cpp:99:13
    #2 0x5d8205 in ultag_close /src/icu/icu4c/source/common/uloc_tag.cpp:2447:5
    #3 0x5d8205 in icu_73::LocalULanguageTagPointer::~LocalULanguageTagPointer() /src/icu/icu4c/source/common/uloc_tag.cpp:364:1
    #4 0x5d26a4 in ultag_parse /src/icu/icu4c/source/common/uloc_tag.cpp:2433:1
    #5 0x5d26a4 in ulocimp_forLanguageTag_73 /src/icu/icu4c/source/common/uloc_tag.cpp:2791:38
    #6 0x5cfd76 in uloc_forLanguageTag_73 /src/icu/icu4c/source/common/uloc_tag.cpp:2761:5
    #7 0x5bc6c2 in _ConvertBCP47 /src/icu/icu4c/source/common/uloc.cpp:485:28
    #8 0x5bc6c2 in _canonicalize(char const*, icu_73::ByteSink&, unsigned int, UErrorCode*) /src/icu/icu4c/source/common/uloc.cpp:1518:27
    #9 0x5ba77c in ulocimp_getName_73 /src/icu/icu4c/source/common/uloc.cpp:1902:5
    #10 0x5ba77c in uloc_getName_73 /src/icu/icu4c/source/common/uloc.cpp:1880:5
    #11 0x581533 in icu_73::Locale::init(char const*, signed char) /src/icu/icu4c/source/common/locid.cpp:1855:13
    #12 0x583ee0 in icu_73::Locale::Locale(char const*, char const*, char const*, char const*) /src/icu/icu4c/source/common/locid.cpp:400:9
    #13 0x4dec6c in LLVMFuzzerTestOneInput /src/icu/icu4c/source/test/fuzzer/locale_fuzzer.cpp:46:15


0x6020000000f0 is located 0 bytes inside of 10-byte region [0x6020000000f0,0x6020000000fa)
freed by thread T0 here:
    #0 0x4a0c12 in free /src/llvm-project/compiler-rt/lib/asan/asan_malloc_linux.cpp:52:3
    #1 0x63eacd in uprv_free_73 /src/icu/icu4c/source/common/cmemory.cpp:99:13
    #2 0x5d058c in ultag_parse /src/icu/icu4c/source/common/uloc_tag.cpp:2094:17
    #3 0x5d058c in ulocimp_forLanguageTag_73 /src/icu/icu4c/source/common/uloc_tag.cpp:2791:38
    #4 0x5cfd76 in uloc_forLanguageTag_73 /src/icu/icu4c/source/common/uloc_tag.cpp:2761:5
    #5 0x5bc6c2 in _ConvertBCP47 /src/icu/icu4c/source/common/uloc.cpp:485:28
    #6 0x5bc6c2 in _canonicalize(char const*, icu_73::ByteSink&, unsigned int, UErrorCode*) /src/icu/icu4c/source/common/uloc.cpp:1518:27
    #7 0x5ba77c in ulocimp_getName_73 /src/icu/icu4c/source/common/uloc.cpp:1902:5
    #8 0x5ba77c in uloc_getName_73 /src/icu/icu4c/source/common/uloc.cpp:1880:5
    #9 0x581533 in icu_73::Locale::init(char const*, signed char) /src/icu/icu4c/source/common/locid.cpp:1855:13
    #10 0x583ee0 in icu_73::Locale::Locale(char const*, char const*, char const*, char const*) /src/icu/icu4c/source/common/locid.cpp:400:9
    #11 0x4dec6c in LLVMFuzzerTestOneInput /src/icu/icu4c/source/test/fuzzer/locale_fuzzer.cpp:46:15

previously allocated by thread T0 here:
    #0 0x4a0eb6 in __interceptor_malloc /src/llvm-project/compiler-rt/lib/asan/asan_malloc_linux.cpp:69:3
    #1 0x4df8a9 in malloc (/out/locale_fuzzer+0x4df8a9)
    #2 0x63e929 in uprv_malloc_73 /src/icu/icu4c/source/common/cmemory.cpp:58:20
    #3 0x5d01be in ultag_parse /src/icu/icu4c/source/common/uloc_tag.cpp:2045:21
    #4 0x5d01be in ulocimp_forLanguageTag_73 /src/icu/icu4c/source/common/uloc_tag.cpp:2791:38
    #5 0x5cfd76 in uloc_forLanguageTag_73 /src/icu/icu4c/source/common/uloc_tag.cpp:2761:5
    #6 0x5bc6c2 in _ConvertBCP47 /src/icu/icu4c/source/common/uloc.cpp:485:28
    #7 0x5bc6c2 in _canonicalize(char const*, icu_73::ByteSink&, unsigned int, UErrorCode*) /src/icu/icu4c/source/common/uloc.cpp:1518:27
    #8 0x5ba77c in ulocimp_getName_73 /src/icu/icu4c/source/common/uloc.cpp:1902:5
    #9 0x5ba77c in uloc_getName_73 /src/icu/icu4c/source/common/uloc.cpp:1880:5
    #10 0x581533 in icu_73::Locale::init(char const*, signed char) /src/icu/icu4c/source/common/locid.cpp:1855:13
    #11 0x583ee0 in icu_73::Locale::Locale(char const*, char const*, char const*, char const*) /src/icu/icu4c/source/common/locid.cpp:400:9
    #12 0x4dec6c in LLVMFuzzerTestOneInput /src/icu/icu4c/source/test/fuzzer/locale_fuzzer.cpp:46:15

It was fixed in the icu project 4 months ago, but it still exists in firefox.

patch commit:
https://github.com/unicode-org/icu/pull/2567

Group: firefox-core-security → layout-core-security
Component: Untriaged → Internationalization
Keywords: csectype-uaf
Product: Firefox → Core
Flags: needinfo?(tom)

It's unclear we actually hit this code, because at least for JavaScript APIs, all language tags are first canonicalised through mozilla::intl::Locale which doesn't call into ICU4C, but instead uses LocaleGenerated.cpp. So when we later call into ICU4C, the language tags are already canonicalised, so the block were the double-free occurs is never executed.

And for non-ESR releases, bug 1858496 should also prevent the double-free from happening.

LocaleCanonicalizer::CanonicalizeICULevel1 directly calls into uloc_canonicalize, but that function is only ever called with the value from the "intl.accept_languages" preference, so it's also not a direct attack surface.

Group: layout-core-security → javascript-core-security
Component: Internationalization → JavaScript: Internationalization API
Keywords: sec-other
Blocks: sm-security
Severity: -- → S3
Priority: -- → P2
Flags: needinfo?(tom)

Sorry for the burst of bugspam: filter on tinkling-glitter-filtrate
Adding reporter-external keyword to security bugs found by non-employees for accounting reasons

Flags: sec-bounty?

bounty-, we can reconsider if an attack vector is demonstrated

Flags: sec-bounty? → sec-bounty-

Fixed in https://github.com/unicode-org/icu/pull/2567, which got integrated in bug 1927706.

Status: UNCONFIRMED → RESOLVED
Closed: 8 months ago
Resolution: --- → FIXED
Group: javascript-core-security → core-security-release
Group: core-security-release
You need to log in before you can comment on or make changes to this bug.