Skip to content

Commit

Permalink
uucore: disable default signal-handlers added by Rust
Browse files Browse the repository at this point in the history
Fixes uutils#6759

Test procedure (verifies that a single SIGSEGV stops a program):
```
ecordonnier@lj8k2dq3:~/dev/coreutils$ ./target/release/coreutils sleep 100 &
[1] 4175464
ecordonnier@lj8k2dq3:~/dev/coreutils$ kill -11 $(pidof coreutils)
ecordonnier@lj8k2dq3:~/dev/coreutils$
[1]+  Segmentation fault      (core dumped) ./target/release/coreutils sleep 100
```
  • Loading branch information
Ecordonnier committed Oct 21, 2024
1 parent 3f694fa commit c4fefa9
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
21 changes: 20 additions & 1 deletion src/uucore/src/lib/features/signals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
use nix::errno::Errno;
#[cfg(unix)]
use nix::sys::signal::{
signal, SigHandler::SigDfl, SigHandler::SigIgn, Signal::SIGINT, Signal::SIGPIPE,
sigaction, signal, SigHandler::SigDfl, SigHandler::SigIgn, Signal::SIGINT, Signal::SIGPIPE, Signal::SIGBUS, Signal::SIGSEGV, SigSet, SaFlags, SigAction
};

/// The default signal value.
Expand Down Expand Up @@ -387,6 +387,25 @@ pub fn ignore_interrupts() -> Result<(), Errno> {
unsafe { signal(SIGINT, SigIgn) }.map(|_| ())
}

// Disables the custom signal handlers installed by Rust for stack-overflow handling. With those custom signal handlers processes ignore the first SIGBUS and SIGSEGV signal they receive.
// See https://github.com/rust-lang/rust/blob/8ac1525e091d3db28e67adcbbd6db1e1deaa37fb/src/libstd/sys/unix/stack_overflow.rs#L71-L92 for details.
#[cfg(unix)]
pub fn disable_rust_signal_handlers() -> Result<(), Errno> {
unsafe {
sigaction(
SIGSEGV,
&SigAction::new(SigDfl, SaFlags::empty(), SigSet::all()),
)
}?;
unsafe {
sigaction(
SIGBUS,
&SigAction::new(SigDfl, SaFlags::empty(), SigSet::all()),
)
}?;
Ok(())
}

#[test]
fn signal_by_value() {
assert_eq!(signal_by_name_or_value("0"), Some(0));
Expand Down
3 changes: 3 additions & 0 deletions src/uucore_procs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ pub fn main(_args: TokenStream, stream: TokenStream) -> TokenStream {

let new = quote!(
pub fn uumain(args: impl uucore::Args) -> i32 {
#[cfg(unix)]
uucore::signals::disable_rust_signal_handlers().expect("Disabling rust signal handlers failed");

#stream
let result = uumain(args);
match result {
Expand Down

0 comments on commit c4fefa9

Please sign in to comment.