Revert r310994

Don't implement some terrible hack on a test by test basis.  The
framework fix is straightforward and can be chased up in the original
bug.

Reviewed by:	ngie ("be my guest")
This commit is contained in:
Conrad Meyer 2017-01-14 01:03:20 +00:00
parent a5deb9babb
commit 47f2efe4fb
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=312103

View file

@ -31,27 +31,23 @@ __FBSDID("$FreeBSD$");
#include <sys/capsicum.h>
#include <sys/sysctl.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <atf-c.h>
#include <assert.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include "freebsd_test_suite/macros.h"
static char *abspath;
static int dirfd = -1;
typedef void (*child_test_fn_t)(void);
static int dirfd = -1;
static char *abspath;
static void
touchat(int _dirfd, const char *name)
touchat(int dirfd, const char *name)
{
int fd;
ATF_REQUIRE((fd = openat(_dirfd, name, O_CREAT | O_TRUNC | O_WRONLY,
ATF_REQUIRE((fd = openat(dirfd, name, O_CREAT | O_TRUNC | O_WRONLY,
0777)) >= 0);
ATF_REQUIRE(close(fd) == 0);
}
@ -82,43 +78,10 @@ prepare_dotdot_tests(void)
static void
check_capsicum(void)
{
ATF_REQUIRE_FEATURE("security_capabilities");
ATF_REQUIRE_FEATURE("security_capability_mode");
}
static void
run_capsicum_test(child_test_fn_t test_func)
{
int child_exit_code, child_status;
pid_t child_pid;
check_capsicum();
prepare_dotdot_tests();
ATF_REQUIRE_MSG((child_pid = fork()) != -1,
"fork failed: %s", strerror(errno));
if (child_pid == 0) {
test_func();
_exit(0);
}
ATF_REQUIRE_MSG(waitpid(child_pid, &child_status, 0) != -1,
"waitpid failed: %s", strerror(errno));
if (WIFEXITED(child_status)) {
child_exit_code = WEXITSTATUS(child_status);
ATF_REQUIRE_MSG(child_exit_code == 0,
"child exited with non-zero exit code: %d",
child_exit_code);
} else if (WIFSIGNALED(child_status))
atf_tc_fail("child exited with signal: %d",
WTERMSIG(child_status));
else
atf_tc_fail("child exited with unexpected status: %d",
child_status);
}
/*
* Positive tests
*/
@ -130,7 +93,6 @@ ATF_TC_HEAD(openat__basic_positive, tc)
ATF_TC_BODY(openat__basic_positive, tc)
{
prepare_dotdot_tests();
ATF_REQUIRE(openat(dirfd, "d1/d2/d3/f3", O_RDONLY) >= 0);
@ -152,22 +114,21 @@ ATF_TC_HEAD(lookup_cap_dotdot__basic, tc)
"Validate cap-mode (testdir)/d1/.. lookup");
}
static void
lookup_cap_dotdot__basic_child(void)
{
cap_rights_t rights;
cap_rights_init(&rights, CAP_LOOKUP, CAP_READ);
assert(cap_rights_limit(dirfd, &rights) >= 0);
assert(cap_enter() >= 0);
assert(openat(dirfd, "d1/..", O_RDONLY) >= 0);
}
ATF_TC_BODY(lookup_cap_dotdot__basic, tc)
{
cap_rights_t rights;
int fd;
run_capsicum_test(lookup_cap_dotdot__basic_child);
check_capsicum();
prepare_dotdot_tests();
cap_rights_init(&rights, CAP_LOOKUP, CAP_READ);
ATF_REQUIRE(cap_rights_limit(dirfd, &rights) >= 0);
ATF_REQUIRE(cap_enter() >= 0);
ATF_REQUIRE_MSG(openat(dirfd, "d1/..", O_RDONLY) >= 0, "%s",
strerror(errno));
}
ATF_TC(lookup_cap_dotdot__advanced);
@ -177,26 +138,23 @@ ATF_TC_HEAD(lookup_cap_dotdot__advanced, tc)
"Validate cap-mode (testdir)/d1/.. lookup");
}
static void
lookup_cap_dotdot__advanced_child(void)
{
cap_rights_t rights;
cap_rights_init(&rights, CAP_LOOKUP, CAP_READ);
assert(cap_rights_limit(dirfd, &rights) >= 0);
assert(cap_enter() >= 0);
assert(openat(dirfd, "d1/d2/d3/../../f1", O_RDONLY) >= 0);
assert(openat(dirfd, "l3/../../f1", O_RDONLY) >= 0);
assert(openat(dirfd, "l3/ld1", O_RDONLY) >= 0);
assert(openat(dirfd, "l3/lf1", O_RDONLY) >= 0);
}
ATF_TC_BODY(lookup_cap_dotdot__advanced, tc)
{
cap_rights_t rights;
int fd;
run_capsicum_test(lookup_cap_dotdot__advanced_child);
check_capsicum();
prepare_dotdot_tests();
cap_rights_init(&rights, CAP_LOOKUP, CAP_READ);
ATF_REQUIRE(cap_rights_limit(dirfd, &rights) >= 0);
ATF_REQUIRE(cap_enter() >= 0);
ATF_REQUIRE(openat(dirfd, "d1/d2/d3/../../f1", O_RDONLY) >= 0);
ATF_REQUIRE(openat(dirfd, "l3/../../f1", O_RDONLY) >= 0);
ATF_REQUIRE(openat(dirfd, "l3/ld1", O_RDONLY) >= 0);
ATF_REQUIRE(openat(dirfd, "l3/lf1", O_RDONLY) >= 0);
}
/*
@ -210,7 +168,6 @@ ATF_TC_HEAD(openat__basic_negative, tc)
ATF_TC_BODY(openat__basic_negative, tc)
{
prepare_dotdot_tests();
ATF_REQUIRE_ERRNO(ENOENT,
@ -225,43 +182,32 @@ ATF_TC_HEAD(capmode__negative, tc)
atf_tc_set_md_var(tc, "descr", "Negative Capability mode testcases");
}
static void
capmode__negative_child(void)
ATF_TC_BODY(capmode__negative, tc)
{
int subdirfd;
assert(cap_enter() == 0);
check_capsicum();
prepare_dotdot_tests();
ATF_REQUIRE(cap_enter() == 0);
/* open() not permitted in capability mode */
assert(open("testdir", O_RDONLY) < 0);
assert(errno == ECAPMODE);
ATF_REQUIRE_ERRNO(ECAPMODE, open("testdir", O_RDONLY) < 0);
/* AT_FDCWD not permitted in capability mode */
assert(openat(AT_FDCWD, "d1/f1", O_RDONLY) < 0);
assert(errno == ECAPMODE);
ATF_REQUIRE_ERRNO(ECAPMODE, openat(AT_FDCWD, "d1/f1", O_RDONLY) < 0);
/* Relative path above dirfd not capable */
assert(openat(dirfd, "..", O_RDONLY) < 0);
assert(errno == ENOTCAPABLE);
assert((subdirfd = openat(dirfd, "l3", O_RDONLY)) >= 0);
assert(openat(subdirfd, "../../f1", O_RDONLY) < 0);
assert(errno == ENOTCAPABLE);
(void)close(subdirfd);
ATF_REQUIRE_ERRNO(ENOTCAPABLE, openat(dirfd, "..", O_RDONLY) < 0);
ATF_REQUIRE((subdirfd = openat(dirfd, "l3", O_RDONLY)) >= 0);
ATF_REQUIRE_ERRNO(ENOTCAPABLE,
openat(subdirfd, "../../f1", O_RDONLY) < 0);
/* Absolute paths not capable */
assert(openat(dirfd, abspath, O_RDONLY) < 0);
assert(errno == ENOTCAPABLE);
ATF_REQUIRE_ERRNO(ENOTCAPABLE, openat(dirfd, abspath, O_RDONLY) < 0);
/* Symlink above dirfd */
assert(openat(dirfd, "lup/f1", O_RDONLY) < 0);
assert(errno == ENOTCAPABLE);
}
ATF_TC_BODY(capmode__negative, tc)
{
run_capsicum_test(capmode__negative_child);
ATF_REQUIRE_ERRNO(ENOTCAPABLE, openat(dirfd, "lup/f1", O_RDONLY) < 0);
}
ATF_TC(lookup_cap_dotdot__negative);
@ -271,30 +217,22 @@ ATF_TC_HEAD(lookup_cap_dotdot__negative, tc)
"Validate cap-mode (testdir)/.. lookup fails");
}
static void
lookup_cap_dotdot__negative_child(void)
{
cap_rights_t rights;
cap_rights_init(&rights, CAP_LOOKUP, CAP_READ);
assert(cap_rights_limit(dirfd, &rights) >= 0);
assert(cap_enter() >= 0);
assert(openat(dirfd, "..", O_RDONLY) < 0);
assert(errno == ENOTCAPABLE);
assert(openat(dirfd, "d1/../..", O_RDONLY) < 0);
assert(errno == ENOTCAPABLE);
assert(openat(dirfd, "../testdir/d1/f1", O_RDONLY) < 0);
assert(errno == ENOTCAPABLE);
}
ATF_TC_BODY(lookup_cap_dotdot__negative, tc)
{
cap_rights_t rights;
int fd;
run_capsicum_test(lookup_cap_dotdot__negative_child);
check_capsicum();
prepare_dotdot_tests();
cap_rights_init(&rights, CAP_LOOKUP, CAP_READ);
ATF_REQUIRE(cap_rights_limit(dirfd, &rights) >= 0);
ATF_REQUIRE(cap_enter() >= 0);
ATF_REQUIRE_ERRNO(ENOTCAPABLE, openat(dirfd, "..", O_RDONLY) < 0);
ATF_REQUIRE_ERRNO(ENOTCAPABLE, openat(dirfd, "d1/../..", O_RDONLY) < 0);
ATF_REQUIRE_ERRNO(ENOTCAPABLE, openat(dirfd, "../testdir/d1/f1", O_RDONLY) < 0);
}
ATF_TP_ADD_TCS(tp)