Skip to content

Commit

Permalink
fix crash caused by array out of bounds
Browse files Browse the repository at this point in the history
  • Loading branch information
qiang101.wang authored and andycall committed Apr 11, 2024
1 parent 6cbcfaa commit d1bd47c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
23 changes: 22 additions & 1 deletion bridge/foundation/wbc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,21 @@ uint8_t* Wbc::prepareWbc(const uint8_t* bytes, size_t length, size_t* targetSize
}

// Extracting header length
if (length < (signatureSize + Wbc::WBC_HEADER_LENGTH)) {
WEBF_LOG(ERROR) << "prepareWbc header length is wrong" << std::endl;
return nullptr;
}

uint32_t headerLength = convertBigEndianToUint32(bytes, signatureSize);
uint32_t bodyOffset = signatureSize + headerLength;
uint32_t headerChecksumOffset = bodyOffset - Wbc::WBC_HEADER_CHECKSUM_LENGTH;

// Calculating Adler32 checksum for header content
if (length < bodyOffset) {
WEBF_LOG(ERROR) << "prepareWbc header is wrong" << std::endl;
return nullptr;
}

uint32_t headerContentAdler32 = calculateAdler32(bytes + signatureSize, headerChecksumOffset - signatureSize);
uint32_t headerAdler32 = convertBigEndianToUint32(bytes, headerChecksumOffset);
if (headerContentAdler32 != headerAdler32) {
Expand All @@ -84,10 +94,21 @@ uint8_t* Wbc::prepareWbc(const uint8_t* bytes, size_t length, size_t* targetSize
}

// Extracting body length
if (length < (bodyOffset + Wbc::WBC_BODY_LENGTH)) {
WEBF_LOG(ERROR) << "prepareWbc body length is wrong" << std::endl;
return nullptr;
}

uint32_t bodyLength = convertBigEndianToUint32(bytes, bodyOffset);
uint32_t bodyChecksumOffset = bodyOffset + bodyLength - Wbc::WBC_BODY_CHECKSUM_LENGTH;
uint32_t endOffset = bodyOffset + bodyLength;
uint32_t bodyChecksumOffset = endOffset - Wbc::WBC_BODY_CHECKSUM_LENGTH;

// Calculating Adler32 checksum for body content
if (length < endOffset) {
WEBF_LOG(ERROR) << "prepareWbc body is wrong" << std::endl;
return nullptr;
}

uint32_t bodyContentAdler32 = calculateAdler32(bytes + bodyOffset, bodyChecksumOffset - bodyOffset);
uint32_t bodyAdler32 = convertBigEndianToUint32(bytes, bodyChecksumOffset);
if (bodyContentAdler32 != bodyAdler32) {
Expand Down
3 changes: 3 additions & 0 deletions bridge/foundation/wbc.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ class Wbc {
// node-lz4 default maximum block size
static const int32_t NODE_LZ4_BLOCK_MAX_SIZE = 4 * 1024 * 1024;

//The length of the header field in the wbc file format.
static const int32_t WBC_HEADER_LENGTH = 4;

// The length of the CHECKSUM field of HEADER in the wbc file format
static const int32_t WBC_HEADER_CHECKSUM_LENGTH = 4;

Expand Down

0 comments on commit d1bd47c

Please sign in to comment.