Skip to content

Commit 144fcd6

Browse files
committed
Add the safe_ffi macro
1 parent ee9f26f commit 144fcd6

File tree

6 files changed

+22
-12
lines changed

6 files changed

+22
-12
lines changed

src/kill.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use libc::{EINVAL, EPERM, ESRCH, SIGKILL, SIGTERM};
88
use crate::errno::errno;
99
use crate::error::{Error, Result};
1010
use crate::process::Process;
11-
use crate::{cli, utils};
11+
use crate::{cli, safe_ffi, utils};
1212

1313
pub fn choose_victim(
1414
proc_buf: &mut [u8],
@@ -101,7 +101,7 @@ pub fn choose_victim(
101101
}
102102

103103
pub fn kill_process(pid: i32, signal: i32) -> Result<()> {
104-
let res = unsafe { kill(pid, signal) };
104+
let res = safe_ffi! { kill(pid, signal) };
105105

106106
if res == -1 {
107107
return Err(match errno() {

src/memory/mem_info.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use libc::sysinfo;
44

55
use crate::{
66
error::{Error, Result},
7+
safe_ffi,
78
utils::bytes_to_megabytes,
89
};
910

@@ -23,7 +24,7 @@ fn sys_info() -> Result<sysinfo> {
2324
let mut sys_info: sysinfo = unsafe { mem::zeroed() };
2425

2526
// Safety: sysinfo() is safe and must not fail when passed a valid reference
26-
let ret_val = unsafe { libc::sysinfo(&mut sys_info) };
27+
let ret_val = safe_ffi! { libc::sysinfo(&mut sys_info) };
2728

2829
if ret_val != 0 {
2930
// The only error that sysinfo() can have happens when

src/memory/mem_lock.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ use libc::{MCL_CURRENT, MCL_FUTURE};
44

55
use crate::errno::errno;
66
use crate::error::{Error, Result};
7+
use crate::safe_ffi;
78

89
extern "C" {
910
pub static _MCL_ONFAULT: libc::c_int;
1011
}
1112

1213
pub fn _mlockall_wrapper(flags: c_int) -> Result<()> {
13-
// Safety: mlockall is safe
14-
let err = unsafe { mlockall(flags) };
14+
let err = safe_ffi! { mlockall(flags) };
1515
if err == 0 {
1616
return Ok(());
1717
}
@@ -38,7 +38,7 @@ pub fn lock_memory_pages() -> Result<()> {
3838
// TODO: check for _MCL_ONFAULT == -1
3939

4040
#[allow(non_snake_case)]
41-
let MCL_ONFAULT: c_int = unsafe { _MCL_ONFAULT };
41+
let MCL_ONFAULT: c_int = safe_ffi! { _MCL_ONFAULT };
4242
match _mlockall_wrapper(MCL_CURRENT | MCL_FUTURE | MCL_ONFAULT) {
4343
Err(err) => {
4444
eprintln!("First try at mlockall failed: {:?}", err);

src/process.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use std::io::Write;
33

44
use libc::getpgid;
55

6+
use crate::safe_ffi;
67
use crate::{
78
error::{Error, Result},
89
utils::{self, str_from_u8},
@@ -35,7 +36,7 @@ impl Process {
3536
/// TODO: would it be better to check for /proc/<PID>/ in here?
3637
pub fn is_alive_from_pid(pid: u32) -> bool {
3738
// Safety: `getpgid` is memory safe
38-
let group_id = unsafe { getpgid(pid as i32) };
39+
let group_id = safe_ffi! { getpgid(pid as i32) };
3940

4041
group_id > 0
4142
}

src/uname.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use std::mem;
33

44
use crate::error::{Error, Result};
55
use crate::linux_version::LinuxVersion;
6+
use crate::safe_ffi;
67
use crate::utils::str_from_u8;
78
use libc::{uname, utsname};
89

@@ -16,8 +17,7 @@ impl Uname {
1617
// can be safely zeroed.
1718
let mut uts_struct: utsname = unsafe { mem::zeroed() };
1819

19-
// No memory unsafety can arise from this call of `uname`
20-
let ret_val = unsafe { uname(&mut uts_struct) };
20+
let ret_val = safe_ffi! { uname(&mut uts_struct) };
2121

2222
// uname returns a negative number upon failure
2323
if ret_val < 0 {
@@ -30,7 +30,6 @@ impl Uname {
3030
pub fn print_info(&self) -> Result<()> {
3131
// Safety: dereference of these raw pointers are safe since we know they're not NULL, since
3232
// the buffers in struct utsname are all correctly allocated in the stack at this moment
33-
3433
let sysname = unsafe { CStr::from_ptr(self.uts_struct.sysname.as_ptr()) };
3534
let hostname = unsafe { CStr::from_ptr(self.uts_struct.nodename.as_ptr()) };
3635
let release = unsafe { CStr::from_ptr(self.uts_struct.release.as_ptr()) };

src/utils.rs

+11-2
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,26 @@ use libc::{getpwuid_r, passwd};
88
use crate::errno::errno;
99
use crate::error::{Error, Result};
1010

11+
/// This macro is used whenever we call a C function but
12+
/// strongly believe that it cannot cause any memory unsafety.
13+
#[macro_export]
14+
macro_rules! safe_ffi {
15+
($e: expr) => {
16+
unsafe { $e }
17+
};
18+
}
19+
1120
/// Gets the effective user ID of the calling process
1221
fn effective_user_id() -> u32 {
1322
// Safety: the POSIX Programmer's Manual states that
1423
// geteuid will always be successful.
15-
unsafe { libc::geteuid() }
24+
safe_ffi! { libc::geteuid() }
1625
}
1726

1827
/// Gets the process group of the process
1928
/// with the given PID.
2029
pub fn get_process_group(pid: i32) -> Result<i32> {
21-
let pgid = unsafe { getpgid(pid) };
30+
let pgid = safe_ffi! { getpgid(pid) };
2231
if pgid == -1 {
2332
return Err(match errno() {
2433
EPERM => Error::NoPermission,

0 commit comments

Comments
 (0)