Skip to content
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
3 changes: 2 additions & 1 deletion src/info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ impl<'a> ELFParser<'a> {
/// * `pagesz` - The page size of the system
///
/// Details about auxiliary vectors are described in <https://articles.manugarg.com/aboutelfauxiliaryvectors.html>
pub fn auxv_vector(&self, pagesz: usize) -> [AuxvEntry; 16] {
pub fn auxv_vector(&self, pagesz: usize) -> [AuxvEntry; 17] {
[
AuxvEntry::new(AuxvType::PHDR, self.phdr()),
AuxvEntry::new(AuxvType::PHENT, self.phent()),
Expand All @@ -160,6 +160,7 @@ impl<'a> ELFParser<'a> {
AuxvEntry::new(AuxvType::EGID, 0),
AuxvEntry::new(AuxvType::RANDOM, 0),
AuxvEntry::new(AuxvType::EXECFN, 0),
AuxvEntry::new(AuxvType::NULL, 0),
]
}

Expand Down
16 changes: 15 additions & 1 deletion src/user_stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,20 @@ fn init_stack(args: &[String], envs: &[String], auxv: &mut [AuxvEntry], sp: usiz
stack.push(padding_null.as_bytes(), &mut data);

stack.push("\0".repeat(stack.get_sp() % 16).as_bytes(), &mut data);
assert!(stack.get_sp() % 16 == 0);

// Align stack to 16 bytes by padding if needed.
// We will push following 8-byte items into stack:
// - auxv (each entry is 2 * usize, so item count = auxv.len() * 2)
// - envp (len + 1 for NULL terminator)
// - argv (len + 1 for NULL terminator)
// - argc (1 item)
// Total items = auxv.len() * 2 + (envs.len() + 1) + (args.len() + 1) + 1
// = auxv.len() * 2 + envs.len() + args.len() + 3
// If odd, the stack top will not be aligned to 16 bytes unless we add 8-byte padding
if (envs.len() + args.len() + 3) & 1 != 0 {
stack.push(padding_null.as_bytes(), &mut data);
}

// Push auxiliary vectors
for auxv_entry in auxv.iter_mut() {
if auxv_entry.get_type() == AuxvType::RANDOM {
Expand All @@ -112,6 +125,7 @@ fn init_stack(args: &[String], envs: &[String], auxv: &mut [AuxvEntry], sp: usiz
stack.push_usize_slice(argv_slice.as_slice(), &mut data);
// Push argc
stack.push_usize_slice(&[args.len()], &mut data);
assert!(stack.get_sp() % 16 == 0);
Copy link
Owner

Choose a reason for hiding this comment

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

You can add stack.push("\0".repeat(stack.get_sp() % 16).as_bytes(), &mut data); code here instead of calculating the pointers_count.

data
}

Expand Down