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 arch;
pub mod random;
pub mod vfs;
pub mod device {

View File

@ -1,8 +1,9 @@
//! Random generation utilities
use libk::device::monotonic_timestamp;
use libk_util::{sync::IrqSafeSpinlock, OneTimeInit};
use crate::device::monotonic_timestamp;
const BUFFER_SIZE: usize = 1024;
struct RandomState {
@ -64,6 +65,14 @@ pub fn read(buf: &mut [u8]) {
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
pub fn init() {
let now = monotonic_timestamp().unwrap();

View File

@ -12,9 +12,12 @@ use libk_mm::{
use libk_util::io::{Read, Seek};
use yggdrasil_abi::{error::Error, io::SeekFrom};
use crate::task::{
use crate::{
random,
task::{
process::ProcessImage,
types::{ProcessTlsInfo, ProcessTlsLayout},
},
};
cfg_if! {
@ -243,7 +246,10 @@ fn elf_load_address(elf_type: u16, virtual_address: usize) -> usize {
match elf_type {
elf::abi::ET_EXEC => virtual_address,
// 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()
_ => unreachable!(),
}

View File

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

View File

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

View File

View File

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

View File

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