Skip to content

Commit

Permalink
guard more tests with CAP_SYS_PTRACE
Browse files Browse the repository at this point in the history
  • Loading branch information
asomers committed Sep 19, 2019
1 parent 7754816 commit 67ed663
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 11 deletions.
7 changes: 7 additions & 0 deletions test/sys/test_ptrace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use std::mem;
fn test_ptrace() {
// Just make sure ptrace can be called at all, for now.
// FIXME: qemu-user doesn't implement ptrace on all arches, so permit ENOSYS
require_capability!(CAP_SYS_PTRACE);
let err = ptrace::attach(getpid()).unwrap_err();
assert!(err == Error::Sys(Errno::EPERM) || err == Error::Sys(Errno::EINVAL) ||
err == Error::Sys(Errno::ENOSYS));
Expand All @@ -21,6 +22,7 @@ fn test_ptrace() {
#[test]
#[cfg(any(target_os = "android", target_os = "linux"))]
fn test_ptrace_setoptions() {
require_capability!(CAP_SYS_PTRACE);
let err = ptrace::setoptions(getpid(), Options::PTRACE_O_TRACESYSGOOD).unwrap_err();
assert!(err != Error::UnsupportedOperation);
}
Expand All @@ -29,6 +31,7 @@ fn test_ptrace_setoptions() {
#[test]
#[cfg(any(target_os = "android", target_os = "linux"))]
fn test_ptrace_getevent() {
require_capability!(CAP_SYS_PTRACE);
let err = ptrace::getevent(getpid()).unwrap_err();
assert!(err != Error::UnsupportedOperation);
}
Expand All @@ -37,6 +40,7 @@ fn test_ptrace_getevent() {
#[test]
#[cfg(any(target_os = "android", target_os = "linux"))]
fn test_ptrace_getsiginfo() {
require_capability!(CAP_SYS_PTRACE);
if let Err(Error::UnsupportedOperation) = ptrace::getsiginfo(getpid()) {
panic!("ptrace_getsiginfo returns Error::UnsupportedOperation!");
}
Expand All @@ -46,6 +50,7 @@ fn test_ptrace_getsiginfo() {
#[test]
#[cfg(any(target_os = "android", target_os = "linux"))]
fn test_ptrace_setsiginfo() {
require_capability!(CAP_SYS_PTRACE);
let siginfo = unsafe { mem::zeroed() };
if let Err(Error::UnsupportedOperation) = ptrace::setsiginfo(getpid(), &siginfo) {
panic!("ptrace_setsiginfo returns Error::UnsupportedOperation!");
Expand All @@ -61,6 +66,8 @@ fn test_ptrace_cont() {
use nix::unistd::fork;
use nix::unistd::ForkResult::*;

require_capability!(CAP_SYS_PTRACE);

let _m = ::FORK_MTX.lock().expect("Mutex got poisoned by another test");

// FIXME: qemu-user doesn't implement ptrace on all architectures
Expand Down
32 changes: 21 additions & 11 deletions test/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,28 @@ extern crate rand;
extern crate sysctl;
extern crate tempfile;

#[cfg(any(target_os = "android", target_os = "linux"))]
macro_rules! require_capability {
($capname:ident) => {
use ::caps::{Capability, CapSet, has_cap};
use ::std::io::{self, Write};
cfg_if! {
if #[cfg(any(target_os = "android", target_os = "linux"))] {
macro_rules! require_capability {
($capname:ident) => {
use ::caps::{Capability, CapSet, has_cap};
use ::std::io::{self, Write};

if !has_cap(None, CapSet::Effective, Capability::$capname).unwrap() {
let stderr = io::stderr();
let mut handle = stderr.lock();
writeln!(handle, "Insufficient capabilities. Skipping test.")
.unwrap();
return;
if !has_cap(None, CapSet::Effective, Capability::$capname)
.unwrap()
{
let stderr = io::stderr();
let mut handle = stderr.lock();
writeln!(handle,
"Insufficient capabilities. Skipping test.")
.unwrap();
return;
}
}
}
} else {
macro_rules! require_capability {
($capname:ident) => {}
}
}
}
Expand Down

0 comments on commit 67ed663

Please sign in to comment.