Fix permission validation in mprotect and mmap

This commit is contained in:
Marsman1996 2026-01-26 15:47:03 +08:00 committed by Tate, Hongliang Tian
parent 306cc714ae
commit ea142155fb
3 changed files with 20 additions and 2 deletions

View File

@ -22,7 +22,7 @@ pub fn sys_mmap(
offset: u64, offset: u64,
ctx: &Context, ctx: &Context,
) -> Result<SyscallReturn> { ) -> Result<SyscallReturn> {
let perms = VmPerms::from_bits_truncate(perms as u32); let perms = VmPerms::from_user_bits_truncate(perms as u32);
let option = MMapOptions::try_from(flags as u32)?; let option = MMapOptions::try_from(flags as u32)?;
let res = do_sys_mmap( let res = do_sys_mmap(
addr as usize, addr as usize,

View File

@ -9,7 +9,7 @@ use crate::{
}; };
pub fn sys_mprotect(addr: Vaddr, len: usize, perms: u64, ctx: &Context) -> Result<SyscallReturn> { pub fn sys_mprotect(addr: Vaddr, len: usize, perms: u64, ctx: &Context) -> Result<SyscallReturn> {
let vm_perms = VmPerms::from_bits_truncate(perms as u32); let vm_perms = VmPerms::from_user_bits(perms as u32)?;
debug!( debug!(
"addr = 0x{:x}, len = 0x{:x}, perms = {:?}", "addr = 0x{:x}, len = 0x{:x}, perms = {:?}",
addr, len, vm_perms addr, len, vm_perms

View File

@ -42,6 +42,24 @@ impl VmPerms {
Ok(()) Ok(())
} }
/// Parses `bits` as requested permissions from user programs and returns errors
/// if there are unknown permissions.
pub fn from_user_bits(bits: u32) -> Result<Self> {
if let Some(vm_perms) = VmPerms::from_bits(bits)
&& Self::ALL_PERMS.contains(vm_perms)
{
Ok(vm_perms)
} else {
return_errno_with_message!(Errno::EINVAL, "invalid permissions");
}
}
/// Parses `bits` as requested permissions from user programs and ignores any
/// unknown permissions.
pub fn from_user_bits_truncate(bits: u32) -> Self {
VmPerms::from_bits_truncate(bits) & Self::ALL_PERMS
}
} }
impl From<Rights> for VmPerms { impl From<Rights> for VmPerms {