proc/elf: implement ASLR for reloc ELFs

This commit is contained in:
Mark Poliakov 2024-03-18 20:08:24 +02:00
parent ffeb4522c9
commit 890204e473
8 changed files with 33 additions and 17 deletions

View File

@ -28,6 +28,7 @@ extern crate alloc;
pub mod task; pub mod task;
pub mod arch; pub mod arch;
pub mod random;
pub mod vfs; pub mod vfs;
pub mod device { pub mod device {

View File

@ -1,8 +1,9 @@
//! Random generation utilities //! Random generation utilities
use libk::device::monotonic_timestamp;
use libk_util::{sync::IrqSafeSpinlock, OneTimeInit}; use libk_util::{sync::IrqSafeSpinlock, OneTimeInit};
use crate::device::monotonic_timestamp;
const BUFFER_SIZE: usize = 1024; const BUFFER_SIZE: usize = 1024;
struct RandomState { struct RandomState {
@ -64,6 +65,14 @@ pub fn read(buf: &mut [u8]) {
state.lock().read_buf(buf) state.lock().read_buf(buf)
} }
pub fn range(a: u64, b: u64) -> u64 {
assert!(b > a);
let mut bytes = [0; 8];
read(&mut bytes);
let v = u64::from_ne_bytes(bytes) % (b - a);
a + v
}
/// Initializes the random generator state /// Initializes the random generator state
pub fn init() { pub fn init() {
let now = monotonic_timestamp().unwrap(); let now = monotonic_timestamp().unwrap();

View File

@ -12,9 +12,12 @@ use libk_mm::{
use libk_util::io::{Read, Seek}; use libk_util::io::{Read, Seek};
use yggdrasil_abi::{error::Error, io::SeekFrom}; use yggdrasil_abi::{error::Error, io::SeekFrom};
use crate::task::{ use crate::{
process::ProcessImage, random,
types::{ProcessTlsInfo, ProcessTlsLayout}, task::{
process::ProcessImage,
types::{ProcessTlsInfo, ProcessTlsLayout},
},
}; };
cfg_if! { cfg_if! {
@ -243,7 +246,10 @@ fn elf_load_address(elf_type: u16, virtual_address: usize) -> usize {
match elf_type { match elf_type {
elf::abi::ET_EXEC => virtual_address, elf::abi::ET_EXEC => virtual_address,
// TODO ASLR through random? // TODO ASLR through random?
elf::abi::ET_DYN => 0x80000, elf::abi::ET_DYN => {
let index = random::range(0x5000, 0x20000);
(index as usize) * 0x1000
}
// Handled in load_elf_from_file() // Handled in load_elf_from_file()
_ => unreachable!(), _ => unreachable!(),
} }

View File

@ -3,7 +3,10 @@
use core::ptr::NonNull; use core::ptr::NonNull;
use kernel_fs::devfs; use kernel_fs::devfs;
use libk::vfs::{impls::read_fn_node, NodeRef}; use libk::{
random,
vfs::{impls::read_fn_node, NodeRef},
};
use libk_mm::{ use libk_mm::{
address::{PhysicalAddress, Virtualize}, address::{PhysicalAddress, Virtualize},
phys, phys,
@ -12,8 +15,6 @@ use libk_util::OneTimeInit;
use memfs::block::{self, BlockAllocator}; use memfs::block::{self, BlockAllocator};
use yggdrasil_abi::{error::Error, io::MountOptions}; use yggdrasil_abi::{error::Error, io::MountOptions};
use crate::proc::random;
// pub mod devfs; // pub mod devfs;
pub mod sysfs; pub mod sysfs;

View File

@ -3,15 +3,16 @@
use abi::error::Error; use abi::error::Error;
use alloc::borrow::ToOwned; use alloc::borrow::ToOwned;
use kernel_fs::devfs; use kernel_fs::devfs;
use libk::task::process::Process; use libk::{
use libk::task::{runtime, thread::Thread}; random,
use libk::vfs::impls::fn_symlink; task::{process::Process, runtime, thread::Thread},
use libk::vfs::{IoContext, NodeRef}; vfs::{impls::fn_symlink, IoContext, NodeRef},
};
use memfs::MemoryFilesystem; use memfs::MemoryFilesystem;
use crate::{ use crate::{
fs::{FileBlockAllocator, INITRD_DATA}, fs::{FileBlockAllocator, INITRD_DATA},
proc::{self, random}, proc::{self},
}; };
fn setup_root() -> Result<NodeRef, Error> { fn setup_root() -> Result<NodeRef, Error> {

View File

View File

@ -7,8 +7,6 @@ use libk::{
vfs::IoContext, vfs::IoContext,
}; };
pub mod random;
/// Loads a binary and creates a process for it. See [libk_thread::binary::load]. /// Loads a binary and creates a process for it. See [libk_thread::binary::load].
#[inline] #[inline]
pub fn load_binary<P: AsRef<Path>>( pub fn load_binary<P: AsRef<Path>>(

View File

@ -10,10 +10,10 @@ pub(crate) use abi::{
process::{Signal, SignalEntryData, SpawnOptions}, process::{Signal, SignalEntryData, SpawnOptions},
system::SystemInfo, system::SystemInfo,
}; };
use libk::task::thread::Thread; use libk::{random, task::thread::Thread};
use libk_mm::phys; use libk_mm::phys;
use crate::{debug::LogLevel, fs, proc::random}; use crate::{debug::LogLevel, fs};
use super::run_with_io; use super::run_with_io;