From owner-svn-src-all@freebsd.org Sun Jun 3 23:36:30 2018 Return-Path: Delivered-To: svn-src-all@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 87080FED799; Sun, 3 Jun 2018 23:36:30 +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 3BD6883D19; Sun, 3 Jun 2018 23:36:30 +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 18951255DE; Sun, 3 Jun 2018 23:36:30 +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 w53NaT4T099685; Sun, 3 Jun 2018 23:36:29 GMT (envelope-from asomers@FreeBSD.org) Received: (from asomers@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id w53NaT89099684; Sun, 3 Jun 2018 23:36:29 GMT (envelope-from asomers@FreeBSD.org) Message-Id: <201806032336.w53NaT89099684@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: asomers set sender to asomers@FreeBSD.org using -f From: Alan Somers Date: Sun, 3 Jun 2018 23:36:29 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r334592 - 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: 334592 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.26 Precedence: list List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 03 Jun 2018 23:36:30 -0000 Author: asomers Date: Sun Jun 3 23:36:29 2018 New Revision: 334592 URL: https://svnweb.freebsd.org/changeset/base/334592 Log: audit(4): add tests for the cl audit class The only syscalls in this class are close, closefrom, munmap, and revoke. Submitted by: aniketp MFC after: 2 weeks Sponsored by: Google, Inc. (GSoC 2018) Differential Revision: https://reviews.freebsd.org/D15650 Added: head/tests/sys/audit/file-close.c (contents, props changed) Modified: head/tests/sys/audit/Makefile Modified: head/tests/sys/audit/Makefile ============================================================================== --- head/tests/sys/audit/Makefile Sun Jun 3 22:42:54 2018 (r334591) +++ head/tests/sys/audit/Makefile Sun Jun 3 23:36:29 2018 (r334592) @@ -4,6 +4,7 @@ TESTSDIR= ${TESTSBASE}/sys/audit ATF_TESTS_C= file-create ATF_TESTS_C+= file-delete +ATF_TESTS_C+= file-close ATF_TESTS_C+= file-write ATF_TESTS_C+= file-read @@ -11,6 +12,8 @@ SRCS.file-create+= file-create.c SRCS.file-create+= utils.c SRCS.file-delete+= file-delete.c SRCS.file-delete+= utils.c +SRCS.file-close+= file-close.c +SRCS.file-close+= utils.c SRCS.file-write+= file-write.c SRCS.file-write+= utils.c SRCS.file-read+= file-read.c @@ -22,6 +25,6 @@ TEST_METADATA+= is_exclusive="true" WARNS?= 6 -LDFLAGS+= -lbsm +LDFLAGS+= -lbsm -lutil .include Added: head/tests/sys/audit/file-close.c ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ head/tests/sys/audit/file-close.c Sun Jun 3 23:36:29 2018 (r334592) @@ -0,0 +1,236 @@ +/*- + * Copyright 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 "utils.h" + +static pid_t pid; +static struct pollfd fds[1]; +static mode_t mode = 0777; +static char extregex[80]; +static struct stat statbuff; +static const char *auclass = "cl"; +static const char *path = "fileforaudit"; +static const char *errpath = "dirdoesnotexist/fileforaudit"; +static const char *failurereg = "fileforaudit.*return,failure"; + + +ATF_TC_WITH_CLEANUP(munmap_success); +ATF_TC_HEAD(munmap_success, tc) +{ + atf_tc_set_md_var(tc, "descr", "Tests the audit of a successful " + "munmap(2) call"); +} + +ATF_TC_BODY(munmap_success, tc) +{ + pid = getpid(); + snprintf(extregex, sizeof(extregex), "munmap.*%d.*return,success", pid); + + /* Allocate sample memory, to be removed by munmap(2) */ + char *addr = mmap(NULL, sizeof(char), PROT_READ , MAP_ANONYMOUS, -1, 0); + FILE *pipefd = setup(fds, auclass); + ATF_REQUIRE_EQ(0, munmap(addr, sizeof(char))); + check_audit(fds, extregex, pipefd); +} + +ATF_TC_CLEANUP(munmap_success, tc) +{ + cleanup(); +} + + +ATF_TC_WITH_CLEANUP(munmap_failure); +ATF_TC_HEAD(munmap_failure, tc) +{ + atf_tc_set_md_var(tc, "descr", "Tests the audit of an unsuccessful " + "munmap(2) call"); +} + +ATF_TC_BODY(munmap_failure, tc) +{ + const char *regex = "munmap.*return,failure : Invalid argument"; + FILE *pipefd = setup(fds, auclass); + ATF_REQUIRE_EQ(-1, munmap((void *)SIZE_MAX, -1)); + check_audit(fds, regex, pipefd); +} + +ATF_TC_CLEANUP(munmap_failure, tc) +{ + cleanup(); +} + + +ATF_TC_WITH_CLEANUP(close_success); +ATF_TC_HEAD(close_success, tc) +{ + atf_tc_set_md_var(tc, "descr", "Tests the audit of a successful " + "close(2) call"); +} + +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' */ + ATF_REQUIRE_EQ(0, stat(path, &statbuff)); + FILE *pipefd = setup(fds, auclass); + ATF_REQUIRE_EQ(0, close(filedesc)); + + /* intmax_t to support all architectures */ + snprintf(extregex, sizeof(extregex), "close.*%jd.*return,succes", + (intmax_t)statbuff.st_ino); + check_audit(fds, extregex, pipefd); +} + +ATF_TC_CLEANUP(close_success, tc) +{ + cleanup(); +} + + +ATF_TC_WITH_CLEANUP(close_failure); +ATF_TC_HEAD(close_failure, tc) +{ + atf_tc_set_md_var(tc, "descr", "Tests the audit of an unsuccessful " + "close(2) call"); +} + +ATF_TC_BODY(close_failure, tc) +{ + const char *regex = "close.*return,failure"; + FILE *pipefd = setup(fds, auclass); + /* Failure reason: file does not exist */ + ATF_REQUIRE_EQ(-1, close(-1)); + check_audit(fds, regex, pipefd); +} + +ATF_TC_CLEANUP(close_failure, tc) +{ + cleanup(); +} + + +ATF_TC_WITH_CLEANUP(closefrom_success); +ATF_TC_HEAD(closefrom_success, tc) +{ + atf_tc_set_md_var(tc, "descr", "Tests the audit of a successful " + "closefrom(2) call"); +} + +ATF_TC_BODY(closefrom_success, tc) +{ + const char *regex = "closefrom.*return,success"; + FILE *pipefd = setup(fds, auclass); + /* closefrom(2) returns 'void' */ + closefrom(INT_MAX); + check_audit(fds, regex, pipefd); +} + +ATF_TC_CLEANUP(closefrom_success, tc) +{ + cleanup(); +} + + +ATF_TC_WITH_CLEANUP(revoke_success); +ATF_TC_HEAD(revoke_success, tc) +{ + atf_tc_set_md_var(tc, "descr", "Tests the audit of a successful " + "revoke(2) call"); +} + +ATF_TC_BODY(revoke_success, tc) +{ + int filedesc; + char *ptyname; + pid = getpid(); + snprintf(extregex, sizeof(extregex), "revoke.*%d.*return,success", pid); + + /* Obtain a pseudo terminal and get the path to slave device */ + ATF_REQUIRE((filedesc = posix_openpt(O_RDWR | O_NOCTTY)) != -1); + ATF_REQUIRE((ptyname = ptsname(filedesc)) != NULL); + + 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)); +} + +ATF_TC_CLEANUP(revoke_success, tc) +{ + cleanup(); +} + + +ATF_TC_WITH_CLEANUP(revoke_failure); +ATF_TC_HEAD(revoke_failure, tc) +{ + atf_tc_set_md_var(tc, "descr", "Tests the audit of an unsuccessful " + "revoke(2) call"); +} + +ATF_TC_BODY(revoke_failure, tc) +{ + FILE *pipefd = setup(fds, auclass); + /* Failure reason: file does not exist */ + ATF_REQUIRE_EQ(-1, revoke(errpath)); + check_audit(fds, failurereg, pipefd); +} + +ATF_TC_CLEANUP(revoke_failure, tc) +{ + cleanup(); +} + + +ATF_TP_ADD_TCS(tp) +{ + ATF_TP_ADD_TC(tp, munmap_success); + ATF_TP_ADD_TC(tp, munmap_failure); + + ATF_TP_ADD_TC(tp, close_success); + ATF_TP_ADD_TC(tp, close_failure); + ATF_TP_ADD_TC(tp, closefrom_success); + + ATF_TP_ADD_TC(tp, revoke_success); + ATF_TP_ADD_TC(tp, revoke_failure); + + return (atf_no_error()); +}