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

td-shim: add a feature to support optional payload relocation #424

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
6 changes: 5 additions & 1 deletion td-loader/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,8 @@ log = "0.4.13"
scroll = { version = "0.10", default-features=false, features = ["derive"] }

[dev-dependencies]
env_logger = "0.9.0"
env_logger = "0.9.0"

[features]
default = []
disable-relocation = []
22 changes: 12 additions & 10 deletions td-loader/src/elf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,18 @@ pub fn relocate_elf_with_per_program_header(
}
}

// relocate to base
for reloc in elf.relocations()? {
if reloc.r_type() == R_X86_64_RELATIVE {
let r_addend = reloc.r_addend;
loaded_buffer
.pwrite::<u64>(
new_image_base.checked_add(r_addend as usize)? as u64,
reloc.r_offset as usize,
)
.ok()?;
if !cfg!(feature = "disable-relocation") {
// relocate to base
for reloc in elf.relocations()? {
if reloc.r_type() == R_X86_64_RELATIVE {
let r_addend = reloc.r_addend;
loaded_buffer
.pwrite::<u64>(
new_image_base.checked_add(r_addend as usize)? as u64,
reloc.r_offset as usize,
)
.ok()?;
}
}
}

Expand Down
26 changes: 14 additions & 12 deletions td-loader/src/pe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,6 @@ pub fn relocate_with_per_section(
let coff_optional_offset = coff_standard_end;
let coff_optional_end = coff_header_end.checked_add(coff_optional_size)?;
image_buffer.len().checked_sub(coff_optional_end)?;
let coff_optional_region = &image_buffer[coff_optional_offset..coff_optional_end];
let image_base = coff_optional_region.pread::<u64>(0).ok()?;

// Validate section header region
// There's no "Data Directories", so "Section Table" follows COFF Optional Fields.
Expand Down Expand Up @@ -210,16 +208,20 @@ pub fn relocate_with_per_section(
}
}

let sections = Sections::parse(sections_buffer, num_sections as usize)?;
for section in sections {
if &section.name == b".reloc\0\0" && image_base != new_image_base as u64 {
reloc_to_base(
loaded_buffer,
image_buffer,
&section,
image_base as usize,
new_image_base as usize,
)?;
if !cfg!(feature = "disable-relocation") {
let coff_optional_region = &image_buffer[coff_optional_offset..coff_optional_end];
let image_base = coff_optional_region.pread::<u64>(0).ok()?;
let sections = Sections::parse(sections_buffer, num_sections as usize)?;
for section in sections {
if &section.name == b".reloc\0\0" && image_base != new_image_base as u64 {
Copy link
Member

Choose a reason for hiding this comment

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

if it is already relocated, why image_base != new_image_base?

reloc_to_base(
loaded_buffer,
image_buffer,
&section,
image_base as usize,
new_image_base as usize,
)?;
}
}
}

Expand Down
1 change: 1 addition & 0 deletions td-shim/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ boot-kernel = ["td-layout/boot-kernel"]
secure-boot = ["der", "ring"]
tdx = ["tdx-tdcall", "td-exception/tdx", "td-logger/tdx", "x86"]
lazy-accept = ["tdx"]
disable-relocation = ["td-loader/disable-relocation"]
main = [
"td-loader",
"linked_list_allocator",
Expand Down