1*43a90889SApple OSS Distributions /* compile: xcrun -sdk macosx.internal clang -ldarwintest -o iopolicy iopolicy.c -g -Weverything */
2*43a90889SApple OSS Distributions
3*43a90889SApple OSS Distributions #include <darwintest.h>
4*43a90889SApple OSS Distributions #include <darwintest_utils.h>
5*43a90889SApple OSS Distributions #include <darwintest_multiprocess.h>
6*43a90889SApple OSS Distributions #include <errno.h>
7*43a90889SApple OSS Distributions #include <stdint.h>
8*43a90889SApple OSS Distributions #include <stdlib.h>
9*43a90889SApple OSS Distributions #include <unistd.h>
10*43a90889SApple OSS Distributions #include <sys/attr.h>
11*43a90889SApple OSS Distributions #include <sys/event.h>
12*43a90889SApple OSS Distributions #include <sys/resource.h>
13*43a90889SApple OSS Distributions
14*43a90889SApple OSS Distributions #ifndef IOPOL_TYPE_VFS_DISALLOW_RW_FOR_O_EVTONLY
15*43a90889SApple OSS Distributions #define IOPOL_TYPE_VFS_DISALLOW_RW_FOR_O_EVTONLY 10
16*43a90889SApple OSS Distributions #endif
17*43a90889SApple OSS Distributions
18*43a90889SApple OSS Distributions #ifndef IOPOL_VFS_DISALLOW_RW_FOR_O_EVTONLY_OFF
19*43a90889SApple OSS Distributions #define IOPOL_VFS_DISALLOW_RW_FOR_O_EVTONLY_OFF 0
20*43a90889SApple OSS Distributions #endif
21*43a90889SApple OSS Distributions
22*43a90889SApple OSS Distributions #ifndef IOPOL_VFS_DISALLOW_RW_FOR_O_EVTONLY_ON
23*43a90889SApple OSS Distributions #define IOPOL_VFS_DISALLOW_RW_FOR_O_EVTONLY_ON 1
24*43a90889SApple OSS Distributions #endif
25*43a90889SApple OSS Distributions
26*43a90889SApple OSS Distributions T_GLOBAL_META(
27*43a90889SApple OSS Distributions T_META_NAMESPACE("xnu.vfs.iopolicy"),
28*43a90889SApple OSS Distributions T_META_RADAR_COMPONENT_NAME("xnu"),
29*43a90889SApple OSS Distributions T_META_RADAR_COMPONENT_VERSION("vfs"),
30*43a90889SApple OSS Distributions T_META_CHECK_LEAKS(false),
31*43a90889SApple OSS Distributions T_META_TAG_VM_PREFERRED);
32*43a90889SApple OSS Distributions
33*43a90889SApple OSS Distributions #define TEST_FILE "testfile"
34*43a90889SApple OSS Distributions
35*43a90889SApple OSS Distributions static char g_testfile[MAXPATHLEN];
36*43a90889SApple OSS Distributions static char g_testdata[1024];
37*43a90889SApple OSS Distributions
38*43a90889SApple OSS Distributions static void
exit_cleanup(void)39*43a90889SApple OSS Distributions exit_cleanup(void)
40*43a90889SApple OSS Distributions {
41*43a90889SApple OSS Distributions (void)remove(g_testfile);
42*43a90889SApple OSS Distributions }
43*43a90889SApple OSS Distributions
44*43a90889SApple OSS Distributions T_DECL(iopol_type_vfs_disallow_rw_for_o_evtonly,
45*43a90889SApple OSS Distributions "test IOPOL_TYPE_VFS_DISALLOW_RW_FOR_O_EVTONLY policy")
46*43a90889SApple OSS Distributions {
47*43a90889SApple OSS Distributions char attrbuf[256];
48*43a90889SApple OSS Distributions struct attrlist attrlist;
49*43a90889SApple OSS Distributions struct kevent vnode_kevent;
50*43a90889SApple OSS Distributions struct timespec kevent_timeout;
51*43a90889SApple OSS Distributions const char *tmpdir = dt_tmpdir();
52*43a90889SApple OSS Distributions void *mapped;
53*43a90889SApple OSS Distributions int err, fd, kq;
54*43a90889SApple OSS Distributions
55*43a90889SApple OSS Distributions T_SETUPBEGIN;
56*43a90889SApple OSS Distributions
57*43a90889SApple OSS Distributions atexit(exit_cleanup);
58*43a90889SApple OSS Distributions
59*43a90889SApple OSS Distributions T_ASSERT_POSIX_ZERO(chdir(tmpdir),
60*43a90889SApple OSS Distributions "Setup: changing to tmpdir: %s", tmpdir);
61*43a90889SApple OSS Distributions
62*43a90889SApple OSS Distributions snprintf(g_testfile, MAXPATHLEN, "%s/%s", tmpdir, TEST_FILE);
63*43a90889SApple OSS Distributions
64*43a90889SApple OSS Distributions T_WITH_ERRNO;
65*43a90889SApple OSS Distributions fd = open(g_testfile, O_CREAT | O_RDWR, 0666);
66*43a90889SApple OSS Distributions T_ASSERT_NE(fd, -1, "Create test file: %s", g_testfile);
67*43a90889SApple OSS Distributions
68*43a90889SApple OSS Distributions T_WITH_ERRNO;
69*43a90889SApple OSS Distributions err = (int)write(fd, g_testfile, sizeof(g_testfile));
70*43a90889SApple OSS Distributions T_ASSERT_NE(err, -1, "Write: %s", g_testfile);
71*43a90889SApple OSS Distributions
72*43a90889SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(close(fd), "Close test file: %s", g_testfile);
73*43a90889SApple OSS Distributions
74*43a90889SApple OSS Distributions kq = kqueue();
75*43a90889SApple OSS Distributions T_ASSERT_NE(kq, -1, "Create kqueue");
76*43a90889SApple OSS Distributions
77*43a90889SApple OSS Distributions T_SETUPEND;
78*43a90889SApple OSS Distributions
79*43a90889SApple OSS Distributions T_WITH_ERRNO;
80*43a90889SApple OSS Distributions err = setiopolicy_np(IOPOL_TYPE_VFS_DISALLOW_RW_FOR_O_EVTONLY,
81*43a90889SApple OSS Distributions IOPOL_SCOPE_THREAD, IOPOL_VFS_DISALLOW_RW_FOR_O_EVTONLY_ON);
82*43a90889SApple OSS Distributions T_ASSERT_TRUE((err == -1) && (errno == EINVAL),
83*43a90889SApple OSS Distributions "setiopolicy_np(IOPOL_TYPE_VFS_DISALLOW_RW_FOR_O_EVTONLY, IOPOL_SCOPE_THREAD, 1)");
84*43a90889SApple OSS Distributions
85*43a90889SApple OSS Distributions T_WITH_ERRNO;
86*43a90889SApple OSS Distributions err = setiopolicy_np(IOPOL_TYPE_VFS_DISALLOW_RW_FOR_O_EVTONLY,
87*43a90889SApple OSS Distributions IOPOL_SCOPE_PROCESS, IOPOL_VFS_DISALLOW_RW_FOR_O_EVTONLY_ON);
88*43a90889SApple OSS Distributions T_ASSERT_NE(err, -1,
89*43a90889SApple OSS Distributions "setiopolicy_np(IOPOL_TYPE_VFS_DISALLOW_RW_FOR_O_EVTONLY, IOPOL_SCOPE_PROCESS, 1)");
90*43a90889SApple OSS Distributions
91*43a90889SApple OSS Distributions T_WITH_ERRNO;
92*43a90889SApple OSS Distributions err = getiopolicy_np(IOPOL_TYPE_VFS_DISALLOW_RW_FOR_O_EVTONLY,
93*43a90889SApple OSS Distributions IOPOL_SCOPE_PROCESS);
94*43a90889SApple OSS Distributions T_ASSERT_EQ(err, IOPOL_VFS_DISALLOW_RW_FOR_O_EVTONLY_ON,
95*43a90889SApple OSS Distributions "getiopolicy_np(IOPOL_TYPE_VFS_DISALLOW_RW_FOR_O_EVTONLY, IOPOL_SCOPE_PROCESS)");
96*43a90889SApple OSS Distributions
97*43a90889SApple OSS Distributions T_WITH_ERRNO;
98*43a90889SApple OSS Distributions err = setiopolicy_np(IOPOL_TYPE_VFS_DISALLOW_RW_FOR_O_EVTONLY,
99*43a90889SApple OSS Distributions IOPOL_SCOPE_PROCESS, IOPOL_VFS_DISALLOW_RW_FOR_O_EVTONLY_OFF);
100*43a90889SApple OSS Distributions T_ASSERT_TRUE((err == -1) && (errno == EINVAL),
101*43a90889SApple OSS Distributions "setiopolicy_np(IOPOL_TYPE_VFS_DISALLOW_RW_FOR_O_EVTONLY, IOPOL_SCOPE_PROCESS, 0)");
102*43a90889SApple OSS Distributions
103*43a90889SApple OSS Distributions T_WITH_ERRNO;
104*43a90889SApple OSS Distributions fd = open(g_testfile, O_RDWR | O_EVTONLY, 0666);
105*43a90889SApple OSS Distributions T_ASSERT_NE(fd, -1, "Open test fi1e in 'O_RDW|O_EVTONLY': %s", g_testfile);
106*43a90889SApple OSS Distributions
107*43a90889SApple OSS Distributions T_WITH_ERRNO;
108*43a90889SApple OSS Distributions err = (int)write(fd, g_testdata, sizeof(g_testdata));
109*43a90889SApple OSS Distributions T_ASSERT_TRUE((err == -1) && (errno == EBADF),
110*43a90889SApple OSS Distributions "Trying to write: %s", g_testfile);
111*43a90889SApple OSS Distributions
112*43a90889SApple OSS Distributions T_WITH_ERRNO;
113*43a90889SApple OSS Distributions err = (int)read(fd, g_testdata, sizeof(g_testdata));
114*43a90889SApple OSS Distributions T_ASSERT_TRUE((err == -1) && (errno == EBADF),
115*43a90889SApple OSS Distributions "Trying to read: %s", g_testfile);
116*43a90889SApple OSS Distributions
117*43a90889SApple OSS Distributions T_WITH_ERRNO;
118*43a90889SApple OSS Distributions mapped = mmap(NULL, sizeof(g_testdata), PROT_READ | PROT_WRITE, MAP_SHARED,
119*43a90889SApple OSS Distributions fd, 0);
120*43a90889SApple OSS Distributions T_ASSERT_TRUE((err == -1) && (errno == EACCES),
121*43a90889SApple OSS Distributions "Trying to mmaped read/write: %s", g_testfile);
122*43a90889SApple OSS Distributions
123*43a90889SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(close(fd), "Close test file: %s", g_testfile);
124*43a90889SApple OSS Distributions
125*43a90889SApple OSS Distributions T_WITH_ERRNO;
126*43a90889SApple OSS Distributions fd = open(g_testfile, O_EVTONLY, 0666);
127*43a90889SApple OSS Distributions T_ASSERT_NE(fd, -1, "Open test fi1e in 'O_EVTONLY': %s", g_testfile);
128*43a90889SApple OSS Distributions
129*43a90889SApple OSS Distributions T_WITH_ERRNO;
130*43a90889SApple OSS Distributions err = (int)read(fd, g_testdata, sizeof(g_testdata));
131*43a90889SApple OSS Distributions T_ASSERT_TRUE((err == -1) && (errno == EBADF),
132*43a90889SApple OSS Distributions "Trying to read: %s", g_testfile);
133*43a90889SApple OSS Distributions
134*43a90889SApple OSS Distributions T_WITH_ERRNO;
135*43a90889SApple OSS Distributions memset(&attrlist, 0, sizeof(attrlist));
136*43a90889SApple OSS Distributions attrlist.bitmapcount = ATTR_BIT_MAP_COUNT;
137*43a90889SApple OSS Distributions attrlist.commonattr = (ATTR_CMN_OBJTYPE | ATTR_CMN_OBJID | ATTR_CMN_MODTIME);
138*43a90889SApple OSS Distributions
139*43a90889SApple OSS Distributions err = fgetattrlist(fd, &attrlist, &attrbuf, sizeof(attrbuf), 0);
140*43a90889SApple OSS Distributions T_ASSERT_NE(err, -1, "Perform getattrlist: %s", g_testfile);
141*43a90889SApple OSS Distributions
142*43a90889SApple OSS Distributions kevent_timeout.tv_sec = kevent_timeout.tv_nsec = 0;
143*43a90889SApple OSS Distributions EV_SET(&vnode_kevent, fd, EVFILT_VNODE, (EV_ADD | EV_ENABLE | EV_CLEAR),
144*43a90889SApple OSS Distributions NOTE_WRITE, 0, (void *)g_testfile);
145*43a90889SApple OSS Distributions err = kevent(kq, &vnode_kevent, 1, NULL, 0, &kevent_timeout);
146*43a90889SApple OSS Distributions T_ASSERT_GE(err, 0, "Register vnode event on kq: %d", kq);
147*43a90889SApple OSS Distributions
148*43a90889SApple OSS Distributions kevent_timeout.tv_sec = 2;
149*43a90889SApple OSS Distributions kevent_timeout.tv_nsec = 0;
150*43a90889SApple OSS Distributions EV_SET(&vnode_kevent, fd, EVFILT_VNODE, EV_CLEAR, 0, 0, 0);
151*43a90889SApple OSS Distributions
152*43a90889SApple OSS Distributions err = kevent(kq, NULL, 0, &vnode_kevent, 1, &kevent_timeout);
153*43a90889SApple OSS Distributions T_ASSERT_NE(err, -1, "Listen for vnode event on kq: %d", kq);
154*43a90889SApple OSS Distributions
155*43a90889SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(close(fd), "Close test file: %s", g_testfile);
156*43a90889SApple OSS Distributions }
157