1 // Tests for POSIX message queue functionality. 2 3 #include <time.h> 4 #include <fcntl.h> 5 #include <sys/stat.h> 6 #include <mqueue.h> 7 8 #include <string> 9 10 #include "capsicum.h" 11 #include "syscalls.h" 12 #include "capsicum-test.h" 13 14 // Run a test case in a forked process, possibly cleaning up a 15 // message after completion 16 #define FORK_TEST_ON_MQ(test_case_name, test_name, test_mq) \ 17 static void test_case_name##_##test_name##_ForkTest(); \ 18 TEST(test_case_name, test_name ## Forked) { \ 19 _RUN_FORKED_FN(test_case_name##_##test_name##_ForkTest, \ 20 #test_case_name, #test_name); \ 21 const char *mqname = test_mq; \ 22 if (mqname) mq_unlink_(mqname); \ 23 } \ 24 static void test_case_name##_##test_name##_ForkTest() 25 26 static bool invoked; 27 void seen_it_done_it(int) { 28 invoked = true; 29 } 30 31 FORK_TEST_ON_MQ(PosixMqueue, CapModeIfMqOpenAvailable, "/cap_mq") { 32 int mq = mq_open_("/cap_mq", O_RDWR|O_CREAT, 0644, NULL); 33 // On FreeBSD, turn on message queue support with: 34 // - 'kldload mqueuefs' 35 // - 'options P1003_1B_MQUEUE' in kernel build config. 36 if (mq < 0 && errno == ENOSYS) { 37 GTEST_SKIP() << "mq_open -> -ENOSYS"; 38 } 39 EXPECT_OK(mq); 40 cap_rights_t r_read; 41 cap_rights_init(&r_read, CAP_READ); 42 cap_rights_t r_write; 43 cap_rights_init(&r_write, CAP_WRITE); 44 cap_rights_t r_poll; 45 cap_rights_init(&r_poll, CAP_EVENT); 46 47 int cap_read_mq = dup(mq); 48 EXPECT_OK(cap_read_mq); 49 EXPECT_OK(cap_rights_limit(cap_read_mq, &r_read)); 50 int cap_write_mq = dup(mq); 51 EXPECT_OK(cap_write_mq); 52 EXPECT_OK(cap_rights_limit(cap_write_mq, &r_write)); 53 int cap_poll_mq = dup(mq); 54 EXPECT_OK(cap_poll_mq); 55 EXPECT_OK(cap_rights_limit(cap_poll_mq, &r_poll)); 56 EXPECT_OK(mq_close_(mq)); 57 58 signal(SIGUSR2, seen_it_done_it); 59 60 EXPECT_OK(cap_enter()); // Enter capability mode 61 62 // Can no longer access the message queue via the POSIX IPC namespace. 63 EXPECT_CAPMODE(mq_open_("/cap_mw", O_RDWR|O_CREAT, 0644, NULL)); 64 65 struct sigevent se; 66 se.sigev_notify = SIGEV_SIGNAL; 67 se.sigev_signo = SIGUSR2; 68 EXPECT_OK(mq_notify_(cap_poll_mq, &se)); 69 EXPECT_NOTCAPABLE(mq_notify_(cap_read_mq, &se)); 70 EXPECT_NOTCAPABLE(mq_notify_(cap_write_mq, &se)); 71 72 const unsigned int kPriority = 10; 73 const char* message = "xyzzy"; 74 struct timespec ts; 75 ts.tv_sec = 1; 76 ts.tv_nsec = 0; 77 EXPECT_OK(mq_timedsend_(cap_write_mq, message, strlen(message) + 1, kPriority, &ts)); 78 EXPECT_NOTCAPABLE(mq_timedsend_(cap_read_mq, message, strlen(message) + 1, kPriority, &ts)); 79 80 sleep(1); // Give the notification a chance to arrive. 81 EXPECT_TRUE(invoked); 82 83 struct mq_attr mqa; 84 EXPECT_OK(mq_getattr_(cap_poll_mq, &mqa)); 85 EXPECT_OK(mq_setattr_(cap_poll_mq, &mqa, NULL)); 86 EXPECT_NOTCAPABLE(mq_getattr_(cap_write_mq, &mqa)); 87 88 char* buffer = (char *)malloc(mqa.mq_msgsize); 89 unsigned int priority; 90 EXPECT_NOTCAPABLE(mq_timedreceive_(cap_write_mq, buffer, mqa.mq_msgsize, &priority, &ts)); 91 EXPECT_OK(mq_timedreceive_(cap_read_mq, buffer, mqa.mq_msgsize, &priority, &ts)); 92 EXPECT_EQ(std::string(message), std::string(buffer)); 93 EXPECT_EQ(kPriority, priority); 94 free(buffer); 95 96 close(cap_read_mq); 97 close(cap_write_mq); 98 close(cap_poll_mq); 99 } 100