Skip to content

Commit

Permalink
audit(4): Fix file descriptor leaks in ATF tests
Browse files Browse the repository at this point in the history
Submitted by:	aniketp
Reported by:	Coverity
CID:		1393343 1393346 1392695 1392781 1391709 1392078 1392413
CID:		1392014 1392521 1393344 1393345 1393347 1393348 1393349
CID:		1393354 1393355 1393356 1393357 1393358 1393360 1393362
CID:		1393368 1393369 1393370 1393371 1393372 1393373 1393376
CID:		1393380 1393384 1393387 1393388 1393389
MFC after:	2 weeks
Sponsored by:	Google, Inc (GSoC 2018)
Differential Revision:	https://reviews.freebsd.org/D15782
  • Loading branch information
asomers committed Jun 13, 2018
1 parent 28723d5 commit a18fc35
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 30 deletions.
6 changes: 4 additions & 2 deletions tests/sys/audit/file-attribute-access.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@

static struct pollfd fds[1];
static mode_t mode = 0777;
static int filedesc;
static char extregex[80];
static struct stat statbuff;
static const char *auclass = "fa";
Expand All @@ -55,10 +56,11 @@ ATF_TC_HEAD(stat_success, tc)
ATF_TC_BODY(stat_success, tc)
{
/* File needs to exist to call stat(2) */
ATF_REQUIRE(open(path, O_CREAT, mode) != -1);
ATF_REQUIRE((filedesc = open(path, O_CREAT, mode)) != -1);
FILE *pipefd = setup(fds, auclass);
ATF_REQUIRE_EQ(0, stat(path, &statbuff));
check_audit(fds, successreg, pipefd);
close(filedesc);
}

ATF_TC_CLEANUP(stat_success, tc)
Expand Down Expand Up @@ -140,7 +142,6 @@ ATF_TC_HEAD(fstat_success, tc)

ATF_TC_BODY(fstat_success, tc)
{
int filedesc;
/* File needs to exist to call fstat(2) */
ATF_REQUIRE((filedesc = open(path, O_CREAT | O_RDWR, mode)) != -1);
FILE *pipefd = setup(fds, auclass);
Expand All @@ -149,6 +150,7 @@ ATF_TC_BODY(fstat_success, tc)
snprintf(extregex, sizeof(extregex),
"fstat.*%jd.*return,success", (intmax_t)statbuff.st_ino);
check_audit(fds, extregex, pipefd);
close(filedesc);
}

ATF_TC_CLEANUP(fstat_success, tc)
Expand Down
7 changes: 2 additions & 5 deletions tests/sys/audit/file-close.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
static pid_t pid;
static struct pollfd fds[1];
static mode_t mode = 0777;
static int filedesc;
static char extregex[80];
static struct stat statbuff;
static const char *auclass = "cl";
Expand Down Expand Up @@ -103,7 +104,6 @@ ATF_TC_HEAD(close_success, tc)

ATF_TC_BODY(close_success, tc)
{
int filedesc;
/* File needs to exist to call close(2) */
ATF_REQUIRE((filedesc = open(path, O_CREAT | O_RDWR, mode)) != -1);
/* Call stat(2) to store the Inode number of 'path' */
Expand Down Expand Up @@ -176,7 +176,6 @@ ATF_TC_HEAD(revoke_success, tc)

ATF_TC_BODY(revoke_success, tc)
{
int filedesc;
char *ptyname;
pid = getpid();
snprintf(extregex, sizeof(extregex), "revoke.*%d.*return,success", pid);
Expand All @@ -188,9 +187,7 @@ ATF_TC_BODY(revoke_success, tc)
FILE *pipefd = setup(fds, auclass);
ATF_REQUIRE_EQ(0, revoke(ptyname));
check_audit(fds, extregex, pipefd);

/* Close the file descriptor to pseudo terminal */
ATF_REQUIRE_EQ(0, close(filedesc));
close(filedesc);
}

ATF_TC_CLEANUP(revoke_success, tc)
Expand Down
13 changes: 9 additions & 4 deletions tests/sys/audit/file-create.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@

static struct pollfd fds[1];
static mode_t mode = 0777;
static int filedesc;
static dev_t dev = 0;
static const char *auclass = "fc";
static const char *path = "fileforaudit";
Expand Down Expand Up @@ -305,10 +306,11 @@ ATF_TC_HEAD(rename_success, tc)

ATF_TC_BODY(rename_success, tc)
{
ATF_REQUIRE(open(path, O_CREAT, mode) != -1);
ATF_REQUIRE((filedesc = open(path, O_CREAT, mode)) != -1);
FILE *pipefd = setup(fds, auclass);
ATF_REQUIRE_EQ(0, rename(path, "renamed"));
check_audit(fds, successreg, pipefd);
close(filedesc);
}

ATF_TC_CLEANUP(rename_success, tc)
Expand Down Expand Up @@ -347,10 +349,11 @@ ATF_TC_HEAD(renameat_success, tc)

ATF_TC_BODY(renameat_success, tc)
{
ATF_REQUIRE(open(path, O_CREAT, mode) != -1);
ATF_REQUIRE((filedesc = open(path, O_CREAT, mode)) != -1);
FILE *pipefd = setup(fds, auclass);
ATF_REQUIRE_EQ(0, renameat(AT_FDCWD, path, AT_FDCWD, "renamed"));
check_audit(fds, successreg, pipefd);
close(filedesc);
}

ATF_TC_CLEANUP(renameat_success, tc)
Expand Down Expand Up @@ -389,10 +392,11 @@ ATF_TC_HEAD(link_success, tc)

ATF_TC_BODY(link_success, tc)
{
ATF_REQUIRE(open(path, O_CREAT, mode) != -1);
ATF_REQUIRE((filedesc = open(path, O_CREAT, mode)) != -1);
FILE *pipefd = setup(fds, auclass);
ATF_REQUIRE_EQ(0, link(path, "hardlink"));
check_audit(fds, successreg, pipefd);
close(filedesc);
}

ATF_TC_CLEANUP(link_success, tc)
Expand Down Expand Up @@ -431,10 +435,11 @@ ATF_TC_HEAD(linkat_success, tc)

ATF_TC_BODY(linkat_success, tc)
{
ATF_REQUIRE(open(path, O_CREAT, mode) != -1);
ATF_REQUIRE((filedesc = open(path, O_CREAT, mode)) != -1);
FILE *pipefd = setup(fds, auclass);
ATF_REQUIRE_EQ(0, linkat(AT_FDCWD, path, AT_FDCWD, "hardlink", 0));
check_audit(fds, successreg, pipefd);
close(filedesc);
}

ATF_TC_CLEANUP(linkat_success, tc)
Expand Down
10 changes: 7 additions & 3 deletions tests/sys/audit/file-delete.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@

static struct pollfd fds[1];
static mode_t mode = 0777;
static int filedesc;
static const char *path = "fileforaudit";
static const char *errpath = "dirdoesnotexist/fileforaudit";
static const char *successreg = "fileforaudit.*return,success";
Expand Down Expand Up @@ -92,10 +93,11 @@ ATF_TC_HEAD(rename_success, tc)

ATF_TC_BODY(rename_success, tc)
{
ATF_REQUIRE(open(path, O_CREAT, mode) != -1);
ATF_REQUIRE((filedesc = open(path, O_CREAT, mode)) != -1);
FILE *pipefd = setup(fds, "fd");
ATF_REQUIRE_EQ(0, rename(path, "renamed"));
check_audit(fds, successreg, pipefd);
close(filedesc);
}

ATF_TC_CLEANUP(rename_success, tc)
Expand Down Expand Up @@ -134,10 +136,11 @@ ATF_TC_HEAD(renameat_success, tc)

ATF_TC_BODY(renameat_success, tc)
{
ATF_REQUIRE(open(path, O_CREAT, mode) != -1);
ATF_REQUIRE((filedesc = open(path, O_CREAT, mode)) != -1);
FILE *pipefd = setup(fds, "fd");
ATF_REQUIRE_EQ(0, renameat(AT_FDCWD, path, AT_FDCWD, "renamed"));
check_audit(fds, successreg, pipefd);
close(filedesc);
}

ATF_TC_CLEANUP(renameat_success, tc)
Expand Down Expand Up @@ -176,10 +179,11 @@ ATF_TC_HEAD(unlink_success, tc)

ATF_TC_BODY(unlink_success, tc)
{
ATF_REQUIRE(open(path, O_CREAT, mode) != -1);
ATF_REQUIRE((filedesc = open(path, O_CREAT, mode)) != -1);
FILE *pipefd = setup(fds, "fd");
ATF_REQUIRE_EQ(0, unlink(path));
check_audit(fds, successreg, pipefd);
close(filedesc);
}

ATF_TC_CLEANUP(unlink_success, tc)
Expand Down
6 changes: 4 additions & 2 deletions tests/sys/audit/file-write.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

static struct pollfd fds[1];
static mode_t mode = 0777;
static int filedesc;
static off_t offlen = 0;
static const char *path = "fileforaudit";
static const char *errpath = "dirdoesnotexist/fileforaudit";
Expand All @@ -49,10 +50,11 @@ ATF_TC_HEAD(truncate_success, tc)
ATF_TC_BODY(truncate_success, tc)
{
/* File needs to exist to call truncate(2) */
ATF_REQUIRE(open(path, O_CREAT, mode) != -1);
ATF_REQUIRE((filedesc = open(path, O_CREAT, mode)) != -1);
FILE *pipefd = setup(fds, "fw");
ATF_REQUIRE_EQ(0, truncate(path, offlen));
check_audit(fds, successreg, pipefd);
close(filedesc);
}

ATF_TC_CLEANUP(truncate_success, tc)
Expand Down Expand Up @@ -91,13 +93,13 @@ ATF_TC_HEAD(ftruncate_success, tc)

ATF_TC_BODY(ftruncate_success, tc)
{
int filedesc;
const char *regex = "ftruncate.*return,success";
/* Valid file descriptor needs to exist to call ftruncate(2) */
ATF_REQUIRE((filedesc = open(path, O_CREAT | O_RDWR)) != -1);
FILE *pipefd = setup(fds, "fw");
ATF_REQUIRE_EQ(0, ftruncate(filedesc, offlen));
check_audit(fds, regex, pipefd);
close(filedesc);
}

ATF_TC_CLEANUP(ftruncate_success, tc)
Expand Down
11 changes: 8 additions & 3 deletions tests/sys/audit/open.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@

static struct pollfd fds[1];
static mode_t o_mode = 0777;
static int filedesc;
static char extregex[80];
static const char *path = "fileforaudit";
static const char *errpath = "adirhasnoname/fileforaudit";
Expand All @@ -71,10 +72,11 @@ ATF_TC_BODY(open_ ## mode ## _success, tc) \
snprintf(extregex, sizeof(extregex), \
"open.*%s.*fileforaudit.*return,success", regex); \
/* File needs to exist for successful open(2) invocation */ \
ATF_REQUIRE(open(path, O_CREAT, o_mode) != -1); \
ATF_REQUIRE((filedesc = open(path, O_CREAT, o_mode)) != -1); \
FILE *pipefd = setup(fds, class); \
ATF_REQUIRE(syscall(SYS_open, path, flag) != -1); \
check_audit(fds, extregex, pipefd); \
close(filedesc); \
} \
ATF_TC_CLEANUP(open_ ## mode ## _success, tc) \
{ \
Expand Down Expand Up @@ -106,13 +108,16 @@ ATF_TC_HEAD(openat_ ## mode ## _success, tc) \
} \
ATF_TC_BODY(openat_ ## mode ## _success, tc) \
{ \
int filedesc2; \
snprintf(extregex, sizeof(extregex), \
"openat.*%s.*fileforaudit.*return,success", regex); \
/* File needs to exist for successful openat(2) invocation */ \
ATF_REQUIRE(open(path, O_CREAT, o_mode) != -1); \
ATF_REQUIRE((filedesc = open(path, O_CREAT, o_mode)) != -1); \
FILE *pipefd = setup(fds, class); \
ATF_REQUIRE(openat(AT_FDCWD, path, flag) != -1); \
ATF_REQUIRE((filedesc2 = openat(AT_FDCWD, path, flag)) != -1); \
check_audit(fds, extregex, pipefd); \
close(filedesc2); \
close(filedesc); \
} \
ATF_TC_CLEANUP(openat_ ## mode ## _success, tc) \
{ \
Expand Down
22 changes: 11 additions & 11 deletions tests/sys/audit/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,16 @@

#include <sys/ioctl.h>

#include <time.h>
#include <bsm/libbsm.h>
#include <security/audit/audit_ioctl.h>

#include <atf-c.h>
#include <errno.h>
#include <fcntl.h>
#include <atf-c.h>
#include <string.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <bsm/libbsm.h>
#include <security/audit/audit_ioctl.h>

#include "utils.h"

Expand Down Expand Up @@ -79,7 +80,7 @@ get_records(const char *auditregex, FILE *pipestream)
}

free(buff);
fclose(memstream);
ATF_REQUIRE_EQ(0, fclose(memstream));
return (atf_utils_grep_string("%s", membuff, auditregex));
}

Expand Down Expand Up @@ -194,9 +195,8 @@ void
check_audit(struct pollfd fd[], const char *auditrgx, FILE *pipestream) {
check_auditpipe(fd, auditrgx, pipestream);

/* Cleanup */
fclose(pipestream);
close(fd[0].fd);
/* Teardown: /dev/auditpipe's instance opened for this test-suite */
ATF_REQUIRE_EQ(0, fclose(pipestream));
}

FILE
Expand All @@ -207,9 +207,9 @@ FILE
nomask = get_audit_mask("no");
FILE *pipestream;

fd[0].fd = open("/dev/auditpipe", O_RDONLY);
ATF_REQUIRE((fd[0].fd = open("/dev/auditpipe", O_RDONLY)) != -1);
ATF_REQUIRE((pipestream = fdopen(fd[0].fd, "r")) != NULL);
fd[0].events = POLLIN;
pipestream = fdopen(fd[0].fd, "r");

/* Set local preselection audit_class as "no" for audit startup */
set_preselect_mode(fd[0].fd, &nomask);
Expand Down

0 comments on commit a18fc35

Please sign in to comment.