diff options
| author | xroneyaa <202318392+xroneyaa@users.noreply.github.com> | 2026-11-24 11:09:18 +0500 |
|---|---|---|
| committer | xroneyaa <202318392+xroneyaa@users.noreply.github.com> | 2026-11-24 11:09:18 +0500 |
| commit | 60905fe78dbcf35da4a10950313dcea7eec05a74 (patch) | |
| tree | 8846cd5075c747f23638e78fae854dd5e53c01b9 /runtime/src | |
| parent | 8f481cf16f261c7f37458aeb45e8f4e19994146d (diff) | |
shader_library: guard zlib limit sums and reject malformed f16_halves keys
Diffstat (limited to 'runtime/src')
| -rw-r--r-- | runtime/src/shader_library.h | 23 |
1 files changed, 17 insertions, 6 deletions
diff --git a/runtime/src/shader_library.h b/runtime/src/shader_library.h index 9f752317e..596ad0ae9 100644 --- a/runtime/src/shader_library.h +++ b/runtime/src/shader_library.h | |||
| @@ -246,7 +246,7 @@ inline ZlibError inflate_codes(BitReader& in, std::vector<std::uint8_t>& out, co | |||
| 246 | if (d < 0 || d >= 30) return ZlibError::data; | 246 | if (d < 0 || d >= 30) return ZlibError::data; |
| 247 | const std::size_t distance = std::size_t(distance_base[d]) + in.bits(distance_extra[d]); | 247 | const std::size_t distance = std::size_t(distance_base[d]) + in.bits(distance_extra[d]); |
| 248 | if (in.failed() || distance > out.size()) return ZlibError::data; | 248 | if (in.failed() || distance > out.size()) return ZlibError::data; |
| 249 | if (out.size() + length > limit) return ZlibError::limit; | 249 | if (out.size() > limit || length > limit - out.size()) return ZlibError::limit; |
| 250 | const std::size_t from = out.size() - distance; | 250 | const std::size_t from = out.size() - distance; |
| 251 | for (std::size_t i = 0; i < length; ++i) out.push_back(out[from + i]); | 251 | for (std::size_t i = 0; i < length; ++i) out.push_back(out[from + i]); |
| 252 | } | 252 | } |
| @@ -284,7 +284,7 @@ inline bool zlib_inflate(std::span<const std::uint8_t> in, std::vector<std::uint | |||
| 284 | if (!reader.byte(x)) return fail(ZlibError::data); | 284 | if (!reader.byte(x)) return fail(ZlibError::data); |
| 285 | const unsigned length = b[0] | (b[1] << 8), complement = b[2] | (b[3] << 8); | 285 | const unsigned length = b[0] | (b[1] << 8), complement = b[2] | (b[3] << 8); |
| 286 | if (length != (~complement & 0xffff)) return fail(ZlibError::data); | 286 | if (length != (~complement & 0xffff)) return fail(ZlibError::data); |
| 287 | if (out.size() + length > limit) return fail(ZlibError::limit); | 287 | if (out.size() > limit || length > limit - out.size()) return fail(ZlibError::limit); |
| 288 | for (unsigned i = 0; i < length; ++i) { | 288 | for (unsigned i = 0; i < length; ++i) { |
| 289 | std::uint8_t x; | 289 | std::uint8_t x; |
| 290 | if (!reader.byte(x)) return fail(ZlibError::data); | 290 | if (!reader.byte(x)) return fail(ZlibError::data); |
| @@ -349,7 +349,7 @@ inline bool zlib_inflate(std::span<const std::uint8_t> in, std::vector<std::uint | |||
| 349 | } | 349 | } |
| 350 | // Adler-32, big-endian, after the deflate data's last whole byte. | 350 | // Adler-32, big-endian, after the deflate data's last whole byte. |
| 351 | const std::size_t at = 2 + reader.position(); | 351 | const std::size_t at = 2 + reader.position(); |
| 352 | if (at + 4 > in.size()) return fail(ZlibError::data); | 352 | if (at > in.size() || in.size() - at < 4) return fail(ZlibError::data); |
| 353 | std::uint32_t a = 1, b = 0; | 353 | std::uint32_t a = 1, b = 0; |
| 354 | for (const std::uint8_t x : out) { | 354 | for (const std::uint8_t x : out) { |
| 355 | a = (a + x) % 65521; | 355 | a = (a + x) % 65521; |
| @@ -1045,9 +1045,20 @@ private: | |||
| 1045 | if (const Json* fh = meta.find("f16_halves"); fh && fh->kind == Json::Kind::object) { | 1045 | if (const Json* fh = meta.find("f16_halves"); fh && fh->kind == Json::Kind::object) { |
| 1046 | for (const auto& [key, val] : fh->object) { | 1046 | for (const auto& [key, val] : fh->object) { |
| 1047 | if (key.empty() || val.kind != Json::Kind::array || val.array.size() != 2) continue; | 1047 | if (key.empty() || val.kind != Json::Kind::array || val.array.size() != 2) continue; |
| 1048 | char* end = nullptr; | 1048 | std::uint64_t param = 0; |
| 1049 | const unsigned long param = std::strtoul(key.c_str(), &end, 10); | 1049 | bool ok = true; |
| 1050 | if (!end || *end != '\0') continue; | 1050 | for (const char c : key) { |
| 1051 | if (c < '0' || c > '9') { | ||
| 1052 | ok = false; | ||
| 1053 | break; | ||
| 1054 | } | ||
| 1055 | param = param * 10u + std::uint64_t(c - '0'); | ||
| 1056 | if (param > UINT32_MAX) { | ||
| 1057 | ok = false; | ||
| 1058 | break; | ||
| 1059 | } | ||
| 1060 | } | ||
| 1061 | if (!ok) continue; | ||
| 1051 | e.f16_halves[std::uint32_t(param)] = {u32(&val.array[0]), u32(&val.array[1])}; | 1062 | e.f16_halves[std::uint32_t(param)] = {u32(&val.array[0]), u32(&val.array[1])}; |
| 1052 | } | 1063 | } |
| 1053 | } | 1064 | } |