Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

zstd decompress error #1861

Merged
merged 2 commits into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 15 additions & 11 deletions core/runtime/common/uncompress_code_if_needed.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@
#include "runtime/common/uncompress_code_if_needed.hpp"

#include <zstd.h>
#include <zstd_errors.h>

OUTCOME_CPP_DEFINE_CATEGORY(kagome::runtime, UncompressError, e) {
using E = kagome::runtime::UncompressError;
switch (e) {
case E::ZSTD_ERROR:
return "WASM code not compressed by zstd!";
case E::BOMB_SIZE_REACHED:
return "Code decompression failed. Maximum size reached - possible bomb";
}
return "Unknown error";
}
Expand All @@ -30,25 +33,26 @@ namespace kagome::runtime {

outcome::result<void> uncompressCodeIfNeeded(common::BufferView buf,
common::Buffer &res) {
if (buf.size() > kZstdPrefixSize
&& std::equal(buf.begin(),
buf.begin() + kZstdPrefixSize,
std::begin(kZstdPrefix),
std::end(kZstdPrefix))) {
if (startsWith(buf, kZstdPrefix)) {
auto zstd = buf.subspan(std::size(kZstdPrefix));
// here we can check that blob is really ZSTD compressed
// but we don't use the result size, it's unknown for the WASM blob
// @see ZSTD_CONTENTSIZE_UNKNOWN
auto check_size = ZSTD_getFrameContentSize(buf.data() + kZstdPrefixSize,
buf.size() - kZstdPrefixSize);
auto check_size = ZSTD_getFrameContentSize(zstd.data(), zstd.size());
if (check_size == ZSTD_CONTENTSIZE_ERROR) {
return UncompressError::ZSTD_ERROR;
}
res.resize(kCodeBlobBombLimit);
auto size = ZSTD_decompress(res.data(),
kCodeBlobBombLimit,
buf.data() + kZstdPrefixSize,
buf.size() - kZstdPrefixSize);
auto size = ZSTD_decompress(
res.data(), kCodeBlobBombLimit, zstd.data(), zstd.size());
if (ZSTD_isError(size)) {
if (ZSTD_getErrorCode(size) == ZSTD_error_dstSize_tooSmall) {
return UncompressError::BOMB_SIZE_REACHED;
}
return UncompressError::ZSTD_ERROR;
}
res.resize(size);
res.shrink_to_fit();
} else {
res = common::Buffer{buf};
}
Expand Down
5 changes: 4 additions & 1 deletion core/runtime/common/uncompress_code_if_needed.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@
#include "common/buffer.hpp"

namespace kagome::runtime {
enum class UncompressError : uint8_t { ZSTD_ERROR };
enum class UncompressError : uint8_t {
ZSTD_ERROR,
BOMB_SIZE_REACHED,
};

outcome::result<void> uncompressCodeIfNeeded(common::BufferView buf,
common::Buffer &res);
Expand Down