Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 13 Jun 2018 17:01:58 +0000 (UTC)
From:      Alan Somers <asomers@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org
Subject:   svn commit: r335067 - head/tests/sys/audit
Message-ID:  <201806131701.w5DH1wma047146@repo.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: asomers
Date: Wed Jun 13 17:01:57 2018
New Revision: 335067
URL: https://svnweb.freebsd.org/changeset/base/335067

Log:
  audit(4): Fix file descriptor leaks in ATF tests
  
  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

Modified:
  head/tests/sys/audit/file-attribute-access.c
  head/tests/sys/audit/file-close.c
  head/tests/sys/audit/file-create.c
  head/tests/sys/audit/file-delete.c
  head/tests/sys/audit/file-write.c
  head/tests/sys/audit/open.c
  head/tests/sys/audit/utils.c

Modified: head/tests/sys/audit/file-attribute-access.c
==============================================================================
--- head/tests/sys/audit/file-attribute-access.c	Wed Jun 13 16:48:07 2018	(r335066)
+++ head/tests/sys/audit/file-attribute-access.c	Wed Jun 13 17:01:57 2018	(r335067)
@@ -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";
@@ -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)
@@ -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);
@@ -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)

Modified: head/tests/sys/audit/file-close.c
==============================================================================
--- head/tests/sys/audit/file-close.c	Wed Jun 13 16:48:07 2018	(r335066)
+++ head/tests/sys/audit/file-close.c	Wed Jun 13 17:01:57 2018	(r335067)
@@ -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";
@@ -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' */
@@ -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);
@@ -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)

Modified: head/tests/sys/audit/file-create.c
==============================================================================
--- head/tests/sys/audit/file-create.c	Wed Jun 13 16:48:07 2018	(r335066)
+++ head/tests/sys/audit/file-create.c	Wed Jun 13 17:01:57 2018	(r335067)
@@ -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";
@@ -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)
@@ -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)
@@ -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)
@@ -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)

Modified: head/tests/sys/audit/file-delete.c
==============================================================================
--- head/tests/sys/audit/file-delete.c	Wed Jun 13 16:48:07 2018	(r335066)
+++ head/tests/sys/audit/file-delete.c	Wed Jun 13 17:01:57 2018	(r335067)
@@ -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";
@@ -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)
@@ -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)
@@ -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)

Modified: head/tests/sys/audit/file-write.c
==============================================================================
--- head/tests/sys/audit/file-write.c	Wed Jun 13 16:48:07 2018	(r335066)
+++ head/tests/sys/audit/file-write.c	Wed Jun 13 17:01:57 2018	(r335067)
@@ -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";
@@ -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)
@@ -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)

Modified: head/tests/sys/audit/open.c
==============================================================================
--- head/tests/sys/audit/open.c	Wed Jun 13 16:48:07 2018	(r335066)
+++ head/tests/sys/audit/open.c	Wed Jun 13 17:01:57 2018	(r335067)
@@ -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";
@@ -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) \
 { \
@@ -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) \
 { \

Modified: head/tests/sys/audit/utils.c
==============================================================================
--- head/tests/sys/audit/utils.c	Wed Jun 13 16:48:07 2018	(r335066)
+++ head/tests/sys/audit/utils.c	Wed Jun 13 17:01:57 2018	(r335067)
@@ -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"
 
@@ -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));
 }
 
@@ -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
@@ -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);



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201806131701.w5DH1wma047146>