xref: /xnu-11417.101.15/tests/rm/pthread_setugid.c (revision e3723e1f17661b24996789d8afc084c0c3303b26)
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_ENABLED(false), /* this test takes 10+minutes on some HW */
37     T_META_TAG_VM_PREFERRED)
38 {
39 	size_t batch = 1024;
40 	size_t count = roundup(0x0FFFFFFFUL + 10, batch);
41 
42 	if (dt_ncpu() < 10) {
43 		T_SKIP("too slow of a test");
44 	}
45 
46 	dispatch_apply(count / batch, DISPATCH_APPLY_AUTO, ^(size_t n) {
47 		int rc;
48 
49 		for (int i = 0; i < batch; i++) {
50 		        rc = pthread_setugid_np(501, 501);
51 		        assert(rc == 0);
52 		        rc = pthread_setugid_np(KAUTH_UID_NONE, KAUTH_UID_NONE);
53 		        assert(rc == 0);
54 		}
55 		if ((n * batch) % (1024 * batch) == 0) {
56 		        T_LOG("%.2f\n", n * batch * 100. / count);
57 		}
58 	});
59 
60 	T_PASS("the kernel shouldn't panic due to a leak");
61 }
62