Skip to content

Commit

Permalink
Disable CFI for weakly linked syscalls
Browse files Browse the repository at this point in the history
Currently, when enabling CFI via -Zsanitizer=cfi and executing e.g.
std::sys::random::getrandom, we can observe a CFI violation. This is
the case for all consumers of the std::sys::pal::weak::weak macro,
as it is defining weak functions which don't show up in LLVM IR
metadata. CFI fails for all these functions.

Similar to other such cases in
rust-lang#115199, this change stops
emitting the CFI typecheck for consumers of the macro via the
\#[no_sanitize(cfi)] attribute.
  • Loading branch information
1c3t3a committed Mar 10, 2025
1 parent 2c6a12e commit 02bb2d4
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 0 deletions.
12 changes: 12 additions & 0 deletions library/std/src/sys/fs/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1454,6 +1454,18 @@ impl File {
Ok(())
}

#[cfg_attr(
any(
target_os = "android",
all(
target_os = "linux",
target_env = "gnu",
target_pointer_width = "32",
not(target_arch = "riscv32")
)
),
no_sanitize(cfi)
)]
pub fn set_times(&self, times: FileTimes) -> io::Result<()> {
#[cfg(not(any(
target_os = "redox",
Expand Down
1 change: 1 addition & 0 deletions library/std/src/sys/pal/unix/fd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ impl FileDesc {
}

#[cfg(all(target_os = "android", target_pointer_width = "32"))]
#[no_sanitize(cfi)]
pub fn read_vectored_at(&self, bufs: &mut [IoSliceMut<'_>], offset: u64) -> io::Result<usize> {
super::weak::weak!(fn preadv64(libc::c_int, *const libc::iovec, libc::c_int, off64_t) -> isize);

Expand Down
1 change: 1 addition & 0 deletions library/std/src/sys/pal/unix/process/process_unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,7 @@ impl Command {
target_os = "nto",
target_vendor = "apple",
))]
#[cfg_attr(target_os = "linux", no_sanitize(cfi))]
fn posix_spawn(
&mut self,
stdio: &ChildPipes,
Expand Down
1 change: 1 addition & 0 deletions library/std/src/sys/pal/unix/thread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ impl Thread {
}

#[cfg(any(target_os = "solaris", target_os = "illumos", target_os = "nto"))]
#[no_sanitize(cfi)]
pub fn set_name(name: &CStr) {
weak! {
fn pthread_setname_np(
Expand Down
9 changes: 9 additions & 0 deletions library/std/src/sys/pal/unix/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,15 @@ impl Timespec {
}
}

#[cfg_attr(
all(
target_os = "linux",
target_env = "gnu",
target_pointer_width = "32",
not(target_arch = "riscv32")
),
no_sanitize(cfi)
)]
pub fn now(clock: libc::clockid_t) -> Timespec {
use crate::mem::MaybeUninit;
use crate::sys::cvt;
Expand Down
3 changes: 3 additions & 0 deletions library/std/src/sys/pal/unix/weak.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,9 @@ unsafe fn fetch(name: &str) -> *mut libc::c_void {
#[cfg(not(any(target_os = "linux", target_os = "android")))]
pub(crate) macro syscall {
(fn $name:ident($($arg_name:ident: $t:ty),*) -> $ret:ty) => (
// FIXME: Rust currently omits weak function definitions
// and its metadata from LLVM IR.
#[no_sanitize(cfi)]
unsafe fn $name($($arg_name: $t),*) -> $ret {
weak! { fn $name($($t),*) -> $ret }

Expand Down

0 comments on commit 02bb2d4

Please sign in to comment.