Skip to content

scx_rustland_core: Forbid mmap() syscall #1812

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

Merged
merged 2 commits into from
May 6, 2025
Merged
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: 2 additions & 0 deletions .github/include/flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@
gnused
isort
jq
libseccomp.lib
llvmPackages.libclang
llvmPackages.libllvm
pkg-config
Expand Down Expand Up @@ -137,6 +138,7 @@
elfutils.out
zlib
zstd.out
libseccomp.lib
]))) + "'")
]
];
Expand Down
20 changes: 20 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions rust/scx_rustland_core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ anyhow = "1.0.65"
plain = "0.2.3"
libbpf-rs = "=0.25.0-beta.1"
libc = "0.2.137"
seccomp = "0.1"
scx_utils = { path = "../scx_utils", version = "1.0.14" }

[build-dependencies]
Expand Down
9 changes: 5 additions & 4 deletions rust/scx_rustland_core/assets/bpf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,10 +205,6 @@ impl<'cb> BpfScheduler<'cb> {
skel_builder.obj_builder.debug(debug);
let mut skel = scx_ops_open!(skel_builder, open_object, rustland)?;

// Lock all the memory to prevent page faults that could trigger potential deadlocks during
// scheduling.
ALLOCATOR.lock_memory();

// Copy one item from the ring buffer.
//
// # Safety
Expand Down Expand Up @@ -282,6 +278,11 @@ impl<'cb> BpfScheduler<'cb> {
let dispatched = libbpf_rs::UserRingBuffer::new(&maps.dispatched)
.expect("failed to create user ringbuf");

// Lock all the memory to prevent page faults that could trigger potential deadlocks during
// scheduling.
ALLOCATOR.lock_memory();
ALLOCATOR.disable_mmap().expect("Failed to disable mmap");

// Make sure to use the SCHED_EXT class at least for the scheduler itself.
match Self::use_sched_ext() {
0 => Ok(Self {
Expand Down
19 changes: 19 additions & 0 deletions rust/scx_rustland_core/src/alloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
// This software may be used and distributed according to the terms of the
// GNU General Public License version 2.

use seccomp::*;
use std::alloc::{GlobalAlloc, Layout};
use std::fs::File;
use std::io::{BufRead, BufReader, Write};
Expand Down Expand Up @@ -456,6 +457,24 @@ impl UserAllocator {
}
}

// Enable a seccomp filter that sends a SIGSYS when mmap() is called.
#[allow(static_mut_refs)]
pub fn disable_mmap(&self) -> Result<(), Box<dyn std::error::Error>> {
let mut ctx = seccomp::Context::default(Action::Allow)?;
let syscall_nr = libc::SYS_mmap as usize;
let cmp = Compare::arg(0)
.using(Op::MaskedEq)
.with(0)
.build()
.ok_or("Failed to build seccomp filter")?;

let rule = Rule::new(syscall_nr, cmp, Action::Errno(libc::EPERM));
ctx.add_rule(rule)?;
ctx.load()?;

Ok(())
}

#[allow(static_mut_refs)]
pub fn lock_memory(&self) {
unsafe {
Expand Down
3 changes: 2 additions & 1 deletion scheds/rust/scx_rustland/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,8 @@ struct Scheduler<'a> {

impl<'a> Scheduler<'a> {
fn init(opts: &Opts, open_object: &'a mut MaybeUninit<OpenObject>) -> Result<Self> {
let stats_server = StatsServer::new(stats::server_data()).launch()?;

// Low-level BPF connector.
let bpf = BpfScheduler::init(
open_object,
Expand All @@ -227,7 +229,6 @@ impl<'a> Scheduler<'a> {
opts.verbose,
true, // Enable built-in idle CPU selection policy
)?;
let stats_server = StatsServer::new(stats::server_data()).launch()?;

info!("{} scheduler attached", SCHEDULER_NAME);

Expand Down