Skip to content

Commit

Permalink
Move the remaining FFI definitions into libc
Browse files Browse the repository at this point in the history
  • Loading branch information
asomers committed Nov 30, 2022
1 parent a1c0b3e commit a0d7a45
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 25 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ targets = [
]

[dependencies]
libc = "0.2.20"
libc = { git = "https://github.com/rust-lang/libc.git", rev = "d6fe782", features = [ "extra_traits" ] }

[build-dependencies]
version_check = "0.9.4"
Expand Down
9 changes: 2 additions & 7 deletions src/fcntl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ impl FcntlRights {
pub fn from_file<T: AsRawFd>(fd: &T) -> CapResult<FcntlRights> {
unsafe {
let mut empty_fcntls = 0;
let res = cap_fcntls_get(fd.as_raw_fd(), &mut empty_fcntls as *mut u32);
let res = libc::cap_fcntls_get(fd.as_raw_fd(), &mut empty_fcntls as *mut u32);
if res < 0 {
Err(CapErr::from(CapErrType::Get))
} else {
Expand All @@ -66,16 +66,11 @@ impl FcntlRights {
impl CapRights for FcntlRights {
fn limit<T: AsRawFd>(&self, fd: &T) -> CapResult<()> {
unsafe {
if cap_fcntls_limit(fd.as_raw_fd(), self.0) < 0 {
if libc::cap_fcntls_limit(fd.as_raw_fd(), self.0) < 0 {
Err(CapErr::from(CapErrType::Get))
} else {
Ok(())
}
}
}
}

extern "C" {
fn cap_fcntls_limit(fd: i32, fcntlrights: u32) -> i32;
fn cap_fcntls_get(fd: i32, fcntlrightsp: *mut u32) -> i32;
}
9 changes: 2 additions & 7 deletions src/ioctl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ impl IoctlRights {
pub fn from_file<T: AsRawFd>(fd: &T, len: usize) -> CapResult<IoctlRights> {
unsafe {
let mut cmds = Vec::with_capacity(len);
let res = cap_ioctls_get(fd.as_raw_fd(), cmds.as_mut_ptr(), len);
let res = libc::cap_ioctls_get(fd.as_raw_fd(), cmds.as_mut_ptr(), len);
if res == CAP_IOCTLS_ALL {
todo!()
} else if let Ok(rlen) = usize::try_from(res) {
Expand All @@ -67,16 +67,11 @@ impl CapRights for IoctlRights {
fn limit<T: AsRawFd>(&self, fd: &T) -> CapResult<()> {
unsafe {
let len = self.0.len();
if cap_ioctls_limit(fd.as_raw_fd(), self.0.as_ptr(), len) < 0 {
if libc::cap_ioctls_limit(fd.as_raw_fd(), self.0.as_ptr(), len) < 0 {
Err(CapErr::from(CapErrType::Limit))
} else {
Ok(())
}
}
}
}

extern "C" {
fn cap_ioctls_limit(fd: i32, cmds: *const libc::u_long, ncmds: usize) -> i32;
fn cap_ioctls_get(fd: i32, cmds: *mut libc::u_long, maxcmds: usize) -> isize;
}
6 changes: 1 addition & 5 deletions src/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub fn enter() -> io::Result<()> {
}

pub fn sandboxed() -> bool {
unsafe { cap_sandboxed() }
unsafe { libc::cap_sandboxed() }
}

pub fn get_mode() -> io::Result<usize> {
Expand All @@ -25,7 +25,3 @@ pub fn get_mode() -> io::Result<usize> {
}
Ok(mode as usize)
}

extern "C" {
fn cap_sandboxed() -> bool;
}
6 changes: 1 addition & 5 deletions src/right.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ impl FileRights {
pub fn from_file<T: AsRawFd>(fd: &T) -> CapResult<FileRights> {
unsafe {
let mut empty_rights = unsafe { mem::zeroed() };
let res = __cap_rights_get(
let res = libc::__cap_rights_get(
RIGHTS_VERSION,
fd.as_raw_fd(),
&mut empty_rights as *mut cap_rights_t,
Expand Down Expand Up @@ -294,10 +294,6 @@ impl CapRights for FileRights {
}
}

extern "C" {
fn __cap_rights_get(version: i32, fd: RawFd, rightsp: *mut cap_rights_t) -> RawFd;
}

#[test]
fn test_macros() {
assert_eq!(cap_right!(0, 1), 144115188075855873u64);
Expand Down

0 comments on commit a0d7a45

Please sign in to comment.