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

Add an entry point when signing Arm images #163

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
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
49 changes: 49 additions & 0 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4594,6 +4594,34 @@ void sign_guts_elf(elf_file* elf, private_t private_key, public_t public_key) {
new_block.items.push_back(version);
}

// Add entry point when signing Arm images
std::shared_ptr<image_type_item> image_type = new_block.get_item<image_type_item>();
if (settings.seal.sign && image_type != nullptr && image_type->image_type() == type_exe && image_type->cpu() == cpu_arm) {
std::shared_ptr<entry_point_item> entry_point = new_block.get_item<entry_point_item>();
if (entry_point == nullptr) {
std::shared_ptr<vector_table_item> vtor = new_block.get_item<vector_table_item>();
uint32_t vtor_loc = elf->header().entry < SRAM_START ? 0x10000000 : 0x20000000;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need to check for XIP_SRAM-only binaries here? I forget if they can be signed (i believe so)

if (vtor != nullptr) {
vtor_loc = vtor->addr;
} else {
std::shared_ptr<rolling_window_delta_item> rwd = new_block.get_item<rolling_window_delta_item>();
if (rwd != nullptr) {
vtor_loc += rwd->addr;
}
}
auto segment = elf->segment_from_physical_address(vtor_loc);
auto content = elf->content(*segment);
auto offset = vtor_loc - segment->physical_address();
uint32_t ep;
memcpy(&ep, content.data() + offset + 4, sizeof(ep));
uint32_t sp;
memcpy(&sp, content.data() + offset, sizeof(sp));
DEBUG_LOG("Adding entry_point_item: ep %08x, sp %08x\n", ep, sp);
entry_point = std::make_shared<entry_point_item>(ep, sp);
new_block.items.push_back(entry_point);
}
}

hash_andor_sign(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Screenshot from 2024-11-19 23-27-37

😉

elf, &new_block, public_key, private_key,
settings.seal.hash, settings.seal.sign,
Expand Down Expand Up @@ -4630,6 +4658,27 @@ vector<uint8_t> sign_guts_bin(iostream_memory_access in, private_t private_key,
new_block.items.push_back(version);
}

// Add entry point when signing Arm images
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm; i hadn't really considered the BIN case where we don't know where the user is planning to load it. Perhaps we don't do all this and just give a warning instead? (or allow a "target address" option) - i'd be happy with the former for now, we can always add the new option later if it is useful

std::shared_ptr<image_type_item> image_type = new_block.get_item<image_type_item>();
if (settings.seal.sign && image_type != nullptr && image_type->image_type() == type_exe && image_type->cpu() == cpu_arm) {
std::shared_ptr<entry_point_item> entry_point = new_block.get_item<entry_point_item>();
if (entry_point == nullptr) {
std::shared_ptr<vector_table_item> vtor = new_block.get_item<vector_table_item>();
uint32_t vtor_loc = 0x10000000;
if (vtor != nullptr) {
vtor_loc = vtor->addr;
}
auto offset = vtor_loc - bin_start;
uint32_t ep;
memcpy(&ep, bin.data() + offset + 4, sizeof(ep));
uint32_t sp;
memcpy(&sp, bin.data() + offset, sizeof(sp));
DEBUG_LOG("Adding entry_point_item: ep %08x, sp %08x\n", ep, sp);
entry_point = std::make_shared<entry_point_item>(ep, sp);
new_block.items.push_back(entry_point);
}
}

auto sig_data = hash_andor_sign(
bin, bin_start, bin_start,
&new_block, public_key, private_key,
Expand Down
Loading