Skip to content

Commit e201dfd

Browse files
bors[bot]asomers
andauthored
Merge #1123
1123: Fix travis r=asomers a=asomers Co-authored-by: Alan Somers <[email protected]>
2 parents 1662466 + 7e2b402 commit e201dfd

File tree

6 files changed

+84
-19
lines changed

6 files changed

+84
-19
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_socket.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,10 @@ pub fn test_af_alg_cipher() {
244244
ControlMessage, MsgFlags};
245245
use nix::sys::socket::sockopt::AlgSetKey;
246246

247+
// Travis's seccomp profile blocks AF_ALG
248+
// https://docs.docker.com/engine/security/seccomp/
249+
skip_if_seccomp!(test_af_alg_cipher);
250+
247251
let alg_type = "skcipher";
248252
let alg_name = "ctr(aes)";
249253
// 256-bits secret key
@@ -308,6 +312,10 @@ pub fn test_af_alg_aead() {
308312
ControlMessage, MsgFlags};
309313
use nix::sys::socket::sockopt::{AlgSetKey, AlgSetAeadAuthSize};
310314

315+
// Travis's seccomp profile blocks AF_ALG
316+
// https://docs.docker.com/engine/security/seccomp/
317+
skip_if_seccomp!(test_af_alg_aead);
318+
311319
let auth_size = 4usize;
312320
let assoc_size = 16u32;
313321

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: 50 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
}
@@ -63,6 +73,35 @@ macro_rules! skip_if_not_root {
6373
};
6474
}
6575

76+
cfg_if! {
77+
if #[cfg(any(target_os = "android", target_os = "linux"))] {
78+
macro_rules! skip_if_seccomp {
79+
($name:expr) => {
80+
if let Ok(s) = std::fs::read_to_string("/proc/self/status") {
81+
for l in s.lines() {
82+
let mut fields = l.split_whitespace();
83+
if fields.next() == Some("Seccomp:") &&
84+
fields.next() != Some("0")
85+
{
86+
use ::std::io::Write;
87+
let stderr = ::std::io::stderr();
88+
let mut handle = stderr.lock();
89+
writeln!(handle,
90+
"{} cannot be run in Seccomp mode. Skipping test.",
91+
stringify!($name)).unwrap();
92+
return;
93+
}
94+
}
95+
}
96+
}
97+
}
98+
} else {
99+
macro_rules! skip_if_seccomp {
100+
($name:expr) => {}
101+
}
102+
}
103+
}
104+
66105
mod sys;
67106
mod test_dir;
68107
mod test_fcntl;

test/test_unistd.rs

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,13 @@ macro_rules! execve_test_factory(
184184
($test_name:ident, $syscall:ident, $exe: expr $(, $pathname:expr, $flags:expr)*) => (
185185
#[test]
186186
fn $test_name() {
187-
let _m = ::FORK_MTX.lock().expect("Mutex got poisoned by another test");
187+
if "execveat" == stringify!($syscall) {
188+
// Though undocumented, Docker's default seccomp profile seems to
189+
// block this syscall. https://github.com/nix-rust/nix/issues/1122
190+
skip_if_seccomp!($test_name);
191+
}
192+
193+
let m = ::FORK_MTX.lock().expect("Mutex got poisoned by another test");
188194
// The `exec`d process will write to `writer`, and we'll read that
189195
// data from `reader`.
190196
let (reader, writer) = pipe().unwrap();
@@ -194,12 +200,9 @@ macro_rules! execve_test_factory(
194200
// The tests make sure not to do that, though.
195201
match fork().unwrap() {
196202
Child => {
197-
// Close stdout.
198-
close(1).unwrap();
199203
// Make `writer` be the stdout of the new process.
200-
dup(writer).unwrap();
201-
// exec!
202-
$syscall(
204+
dup2(writer, 1).unwrap();
205+
let r = $syscall(
203206
$exe,
204207
$(&CString::new($pathname).unwrap(), )*
205208
&[CString::new(b"".as_ref()).unwrap(),
@@ -208,11 +211,17 @@ macro_rules! execve_test_factory(
208211
.as_ref()).unwrap()],
209212
&[CString::new(b"foo=bar".as_ref()).unwrap(),
210213
CString::new(b"baz=quux".as_ref()).unwrap()]
211-
$(, $flags)*).unwrap();
214+
$(, $flags)*);
215+
let _ = std::io::stderr()
216+
.write_all(format!("{:?}", r).as_bytes());
217+
// Should only get here in event of error
218+
unsafe{ _exit(1) };
212219
},
213220
Parent { child } => {
214221
// Wait for the child to exit.
215-
waitpid(child, None).unwrap();
222+
let ws = waitpid(child, None);
223+
drop(m);
224+
assert_eq!(ws, Ok(WaitStatus::Exited(child, 0)));
216225
// Read 1024 bytes.
217226
let mut buf = [0u8; 1024];
218227
read(reader, &mut buf).unwrap();

0 commit comments

Comments
 (0)