xref: /xnu-11215/tests/rm/pthread_setugid.c (revision 4f1223e8)
1 #include <darwintest.h>
2 #include <darwintest_utils.h>
3 #include <dispatch/dispatch.h>
4 #include <sys/kauth.h>
5 #include <sys/param.h>
6 #include <sys/unistd.h>
7 
8 T_GLOBAL_META(T_META_NAMESPACE("xnu.rm"),
9     T_META_RADAR_COMPONENT_NAME("xnu"),
10     T_META_RADAR_COMPONENT_VERSION("rm"),
11     T_META_OWNER("phabouzit")
12     );
13 
14 T_DECL(pthread_setugid_np_81523076,
15     "Make sure pthread_setugid_np() isn't sticky to workqueue threads",
16     T_META_CHECK_LEAKS(false),
17     T_META_ASROOT(true),
18     T_META_TAG_VM_PREFERRED)
19 {
20 	int rc;
21 
22 	rc = pthread_setugid_np(501, getgid());
23 	T_ASSERT_POSIX_SUCCESS(rc, "pthread_setugid_np(501, getgid())");
24 
25 	dispatch_async(dispatch_get_global_queue(0, 0), ^{
26 		T_ASSERT_EQ(getuid(), 0, "getuid should still be 0");
27 		T_END;
28 	});
29 	pause();
30 }
31 
32 T_DECL(pthread_setugid_np_124671138,
33     "Make sure pthread_setugid_np() isn't sticky to workqueue threads",
34     T_META_CHECK_LEAKS(false),
35     T_META_ASROOT(true),
36     T_META_TAG_VM_PREFERRED)
37 {
38 	size_t batch = 1024;
39 	size_t count = roundup(0x0FFFFFFFUL + 10, batch);
40 
41 	if (dt_ncpu() < 10) {
42 		T_SKIP("too slow of a test");
43 	}
44 
45 	dispatch_apply(count / batch, DISPATCH_APPLY_AUTO, ^(size_t n) {
46 		int rc;
47 
48 		for (int i = 0; i < batch; i++) {
49 		        rc = pthread_setugid_np(501, 501);
50 		        assert(rc == 0);
51 		        rc = pthread_setugid_np(KAUTH_UID_NONE, KAUTH_UID_NONE);
52 		        assert(rc == 0);
53 		}
54 		if ((n * batch) % (1024 * batch) == 0) {
55 		        T_LOG("%.2f\n", n * batch * 100. / count);
56 		}
57 	});
58 
59 	T_PASS("the kernel shouldn't panic due to a leak");
60 }
61