xref: /xnu-10002.1.13/tests/atm_diagnostic_flag.c (revision 1031c584a5e37aff177559b9f69dbd3c8c3fd30a)
1*1031c584SApple OSS Distributions #include <darwintest.h>
2*1031c584SApple OSS Distributions 
3*1031c584SApple OSS Distributions #include <mach/mach_error.h>
4*1031c584SApple OSS Distributions #include <mach/mach_host.h>
5*1031c584SApple OSS Distributions 
6*1031c584SApple OSS Distributions #include "drop_priv.h"
7*1031c584SApple OSS Distributions 
8*1031c584SApple OSS Distributions T_GLOBAL_META(T_META_NAMESPACE("xnu.debugging"));
9*1031c584SApple OSS Distributions 
10*1031c584SApple OSS Distributions /*
11*1031c584SApple OSS Distributions  * The low 8 bits may be in use, so modify one
12*1031c584SApple OSS Distributions  * of the upper 8 bits to ensure round-tripping.
13*1031c584SApple OSS Distributions  */
14*1031c584SApple OSS Distributions #define LIBTRACE_PRIVATE_DATA  0x01000000
15*1031c584SApple OSS Distributions 
16*1031c584SApple OSS Distributions static bool _needs_reset;
17*1031c584SApple OSS Distributions static uint32_t _original;
18*1031c584SApple OSS Distributions 
19*1031c584SApple OSS Distributions static uint32_t
_save_atm_diagnostic_flag(void)20*1031c584SApple OSS Distributions _save_atm_diagnostic_flag(void)
21*1031c584SApple OSS Distributions {
22*1031c584SApple OSS Distributions 	kern_return_t kr;
23*1031c584SApple OSS Distributions 	kr = host_get_atm_diagnostic_flag(mach_host_self(), &_original);
24*1031c584SApple OSS Distributions 	T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "host_get_atm_diagnostic_flag()");
25*1031c584SApple OSS Distributions 	T_LOG("Original ATM diagnostic flag: 0x%08x", _original);
26*1031c584SApple OSS Distributions 	return _original;
27*1031c584SApple OSS Distributions }
28*1031c584SApple OSS Distributions 
29*1031c584SApple OSS Distributions static kern_return_t
_mutate_atm_diagnostic_flag(uint32_t v)30*1031c584SApple OSS Distributions _mutate_atm_diagnostic_flag(uint32_t v)
31*1031c584SApple OSS Distributions {
32*1031c584SApple OSS Distributions 	T_LOG("Try to set ATM diagnostic flag to: 0x%08x", v);
33*1031c584SApple OSS Distributions 	kern_return_t kr = host_set_atm_diagnostic_flag(mach_host_self(), v);
34*1031c584SApple OSS Distributions 	if (kr == KERN_SUCCESS) {
35*1031c584SApple OSS Distributions 		_needs_reset = true;
36*1031c584SApple OSS Distributions 	}
37*1031c584SApple OSS Distributions 	return kr;
38*1031c584SApple OSS Distributions }
39*1031c584SApple OSS Distributions 
40*1031c584SApple OSS Distributions static void
_reset_atm_diagnostic_flag(void)41*1031c584SApple OSS Distributions _reset_atm_diagnostic_flag(void)
42*1031c584SApple OSS Distributions {
43*1031c584SApple OSS Distributions 	if (!_needs_reset) {
44*1031c584SApple OSS Distributions 		return;
45*1031c584SApple OSS Distributions 	}
46*1031c584SApple OSS Distributions 	T_LOG("Reset ATM diagnostic flag to: 0x%08x", _original);
47*1031c584SApple OSS Distributions 	kern_return_t kr;
48*1031c584SApple OSS Distributions 	kr = host_set_atm_diagnostic_flag(mach_host_self(), _original);
49*1031c584SApple OSS Distributions 	if (kr != KERN_SUCCESS) {
50*1031c584SApple OSS Distributions 		T_ASSERT_FAIL("host_set_atm_diagnostic_flag() failed: %s",
51*1031c584SApple OSS Distributions 		    mach_error_string(kr));
52*1031c584SApple OSS Distributions 	}
53*1031c584SApple OSS Distributions }
54*1031c584SApple OSS Distributions 
55*1031c584SApple OSS Distributions static void
_toggle_atm_diagnostic_flag(void)56*1031c584SApple OSS Distributions _toggle_atm_diagnostic_flag(void)
57*1031c584SApple OSS Distributions {
58*1031c584SApple OSS Distributions 	T_ATEND(_reset_atm_diagnostic_flag);
59*1031c584SApple OSS Distributions 	uint32_t f = _save_atm_diagnostic_flag();
60*1031c584SApple OSS Distributions 	f ^= LIBTRACE_PRIVATE_DATA;
61*1031c584SApple OSS Distributions 	kern_return_t kr = _mutate_atm_diagnostic_flag(f);
62*1031c584SApple OSS Distributions 	if (kr == KERN_NOT_SUPPORTED) {
63*1031c584SApple OSS Distributions 		T_SKIP("Seems ATM is disabled on this platform. "
64*1031c584SApple OSS Distributions 		    "Ignoring host_set_atm_diagnostic_flag functionality. "
65*1031c584SApple OSS Distributions 		    "Bailing gracefully.");
66*1031c584SApple OSS Distributions 	}
67*1031c584SApple OSS Distributions 	T_EXPECT_MACH_ERROR(KERN_NO_ACCESS, kr,
68*1031c584SApple OSS Distributions 	    "Deny change to atm_diagnostic_flag");
69*1031c584SApple OSS Distributions }
70*1031c584SApple OSS Distributions 
71*1031c584SApple OSS Distributions T_DECL(atm_diagnostic_flag_unentitled_privileged,
72*1031c584SApple OSS Distributions     "expect to fail to set the atm_diagnostic_flag (unentitled, privileged)",
73*1031c584SApple OSS Distributions     T_META_ASROOT(true))
74*1031c584SApple OSS Distributions {
75*1031c584SApple OSS Distributions 	_toggle_atm_diagnostic_flag();
76*1031c584SApple OSS Distributions }
77*1031c584SApple OSS Distributions 
78*1031c584SApple OSS Distributions T_DECL(atm_diagnostic_flag_unentitled_unprivileged,
79*1031c584SApple OSS Distributions     "expect to fail to set the atm_diagnostic_flag (unentitled, unprivileged)",
80*1031c584SApple OSS Distributions     T_META_ASROOT(false))
81*1031c584SApple OSS Distributions {
82*1031c584SApple OSS Distributions 	if (running_as_root()) {
83*1031c584SApple OSS Distributions 		drop_priv();
84*1031c584SApple OSS Distributions 	}
85*1031c584SApple OSS Distributions 	_toggle_atm_diagnostic_flag();
86*1031c584SApple OSS Distributions }
87