• R/O
  • HTTP
  • SSH
  • HTTPS

提交

標籤
無標籤

Frequently used words (click to add to your profile)

javac++androidlinuxc#windowsobjective-ccocoa誰得qtpythonphprubygameguibathyscaphec計画中(planning stage)翻訳omegatframeworktwitterdomtestvb.netdirectxゲームエンジンbtronarduinopreviewer

system/core


Commit MetaInfo

修訂92c238198805c3c846f017503e1deb8abcc8994b (tree)
時間2017-09-21 04:24:05
作者Adam Vartanian <flooey@goog...>
CommiterNikoli Cartagena

Log Message

Fix integer overflow in utf{16,32}_to_utf8_length

Without an explicit check, the return value can wrap around and return
a value that is far too small to hold the data from the resulting
conversion.

No CTS test is provided because it would need to allocate at least
SSIZE_MAX / 2 bytes of UTF-16 data, which is unreasonable on 64-bit
devices.

Bug: 37723026
Test: run cts -p android.security
Change-Id: Ie2606b92b9eab1acfe8ce4663b43b81156a4cad7
Merged-In: I56ba5e31657633b7f33685dd8839d4b3b998e586
(cherry picked from commit f0a43dede921178b3048d40c330a8b664e94cca3)

Change Summary

差異

--- a/libutils/Unicode.cpp
+++ b/libutils/Unicode.cpp
@@ -18,6 +18,7 @@
1818 #include <utils/Unicode.h>
1919
2020 #include <stddef.h>
21+#include <limits.h>
2122
2223 #if defined(_WIN32)
2324 # undef nhtol
@@ -178,7 +179,15 @@ ssize_t utf32_to_utf8_length(const char32_t *src, size_t src_len)
178179 size_t ret = 0;
179180 const char32_t *end = src + src_len;
180181 while (src < end) {
181- ret += utf32_codepoint_utf8_length(*src++);
182+ size_t char_len = utf32_codepoint_utf8_length(*src++);
183+ if (SSIZE_MAX - char_len < ret) {
184+ // If this happens, we would overflow the ssize_t type when
185+ // returning from this function, so we cannot express how
186+ // long this string is in an ssize_t.
187+ android_errorWriteLog(0x534e4554, "37723026");
188+ return -1;
189+ }
190+ ret += char_len;
182191 }
183192 return ret;
184193 }
@@ -438,14 +447,23 @@ ssize_t utf16_to_utf8_length(const char16_t *src, size_t src_len)
438447 size_t ret = 0;
439448 const char16_t* const end = src + src_len;
440449 while (src < end) {
450+ size_t char_len;
441451 if ((*src & 0xFC00) == 0xD800 && (src + 1) < end
442452 && (*(src + 1) & 0xFC00) == 0xDC00) {
443453 // surrogate pairs are always 4 bytes.
444- ret += 4;
454+ char_len = 4;
445455 src += 2;
446456 } else {
447- ret += utf32_codepoint_utf8_length((char32_t) *src++);
457+ char_len = utf32_codepoint_utf8_length((char32_t)*src++);
458+ }
459+ if (SSIZE_MAX - char_len < ret) {
460+ // If this happens, we would overflow the ssize_t type when
461+ // returning from this function, so we cannot express how
462+ // long this string is in an ssize_t.
463+ android_errorWriteLog(0x534e4554, "37723026");
464+ return -1;
448465 }
466+ ret += char_len;
449467 }
450468 return ret;
451469 }