audit(4): add tests for chown(2) and friends
Includes chown, fchown, lchown, and fchownat Submitted by: aniketp MFC after: 2 weeks Sponsored by: Google, Inc. (GSoC 2018) Differential Revision: https://reviews.freebsd.org/D15825
This commit is contained in:
parent
babaf5cb1c
commit
5c9a473888
Notes:
svn2git
2020-12-20 02:59:44 +00:00
svn path=/head/; revision=335256
@ -35,6 +35,8 @@
|
||||
#include "utils.h"
|
||||
|
||||
static pid_t pid;
|
||||
static uid_t uid = -1;
|
||||
static gid_t gid = -1;
|
||||
static int filedesc;
|
||||
static struct pollfd fds[1];
|
||||
static mode_t mode = 0777;
|
||||
@ -369,6 +371,185 @@ ATF_TC_CLEANUP(fchmodat_failure, tc)
|
||||
}
|
||||
|
||||
|
||||
ATF_TC_WITH_CLEANUP(chown_success);
|
||||
ATF_TC_HEAD(chown_success, tc)
|
||||
{
|
||||
atf_tc_set_md_var(tc, "descr", "Tests the audit of a successful "
|
||||
"chown(2) call");
|
||||
}
|
||||
|
||||
ATF_TC_BODY(chown_success, tc)
|
||||
{
|
||||
/* File needs to exist to call chown(2) */
|
||||
ATF_REQUIRE((filedesc = open(path, O_CREAT, mode)) != -1);
|
||||
FILE *pipefd = setup(fds, auclass);
|
||||
ATF_REQUIRE_EQ(0, chown(path, uid, gid));
|
||||
check_audit(fds, successreg, pipefd);
|
||||
close(filedesc);
|
||||
}
|
||||
|
||||
ATF_TC_CLEANUP(chown_success, tc)
|
||||
{
|
||||
cleanup();
|
||||
}
|
||||
|
||||
|
||||
ATF_TC_WITH_CLEANUP(chown_failure);
|
||||
ATF_TC_HEAD(chown_failure, tc)
|
||||
{
|
||||
atf_tc_set_md_var(tc, "descr", "Tests the audit of an unsuccessful "
|
||||
"chown(2) call");
|
||||
}
|
||||
|
||||
ATF_TC_BODY(chown_failure, tc)
|
||||
{
|
||||
FILE *pipefd = setup(fds, auclass);
|
||||
/* Failure reason: file does not exist */
|
||||
ATF_REQUIRE_EQ(-1, chown(errpath, uid, gid));
|
||||
check_audit(fds, failurereg, pipefd);
|
||||
}
|
||||
|
||||
ATF_TC_CLEANUP(chown_failure, tc)
|
||||
{
|
||||
cleanup();
|
||||
}
|
||||
|
||||
|
||||
ATF_TC_WITH_CLEANUP(fchown_success);
|
||||
ATF_TC_HEAD(fchown_success, tc)
|
||||
{
|
||||
atf_tc_set_md_var(tc, "descr", "Tests the audit of a successful "
|
||||
"fchown(2) call");
|
||||
}
|
||||
|
||||
ATF_TC_BODY(fchown_success, tc)
|
||||
{
|
||||
pid = getpid();
|
||||
snprintf(extregex, sizeof(extregex), "fchown.*%d.*return,success", pid);
|
||||
|
||||
/* File needs to exist to call fchown(2) */
|
||||
ATF_REQUIRE((filedesc = open(path, O_CREAT, mode)) != -1);
|
||||
FILE *pipefd = setup(fds, auclass);
|
||||
ATF_REQUIRE_EQ(0, fchown(filedesc, uid, gid));
|
||||
check_audit(fds, extregex, pipefd);
|
||||
close(filedesc);
|
||||
}
|
||||
|
||||
ATF_TC_CLEANUP(fchown_success, tc)
|
||||
{
|
||||
cleanup();
|
||||
}
|
||||
|
||||
|
||||
ATF_TC_WITH_CLEANUP(fchown_failure);
|
||||
ATF_TC_HEAD(fchown_failure, tc)
|
||||
{
|
||||
atf_tc_set_md_var(tc, "descr", "Tests the audit of an unsuccessful "
|
||||
"fchown(2) call");
|
||||
}
|
||||
|
||||
ATF_TC_BODY(fchown_failure, tc)
|
||||
{
|
||||
const char *regex = "fchown.*return,failure : Bad file descriptor";
|
||||
FILE *pipefd = setup(fds, auclass);
|
||||
/* Failure reason: Invalid file descriptor */
|
||||
ATF_REQUIRE_EQ(-1, fchown(-1, uid, gid));
|
||||
check_audit(fds, regex, pipefd);
|
||||
}
|
||||
|
||||
ATF_TC_CLEANUP(fchown_failure, tc)
|
||||
{
|
||||
cleanup();
|
||||
}
|
||||
|
||||
|
||||
ATF_TC_WITH_CLEANUP(lchown_success);
|
||||
ATF_TC_HEAD(lchown_success, tc)
|
||||
{
|
||||
atf_tc_set_md_var(tc, "descr", "Tests the audit of a successful "
|
||||
"lchown(2) call");
|
||||
}
|
||||
|
||||
ATF_TC_BODY(lchown_success, tc)
|
||||
{
|
||||
/* Symbolic link needs to exist to call lchown(2) */
|
||||
ATF_REQUIRE_EQ(0, symlink("symlink", path));
|
||||
FILE *pipefd = setup(fds, auclass);
|
||||
ATF_REQUIRE_EQ(0, lchown(path, uid, gid));
|
||||
check_audit(fds, successreg, pipefd);
|
||||
}
|
||||
|
||||
ATF_TC_CLEANUP(lchown_success, tc)
|
||||
{
|
||||
cleanup();
|
||||
}
|
||||
|
||||
|
||||
ATF_TC_WITH_CLEANUP(lchown_failure);
|
||||
ATF_TC_HEAD(lchown_failure, tc)
|
||||
{
|
||||
atf_tc_set_md_var(tc, "descr", "Tests the audit of an unsuccessful "
|
||||
"lchown(2) call");
|
||||
}
|
||||
|
||||
ATF_TC_BODY(lchown_failure, tc)
|
||||
{
|
||||
FILE *pipefd = setup(fds, auclass);
|
||||
/* Failure reason: Symbolic link does not exist */
|
||||
ATF_REQUIRE_EQ(-1, lchown(errpath, uid, gid));
|
||||
check_audit(fds, failurereg, pipefd);
|
||||
}
|
||||
|
||||
ATF_TC_CLEANUP(lchown_failure, tc)
|
||||
{
|
||||
cleanup();
|
||||
}
|
||||
|
||||
|
||||
ATF_TC_WITH_CLEANUP(fchownat_success);
|
||||
ATF_TC_HEAD(fchownat_success, tc)
|
||||
{
|
||||
atf_tc_set_md_var(tc, "descr", "Tests the audit of a successful "
|
||||
"fchownat(2) call");
|
||||
}
|
||||
|
||||
ATF_TC_BODY(fchownat_success, tc)
|
||||
{
|
||||
/* File needs to exist to call fchownat(2) */
|
||||
ATF_REQUIRE((filedesc = open(path, O_CREAT, mode)) != -1);
|
||||
FILE *pipefd = setup(fds, auclass);
|
||||
ATF_REQUIRE_EQ(0, fchownat(AT_FDCWD, path, uid, gid, 0));
|
||||
check_audit(fds, successreg, pipefd);
|
||||
close(filedesc);
|
||||
}
|
||||
|
||||
ATF_TC_CLEANUP(fchownat_success, tc)
|
||||
{
|
||||
cleanup();
|
||||
}
|
||||
|
||||
|
||||
ATF_TC_WITH_CLEANUP(fchownat_failure);
|
||||
ATF_TC_HEAD(fchownat_failure, tc)
|
||||
{
|
||||
atf_tc_set_md_var(tc, "descr", "Tests the audit of an unsuccessful "
|
||||
"fchownat(2) call");
|
||||
}
|
||||
|
||||
ATF_TC_BODY(fchownat_failure, tc)
|
||||
{
|
||||
FILE *pipefd = setup(fds, auclass);
|
||||
/* Failure reason: file does not exist */
|
||||
ATF_REQUIRE_EQ(-1, fchownat(AT_FDCWD, errpath, uid, gid, 0));
|
||||
check_audit(fds, failurereg, pipefd);
|
||||
}
|
||||
|
||||
ATF_TC_CLEANUP(fchownat_failure, tc)
|
||||
{
|
||||
cleanup();
|
||||
}
|
||||
|
||||
|
||||
ATF_TP_ADD_TCS(tp)
|
||||
{
|
||||
ATF_TP_ADD_TC(tp, flock_success);
|
||||
@ -387,5 +568,14 @@ ATF_TP_ADD_TCS(tp)
|
||||
ATF_TP_ADD_TC(tp, fchmodat_success);
|
||||
ATF_TP_ADD_TC(tp, fchmodat_failure);
|
||||
|
||||
ATF_TP_ADD_TC(tp, chown_success);
|
||||
ATF_TP_ADD_TC(tp, chown_failure);
|
||||
ATF_TP_ADD_TC(tp, fchown_success);
|
||||
ATF_TP_ADD_TC(tp, fchown_failure);
|
||||
ATF_TP_ADD_TC(tp, lchown_success);
|
||||
ATF_TP_ADD_TC(tp, lchown_failure);
|
||||
ATF_TP_ADD_TC(tp, fchownat_success);
|
||||
ATF_TP_ADD_TC(tp, fchownat_failure);
|
||||
|
||||
return (atf_no_error());
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user