From 8fcbf152eb0450a688628496f183f7c59d0d3703 Mon Sep 17 00:00:00 2001 From: Jeremy Soller Date: Mon, 31 Dec 2018 21:04:21 -0700 Subject: [PATCH] Add mprotect --- Cargo.lock | 10 ++++----- src/syscall/mod.rs | 1 + src/syscall/process.rs | 49 +++++++++++++++++++++++++++++++++++++++++- syscall | 2 +- 4 files changed, 55 insertions(+), 7 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index a243fc8..ad0f8f4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -65,7 +65,7 @@ dependencies = [ "semver 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.83 (registry+https://github.com/rust-lang/crates.io-index)", "serde_derive 1.0.83 (registry+https://github.com/rust-lang/crates.io-index)", - "serde_json 1.0.33 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_json 1.0.34 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -178,7 +178,7 @@ dependencies = [ "goblin 0.0.15 (registry+https://github.com/rust-lang/crates.io-index)", "linked_list_allocator 0.6.3 (registry+https://github.com/rust-lang/crates.io-index)", "raw-cpuid 4.0.0 (registry+https://github.com/rust-lang/crates.io-index)", - "redox_syscall 0.1.49", + "redox_syscall 0.1.50", "slab_allocator 0.3.1", "spin 0.4.10 (registry+https://github.com/rust-lang/crates.io-index)", "x86 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)", @@ -279,7 +279,7 @@ dependencies = [ [[package]] name = "redox_syscall" -version = "0.1.49" +version = "0.1.50" [[package]] name = "regex" @@ -358,7 +358,7 @@ dependencies = [ [[package]] name = "serde_json" -version = "1.0.33" +version = "1.0.34" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "itoa 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)", @@ -526,7 +526,7 @@ dependencies = [ "checksum semver-parser 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "388a1df253eca08550bef6c72392cfe7c30914bf41df5269b68cbd6ff8f570a3" "checksum serde 1.0.83 (registry+https://github.com/rust-lang/crates.io-index)" = "157e12af46859e968da75dea9845530e13d03bcab2009a41b9b7bb3cf4eb3ec2" "checksum serde_derive 1.0.83 (registry+https://github.com/rust-lang/crates.io-index)" = "9469829702497daf2daf3c190e130c3fa72f719920f73c86160d43e8f8d76951" -"checksum serde_json 1.0.33 (registry+https://github.com/rust-lang/crates.io-index)" = "c37ccd6be3ed1fdf419ee848f7c758eb31b054d7cd3ae3600e3bae0adf569811" +"checksum serde_json 1.0.34 (registry+https://github.com/rust-lang/crates.io-index)" = "bdf540260cfee6da923831f4776ddc495ada940c30117977c70f1313a6130545" "checksum spin 0.4.10 (registry+https://github.com/rust-lang/crates.io-index)" = "ceac490aa12c567115b40b7b7fceca03a6c9d53d5defea066123debc83c5dc1f" "checksum syn 0.15.23 (registry+https://github.com/rust-lang/crates.io-index)" = "9545a6a093a3f0bd59adb472700acc08cad3776f860f16a897dfce8c88721cbc" "checksum thread_local 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "c6b53e329000edc2b34dbe8545fd20e55a333362d0a321909685a19bd28c3f1b" diff --git a/src/syscall/mod.rs b/src/syscall/mod.rs index 3f97a4e..399c4e0 100644 --- a/src/syscall/mod.rs +++ b/src/syscall/mod.rs @@ -106,6 +106,7 @@ pub fn syscall(a: usize, b: usize, c: usize, d: usize, e: usize, f: usize, bp: u SYS_GETGID => getgid(), SYS_GETNS => getns(), SYS_GETUID => getuid(), + SYS_MPROTECT => mprotect(b, c, d), SYS_MKNS => mkns(validate_slice(b as *const [usize; 2], c)?), SYS_SETPGID => setpgid(ContextId::from(b), ContextId::from(c)), SYS_SETREUID => setreuid(b as u32, c as u32), diff --git a/src/syscall/process.rs b/src/syscall/process.rs index 403cb28..60afbf3 100644 --- a/src/syscall/process.rs +++ b/src/syscall/process.rs @@ -9,6 +9,7 @@ use spin::Mutex; use memory::allocate_frames; use paging::{ActivePageTable, InactivePageTable, Page, VirtualAddress}; use paging::entry::EntryFlags; +use paging::mapper::MapperFlushAll; use paging::temporary_page::TemporaryPage; use start::usermode; use interrupt; @@ -22,7 +23,9 @@ use scheme::FileHandle; use syscall; use syscall::data::{SigAction, Stat}; use syscall::error::*; -use syscall::flag::{CLONE_VFORK, CLONE_VM, CLONE_FS, CLONE_FILES, CLONE_SIGHAND, SIG_DFL, SIGCONT, SIGTERM, WCONTINUED, WNOHANG, WUNTRACED, wifcontinued, wifstopped}; +use syscall::flag::{CLONE_VFORK, CLONE_VM, CLONE_FS, CLONE_FILES, CLONE_SIGHAND, + PROT_EXEC, PROT_READ, PROT_WRITE, SIG_DFL, SIGCONT, SIGTERM, + WCONTINUED, WNOHANG, WUNTRACED, wifcontinued, wifstopped}; use syscall::validate::{validate_slice, validate_slice_mut}; pub fn brk(address: usize) -> Result { @@ -1127,6 +1130,50 @@ pub fn kill(pid: ContextId, sig: usize) -> Result { } } +pub fn mprotect(address: usize, size: usize, flags: usize) -> Result { + println!("mprotect {:#X}, {}, {:#X}", address, size, flags); + + let end_offset = size.checked_sub(1).ok_or(Error::new(EFAULT))?; + let end_address = address.checked_add(end_offset).ok_or(Error::new(EFAULT))?; + + let mut active_table = unsafe { ActivePageTable::new() }; + + let mut flush_all = MapperFlushAll::new(); + + let start_page = Page::containing_address(VirtualAddress::new(address)); + let end_page = Page::containing_address(VirtualAddress::new(end_address)); + for page in Page::range_inclusive(start_page, end_page) { + if let Some(mut page_flags) = active_table.translate_page_flags(page) { + if flags & PROT_EXEC > 0 { + page_flags.remove(EntryFlags::NO_EXECUTE); + } else { + page_flags.insert(EntryFlags::NO_EXECUTE); + } + + if flags & PROT_WRITE > 0 { + //TODO: Not allowing gain of write privileges + } else { + page_flags.remove(EntryFlags::WRITABLE); + } + + if flags & PROT_READ > 0 { + //TODO: No flags for readable pages + } else { + //TODO: No flags for readable pages + } + + let flush = active_table.remap(page, page_flags); + flush_all.consume(flush); + } else { + return Err(Error::new(EFAULT)); + } + } + + flush_all.flush(&mut active_table); + + Ok(0) +} + pub fn setpgid(pid: ContextId, pgid: ContextId) -> Result { let contexts = context::contexts(); diff --git a/syscall b/syscall index 6909fb1..9a3734c 160000 --- a/syscall +++ b/syscall @@ -1 +1 @@ -Subproject commit 6909fb1c32d2c4dd75c0967ab06b894c8f7ed308 +Subproject commit 9a3734c41ac452bc3f8fe6d9a6b687c96cc3ca15