Skip to content

armgit / godot

Public Imported from GitHub
0

Godot Engine – Multi-platform 2D and 3D game engine

Fix out-of-bounds reads with corrupt OptimizedTranslation data

Rémi Verscheldea3019c809c02
diff --git a/core/string/optimized_translation.cpp b/core/string/optimized_translation.cpp
index d084e8c1fa2..a2f814271a8 100644
--- a/core/string/optimized_translation.cpp
+++ b/core/string/optimized_translation.cpp
@@ -231,6 +231,32 @@ bool OptimizedTranslation::_get(const StringName &p_name, Variant &r_ret) const
return true;
}
+const OptimizedTranslation::Bucket *OptimizedTranslation::_get_bucket(uint32_t p_offset) const {
+ const uint64_t bucket_table_size = bucket_table.size();
+ ERR_FAIL_COND_V_MSG((uint64_t)p_offset + 2 > bucket_table_size, nullptr, "Invalid bucket offset, corrupt translation.");
+
+ const Bucket *bucket = (const Bucket *)&bucket_table.ptr()[p_offset];
+ ERR_FAIL_COND_V_MSG(bucket->size < 0 || (uint64_t)p_offset + 2 + (uint64_t)bucket->size * 4 > bucket_table_size, nullptr, "Invalid bucket size, corrupt translation.");
+ return bucket;
+}
+
+String OptimizedTranslation::_get_string(const Bucket::Elem &p_elem) const {
+ ERR_FAIL_COND_V_MSG((uint64_t)p_elem.str_offset + p_elem.comp_size > (uint64_t)strings.size(), String(), "Invalid string offset, corrupt translation.");
+ const char *sptr = (const char *)strings.ptr();
+
+ if (p_elem.comp_size == p_elem.uncomp_size) {
+ return String::utf8(&sptr[p_elem.str_offset], p_elem.uncomp_size);
+ }
+
+ ERR_FAIL_COND_V_MSG(p_elem.uncomp_size > INT32_MAX, String(), "Invalid string size, corrupt translation.");
+ CharString uncomp;
+ Error err = uncomp.resize_uninitialized(p_elem.uncomp_size);
+ ERR_FAIL_COND_V(err != OK, String());
+ int len = smaz_decompress(&sptr[p_elem.str_offset], p_elem.comp_size, uncomp.ptrw(), p_elem.uncomp_size);
+ ERR_FAIL_COND_V_MSG(len > (int)p_elem.uncomp_size, String(), "Invalid compressed string, corrupt translation.");
+ return String::utf8(uncomp.get_data(), len);
+}
+
StringName OptimizedTranslation::get_message(const StringName &p_src_text, const StringName &p_context) const {
// p_context passed in is ignore. The use of context is not yet supported in OptimizedTranslation.
@@ -245,10 +271,6 @@ StringName OptimizedTranslation::get_message(const StringName &p_src_text, const
const int *htr = hash_table.ptr();
const uint32_t *htptr = (const uint32_t *)&htr[0];
- const int *btr = bucket_table.ptr();
- const uint32_t *btptr = (const uint32_t *)&btr[0];
- const uint8_t *sr = strings.ptr();
- const char *sptr = (const char *)&sr[0];
uint32_t p = htptr[h % htsize];
@@ -256,14 +278,17 @@ StringName OptimizedTranslation::get_message(const StringName &p_src_text, const
return StringName(); //nothing
}
- const Bucket &bucket = *(const Bucket *)&btptr[p];
+ const Bucket *bucket = _get_bucket(p);
+ if (unlikely(!bucket)) {
+ return StringName(); // Helper already prints an error.
+ }
- h = hash(bucket.func, str.get_data());
+ h = hash(bucket->func, str.get_data());
int idx = -1;
- for (int i = 0; i < bucket.size; i++) {
- if (bucket.elem[i].key == h) {
+ for (int i = 0; i < bucket->size; i++) {
+ if (bucket->elem[i].key == h) {
idx = i;
break;
}
@@ -273,14 +298,7 @@ StringName OptimizedTranslation::get_message(const StringName &p_src_text, const
return StringName();
}
- if (bucket.elem[idx].comp_size == bucket.elem[idx].uncomp_size) {
- return String::utf8(&sptr[bucket.elem[idx].str_offset], bucket.elem[idx].uncomp_size);
- } else {
- CharString uncomp;
- uncomp.resize_uninitialized(bucket.elem[idx].uncomp_size + 1);
- smaz_decompress(&sptr[bucket.elem[idx].str_offset], bucket.elem[idx].comp_size, uncomp.ptrw(), bucket.elem[idx].uncomp_size);
- return String::utf8(uncomp.get_data());
- }
+ return _get_string(bucket->elem[idx]);
}
Vector<String> OptimizedTranslation::get_translated_message_list() const {
@@ -288,26 +306,16 @@ Vector<String> OptimizedTranslation::get_translated_message_list() const {
const int *htr = hash_table.ptr();
const uint32_t *htptr = (const uint32_t *)&htr[0];
- const int *btr = bucket_table.ptr();
- const uint32_t *btptr = (const uint32_t *)&btr[0];
- const uint8_t *sr = strings.ptr();
- const char *sptr = (const char *)&sr[0];
for (int i = 0; i < hash_table.size(); i++) {
uint32_t p = htptr[i];
if (p != 0xFFFFFFFF) {
- const Bucket &bucket = *(const Bucket *)&btptr[p];
- for (int j = 0; j < bucket.size; j++) {
- if (bucket.elem[j].comp_size == bucket.elem[j].uncomp_size) {
- String rstr = String::utf8(&sptr[bucket.elem[j].str_offset], bucket.elem[j].uncomp_size);
- msgs.push_back(rstr);
- } else {
- CharString uncomp;
- uncomp.resize_uninitialized(bucket.elem[j].uncomp_size + 1);
- smaz_decompress(&sptr[bucket.elem[j].str_offset], bucket.elem[j].comp_size, uncomp.ptrw(), bucket.elem[j].uncomp_size);
- String rstr = String::utf8(uncomp.get_data());
- msgs.push_back(rstr);
- }
+ const Bucket *bucket = _get_bucket(p);
+ if (unlikely(!bucket)) {
+ return Vector<String>(); // Helper already prints an error.
+ }
+ for (int j = 0; j < bucket->size; j++) {
+ msgs.push_back(_get_string(bucket->elem[j]));
}
}
}
diff --git a/core/string/optimized_translation.h b/core/string/optimized_translation.h
index 25d5cd45845..44c4d0192e5 100644
--- a/core/string/optimized_translation.h
+++ b/core/string/optimized_translation.h
@@ -83,6 +83,10 @@ class OptimizedTranslation : public Translation {
return p_d;
}
+ // Helpers to guard against corrupted resources.
+ const Bucket *_get_bucket(uint32_t p_offset) const;
+ String _get_string(const Bucket::Elem &p_elem) const;
+
virtual Vector<String> _get_message_list() const override;
protected:
diff --git a/thirdparty/README.md b/thirdparty/README.md
index c8db0d10ef7..f9c0ba9e577 100644
--- a/thirdparty/README.md
+++ b/thirdparty/README.md
@@ -864,6 +864,7 @@ Collection of single-file libraries used in Godot components.
* Modifications: License included in header.
* Patches:
- `smaz-0001-write-string-warning.patch` ([GH-8572](https://github.com/godotengine/godot/pull/8572))
+ - `smaz-0002-decompress-input-bounds.patch` ([GH-124701](https://github.com/godotengine/godot/pull/124701))
- `smolv.{cpp,h}`
* Upstream: https://github.com/aras-p/smol-v
* Version: git (9dd54c379ac29fa148cb1b829bb939ba7381d8f4, 2024)
diff --git a/thirdparty/misc/patches/smaz-0002-decompress-input-bounds.patch b/thirdparty/misc/patches/smaz-0002-decompress-input-bounds.patch
new file mode 100644
index 00000000000..c518f1eacda
--- /dev/null
+++ b/thirdparty/misc/patches/smaz-0002-decompress-input-bounds.patch
@@ -0,0 +1,26 @@
+diff --git a/thirdparty/misc/smaz.c b/thirdparty/misc/smaz.c
+index 555dfea844..a4d60c7a9b 100644
+--- a/thirdparty/misc/smaz.c
++++ b/thirdparty/misc/smaz.c
+@@ -172,9 +172,10 @@ int smaz_decompress(const char *in, int inlen, char *out, int outlen) {
+ char *_out = out;
+ int _outlen = outlen;
+
+- while(inlen) {
++ while(inlen > 0) {
+ if (*c == 254) {
+ /* Verbatim byte */
++ if (inlen < 2) return _outlen+1;
+ if (outlen < 1) return _outlen+1;
+ *out = *(c+1);
+ out++;
+@@ -183,7 +184,9 @@ int smaz_decompress(const char *in, int inlen, char *out, int outlen) {
+ inlen -= 2;
+ } else if (*c == 255) {
+ /* Verbatim string */
++ if (inlen < 2) return _outlen+1;
+ int len = (*(c+1))+1;
++ if (inlen < 2+len) return _outlen+1;
+ if (outlen < len) return _outlen+1;
+ memcpy(out,c+2,len);
+ out += len;
diff --git a/thirdparty/misc/smaz.c b/thirdparty/misc/smaz.c
index 555dfea844c..a4d60c7a9bc 100644
--- a/thirdparty/misc/smaz.c
+++ b/thirdparty/misc/smaz.c
@@ -172,9 +172,10 @@ int smaz_decompress(const char *in, int inlen, char *out, int outlen) {
char *_out = out;
int _outlen = outlen;
- while(inlen) {
+ while(inlen > 0) {
if (*c == 254) {
/* Verbatim byte */
+ if (inlen < 2) return _outlen+1;
if (outlen < 1) return _outlen+1;
*out = *(c+1);
out++;
@@ -183,7 +184,9 @@ int smaz_decompress(const char *in, int inlen, char *out, int outlen) {
inlen -= 2;
} else if (*c == 255) {
/* Verbatim string */
+ if (inlen < 2) return _outlen+1;
int len = (*(c+1))+1;
+ if (inlen < 2+len) return _outlen+1;
if (outlen < len) return _outlen+1;
memcpy(out,c+2,len);
out += len;