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

Use "read" to fetch misaligned 64bit values from bundle header #63431

Merged
merged 3 commits into from
Jan 7, 2022
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
15 changes: 9 additions & 6 deletions src/native/corehost/bundle/file_entry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,18 @@ file_entry_t file_entry_t::read(reader_t &reader, uint32_t bundle_major_version,
// First read the fixed-sized portion of file-entry
file_entry_fixed_t fixed_data;

fixed_data.offset = *(int64_t*)reader.read_direct(sizeof(int64_t));
fixed_data.size = *(int64_t*)reader.read_direct(sizeof(int64_t));
// NB: the file data is potentially unaligned, thus we use "read" to fetch 64bit values
reader.read(&fixed_data.offset, sizeof(int64_t));
reader.read(&fixed_data.size, sizeof(int64_t));

// compressedSize is present only in v6+ headers
fixed_data.compressedSize = bundle_major_version >= 6 ?
*(int64_t*)reader.read_direct(sizeof(int64_t)) :
0;
fixed_data.compressedSize = 0;
if (bundle_major_version >= 6)
{
reader.read(&fixed_data.compressedSize, sizeof(int64_t));
}

fixed_data.type = *(file_type_t*)reader.read_direct(sizeof(file_type_t));
fixed_data.type = (file_type_t)reader.read_byte();

file_entry_t entry(&fixed_data, force_extraction);

Expand Down
12 changes: 6 additions & 6 deletions src/native/corehost/bundle/header.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,23 @@ bool header_fixed_t::is_valid() const

header_t header_t::read(reader_t& reader)
{
const header_fixed_t* fixed_header = reinterpret_cast<const header_fixed_t*>(reader.read_direct(sizeof(header_fixed_t)));
header_fixed_t fixed_header;
reader.read(&fixed_header, sizeof(header_fixed_t));

if (!fixed_header->is_valid())
if (!fixed_header.is_valid())
{
trace::error(_X("Failure processing application bundle."));
trace::error(_X("Bundle header version compatibility check failed. Header version: %d.%d"), fixed_header->major_version, fixed_header->minor_version);
trace::error(_X("Bundle header version compatibility check failed. Header version: %d.%d"), fixed_header.major_version, fixed_header.minor_version);

throw StatusCode::BundleExtractionFailure;
}

header_t header(fixed_header->major_version, fixed_header->minor_version, fixed_header->num_embedded_files);
header_t header(fixed_header.major_version, fixed_header.minor_version, fixed_header.num_embedded_files);

// bundle_id is a component of the extraction path
reader.read_path_string(header.m_bundle_id);

const header_fixed_v2_t *v2_header = reinterpret_cast<const header_fixed_v2_t*>(reader.read_direct(sizeof(header_fixed_v2_t)));
header.m_v2_header = *v2_header;
reader.read(&header.m_v2_header, sizeof(header_fixed_v2_t));

return header;
}
4 changes: 2 additions & 2 deletions src/native/corehost/bundle/reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ size_t reader_t::read_path_length()
{
size_t length = 0;

int8_t first_byte = read();
int8_t first_byte = read_byte();

// If the high bit is set, it means there are more bytes to read.
if ((first_byte & 0x80) == 0)
Expand All @@ -63,7 +63,7 @@ size_t reader_t::read_path_length()
}
else
{
int8_t second_byte = read();
int8_t second_byte = read_byte();

if (second_byte & 0x80)
{
Expand Down
12 changes: 1 addition & 11 deletions src/native/corehost/bundle/reader.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ namespace bundle
return m_ptr;
}

int8_t read()
int8_t read_byte()
{
bounds_check();
return *m_ptr++;
Expand All @@ -100,16 +100,6 @@ namespace bundle
m_ptr += len;
}

// Return a pointer to the requested bytes within the memory-mapped file.
// Skip over len bytes.
const char* read_direct(int64_t len)
{
bounds_check(len);
const char *ptr = m_ptr;
m_ptr += len;
return ptr;
}

size_t read_path_length();
size_t read_path_string(pal::string_t &str);

Expand Down