Skip to content

Commit adcfaf5

Browse files
committed
Require CAP_SYS_PTRACE for certain tests
process_vm_readv requires it, and I'm not exactly sure which other things do too.
1 parent 1662466 commit adcfaf5

File tree

4 files changed

+30
-11
lines changed

4 files changed

+30
-11
lines changed

test/sys/test_ptrace.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use std::mem;
1212
fn test_ptrace() {
1313
// Just make sure ptrace can be called at all, for now.
1414
// FIXME: qemu-user doesn't implement ptrace on all arches, so permit ENOSYS
15+
require_capability!(CAP_SYS_PTRACE);
1516
let err = ptrace::attach(getpid()).unwrap_err();
1617
assert!(err == Error::Sys(Errno::EPERM) || err == Error::Sys(Errno::EINVAL) ||
1718
err == Error::Sys(Errno::ENOSYS));
@@ -21,6 +22,7 @@ fn test_ptrace() {
2122
#[test]
2223
#[cfg(any(target_os = "android", target_os = "linux"))]
2324
fn test_ptrace_setoptions() {
25+
require_capability!(CAP_SYS_PTRACE);
2426
let err = ptrace::setoptions(getpid(), Options::PTRACE_O_TRACESYSGOOD).unwrap_err();
2527
assert!(err != Error::UnsupportedOperation);
2628
}
@@ -29,6 +31,7 @@ fn test_ptrace_setoptions() {
2931
#[test]
3032
#[cfg(any(target_os = "android", target_os = "linux"))]
3133
fn test_ptrace_getevent() {
34+
require_capability!(CAP_SYS_PTRACE);
3235
let err = ptrace::getevent(getpid()).unwrap_err();
3336
assert!(err != Error::UnsupportedOperation);
3437
}
@@ -37,6 +40,7 @@ fn test_ptrace_getevent() {
3740
#[test]
3841
#[cfg(any(target_os = "android", target_os = "linux"))]
3942
fn test_ptrace_getsiginfo() {
43+
require_capability!(CAP_SYS_PTRACE);
4044
if let Err(Error::UnsupportedOperation) = ptrace::getsiginfo(getpid()) {
4145
panic!("ptrace_getsiginfo returns Error::UnsupportedOperation!");
4246
}
@@ -46,6 +50,7 @@ fn test_ptrace_getsiginfo() {
4650
#[test]
4751
#[cfg(any(target_os = "android", target_os = "linux"))]
4852
fn test_ptrace_setsiginfo() {
53+
require_capability!(CAP_SYS_PTRACE);
4954
let siginfo = unsafe { mem::zeroed() };
5055
if let Err(Error::UnsupportedOperation) = ptrace::setsiginfo(getpid(), &siginfo) {
5156
panic!("ptrace_setsiginfo returns Error::UnsupportedOperation!");
@@ -61,6 +66,8 @@ fn test_ptrace_cont() {
6166
use nix::unistd::fork;
6267
use nix::unistd::ForkResult::*;
6368

69+
require_capability!(CAP_SYS_PTRACE);
70+
6471
let _m = ::FORK_MTX.lock().expect("Mutex got poisoned by another test");
6572

6673
// FIXME: qemu-user doesn't implement ptrace on all architectures

test/sys/test_uio.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ fn test_process_vm_readv() {
200200
use nix::sys::signal::*;
201201
use nix::sys::wait::*;
202202

203+
require_capability!(CAP_SYS_PTRACE);
203204
let _ = ::FORK_MTX.lock().expect("Mutex got poisoned by another test");
204205

205206
// Pre-allocate memory in the child, since allocation isn't safe

test/sys/test_wait.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ mod ptrace {
9494

9595
#[test]
9696
fn test_wait_ptrace() {
97+
require_capability!(CAP_SYS_PTRACE);
9798
let _m = ::FORK_MTX.lock().expect("Mutex got poisoned by another test");
9899

99100
match fork().expect("Error: Fork Failed") {

test/test.rs

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,28 @@ extern crate rand;
1515
extern crate sysctl;
1616
extern crate tempfile;
1717

18-
#[cfg(any(target_os = "android", target_os = "linux"))]
19-
macro_rules! require_capability {
20-
($capname:ident) => {
21-
use ::caps::{Capability, CapSet, has_cap};
22-
use ::std::io::{self, Write};
18+
cfg_if! {
19+
if #[cfg(any(target_os = "android", target_os = "linux"))] {
20+
macro_rules! require_capability {
21+
($capname:ident) => {
22+
use ::caps::{Capability, CapSet, has_cap};
23+
use ::std::io::{self, Write};
2324

24-
if !has_cap(None, CapSet::Effective, Capability::$capname).unwrap() {
25-
let stderr = io::stderr();
26-
let mut handle = stderr.lock();
27-
writeln!(handle, "Insufficient capabilities. Skipping test.")
28-
.unwrap();
29-
return;
25+
if !has_cap(None, CapSet::Effective, Capability::$capname)
26+
.unwrap()
27+
{
28+
let stderr = io::stderr();
29+
let mut handle = stderr.lock();
30+
writeln!(handle,
31+
"Insufficient capabilities. Skipping test.")
32+
.unwrap();
33+
return;
34+
}
35+
}
36+
}
37+
} else {
38+
macro_rules! require_capability {
39+
($capname:ident) => {}
3040
}
3141
}
3242
}

0 commit comments

Comments
 (0)