xref: /freebsd/contrib/capsicum-test/mqueue.cc (revision 52c2bb75163559a6e2866ad374a7de67a4ea1273)
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, CapMode, "/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     TEST_SKIPPED("mq_open -> -ENOSYS");
38     return;
39   }
40   EXPECT_OK(mq);
41   cap_rights_t r_read;
42   cap_rights_init(&r_read, CAP_READ);
43   cap_rights_t r_write;
44   cap_rights_init(&r_write, CAP_WRITE);
45   cap_rights_t r_poll;
46   cap_rights_init(&r_poll, CAP_EVENT);
47 
48   int cap_read_mq = dup(mq);
49   EXPECT_OK(cap_read_mq);
50   EXPECT_OK(cap_rights_limit(cap_read_mq, &r_read));
51   int cap_write_mq = dup(mq);
52   EXPECT_OK(cap_write_mq);
53   EXPECT_OK(cap_rights_limit(cap_write_mq, &r_write));
54   int cap_poll_mq = dup(mq);
55   EXPECT_OK(cap_poll_mq);
56   EXPECT_OK(cap_rights_limit(cap_poll_mq, &r_poll));
57   EXPECT_OK(mq_close_(mq));
58 
59   signal(SIGUSR2, seen_it_done_it);
60 
61   EXPECT_OK(cap_enter());  // Enter capability mode
62 
63   // Can no longer access the message queue via the POSIX IPC namespace.
64   EXPECT_CAPMODE(mq_open_("/cap_mw", O_RDWR|O_CREAT, 0644, NULL));
65 
66   struct sigevent se;
67   se.sigev_notify = SIGEV_SIGNAL;
68   se.sigev_signo = SIGUSR2;
69   EXPECT_OK(mq_notify_(cap_poll_mq, &se));
70   EXPECT_NOTCAPABLE(mq_notify_(cap_read_mq, &se));
71   EXPECT_NOTCAPABLE(mq_notify_(cap_write_mq, &se));
72 
73   const unsigned int kPriority = 10;
74   const char* message = "xyzzy";
75   struct timespec ts;
76   ts.tv_sec = 1;
77   ts.tv_nsec = 0;
78   EXPECT_OK(mq_timedsend_(cap_write_mq, message, strlen(message) + 1, kPriority, &ts));
79   EXPECT_NOTCAPABLE(mq_timedsend_(cap_read_mq, message, strlen(message) + 1, kPriority, &ts));
80 
81   sleep(1);  // Give the notification a chance to arrive.
82   EXPECT_TRUE(invoked);
83 
84   struct mq_attr mqa;
85   EXPECT_OK(mq_getattr_(cap_poll_mq, &mqa));
86   EXPECT_OK(mq_setattr_(cap_poll_mq, &mqa, NULL));
87   EXPECT_NOTCAPABLE(mq_getattr_(cap_write_mq, &mqa));
88 
89   char* buffer = (char *)malloc(mqa.mq_msgsize);
90   unsigned int priority;
91   EXPECT_NOTCAPABLE(mq_timedreceive_(cap_write_mq, buffer, mqa.mq_msgsize, &priority, &ts));
92   EXPECT_OK(mq_timedreceive_(cap_read_mq, buffer, mqa.mq_msgsize, &priority, &ts));
93   EXPECT_EQ(std::string(message), std::string(buffer));
94   EXPECT_EQ(kPriority, priority);
95   free(buffer);
96 
97   close(cap_read_mq);
98   close(cap_write_mq);
99   close(cap_poll_mq);
100 }
101