Rust tests that pass because the command failed
Our "another user cannot write here" tests passed for the wrong reason. A positive control, a cleared group list and a hard fail on spawn errors fixed them.
Contents
We have a Rust function that decides whether a directory is safe to publish files into. One
thing it must refuse: a parent directory that another user can write to. To test it, the test
runs as root and then drops to a different user, uid 65534 (nobody), and tries to create,
rename and delete things in the directory. The assertion is that every one of those attempts
fails.
That is a negative test, and negative tests have a bad habit. They pass when the thing you are checking works, and they also pass when the attempt fails for any other reason. A review of our tests found three such reasons. This post shows how each one hid, and the four changes that closed them.
What the first version did
The helper ran a command as nobody and returned a boolean:
fn as_nobody(program: &str, args: &[&Path]) -> bool {
// returned false on spawn failure OR any non-zero exit
}
"False" meant "the attacker was stopped". But false was also what you got if sudo was not
found, if the program did not exist, or if the command died on something unrelated.
Three ways it passed while testing nothing
The temp directory was unreadable. On macOS the tests ran under sudo -E, which kept the
user's own TMPDIR. That directory is mode 0700. Uid 65534 could not even walk into it, so
every create, rename and delete "failed". The reviewer's note on this was blunt: the test would
pass against a broken implementation, because nothing the attacker tried could succeed
anywhere in that tree.
The child kept root's groups. Setting the uid and gid on a child process does not clear the supplementary group list. The process was uid 65534 on paper but still carried root's groups. That is the opposite problem: the attacker was stronger than the test claimed. A test that says "an unrelated user cannot write here" has to run as an unrelated user, and a process that still holds root's groups is not one. Whatever the test proved, it did not prove that.
The negative cases passed for an accidental reason. In a few tests, a cd into a staging
directory failed because the staging directory was mode 0700 by design, not because of the
control the test was written to exercise. The header comment claimed a positive control for
every command. That claim was overstated.
The four changes
Drop privileges with the OS tool, and clear the groups. On Linux the child is started with
setpriv --reuid=65534 --regid=65534 --clear-groups. On macOS it issudo -n -u '#65534' -g '#65534'.fn nobody_cmd(program: &str, args: &[&Path]) -> std::process::Command { let mut c; if cfg!(target_os = "linux") { c = std::process::Command::new("setpriv"); c.args(["--reuid=65534", "--regid=65534", "--clear-groups"]); } else { c = std::process::Command::new("sudo"); c.args(["-n", "-u", "#65534", "-g", "#65534"]); } c.arg(program).args(args); c }A spawn failure panics. The helper now calls
.expect("spawn attacker child"). A tool that is missing is an error in the test setup. It must never count as "the attacker failed".A positive control before every negative. A function runs first and checks two things. The child is exactly uid 65534, gid 65534, with no other group (it runs
id -u; id -g; id -Gas the attacker and asserts the output). And in a directory made deliberately open, mode 0777, the same create, rename and delete commands succeed. Only after that proof do the negative assertions mean anything.Skipping is a failure when you ask for it. These tests need real root, so they are marked
#[ignore]and a normalcargo testleaves them out. A normal run showed "7 ignored", which looks fine and proves nothing about the foreign-user behaviour. We added an environment variable,PP3D_REQUIRE_FOREIGN=1. With it set, a skipped foreign-user test fails the run. The release step also requires a count: it expects 4 passed, and anything else is red.
The temp directory moved to /tmp for these tests, so uid 65534 can reach it.
How to use the idea in your own tests
A negative test is only as good as the proof that the positive case works in the same setup. Before you assert "user X cannot do Y", assert "user X can do Y in a place where it should be allowed", using the same command and the same environment. If the control fails, the test setup is broken and the real assertion is meaningless.
Three smaller rules came with it:
- Make the "could not run the command" path loud.
unwrap_or(false)and.is_ok()turn setup errors into passes. - Assert the identity you think you have. Print and check the uid, gid and group list, do not assume them.
- Count the tests that ran. A suite that skips everything reports green.
Why root at all
It is fair to ask why a test needs root. Dropping to another uid is a privileged operation, so
the test process must start as root and then hand the attacker command a different identity.
That is also why a normal developer run cannot exercise these tests: they are marked
#[ignore] and wait for a deliberate run as root. The danger of that setup is that the test
that is hardest to run is also the one that is easiest to skip and forget. Everything above
exists to make "I did not run it" and "it ran and proved nothing" both look like failures.
Checking your own tests for this
Take any test of the form "X must not be allowed". Break the code under test on purpose, so that X is allowed, and see if the test goes red. If it stays green, the test cannot tell the difference between a working guard and no guard. We did not do this for every test, but the reviewer's finding came down to the same question: would this pass against a broken implementation? For the 0700 temp directory case, the answer was yes.
Result
After the changes we ran the full workspace on a Linux VM as real root, with
PP3D_REQUIRE_FOREIGN=1: 378 passed, 0 failed, 7 ignored, and the foreign-user tests were
among those that ran. The 7 ignored are the root-only ones in a normal, non-root run.
The tests did not find a bug in the product code. They found that our proof of the product code was weaker than it looked, which is a cheaper place to find it.