-
Notifications
You must be signed in to change notification settings - Fork 186
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
Android/Linux: Support msan; unpoison output of getrandom syscall. #463
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
// Support for raw system calls on Linux. | ||
// | ||
// # Sanitizers | ||
// | ||
// Currently only Memory Sanitizer is actively supported. | ||
// | ||
// TODO: Support address sanitizer, in particular in `pre_write_range`. | ||
// | ||
// ## Memory Sanitizer | ||
// | ||
// See https://github.com/llvm/llvm-project/commit/ac9ee01fcbfac745aaedca0393a8e1c8a33acd8d: | ||
// LLVM uses: | ||
// ```c | ||
// COMMON_INTERCEPTOR_ENTER(ctx, getrandom, buf, buflen, flags); | ||
// SSIZE_T n = REAL(getrandom)(buf, buflen, flags); | ||
// if (n > 0) { | ||
// COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, n); | ||
// } | ||
// ``` | ||
// and: | ||
// ```c | ||
// #define PRE_SYSCALL(name) \ | ||
// SANITIZER_INTERFACE_ATTRIBUTE void __sanitizer_syscall_pre_impl_##name | ||
// #define PRE_WRITE(p, s) COMMON_SYSCALL_PRE_WRITE_RANGE(p, s) | ||
// #define POST_WRITE(p, s) COMMON_SYSCALL_POST_WRITE_RANGE(p, s) | ||
// PRE_SYSCALL(getrandom)(void *buf, uptr count, long flags) { | ||
// if (buf) { | ||
// PRE_WRITE(buf, count); | ||
// } | ||
// } | ||
// | ||
// POST_SYSCALL(getrandom)(long res, void *buf, uptr count, long flags) { | ||
// if (res > 0 && buf) { | ||
// POST_WRITE(buf, res); | ||
// } | ||
// } | ||
// ``` | ||
|
||
use core::mem::MaybeUninit; | ||
|
||
// MSAN defines: | ||
// | ||
// ```c | ||
// #define COMMON_INTERCEPTOR_ENTER(ctx, func, ...) \ | ||
// if (msan_init_is_running) \ | ||
// return REAL(func)(__VA_ARGS__); \ | ||
// ENSURE_MSAN_INITED(); \ | ||
// MSanInterceptorContext msan_ctx = {IsInInterceptorScope()}; \ | ||
// ctx = (void *)&msan_ctx; \ | ||
// (void)ctx; \ | ||
// InterceptorScope interceptor_scope; \ | ||
// __msan_unpoison(__errno_location(), sizeof(int)); | ||
// ``` | ||
// | ||
// * We assume that memory sanitizer will not use the this crate during the | ||
// initialization of msan, so we don't have to worry about | ||
// `msan_init_is_running`. | ||
// * We assume that rustc/LLVM initializes MSAN before executing any Rust code, | ||
// so we don't need to call `ENSURE_MSAN_INITED`. | ||
// * Notice that `COMMON_INTERCEPTOR_WRITE_RANGE` doesn't use `ctx`, which | ||
// means it is oblivious to `IsInInterceptorScope()`, so we don't have to | ||
// call it. More generally, we don't have to worry about interceptor scopes | ||
// because we are not an interceptor. | ||
// * We don't read from `__errno_location()` so we don't need to unpoison it. | ||
// | ||
// Consequently, MSAN's `COMMON_INTERCEPTOR_ENTER` is a no-op. | ||
// | ||
// MSAN defines: | ||
// ```c | ||
// #define COMMON_SYSCALL_PRE_WRITE_RANGE(p, s) \ | ||
// do { \ | ||
// } while (false) | ||
// ``` | ||
// So MSAN's PRE_SYSCALL hook is also a no-op. | ||
// | ||
// Consequently, we have nothing to do before invoking the syscall unless/until | ||
// we support other sanitizers like ASAN. | ||
#[allow(unused_variables)] | ||
pub fn pre_write_range(_ptr: *mut MaybeUninit<u8>, _size: usize) {} | ||
|
||
// MSNA defines: | ||
// ```c | ||
// #define COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, size) \ | ||
// __msan_unpoison(ptr, size) | ||
// ``` | ||
// and: | ||
// ```c | ||
// #define COMMON_SYSCALL_POST_WRITE_RANGE(p, s) __msan_unpoison(p, s) | ||
// ``` | ||
#[allow(unused_variables)] | ||
pub unsafe fn post_write_range(ptr: *mut MaybeUninit<u8>, size: usize) { | ||
#[cfg(feature = "unstable-sanitize")] | ||
{ | ||
#[cfg(sanitize = "memory")] | ||
{ | ||
use core::ffi::c_void; | ||
extern "C" { | ||
// void __msan_unpoison(const volatile void *a, size_t size); | ||
fn __msan_unpoison(a: *mut c_void, size: usize); | ||
} | ||
__msan_unpoison(ptr.cast::<c_void>(), size) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Isn't it better to use a configuration flag for this instead of this crate feature?
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.
Maybe. The good thing about using a feature flag is that other crates can then trigger it automatically with their own feature flag depending on it. That's what I've been planning to do in ring, for example.