svn commit: r335307 - head/tests/sys/audit

Alan Somers asomers at FreeBSD.org
Sun Jun 17 21:29:36 UTC 2018


Author: asomers
Date: Sun Jun 17 21:29:35 2018
New Revision: 335307
URL: https://svnweb.freebsd.org/changeset/base/335307

Log:
  audit(4): add tests for Sys V shared memory syscalls
  
  includes shmget, shmat, shmdt, and shmctl
  
  Submitted by:	aniketp
  MFC after:	2 weeks
  Sponsored by:	Google, Inc (GSoC 2018)
  Differential Revision:	https://reviews.freebsd.org/D15860

Modified:
  head/tests/sys/audit/inter-process.c

Modified: head/tests/sys/audit/inter-process.c
==============================================================================
--- head/tests/sys/audit/inter-process.c	Sun Jun 17 21:09:21 2018	(r335306)
+++ head/tests/sys/audit/inter-process.c	Sun Jun 17 21:29:35 2018	(r335307)
@@ -29,11 +29,11 @@
 #include <sys/ipc.h>
 #include <sys/mman.h>
 #include <sys/msg.h>
+#include <sys/shm.h>
 #include <sys/stat.h>
 
 #include <atf-c.h>
 #include <fcntl.h>
-#include <string.h>
 #include <unistd.h>
 
 #include "utils.h"
@@ -45,9 +45,11 @@ struct msgstr {
 };
 typedef struct msgstr msgstr_t;
 
-static int msqid;
+static pid_t pid;
+static int msqid, shmid;
 static struct pollfd fds[1];
 static struct msqid_ds msgbuff;
+static struct shmid_ds shmbuff;
 static char ipcregex[BUFFSIZE];
 static const char *auclass = "ip";
 
@@ -392,6 +394,341 @@ ATF_TC_CLEANUP(msgctl_illegal_command, tc)
 }
 
 
+ATF_TC_WITH_CLEANUP(shmget_success);
+ATF_TC_HEAD(shmget_success, tc)
+{
+	atf_tc_set_md_var(tc, "descr", "Tests the audit of a successful "
+					"shmget(2) call");
+}
+
+ATF_TC_BODY(shmget_success, tc)
+{
+	FILE *pipefd = setup(fds, auclass);
+	ATF_REQUIRE((shmid =
+		shmget(IPC_PRIVATE, 1, IPC_CREAT | S_IRUSR)) != -1);
+	/* Check the presence of shared memory ID in audit record */
+	snprintf(ipcregex, sizeof(ipcregex), "shmget.*ret.*success,%d", shmid);
+	check_audit(fds, ipcregex, pipefd);
+
+	/* Destroy the shared memory with ID = shmid */
+	ATF_REQUIRE_EQ(0, shmctl(shmid, IPC_RMID, NULL));
+}
+
+ATF_TC_CLEANUP(shmget_success, tc)
+{
+	cleanup();
+}
+
+
+ATF_TC_WITH_CLEANUP(shmget_failure);
+ATF_TC_HEAD(shmget_failure, tc)
+{
+	atf_tc_set_md_var(tc, "descr", "Tests the audit of an unsuccessful "
+					"shmget(2) call");
+}
+
+ATF_TC_BODY(shmget_failure, tc)
+{
+	const char *regex = "shmget.*return,failure.*No such file or directory";
+	FILE *pipefd = setup(fds, auclass);
+	ATF_REQUIRE_EQ(-1, shmget((key_t)(-1), 0, 0));
+	check_audit(fds, regex, pipefd);
+}
+
+ATF_TC_CLEANUP(shmget_failure, tc)
+{
+	cleanup();
+}
+
+
+ATF_TC_WITH_CLEANUP(shmat_success);
+ATF_TC_HEAD(shmat_success, tc)
+{
+	atf_tc_set_md_var(tc, "descr", "Tests the audit of a successful "
+					"shmat(2) call");
+}
+
+ATF_TC_BODY(shmat_success, tc)
+{
+	void *addr;
+	/* Create a shared memory segment and obtain the identifier */
+	ATF_REQUIRE((shmid =
+		shmget(IPC_PRIVATE, 1, IPC_CREAT | S_IRUSR)) != -1);
+
+	FILE *pipefd = setup(fds, auclass);
+	ATF_REQUIRE((intptr_t)(addr = shmat(shmid, NULL, 0)) != -1);
+
+	/* Check for shared memory ID and process address in record */
+	snprintf(ipcregex, sizeof(ipcregex), "shmat.*Shared Memory "
+			"IPC.*%d.*return,success,%d", shmid, (int)addr);
+	check_audit(fds, ipcregex, pipefd);
+
+	/* Destroy the shared memory with ID = shmid */
+	ATF_REQUIRE_EQ(0, shmctl(shmid, IPC_RMID, NULL));
+}
+
+ATF_TC_CLEANUP(shmat_success, tc)
+{
+	cleanup();
+}
+
+
+ATF_TC_WITH_CLEANUP(shmat_failure);
+ATF_TC_HEAD(shmat_failure, tc)
+{
+	atf_tc_set_md_var(tc, "descr", "Tests the audit of an unsuccessful "
+					"shmat(2) call");
+}
+
+ATF_TC_BODY(shmat_failure, tc)
+{
+	const char *regex = "shmat.*Shared Memory IPC.*return,failure";
+	FILE *pipefd = setup(fds, auclass);
+	ATF_REQUIRE_EQ(-1, (intptr_t)shmat(-1, NULL, 0));
+	check_audit(fds, regex, pipefd);
+}
+
+ATF_TC_CLEANUP(shmat_failure, tc)
+{
+	cleanup();
+}
+
+
+ATF_TC_WITH_CLEANUP(shmdt_success);
+ATF_TC_HEAD(shmdt_success, tc)
+{
+	atf_tc_set_md_var(tc, "descr", "Tests the audit of a successful "
+					"shmdt(2) call");
+}
+
+ATF_TC_BODY(shmdt_success, tc)
+{
+	void *addr;
+	pid = getpid();
+	snprintf(ipcregex, sizeof(ipcregex), "shmdt.*%d.*return,success", pid);
+
+	/* Create a shared memory segment and obtain the identifier */
+	ATF_REQUIRE((shmid =
+		shmget(IPC_PRIVATE, 1, IPC_CREAT | S_IRUSR)) != -1);
+
+	/* Attach the shared memory to calling process's address space */
+	ATF_REQUIRE((intptr_t)(addr = shmat(shmid, NULL, 0)) != -1);
+
+	FILE *pipefd = setup(fds, auclass);
+	ATF_REQUIRE_EQ(0, shmdt(addr));
+	check_audit(fds, ipcregex, pipefd);
+
+	/* Destroy the shared memory with ID = shmid */
+	ATF_REQUIRE_EQ(0, shmctl(shmid, IPC_RMID, NULL));
+}
+
+ATF_TC_CLEANUP(shmdt_success, tc)
+{
+	cleanup();
+}
+
+
+ATF_TC_WITH_CLEANUP(shmdt_failure);
+ATF_TC_HEAD(shmdt_failure, tc)
+{
+	atf_tc_set_md_var(tc, "descr", "Tests the audit of an unsuccessful "
+					"shmdt(2) call");
+}
+
+ATF_TC_BODY(shmdt_failure, tc)
+{
+	const char *regex = "shmdt.*return,failure : Invalid argument";
+	FILE *pipefd = setup(fds, auclass);
+	ATF_REQUIRE_EQ(-1, shmdt(NULL));
+	check_audit(fds, regex, pipefd);
+}
+
+ATF_TC_CLEANUP(shmdt_failure, tc)
+{
+	cleanup();
+}
+
+
+ATF_TC_WITH_CLEANUP(shmctl_rmid_success);
+ATF_TC_HEAD(shmctl_rmid_success, tc)
+{
+	atf_tc_set_md_var(tc, "descr", "Tests the audit of a successful "
+					"shmctl(2) call for IPC_RMID command");
+}
+
+ATF_TC_BODY(shmctl_rmid_success, tc)
+{
+	/* Create a shared memory segment and obtain the identifier */
+	ATF_REQUIRE((shmid =
+		shmget(IPC_PRIVATE, 1, IPC_CREAT | S_IRUSR)) != -1);
+
+	FILE *pipefd = setup(fds, auclass);
+	ATF_REQUIRE_EQ(0, shmctl(shmid, IPC_RMID, NULL));
+	/* Check the presence of shmid and IPC_RMID in audit record */
+	snprintf(ipcregex, sizeof(ipcregex),
+		"shmctl.*IPC_RMID.*%d.*return,success", shmid);
+	check_audit(fds, ipcregex, pipefd);
+}
+
+ATF_TC_CLEANUP(shmctl_rmid_success, tc)
+{
+	cleanup();
+}
+
+
+ATF_TC_WITH_CLEANUP(shmctl_rmid_failure);
+ATF_TC_HEAD(shmctl_rmid_failure, tc)
+{
+	atf_tc_set_md_var(tc, "descr", "Tests the audit of an unsuccessful "
+					"shmctl(2) call for IPC_RMID command");
+}
+
+ATF_TC_BODY(shmctl_rmid_failure, tc)
+{
+	const char *regex = "shmctl.*IPC_RMID.*return,fail.*Invalid argument";
+	FILE *pipefd = setup(fds, auclass);
+	ATF_REQUIRE_EQ(-1, shmctl(-1, IPC_RMID, NULL));
+	check_audit(fds, regex, pipefd);
+}
+
+ATF_TC_CLEANUP(shmctl_rmid_failure, tc)
+{
+	cleanup();
+}
+
+
+ATF_TC_WITH_CLEANUP(shmctl_stat_success);
+ATF_TC_HEAD(shmctl_stat_success, tc)
+{
+	atf_tc_set_md_var(tc, "descr", "Tests the audit of a successful "
+					"shmctl(2) call for IPC_STAT command");
+}
+
+ATF_TC_BODY(shmctl_stat_success, tc)
+{
+	/* Create a shared memory segment and obtain the identifier */
+	ATF_REQUIRE((shmid =
+		shmget(IPC_PRIVATE, 1, IPC_CREAT | S_IRUSR)) != -1);
+
+	FILE *pipefd = setup(fds, auclass);
+	ATF_REQUIRE_EQ(0, shmctl(shmid, IPC_STAT, &shmbuff));
+	/* Check if shared memory ID and IPC_STAT are present in audit record */
+	snprintf(ipcregex, sizeof(ipcregex),
+		"shmctl.*IPC_STAT.*%d.*return,success", shmid);
+	check_audit(fds, ipcregex, pipefd);
+
+	/* Destroy the shared memory with ID = shmid */
+	ATF_REQUIRE_EQ(0, shmctl(shmid, IPC_RMID, NULL));
+}
+
+ATF_TC_CLEANUP(shmctl_stat_success, tc)
+{
+	cleanup();
+}
+
+
+ATF_TC_WITH_CLEANUP(shmctl_stat_failure);
+ATF_TC_HEAD(shmctl_stat_failure, tc)
+{
+	atf_tc_set_md_var(tc, "descr", "Tests the audit of an unsuccessful "
+					"shmctl(2) call for IPC_STAT command");
+}
+
+ATF_TC_BODY(shmctl_stat_failure, tc)
+{
+	const char *regex = "shmctl.*IPC_STAT.*return,fail.*Invalid argument";
+	FILE *pipefd = setup(fds, auclass);
+	ATF_REQUIRE_EQ(-1, shmctl(-1, IPC_STAT, &shmbuff));
+	check_audit(fds, regex, pipefd);
+}
+
+ATF_TC_CLEANUP(shmctl_stat_failure, tc)
+{
+	cleanup();
+}
+
+
+ATF_TC_WITH_CLEANUP(shmctl_set_success);
+ATF_TC_HEAD(shmctl_set_success, tc)
+{
+	atf_tc_set_md_var(tc, "descr", "Tests the audit of a successful "
+					"shmctl(2) call for IPC_SET command");
+}
+
+ATF_TC_BODY(shmctl_set_success, tc)
+{
+	/* Create a shared memory segment and obtain the identifier */
+	ATF_REQUIRE((shmid =
+		shmget(IPC_PRIVATE, 1, IPC_CREAT | S_IRUSR)) != -1);
+	/* Fill up the shmbuff structure to be used with IPC_SET */
+	ATF_REQUIRE_EQ(0, shmctl(shmid, IPC_STAT, &shmbuff));
+
+	FILE *pipefd = setup(fds, auclass);
+	ATF_REQUIRE_EQ(0, shmctl(shmid, IPC_SET, &shmbuff));
+	/* Check the presence of shared memory ID in audit record */
+	snprintf(ipcregex, sizeof(ipcregex),
+		"shmctl.*IPC_SET.*%d.*return,success", msqid);
+	check_audit(fds, ipcregex, pipefd);
+
+	/* Destroy the shared memory with ID = shmid */
+	ATF_REQUIRE_EQ(0, shmctl(shmid, IPC_RMID, NULL));
+}
+
+ATF_TC_CLEANUP(shmctl_set_success, tc)
+{
+	cleanup();
+}
+
+
+ATF_TC_WITH_CLEANUP(shmctl_set_failure);
+ATF_TC_HEAD(shmctl_set_failure, tc)
+{
+	atf_tc_set_md_var(tc, "descr", "Tests the audit of an unsuccessful "
+					"shmctl(2) call for IPC_SET command");
+}
+
+ATF_TC_BODY(shmctl_set_failure, tc)
+{
+	const char *regex = "shmctl.*IPC_SET.*return,failure.*Invalid argument";
+	FILE *pipefd = setup(fds, auclass);
+	ATF_REQUIRE_EQ(-1, shmctl(-1, IPC_SET, &shmbuff));
+	check_audit(fds, regex, pipefd);
+}
+
+ATF_TC_CLEANUP(shmctl_set_failure, tc)
+{
+	cleanup();
+}
+
+
+ATF_TC_WITH_CLEANUP(shmctl_illegal_command);
+ATF_TC_HEAD(shmctl_illegal_command, tc)
+{
+	atf_tc_set_md_var(tc, "descr", "Tests the audit of an unsuccessful "
+					"shmctl(2) call for illegal cmd value");
+}
+
+ATF_TC_BODY(shmctl_illegal_command, tc)
+{
+	/* Create a shared memory segment and obtain the identifier */
+	ATF_REQUIRE((shmid =
+		shmget(IPC_PRIVATE, 1, IPC_CREAT | S_IRUSR)) != -1);
+
+	const char *regex = "shmctl.*illegal command.*fail.*Invalid argument";
+	FILE *pipefd = setup(fds, auclass);
+	ATF_REQUIRE_EQ(-1, shmctl(shmid, -1, &shmbuff));
+	check_audit(fds, regex, pipefd);
+
+	/* Destroy the shared memory with ID = shmid */
+	ATF_REQUIRE_EQ(0, shmctl(shmid, IPC_RMID, NULL));
+}
+
+ATF_TC_CLEANUP(shmctl_illegal_command, tc)
+{
+	cleanup();
+}
+
+
 ATF_TP_ADD_TCS(tp)
 {
 	ATF_TP_ADD_TC(tp, msgget_success);
@@ -408,6 +745,21 @@ ATF_TP_ADD_TCS(tp)
 	ATF_TP_ADD_TC(tp, msgctl_set_success);
 	ATF_TP_ADD_TC(tp, msgctl_set_failure);
 	ATF_TP_ADD_TC(tp, msgctl_illegal_command);
+
+	ATF_TP_ADD_TC(tp, shmget_success);
+	ATF_TP_ADD_TC(tp, shmget_failure);
+	ATF_TP_ADD_TC(tp, shmat_success);
+	ATF_TP_ADD_TC(tp, shmat_failure);
+	ATF_TP_ADD_TC(tp, shmdt_success);
+	ATF_TP_ADD_TC(tp, shmdt_failure);
+
+	ATF_TP_ADD_TC(tp, shmctl_rmid_success);
+	ATF_TP_ADD_TC(tp, shmctl_rmid_failure);
+	ATF_TP_ADD_TC(tp, shmctl_stat_success);
+	ATF_TP_ADD_TC(tp, shmctl_stat_failure);
+	ATF_TP_ADD_TC(tp, shmctl_set_success);
+	ATF_TP_ADD_TC(tp, shmctl_set_failure);
+	ATF_TP_ADD_TC(tp, shmctl_illegal_command);
 
 	return (atf_no_error());
 }


More information about the svn-src-all mailing list