Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion etc/syscalls_linux_aarch64.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@
| 0x72 (114) | clock_getres | (const clockid_t which_clock, struct __kernel_timespec *tp) | __arm64_sys_clock_getres | false |
| 0x73 (115) | clock_nanosleep | (const clockid_t which_clock, int flags, const struct __kernel_timespec *rqtp, struct __kernel_timespec *rmtp) | __arm64_sys_clock_nanosleep | false |
| 0x74 (116) | syslog | (int type, char *buf, int len) | __arm64_sys_syslog | false |
| 0x75 (117) | ptrace | (long request, long pid, unsigned long addr, unsigned long data) | __arm64_sys_ptrace | false |
| 0x75 (117) | ptrace | (long request, long pid, unsigned long addr, unsigned long data) | __arm64_sys_ptrace | partially |
| 0x76 (118) | sched_setparam | (pid_t pid, struct sched_param *param) | __arm64_sys_sched_setparam | false |
| 0x77 (119) | sched_setscheduler | (pid_t pid, int policy, struct sched_param *param) | __arm64_sys_sched_setscheduler | false |
| 0x78 (120) | sched_getscheduler | (pid_t pid) | __arm64_sys_sched_getscheduler | false |
Expand Down
8 changes: 8 additions & 0 deletions src/arch/arm64/exceptions/syscall.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
fcntl::sys_fcntl,
select::{sys_ppoll, sys_pselect6},
},
ptrace::sys_ptrace,
sleep::sys_nanosleep,
thread_group::{
Pgid,
Expand Down Expand Up @@ -287,9 +288,16 @@
)
.await
}
0x63 => sys_set_robust_list(TUA::from_value(arg1 as _), arg2 as _).await,

Check warning on line 291 in src/arch/arm64/exceptions/syscall.rs

View workflow job for this annotation

GitHub Actions / build-test

Diff in /home/runner/work/moss-kernel/moss-kernel/src/arch/arm64/exceptions/syscall.rs
0x65 => sys_nanosleep(TUA::from_value(arg1 as _), TUA::from_value(arg2 as _)).await,
0x71 => sys_clock_gettime(arg1 as _, TUA::from_value(arg2 as _)).await,
0x75 => sys_ptrace(
arg1 as _,
arg2 as _,
TUA::from_value(arg3 as _),
TUA::from_value(arg4 as _),
)
.await,
0x7b => Err(KernelError::NotSupported),
0x7c => sys_sched_yield(),
0x81 => sys_kill(arg1 as _, arg2.into()),
Expand Down
2 changes: 2 additions & 0 deletions src/process/clone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
use crate::{
process::{TASK_LIST, Task, TaskState},
sched::{self, current::current_task},
sync::SpinLock,

Check warning on line 8 in src/process/clone.rs

View workflow job for this annotation

GitHub Actions / build-test

Diff in /home/runner/work/moss-kernel/moss-kernel/src/process/clone.rs
};
use alloc::boxed::Box;
use core::sync::atomic::AtomicBool;
use bitflags::bitflags;
use libkernel::memory::address::TUA;
use libkernel::{
Expand Down Expand Up @@ -150,9 +151,10 @@
fd_table: files,
cwd,
root,
creds: SpinLock::new(creds),

Check warning on line 154 in src/process/clone.rs

View workflow job for this annotation

GitHub Actions / build-test

Diff in /home/runner/work/moss-kernel/moss-kernel/src/process/clone.rs
state: Arc::new(SpinLock::new(TaskState::Runnable)),
last_cpu: SpinLock::new(CpuId::this()),
ptrace: AtomicBool::new(flags.contains(CloneFlags::CLONE_PTRACE) && current_task.ptrace.load(core::sync::atomic::Ordering::SeqCst)),
}),
}
};
Expand Down
3 changes: 3 additions & 0 deletions src/process/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
sync::{Arc, Weak},
};
use core::fmt::Display;
use core::sync::atomic::AtomicBool;
use creds::Credentials;
use fd_table::FileDescriptorTable;
use libkernel::{VirtualMemory, fs::Inode};
Expand All @@ -18,6 +19,7 @@
pub mod exit;
pub mod fd_table;
pub mod owned;
pub mod ptrace;
pub mod sleep;
pub mod thread_group;
pub mod threading;
Expand Down Expand Up @@ -160,9 +162,10 @@
pub cwd: Arc<SpinLock<(Arc<dyn Inode>, PathBuf)>>,
pub root: Arc<SpinLock<(Arc<dyn Inode>, PathBuf)>>,
pub creds: SpinLock<Credentials>,
pub fd_table: Arc<SpinLock<FileDescriptorTable>>,

Check warning on line 165 in src/process/mod.rs

View workflow job for this annotation

GitHub Actions / build-test

Diff in /home/runner/work/moss-kernel/moss-kernel/src/process/mod.rs
pub state: Arc<SpinLock<TaskState>>,
pub last_cpu: SpinLock<CpuId>,
pub ptrace: AtomicBool
}

impl Task {
Expand Down
3 changes: 3 additions & 0 deletions src/process/owned.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use crate::{
sync::SpinLock,
};
use alloc::sync::Arc;
use core::sync::atomic::AtomicBool;
use libkernel::{
VirtualMemory,
fs::pathbuf::PathBuf,
Expand Down Expand Up @@ -75,6 +76,7 @@ impl OwnedTask {
vm: Arc::new(SpinLock::new(vm)),
fd_table: Arc::new(SpinLock::new(FileDescriptorTable::new())),
last_cpu: SpinLock::new(CpuId::this()),
ptrace: AtomicBool::new(false),
};

Self {
Expand Down Expand Up @@ -102,6 +104,7 @@ impl OwnedTask {
)),
fd_table: Arc::new(SpinLock::new(FileDescriptorTable::new())),
last_cpu: SpinLock::new(CpuId::this()),
ptrace: AtomicBool::new(false),
};

Self {
Expand Down
91 changes: 91 additions & 0 deletions src/process/ptrace.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
use alloc::sync::Arc;

Check warning on line 1 in src/process/ptrace.rs

View workflow job for this annotation

GitHub Actions / build-test

Diff in /home/runner/work/moss-kernel/moss-kernel/src/process/ptrace.rs
use core::sync::atomic::Ordering;
use libkernel::error::{KernelError, Result};
use libkernel::memory::address::UA;

Check warning on line 4 in src/process/ptrace.rs

View workflow job for this annotation

GitHub Actions / build-test

Diff in /home/runner/work/moss-kernel/moss-kernel/src/process/ptrace.rs
use crate::process::{find_task_by_descriptor, Task, Tid, TASK_LIST};
use crate::process::thread_group::signal::SigId;
use crate::sched::current::current_task_shared;

#[repr(i32)]
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
enum PtraceOperation {
TraceMe = 0,
PeekText = 1,
PeekData = 2,
PeekUser = 3,
PokeText = 4,
PokeData = 5,
PokeUser = 6,
Cont = 7,
Kill = 8,
SingleStep = 9,
GetRegs = 12,
SetRegs = 13,
GetFpRegs = 14,
SetFpRegs = 15,
Attach = 16,
Detach = 17,
Syscall = 24,
}

impl TryFrom<i32> for PtraceOperation {
type Error = KernelError;

fn try_from(value: i32) -> Result<Self> {
match value {
0 => Ok(PtraceOperation::TraceMe),
1 => Ok(PtraceOperation::PeekText),

Check warning on line 37 in src/process/ptrace.rs

View workflow job for this annotation

GitHub Actions / build-test

Diff in /home/runner/work/moss-kernel/moss-kernel/src/process/ptrace.rs
2 => Ok(PtraceOperation::PeekData),
// TODO: Should be EIO
_ => Err(KernelError::InvalidValue)
}
}
}

pub async fn sys_ptrace(op: i32, pid: Tid, addr: UA, data: UA) -> Result<usize> {
let op = PtraceOperation::try_from(op)?;
if op == PtraceOperation::TraceMe {
// Change ptrace status
let current_task = current_task_shared();
current_task.ptrace.store(true, Ordering::SeqCst);
return Ok(0);
}
let task_list = TASK_LIST.lock_save_irq();
let id = task_list
.iter()
.find(|(desc, _)| desc.tid == pid)
.map(|(desc, _)| *desc);
drop(task_list);
let task_details = if let Some(desc) = id {
find_task_by_descriptor(&desc)
} else {
None
};
// TODO: Wrong error?
let task = task_details.ok_or(KernelError::NoProcess)?;
// Check if the current task is allowed to ptrace the target task

Check warning on line 66 in src/process/ptrace.rs

View workflow job for this annotation

GitHub Actions / build-test

Diff in /home/runner/work/moss-kernel/moss-kernel/src/process/ptrace.rs
let current_task = current_task_shared();
// TODO: Check CAP_SYS_PTRACE & security
if !current_task.process.children.lock_save_irq().iter().any(|(ch_id, _)| &task.process.tgid == ch_id) {
// TODO: Wrong error
return Err(KernelError::NotSupported);
}
match op {
PtraceOperation::TraceMe => {
unreachable!();
}
PtraceOperation::Attach => {
if !task.ptrace.load(Ordering::SeqCst) {
// TODO: Wrong error
return Err(KernelError::InvalidValue);
}
task.process
.signals
.lock_save_irq()
.set_pending(SigId::SIGSTOP);
Ok(0)
}
// TODO: Wrong error
_ => Err(KernelError::InvalidValue),

Check warning on line 89 in src/process/ptrace.rs

View workflow job for this annotation

GitHub Actions / build-test

Diff in /home/runner/work/moss-kernel/moss-kernel/src/process/ptrace.rs
}
}
Loading