-
Notifications
You must be signed in to change notification settings - Fork 196
Draft: seccomp syscall and ioctls #1458
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,7 +15,8 @@ use crate::fd::BorrowedFd; | |
use crate::io; | ||
use crate::pid::Pid; | ||
use crate::thread::{ | ||
futex, ClockId, Cpuid, MembarrierCommand, MembarrierQuery, NanosleepRelativeResult, Timespec, | ||
futex, ClockId, Cpuid, MembarrierCommand, MembarrierQuery, NanosleepRelativeResult, | ||
SetSecureComputingFilterFlags, Timespec, | ||
}; | ||
use crate::utils::as_mut_ptr; | ||
use core::mem::MaybeUninit; | ||
|
@@ -547,3 +548,12 @@ pub(crate) fn membarrier_cpu(cmd: MembarrierCommand, cpu: Cpuid) -> io::Result<( | |
)) | ||
} | ||
} | ||
|
||
#[cfg(linux_kernel)] | ||
pub(crate) fn seccomp( | ||
operation: super::types::SeccompOperation, | ||
flags: Option<SetSecureComputingFilterFlags>, | ||
args: *mut c::c_void, | ||
) -> io::Result<c::c_int> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This function should be |
||
unsafe { ret_c_int(syscall!(__NR_seccomp, operation, flags, args)) } | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
use core::marker::PhantomData; | ||
use core::ptr::null_mut; | ||
|
||
use bitflags::bitflags; | ||
|
||
use crate::backend::c; | ||
use crate::backend::thread::syscalls; | ||
use crate::backend::thread::types::SeccompOperation; | ||
use crate::io; | ||
|
||
bitflags! { | ||
/// fixme | ||
pub struct SetSecureComputingFilterFlags: u32 { | ||
/// fixme | ||
const TSYNC = c::SECCOMP_FILTER_FLAG_TSYNC; | ||
/// fixme | ||
const LOG = c::SECCOMP_FILTER_FLAG_LOG; | ||
/// fixme | ||
const SPEC_ALLOW = c::SECCOMP_FILTER_FLAG_SPEC_ALLOW; | ||
/// fixme | ||
const NEW_LISTENER = c::SECCOMP_FILTER_FLAG_NEW_LISTENER; | ||
/// fixme | ||
const TSYNC_ESRCH = c::SECCOMP_FILTER_FLAG_TSYNC_ESRCH; | ||
/// fixme | ||
const WAIT_KILLABLE_RECV = c::SECCOMP_FILTER_FLAG_WAIT_KILLABLE_RECV; | ||
|
||
const _ = !0; | ||
} | ||
} | ||
|
||
/// fixme | ||
#[derive(Debug)] | ||
#[repr(transparent)] | ||
pub struct SecureComputingFilterLine(c::sock_filter); | ||
|
||
/// fixme | ||
#[derive(Debug)] | ||
#[repr(transparent)] | ||
pub struct SecureComputingFilter<'a>(c::sock_fprog, PhantomData<&'a [SecureComputingFilterLine]>); | ||
impl<'a> From<&'a [SecureComputingFilterLine]> for SecureComputingFilter<'a> { | ||
fn from(filter_lines: &'a [SecureComputingFilterLine]) -> Self { | ||
Self( | ||
c::sock_fprog { | ||
// usize as u16 is lossy. However filter programs with more than BPF_MAXINSNS (4096) | ||
// will be rejected by the kernel with EINVAL. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If the user passes a filter list that's too long, it seems like it's be better to fail than to silently truncate the list. The user may be assuming that if the call succeeds, the entire filter list is installed. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The kernel will return EINVAL for
|
||
len: filter_lines.len() as u16, | ||
filter: filter_lines.as_ptr() as *mut c::sock_filter, | ||
}, | ||
PhantomData, | ||
) | ||
} | ||
} | ||
|
||
/// fixme | ||
pub fn set_secure_computing_mode_strict() -> io::Result<()> { | ||
syscalls::seccomp(SeccompOperation::SetModeStrict, None, null_mut()).map(|_| ()) | ||
} | ||
|
||
/// fixme | ||
/// | ||
/// ... low level interface ... you likely want to use a library like libseccomp. | ||
/// | ||
/// ... return value can be anything .... no I/O Safety ... | ||
pub fn set_secure_computing_mode_filter( | ||
filter: &SecureComputingFilter, | ||
flags: SetSecureComputingFilterFlags, | ||
) -> io::Result<i32> { | ||
syscalls::seccomp( | ||
SeccompOperation::SetModeFilter, | ||
Some(flags), | ||
filter as *const _ as *mut c::c_void, | ||
) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function should also be
unsafe
.