Add test cases for event types 60/180160/9
authoryeji01.kim <yeji01.kim@samsung.com>
Fri, 25 May 2018 06:49:35 +0000 (15:49 +0900)
committeryeji01.kim <yeji01.kim@samsung.com>
Thu, 31 May 2018 23:46:09 +0000 (08:46 +0900)
Change-Id: I58560b43830d7dd2762ab5d22cf4310927eff7bd
Signed-off-by: yeji01.kim <yeji01.kim@samsung.com>
tools/tests/CMakeLists.txt
tools/tests/groups/file.cpp
tools/tests/groups/ipc.cpp
tools/tests/groups/mac.cpp
tools/tests/groups/system.cpp

index 3fffcbe39f670c20cedfff17d39701f47749cd67..814472ff6770479f3d0d85afd32bfdbf29294196 100644 (file)
@@ -47,6 +47,10 @@ SET_TARGET_PROPERTIES(${SPEED_NAME} PROPERTIES PREFIX ""
        COMPILE_FLAGS "-fPIE"
        LINK_FLAGS "-pie"
 )
+SET_TARGET_PROPERTIES(${RULES_NAME} PROPERTIES PREFIX ""
+       COMPILE_FLAGS "-fPIE"
+       LINK_FLAGS "-pie"
+)
 SET_TARGET_PROPERTIES(${OVERHEAD_NAME} PROPERTIES PREFIX ""
        COMPILE_FLAGS "-fPIE"
        LINK_FLAGS "-pie"
@@ -59,6 +63,7 @@ PKG_CHECK_MODULES(CLI_DEPS    REQUIRED
 
 INCLUDE_DIRECTORIES(SYSTEM ${CLI_DEPS_INCLUDE_DIRS} ${AUDIT_TRAIL_LIB} groups)
 TARGET_LINK_LIBRARIES(${SPEED_NAME} ${CLI_DEPS_LIBRARIES} ${PROJECT_NAME} audit-trail)
+TARGET_LINK_LIBRARIES(${RULES_NAME} pthread)
 TARGET_LINK_LIBRARIES(${OVERHEAD_NAME} ${CLI_DEPS_LIBRARIES} ${PROJECT_NAME})
 
 INSTALL(TARGETS ${SEND_NAME} DESTINATION sbin)
index 0b8f3fa77378f0365a387b822c96a16caddcf5d1..91d5727b987955b3870f6ca51881ca06b9569a45 100644 (file)
@@ -38,6 +38,7 @@ public:
                accessFiles();
                destroyFile();
                createLink();
+               modifyDirectories();
                changeFileConfDAC();
                changeFileConfMAC();
        }
@@ -392,7 +393,7 @@ public:
                                close(fd);
                        if ((lstat(fileName.c_str(), &st) == 0) && (unlink(fileName.c_str()) != 0)) {
                                Display::printError();
-                               Display::printResult(Display::FAIL, "createLink restore");\r
+                               Display::printResult(Display::FAIL, "createLink restore");
                        }
                }
        }
index 8c3882f78d1f86555f9b12ad2b1f2ea37e45589a..ff1762ff104b61420d547f22727f223ba7a21bf9 100644 (file)
 
 #include <fcntl.h>
 #include <unistd.h>
+#include <string.h>
 #include <sys/types.h>
 #include <sys/ipc.h>
 #include <sys/msg.h>
 #include <sys/stat.h>
 #include <sys/sem.h>
 #include <sys/shm.h>
+#include <sys/socket.h>
+#include <sys/un.h>
+
+#include <thread>
 
 #include "group.h"
 
@@ -37,6 +42,7 @@ public:
        {
                Display::printResult(Display::NONE, "Ipc group test start");
                useIpc();
+               useSocket();
        }
 
        virtual ~IpcGroup()
@@ -49,6 +55,7 @@ public:
                        std::string msgKeyPath("/tmp/msg_key");
                        int ret;
                        int msgKey, qid;
+                       struct msqid_ds tmpbuf;
 
                        if (mkdir(msgKeyPath.c_str(), 0644) != 0) {
                                ret = Display::FAIL;
@@ -72,6 +79,18 @@ public:
                                Display::printResult(Display::SUCCESS, "msgget(304) positive");
                        }
 
+                       if (msgctl(qid, IPC_STAT, &tmpbuf) == -1) {
+                               ret = Display::FAIL;
+                               Display::printError();
+                       } else {
+                               if (msgctl(qid, IPC_SET, &tmpbuf) == -1) {
+                                       Display::printError();
+                                       Display::printResult(Display::FAIL, "IPC_SET operation positive");
+                               } else {
+                                       Display::printResult(Display::SUCCESS, "IPC_SET operation positive");
+                               }
+                       }
+
                        if (msgctl(qid, IPC_RMID, NULL) == -1) {
                                ret = Display::FAIL;
                                Display::printError();
@@ -159,6 +178,93 @@ public:
                        Display::printResult(ret, "shmctl(308) positive");
                }
        }
+
+       void useSocket() {
+               std::string sockaddr("/tmp/.audit-test.sock");
+               char buffer[100];
+               int fd = -1, cliFd = -1;
+               int recvlen = 0;
+               int backlog = 10;
+               struct sockaddr_un addr;
+               struct stat st;
+               std::thread client;
+
+               if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
+                       Display::printError();
+                       Display::printResult(Display::FAIL, "useSocket positive");
+                       return;
+               }
+
+               memset(&addr, 0, sizeof(addr));
+               addr.sun_family = AF_UNIX;
+               strncpy(addr.sun_path, sockaddr.c_str(), sizeof(sockaddr_un::sun_path));
+
+               if (bind(fd, reinterpret_cast<struct sockaddr *>(&addr), sizeof(struct sockaddr_un)) == -1) {
+                       close(fd);
+                       Display::printError();
+                       Display::printResult(Display::FAIL, "useSocket positive");
+                       return;
+               }
+
+               if (listen(fd, backlog) == -1) {
+                               close(fd);
+                               Display::printError();
+                               Display::printResult(Display::FAIL, "useSocket positive");
+                               return;
+               }
+
+               client = std::thread([&]() {
+                       int clientFd = -1;
+                       std::string message("audit socket event testing");
+
+                       struct sockaddr_un serveraddr;
+                       if ((clientFd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
+                               Display::printError();
+                               return;
+                       }
+
+                       memset(&serveraddr, 0, sizeof(serveraddr));
+                       serveraddr.sun_family = AF_UNIX;
+                       strncpy(serveraddr.sun_path, sockaddr.c_str(), sizeof(sockaddr_un::sun_path));
+
+                       if (connect(clientFd, reinterpret_cast<struct sockaddr *>(&serveraddr), sizeof(serveraddr)) == -1) {
+                               close(clientFd);
+                               Display::printError();
+                               return;
+                       }
+                       send(clientFd, message.c_str(), message.size(), 0);
+
+                       if (clientFd >= 0)
+                               close(clientFd);
+               });
+
+               if ((cliFd = accept(fd, NULL, NULL)) == -1) {
+                       close(fd);
+                       Display::printError();
+                       Display::printResult(Display::FAIL, "useSocket positive");
+                       return;
+               }
+               memset(buffer, 0, sizeof(buffer));
+
+               if((recvlen = recv(cliFd, buffer, sizeof(buffer), 0)) == -1)
+                       Display::printError();
+
+               if (recvlen > 0) {
+                       Display::printResult(Display::NONE, std::string(buffer));
+                       Display::printResult(Display::SUCCESS, "useSocket positive");
+               } else {
+                       Display::printResult(Display::FAIL, "useSocket positive");
+               }
+
+               client.join();
+
+               if (cliFd >= 0)
+                       close(cliFd);
+               if (fd >= 0)
+                       close(fd);
+               if ((lstat(sockaddr.c_str(), &st) == 0) && (unlink(sockaddr.c_str()) != 0))
+                       Display::printError();
+       }
 };
 
 GroupBuilder<IpcGroup> ipcGroup("Ipc");
index 750edfd1aecca22abe6fe8d5ceba17571459da46..96bf96205b9a897427c6b8e81f3e9a00cae467ef 100644 (file)
@@ -17,6 +17,7 @@
 
 #include <fcntl.h>
 #include <unistd.h>
+#include <wait.h>
 
 #include "group.h"
 
@@ -33,6 +34,7 @@ public:
        {
                Display::printResult(Display::NONE, "MAC group test start");
                changeMacPolicy();
+               makeSmackDeny();
        }
 
        virtual ~MacGroup()
@@ -99,6 +101,58 @@ public:
                        Display::printResult(Display::SUCCESS, "/etc/nether access");
                }
        }
+
+       void makeSmackDeny() {
+               pid_t pid;
+               std::string testFile("/tmp/audit-test");
+               std::string writeString("test");
+               std::string commandString("chsmack -a test /tmp/audit-test");
+
+               int fd = -1;
+               fd = creat(testFile.c_str(), 0644);
+               if (fd < 0) {
+                       Display::printError();
+                       Display::printResult(Display::FAIL, "makeSmackDeny");
+                       return;
+               }
+
+               if (system(commandString.c_str()) == -1) {
+                       Display::printResult(Display::FAIL, "makeSmackDeny");
+                       goto removeTestFile;
+               }
+
+
+               pid = fork();
+               if (pid == -1) {
+                       Display::printResult(Display::FAIL, "makeSmackDeny");
+                       Display::printError();
+                       goto removeTestFile;
+               }
+
+               if (pid == 0) {
+                       std::string command("cat /tmp/audit-test");
+                       if (setuid(5001) == -1) {
+                               Display::printError();
+                               Display::printResult(Display::FAIL, "makeSmackDeny");
+                               exit(0);
+                       }
+
+                       if (system(command.c_str()) == -1) {
+                               Display::printError();
+                               Display::printResult(Display::FAIL, "makeSmackDeny");
+                       } else {
+                               Display::printResult(Display::SUCCESS, "makeSmackDeny");
+                       }
+
+                       exit(0);
+               } else {
+                       wait(NULL);
+               }
+
+       removeTestFile:
+               close(fd);
+               unlink(testFile.c_str());
+       }
 };
 
 GroupBuilder<MacGroup> macGroup("Mac");
index 16b3a496fec0c54bcf95fd881fab4adaff67effb..2e7c1c1b6150f3136289e3d5a1a1578f4fe3c08c 100644 (file)
@@ -54,6 +54,8 @@ public:
                debugging();
                container();
                executeCommands();
+               callExecve();
+               makeKillSignal();
        }
 
        virtual ~SystemGroup()
@@ -331,6 +333,68 @@ public:
 
                Display::printResult(ret, "execute /sbin/findfs");
        }
+
+       static int childExecve(void *arg) {
+               int *ret = reinterpret_cast<int *>(arg);
+               std::string fileName("/usr/bin/pwd");
+               char *argv[] = {NULL};
+               char *envp[] = {NULL};
+               if (execve(fileName.c_str(), argv, envp) == -1) {
+                       Display::printError();
+                       *ret = -1;
+               }
+               return 0;
+       }
+
+       void callExecve() {
+               pid_t pid;
+               const int stacksize = 1024 * 1024;
+               void *childStack = malloc(stacksize);
+               int ret = 1;
+               if (childStack == NULL) {
+                       Display::printError();
+                       Display::printResult(Display::FAIL, "callExecve positive");
+                       return;
+               }
+
+               if ((pid = clone(childExecve, (void *)((char *)childStack+ stacksize), SIGCHLD | CLONE_VM, &ret))< 0) {
+                       Display::printError();
+                       Display::printResult(Display::FAIL, "callExecve positive");
+                       free(childStack);
+                       return;
+               }
+
+               wait(NULL);
+               free(childStack);
+
+               if (ret == 1)
+                       Display::printResult(Display::SUCCESS, "callExecve positive");
+               else
+                       Display::printResult(Display::FAIL, "callExecve positive");
+       }
+
+       void makeKillSignal() {
+               pid_t pid;
+
+               pid = fork();
+               if (pid == -1) {
+                       Display::printResult(Display::FAIL, "kill signal positive");
+                       return;
+               }
+
+               if (pid == 0) {
+                       sleep(10);
+                       exit(0);
+               } else {
+                       if (kill(pid, SIGKILL) == -1) {
+                               Display::printError();
+                               Display::printResult(Display::FAIL, "kill signal positive");
+                       } else {
+                               Display::printResult(Display::SUCCESS, "kill signal positive");
+                       }
+                       wait(NULL);
+               }
+       }
 };
 
 GroupBuilder<SystemGroup> systemGroup("System");