Add syscall chroot

Signed-off-by: Zhenchen Wang <m202372036@hust.edu.cn>

Signed-off-by: Zhenchen Wang <m202372036@hust.edu.cn>
This commit is contained in:
Zhenchen Wang
2024-03-01 09:47:14 +08:00
committed by Tate, Hongliang Tian
parent f924eb3694
commit ed0752aacc
3 changed files with 41 additions and 0 deletions

View File

@ -57,6 +57,11 @@ impl FsResolver {
self.cwd = dentry;
}
/// Set the root directory
pub fn set_root(&mut self, dentry: Arc<Dentry>) {
self.root = dentry;
}
/// Open or create a file inode handler.
pub fn open(&self, path: &FsPath, flags: u32, mode: u16) -> Result<InodeHandle> {
let creation_flags = CreationFlags::from_bits_truncate(flags);

View File

@ -0,0 +1,32 @@
// SPDX-License-Identifier: MPL-2.0
use super::{SyscallReturn, SYS_CHROOT};
use crate::{
fs::{fs_resolver::FsPath, utils::InodeType},
log_syscall_entry,
prelude::*,
syscall::constants::MAX_FILENAME_LEN,
util::read_cstring_from_user,
};
pub fn sys_chroot(pathname_addr: Vaddr) -> Result<SyscallReturn> {
log_syscall_entry!(SYS_CHROOT);
let pathname = read_cstring_from_user(pathname_addr, MAX_FILENAME_LEN)?;
debug!("pathname = {:?}", pathname);
let current = current!();
let mut fs = current.fs().write();
let dentry = {
let pathname = pathname.to_string_lossy();
if pathname.is_empty() {
return_errno_with_message!(Errno::ENOENT, "path is empty");
}
let fs_path = FsPath::try_from(pathname.as_ref())?;
fs.lookup(&fs_path)?
};
if dentry.type_() != InodeType::Dir {
return_errno_with_message!(Errno::ENOTDIR, "must be directory");
}
fs.set_root(dentry);
Ok(SyscallReturn::Return(0))
}

View File

@ -25,6 +25,7 @@ use crate::{
chdir::{sys_chdir, sys_fchdir},
chmod::{sys_chmod, sys_fchmod, sys_fchmodat},
chown::{sys_chown, sys_fchown, sys_fchownat, sys_lchown},
chroot::sys_chroot,
clock_gettime::sys_clock_gettime,
clock_nanosleep::sys_clock_nanosleep,
clone::sys_clone,
@ -104,6 +105,7 @@ mod brk;
mod chdir;
mod chmod;
mod chown;
mod chroot;
mod clock_gettime;
mod clock_nanosleep;
mod clone;
@ -330,6 +332,7 @@ define_syscall_nums!(
SYS_SET_PRIORITY = 141,
SYS_PRCTL = 157,
SYS_ARCH_PRCTL = 158,
SYS_CHROOT = 161,
SYS_SYNC = 162,
SYS_GETTID = 186,
SYS_TIME = 201,
@ -517,6 +520,7 @@ pub fn syscall_dispatch(
SYS_SET_PRIORITY => syscall_handler!(3, sys_set_priority, args),
SYS_PRCTL => syscall_handler!(5, sys_prctl, args),
SYS_ARCH_PRCTL => syscall_handler!(2, sys_arch_prctl, args, context),
SYS_CHROOT => syscall_handler!(1, sys_chroot, args),
SYS_SYNC => syscall_handler!(0, sys_sync),
SYS_GETTID => syscall_handler!(0, sys_gettid),
SYS_TIME => syscall_handler!(1, sys_time, args),