From owner-svn-src-head@freebsd.org Sat Jun 16 18:22:36 2018 Return-Path: Delivered-To: svn-src-head@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 96128101E66D; Sat, 16 Jun 2018 18:22:36 +0000 (UTC) (envelope-from asomers@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client CN "mxrelay.nyi.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 480F86A8CD; Sat, 16 Jun 2018 18:22:36 +0000 (UTC) (envelope-from asomers@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 28A361F6E; Sat, 16 Jun 2018 18:22:36 +0000 (UTC) (envelope-from asomers@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id w5GIMaS7016639; Sat, 16 Jun 2018 18:22:36 GMT (envelope-from asomers@FreeBSD.org) Received: (from asomers@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id w5GIMZ9q016637; Sat, 16 Jun 2018 18:22:35 GMT (envelope-from asomers@FreeBSD.org) Message-Id: <201806161822.w5GIMZ9q016637@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: asomers set sender to asomers@FreeBSD.org using -f From: Alan Somers Date: Sat, 16 Jun 2018 18:22:35 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r335260 - head/tests/sys/audit X-SVN-Group: head X-SVN-Commit-Author: asomers X-SVN-Commit-Paths: head/tests/sys/audit X-SVN-Commit-Revision: 335260 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-head@freebsd.org X-Mailman-Version: 2.1.26 Precedence: list List-Id: SVN commit messages for the src tree for head/-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 16 Jun 2018 18:22:36 -0000 Author: asomers Date: Sat Jun 16 18:22:35 2018 New Revision: 335260 URL: https://svnweb.freebsd.org/changeset/base/335260 Log: audit(4): add tests for POSIX message queues Submitted by: aniketp MFC after: 2 weeks Sponsored by: Google, Inc. (GSoC 2018) Differential Revision: https://reviews.freebsd.org/D15848 Added: head/tests/sys/audit/inter-process.c (contents, props changed) Modified: head/tests/sys/audit/Makefile Modified: head/tests/sys/audit/Makefile ============================================================================== --- head/tests/sys/audit/Makefile Sat Jun 16 17:45:44 2018 (r335259) +++ head/tests/sys/audit/Makefile Sat Jun 16 18:22:35 2018 (r335260) @@ -11,6 +11,7 @@ ATF_TESTS_C+= file-write ATF_TESTS_C+= file-read ATF_TESTS_C+= open ATF_TESTS_C+= network +ATF_TESTS_C+= inter-process SRCS.file-attribute-access+= file-attribute-access.c SRCS.file-attribute-access+= utils.c @@ -30,6 +31,8 @@ SRCS.open+= open.c SRCS.open+= utils.c SRCS.network+= network.c SRCS.network+= utils.c +SRCS.inter-process+= inter-process.c +SRCS.inter-process+= utils.c TEST_METADATA+= timeout="30" TEST_METADATA+= required_user="root" Added: head/tests/sys/audit/inter-process.c ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ head/tests/sys/audit/inter-process.c Sat Jun 16 18:22:35 2018 (r335260) @@ -0,0 +1,413 @@ +/*- + * Copyright (c) 2018 Aniket Pandey + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * SUCH DAMAGE. + * + * $FreeBSD$ + */ + +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +#include "utils.h" +#define BUFFSIZE 80 + +struct msgstr { + long int mtype; + char mtext[BUFFSIZE]; +}; +typedef struct msgstr msgstr_t; + +static int msqid; +static struct pollfd fds[1]; +static struct msqid_ds msgbuff; +static char ipcregex[BUFFSIZE]; +static const char *auclass = "ip"; + + +ATF_TC_WITH_CLEANUP(msgget_success); +ATF_TC_HEAD(msgget_success, tc) +{ + atf_tc_set_md_var(tc, "descr", "Tests the audit of a successful " + "msgget(2) call"); +} + +ATF_TC_BODY(msgget_success, tc) +{ + FILE *pipefd = setup(fds, auclass); + /* Create a message queue and obtain the corresponding identifier */ + ATF_REQUIRE((msqid = msgget(IPC_PRIVATE, IPC_CREAT | S_IRUSR)) != -1); + /* Check the presence of message queue ID in audit record */ + snprintf(ipcregex, sizeof(ipcregex), + "msgget.*return,success,%d", msqid); + check_audit(fds, ipcregex, pipefd); + + /* Destroy the message queue with ID = msqid */ + ATF_REQUIRE_EQ(0, msgctl(msqid, IPC_RMID, NULL)); +} + +ATF_TC_CLEANUP(msgget_success, tc) +{ + cleanup(); +} + + +ATF_TC_WITH_CLEANUP(msgget_failure); +ATF_TC_HEAD(msgget_failure, tc) +{ + atf_tc_set_md_var(tc, "descr", "Tests the audit of an unsuccessful " + "msgget(2) call"); +} + +ATF_TC_BODY(msgget_failure, tc) +{ + const char *regex = "msgget.*return,failure.*No such file or directory"; + FILE *pipefd = setup(fds, auclass); + ATF_REQUIRE_EQ(-1, msgget((key_t)(-1), 0)); + check_audit(fds, regex, pipefd); +} + +ATF_TC_CLEANUP(msgget_failure, tc) +{ + cleanup(); +} + + +ATF_TC_WITH_CLEANUP(msgsnd_success); +ATF_TC_HEAD(msgsnd_success, tc) +{ + atf_tc_set_md_var(tc, "descr", "Tests the audit of a successful " + "msgsnd(2) call"); +} + +ATF_TC_BODY(msgsnd_success, tc) +{ + /* Create a message queue and obtain the corresponding identifier */ + ATF_REQUIRE((msqid = msgget(IPC_PRIVATE, IPC_CREAT | S_IRUSR)) != -1); + + /* Initialize a msgstr_t structure to store message */ + msgstr_t msg; + msg.mtype = 1; + memset(msg.mtext, 0, BUFFSIZE); + + /* Check the presence of message queue ID in audit record */ + snprintf(ipcregex, sizeof(ipcregex), + "msgsnd.*Message IPC.*%d.*return,success", msqid); + + FILE *pipefd = setup(fds, auclass); + ATF_REQUIRE_EQ(0, msgsnd(msqid, &msg, BUFFSIZE, IPC_NOWAIT)); + check_audit(fds, ipcregex, pipefd); + + /* Destroy the message queue with ID = msqid */ + ATF_REQUIRE_EQ(0, msgctl(msqid, IPC_RMID, NULL)); +} + +ATF_TC_CLEANUP(msgsnd_success, tc) +{ + cleanup(); +} + + +ATF_TC_WITH_CLEANUP(msgsnd_failure); +ATF_TC_HEAD(msgsnd_failure, tc) +{ + atf_tc_set_md_var(tc, "descr", "Tests the audit of an unsuccessful " + "msgsnd(2) call"); +} + +ATF_TC_BODY(msgsnd_failure, tc) +{ + const char *regex = "msgsnd.*Message IPC.*return,failure : Bad address"; + FILE *pipefd = setup(fds, auclass); + ATF_REQUIRE_EQ(-1, msgsnd(-1, NULL, 0, IPC_NOWAIT)); + check_audit(fds, regex, pipefd); +} + +ATF_TC_CLEANUP(msgsnd_failure, tc) +{ + cleanup(); +} + + +ATF_TC_WITH_CLEANUP(msgrcv_success); +ATF_TC_HEAD(msgrcv_success, tc) +{ + atf_tc_set_md_var(tc, "descr", "Tests the audit of a successful " + "msgrcv(2) call"); +} + +ATF_TC_BODY(msgrcv_success, tc) +{ + ssize_t recv_bytes; + /* Create a message queue and obtain the corresponding identifier */ + ATF_REQUIRE((msqid = msgget(IPC_PRIVATE, IPC_CREAT | S_IRUSR)) != -1); + + /* Initialize two msgstr_t structures to store respective messages */ + msgstr_t msg1, msg2; + msg1.mtype = 1; + memset(msg1.mtext, 0, BUFFSIZE); + + /* Send a message to the queue with ID = msqid */ + ATF_REQUIRE_EQ(0, msgsnd(msqid, &msg1, BUFFSIZE, IPC_NOWAIT)); + + FILE *pipefd = setup(fds, auclass); + ATF_REQUIRE((recv_bytes = msgrcv(msqid, &msg2, + BUFFSIZE, 0, MSG_NOERROR | IPC_NOWAIT)) != -1); + /* Check the presence of queue ID and returned bytes in audit record */ + snprintf(ipcregex, sizeof(ipcregex), + "msgrcv.*Message IPC,*%d.*return,success,%zd", msqid, recv_bytes); + check_audit(fds, ipcregex, pipefd); + + /* Destroy the message queue with ID = msqid */ + ATF_REQUIRE_EQ(0, msgctl(msqid, IPC_RMID, NULL)); +} + +ATF_TC_CLEANUP(msgrcv_success, tc) +{ + cleanup(); +} + + +ATF_TC_WITH_CLEANUP(msgrcv_failure); +ATF_TC_HEAD(msgrcv_failure, tc) +{ + atf_tc_set_md_var(tc, "descr", "Tests the audit of an unsuccessful " + "msgrcv(2) call"); +} + +ATF_TC_BODY(msgrcv_failure, tc) +{ + const char *regex = "msgrcv.*return,failure : Invalid argument"; + FILE *pipefd = setup(fds, auclass); + ATF_REQUIRE_EQ(-1, msgrcv(-1, NULL, 0, 0, MSG_NOERROR | IPC_NOWAIT)); + check_audit(fds, regex, pipefd); +} + +ATF_TC_CLEANUP(msgrcv_failure, tc) +{ + cleanup(); +} + + +ATF_TC_WITH_CLEANUP(msgctl_rmid_success); +ATF_TC_HEAD(msgctl_rmid_success, tc) +{ + atf_tc_set_md_var(tc, "descr", "Tests the audit of a successful " + "msgctl(2) call for IPC_RMID command"); +} + +ATF_TC_BODY(msgctl_rmid_success, tc) +{ + /* Create a message queue and obtain the corresponding identifier */ + ATF_REQUIRE((msqid = msgget(IPC_PRIVATE, IPC_CREAT | S_IRUSR)) != -1); + + FILE *pipefd = setup(fds, auclass); + ATF_REQUIRE_EQ(0, msgctl(msqid, IPC_RMID, NULL)); + /* Check the presence of queue ID and IPC_RMID in audit record */ + snprintf(ipcregex, sizeof(ipcregex), + "msgctl.*IPC_RMID.*%d.*return,success", msqid); + check_audit(fds, ipcregex, pipefd); +} + +ATF_TC_CLEANUP(msgctl_rmid_success, tc) +{ + cleanup(); +} + + +ATF_TC_WITH_CLEANUP(msgctl_rmid_failure); +ATF_TC_HEAD(msgctl_rmid_failure, tc) +{ + atf_tc_set_md_var(tc, "descr", "Tests the audit of an unsuccessful " + "msgctl(2) call for IPC_RMID command"); +} + +ATF_TC_BODY(msgctl_rmid_failure, tc) +{ + const char *regex = "msgctl.*IPC_RMID.*return,failur.*Invalid argument"; + FILE *pipefd = setup(fds, auclass); + ATF_REQUIRE_EQ(-1, msgctl(-1, IPC_RMID, NULL)); + check_audit(fds, regex, pipefd); +} + +ATF_TC_CLEANUP(msgctl_rmid_failure, tc) +{ + cleanup(); +} + + +ATF_TC_WITH_CLEANUP(msgctl_stat_success); +ATF_TC_HEAD(msgctl_stat_success, tc) +{ + atf_tc_set_md_var(tc, "descr", "Tests the audit of a successful " + "msgctl(2) call for IPC_STAT command"); +} + +ATF_TC_BODY(msgctl_stat_success, tc) +{ + /* Create a message queue and obtain the corresponding identifier */ + ATF_REQUIRE((msqid = msgget(IPC_PRIVATE, IPC_CREAT | S_IRUSR)) != -1); + + FILE *pipefd = setup(fds, auclass); + ATF_REQUIRE_EQ(0, msgctl(msqid, IPC_STAT, &msgbuff)); + /* Check the presence of queue ID and IPC_STAT in audit record */ + snprintf(ipcregex, sizeof(ipcregex), + "msgctl.*IPC_STAT.*%d.*return,success", msqid); + check_audit(fds, ipcregex, pipefd); + + /* Destroy the message queue with ID = msqid */ + ATF_REQUIRE_EQ(0, msgctl(msqid, IPC_RMID, NULL)); +} + +ATF_TC_CLEANUP(msgctl_stat_success, tc) +{ + cleanup(); +} + + +ATF_TC_WITH_CLEANUP(msgctl_stat_failure); +ATF_TC_HEAD(msgctl_stat_failure, tc) +{ + atf_tc_set_md_var(tc, "descr", "Tests the audit of an unsuccessful " + "msgctl(2) call for IPC_STAT command"); +} + +ATF_TC_BODY(msgctl_stat_failure, tc) +{ + const char *regex = "msgctl.*IPC_STAT.*return,failur.*Invalid argument"; + FILE *pipefd = setup(fds, auclass); + ATF_REQUIRE_EQ(-1, msgctl(-1, IPC_STAT, &msgbuff)); + check_audit(fds, regex, pipefd); +} + +ATF_TC_CLEANUP(msgctl_stat_failure, tc) +{ + cleanup(); +} + + +ATF_TC_WITH_CLEANUP(msgctl_set_success); +ATF_TC_HEAD(msgctl_set_success, tc) +{ + atf_tc_set_md_var(tc, "descr", "Tests the audit of a successful " + "msgctl(2) call for IPC_SET command"); +} + +ATF_TC_BODY(msgctl_set_success, tc) +{ + /* Create a message queue and obtain the corresponding identifier */ + ATF_REQUIRE((msqid = msgget(IPC_PRIVATE, IPC_CREAT | S_IRUSR)) != -1); + /* Fill up the msgbuff structure to be used with IPC_SET */ + ATF_REQUIRE_EQ(0, msgctl(msqid, IPC_STAT, &msgbuff)); + + FILE *pipefd = setup(fds, auclass); + ATF_REQUIRE_EQ(0, msgctl(msqid, IPC_SET, &msgbuff)); + /* Check the presence of message queue ID in audit record */ + snprintf(ipcregex, sizeof(ipcregex), + "msgctl.*IPC_SET.*%d.*return,success", msqid); + check_audit(fds, ipcregex, pipefd); + + /* Destroy the message queue with ID = msqid */ + ATF_REQUIRE_EQ(0, msgctl(msqid, IPC_RMID, NULL)); +} + +ATF_TC_CLEANUP(msgctl_set_success, tc) +{ + cleanup(); +} + + +ATF_TC_WITH_CLEANUP(msgctl_set_failure); +ATF_TC_HEAD(msgctl_set_failure, tc) +{ + atf_tc_set_md_var(tc, "descr", "Tests the audit of an unsuccessful " + "msgctl(2) call for IPC_SET command"); +} + +ATF_TC_BODY(msgctl_set_failure, tc) +{ + const char *regex = "msgctl.*IPC_SET.*return,failure.*Invalid argument"; + FILE *pipefd = setup(fds, auclass); + ATF_REQUIRE_EQ(-1, msgctl(-1, IPC_SET, &msgbuff)); + check_audit(fds, regex, pipefd); +} + +ATF_TC_CLEANUP(msgctl_set_failure, tc) +{ + cleanup(); +} + + +ATF_TC_WITH_CLEANUP(msgctl_illegal_command); +ATF_TC_HEAD(msgctl_illegal_command, tc) +{ + atf_tc_set_md_var(tc, "descr", "Tests the audit of an unsuccessful " + "msgctl(2) call for illegal cmd value"); +} + +ATF_TC_BODY(msgctl_illegal_command, tc) +{ + /* Create a message queue and obtain the corresponding identifier */ + ATF_REQUIRE((msqid = msgget(IPC_PRIVATE, IPC_CREAT | S_IRUSR)) != -1); + + const char *regex = "msgctl.*illegal command.*failur.*Invalid argument"; + FILE *pipefd = setup(fds, auclass); + ATF_REQUIRE_EQ(-1, msgctl(msqid, -1, &msgbuff)); + check_audit(fds, regex, pipefd); + + /* Destroy the message queue with ID = msqid */ + ATF_REQUIRE_EQ(0, msgctl(msqid, IPC_RMID, NULL)); +} + +ATF_TC_CLEANUP(msgctl_illegal_command, tc) +{ + cleanup(); +} + + +ATF_TP_ADD_TCS(tp) +{ + ATF_TP_ADD_TC(tp, msgget_success); + ATF_TP_ADD_TC(tp, msgget_failure); + ATF_TP_ADD_TC(tp, msgsnd_success); + ATF_TP_ADD_TC(tp, msgsnd_failure); + ATF_TP_ADD_TC(tp, msgrcv_success); + ATF_TP_ADD_TC(tp, msgrcv_failure); + + ATF_TP_ADD_TC(tp, msgctl_rmid_success); + ATF_TP_ADD_TC(tp, msgctl_rmid_failure); + ATF_TP_ADD_TC(tp, msgctl_stat_success); + ATF_TP_ADD_TC(tp, msgctl_stat_failure); + ATF_TP_ADD_TC(tp, msgctl_set_success); + ATF_TP_ADD_TC(tp, msgctl_set_failure); + ATF_TP_ADD_TC(tp, msgctl_illegal_command); + + return (atf_no_error()); +}