Skip to content
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

allow calling unsafe function and ffi function safely:) #41

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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 src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
//! So far, all our bugs are implemented using a single soundness hole in the Rust compiler.
//!
//! The explanation is detailed in the [`lifetime_expansion`] module.

#![deny(unsafe_code)]

// The actual exploit
Expand All @@ -13,6 +12,7 @@ pub mod references;
pub mod segfault;
pub mod transmute;
pub mod use_after_free;
pub mod safe_ffi;

pub use lifetime_expansion::*;

Expand Down
53 changes: 53 additions & 0 deletions src/safe_ffi.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#[macro_export]
macro_rules! ffi {
(@ ffi {$($vis:tt)*} {$($id:tt)*} fn $i:ident ($($t:ident : $ty:ty),*$(,)?) $(-> $ret:ty)? $(;$($other:tt)*)? ) => {
$($vis:tt)* use safe_wrapper::safe_wrapper as $i;
mod safe_wrapper {
safe!{pub(super) $($id)* fn $i ($($t:$ty),*) $(->$ret)? := safe_wrapper}
$($id)* {
fn $i ($($t:$ty),*) $(-> $ret)?;
}
}
$(
ffi!{$($other)*}
)?
};
(@ safe {$($vis:tt)*} {$($id:tt)*} fn $i:ident ($($t:ident : $ty:ty),*$(,)?) $(-> $ret:ty)? := $ni:ident) => {
$($vis)? $($id)* fn $ni ($($t:$ty),*) $(-> $ret)? {
crate::transmute::transmute::<unsafe $($id)* fn ($($ty),*) $(->$ret)?, $($id)* fn ($($ty),*) $(->$ret)?>($i)($($t),*)
}
};
(@ $command:ident {$($t1:tt)*} {$($t2:tt)*} pub ( $($vis:tt)* ) $($t3:tt)* ) => {ffi!{@ $command {$($t1)*pub($($vis)*)} {$($t2)*} $($t3)*}};
(@ $command:ident {$($t1:tt)*} {$($t2:tt)*} pub $($t3:tt)* ) => {ffi!{@ $command {$($t1)*pub} {$($t2)*} $($t3)*}};
(@ $command:ident {$($t1:tt)*} {$($t2:tt)*} $i:tt $($t3:tt)* ) => {ffi!{@ $command {$($t1)*} {$($t2)*$i} $($t3)*}};
(@ $command:ident $($t:tt)*) => {compile_error!{concat!("no rules matches @ ",stringify!($command))}}; // should be error
()=>{};
($($t:tt)*) => {ffi!{@ffi {} {} $($t)*}}
}
#[macro_export]
macro_rules! safe {
()=>{};
($($t:tt)*) => {ffi!{@safe {} {} $($t)*}};
}
#[cfg(test)]
mod test {
// create safe printf function
ffi!{
extern "C" fn printf(fmt: *const i8)->i32
}
// create an unsafe function which may call later in totally safe block.
mod r#unsafe {
#[allow(unsafe_code)]
pub unsafe fn r#unsafe()->i32{1}
}
// use it and send it to safe macro
use r#unsafe::r#unsafe;
safe!{
fn r#unsafe()->i32 := very_safe
}
#[test]
fn test_unsafe_and_ffi() {
assert_eq!(very_safe(),1); // it is r#unsafe which is called
assert_eq!(printf(c"Fine.\n".as_ptr()),6); // printf in libc is called.
}
}