-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Read vDSO from auxiliary vector (#3)
* Read vDSO from auxiliary vector * Read the auxv in during the startup phase Having `putenv()` called overwrites the auxiliary vector, giving garbage values for the vDSO base address and the page size. Reading these values from the auxiliary constructor during the startup phase, before any actual program code executes prevents the issue. The auxiliary vector is only read once and stored as a static variable.
- Loading branch information
1 parent
16798e3
commit 77594f1
Showing
7 changed files
with
123 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
use std::error::Error; | ||
use ctor::ctor; | ||
|
||
#[derive(Debug, Copy, Clone)] | ||
pub struct AuxVecValues { | ||
pub(crate) vdso_base: usize, | ||
pub(crate) page_size: usize, | ||
} | ||
|
||
extern "C" { | ||
static environ: *const *const u8; | ||
} | ||
|
||
unsafe fn get_auxv_ptr() -> *const usize { | ||
// the auxiliary vector is right behind the environment variables, which | ||
// is an array of strings, delimited by a nullpointer. | ||
let mut env_entry_ptr = environ; | ||
|
||
while !(*env_entry_ptr).is_null() { | ||
env_entry_ptr = env_entry_ptr.offset(1); | ||
} | ||
|
||
env_entry_ptr = env_entry_ptr.offset(1); | ||
|
||
return std::mem::transmute::<*const *const u8, *const usize>(env_entry_ptr); | ||
} | ||
|
||
pub(crate) fn read_aux_vec() -> Result<AuxVecValues, Box<dyn Error>> { | ||
Ok(*AUX) | ||
} | ||
//#[cfg_attr(any(target_os = "linux"), link_section = ".init_array")] | ||
#[ctor] | ||
static AUX: AuxVecValues = { | ||
// The auxiliary vector is an array of key:value tuples, represented as [usize, usize] | ||
// The end is delimited by having the key == AT_NULL | ||
let mut out = unsafe { get_auxv_ptr() }; | ||
let mut ptr = 0; | ||
let mut pagesize = 0; | ||
unsafe { | ||
while *out != libc::AT_NULL as usize { | ||
let key = *out; | ||
let val = *out.offset(1); | ||
if key == libc::AT_SYSINFO_EHDR as usize { | ||
ptr = val; | ||
} | ||
if key == libc::AT_PAGESZ as usize { | ||
pagesize = val; | ||
} | ||
out = out.offset(2); | ||
} | ||
} | ||
if ptr == 0 { | ||
panic!("Could not find vDSO base"); | ||
} | ||
if pagesize == 0 { | ||
panic!("Could not find page size"); | ||
} | ||
AuxVecValues {vdso_base: ptr, page_size: pagesize} | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters