Skip to content

Commit

Permalink
Merge pull request #78 from thesecretmaster/tsm/remove-errno-dep
Browse files Browse the repository at this point in the history
Remove dependency on `errno` crate in favor of std functions
  • Loading branch information
lucab authored Aug 5, 2022
2 parents 6eb6e67 + 4b96829 commit 4a05ffc
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 15 deletions.
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ exclude = [
]

[dependencies]
errno = "^0.2"
libc = "^0.2"
thiserror = "^1.0"
serde = { version = "^1.0", features = ["derive"], optional = true}
Expand Down
13 changes: 9 additions & 4 deletions src/ambient.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,17 @@ use crate::errors::CapsError;
use crate::nr;
use crate::runtime;
use crate::{Capability, CapsHashSet};
use std::io::Error;

pub fn clear() -> Result<(), CapsError> {
let ret = unsafe { libc::prctl(nr::PR_CAP_AMBIENT, nr::PR_CAP_AMBIENT_CLEAR_ALL, 0, 0, 0) };
match ret {
0 => Ok(()),
_ => Err(format!("PR_CAP_AMBIENT_CLEAR_ALL failure, errno {}", errno::errno()).into()),
_ => Err(format!(
"PR_CAP_AMBIENT_CLEAR_ALL failure: {}",
Error::last_os_error()
)
.into()),
}
}

Expand All @@ -25,7 +30,7 @@ pub fn drop(cap: Capability) -> Result<(), CapsError> {
};
match ret {
0 => Ok(()),
_ => Err(format!("PR_CAP_AMBIENT_LOWER failure, errno {}", errno::errno()).into()),
_ => Err(format!("PR_CAP_AMBIENT_LOWER failure: {}", Error::last_os_error()).into()),
}
}

Expand All @@ -42,7 +47,7 @@ pub fn has_cap(cap: Capability) -> Result<bool, CapsError> {
match ret {
0 => Ok(false),
1 => Ok(true),
_ => Err(format!("PR_CAP_AMBIENT_IS_SET failure, errno {}", errno::errno()).into()),
_ => Err(format!("PR_CAP_AMBIENT_IS_SET failure: {}", Error::last_os_error()).into()),
}
}

Expand All @@ -58,7 +63,7 @@ pub fn raise(cap: Capability) -> Result<(), CapsError> {
};
match ret {
0 => Ok(()),
_ => Err(format!("PR_CAP_AMBIENT_RAISE failure, errno {}", errno::errno()).into()),
_ => Err(format!("PR_CAP_AMBIENT_RAISE failure: {}", Error::last_os_error()).into()),
}
}

Expand Down
5 changes: 3 additions & 2 deletions src/base.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::errors::CapsError;
use crate::nr;
use crate::{CapSet, Capability, CapsHashSet};
use std::io::Error;

#[allow(clippy::unreadable_literal)]
const CAPS_V3: u32 = 0x20080522;
Expand All @@ -9,15 +10,15 @@ fn capget(hdr: &mut CapUserHeader, data: &mut CapUserData) -> Result<(), CapsErr
let r = unsafe { libc::syscall(nr::CAPGET, hdr, data) };
match r {
0 => Ok(()),
_ => Err(format!("capget failure, errno {}", errno::errno()).into()),
_ => Err(format!("capget failure: {}", Error::last_os_error()).into()),
}
}

fn capset(hdr: &mut CapUserHeader, data: &CapUserData) -> Result<(), CapsError> {
let r = unsafe { libc::syscall(nr::CAPSET, hdr, data) };
match r {
0 => Ok(()),
_ => Err(format!("capset failure, errno {}", errno::errno()).into()),
_ => Err(format!("capset failure: {}", Error::last_os_error()).into()),
}
}

Expand Down
9 changes: 5 additions & 4 deletions src/bounding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::errors::CapsError;
use crate::nr;
use crate::runtime;
use crate::Capability;
use std::io::Error;

pub fn clear() -> Result<(), CapsError> {
for c in super::all() {
Expand All @@ -17,8 +18,8 @@ pub fn drop(cap: Capability) -> Result<(), CapsError> {
match ret {
0 => Ok(()),
_ => Err(CapsError::from(format!(
"PR_CAPBSET_DROP failure, errno {}",
errno::errno()
"PR_CAPBSET_DROP failure: {}",
Error::last_os_error()
))),
}
}
Expand All @@ -29,8 +30,8 @@ pub fn has_cap(cap: Capability) -> Result<bool, CapsError> {
0 => Ok(false),
1 => Ok(true),
_ => Err(CapsError::from(format!(
"PR_CAPBSET_READ failure, errno {}",
errno::errno()
"PR_CAPBSET_READ failure: {}",
Error::last_os_error()
))),
}
}
Expand Down
9 changes: 5 additions & 4 deletions src/securebits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use crate::errors::CapsError;
use crate::nr;
use std::io::Error;

/// Return whether the current thread's "keep capabilities" flag is set.
pub fn has_keepcaps() -> Result<bool, CapsError> {
Expand All @@ -14,8 +15,8 @@ pub fn has_keepcaps() -> Result<bool, CapsError> {
0 => Ok(false),
1 => Ok(true),
_ => Err(CapsError::from(format!(
"PR_GET_KEEPCAPS failure, errno {}",
errno::errno()
"PR_GET_KEEPCAPS failure: {}",
Error::last_os_error()
))),
}
}
Expand All @@ -27,8 +28,8 @@ pub fn set_keepcaps(keep_caps: bool) -> Result<(), CapsError> {
match ret {
0 => Ok(()),
_ => Err(CapsError::from(format!(
"PR_SET_KEEPCAPS failure, errno {}",
errno::errno()
"PR_SET_KEEPCAPS failure: {}",
Error::last_os_error()
))),
}
}

0 comments on commit 4a05ffc

Please sign in to comment.