1*8d741a5dSApple OSS Distributions #include <darwintest.h> 2*8d741a5dSApple OSS Distributions #include <mach/mach.h> 3*8d741a5dSApple OSS Distributions #include <stdlib.h> 4*8d741a5dSApple OSS Distributions #include <stdio.h> 5*8d741a5dSApple OSS Distributions 6*8d741a5dSApple OSS Distributions T_GLOBAL_META(T_META_RUN_CONCURRENTLY(true), 7*8d741a5dSApple OSS Distributions T_META_NAMESPACE("xnu.ipc"), 8*8d741a5dSApple OSS Distributions T_META_RADAR_COMPONENT_NAME("xnu"), 9*8d741a5dSApple OSS Distributions T_META_RADAR_COMPONENT_VERSION("IPC")); 10*8d741a5dSApple OSS Distributions 11*8d741a5dSApple OSS Distributions T_DECL(mach_port_mod_refs, "mach_port_mod_refs", T_META_TAG_VM_PREFERRED){ 12*8d741a5dSApple OSS Distributions mach_port_t port_set; 13*8d741a5dSApple OSS Distributions mach_port_t port; 14*8d741a5dSApple OSS Distributions task_exc_guard_behavior_t old, new; 15*8d741a5dSApple OSS Distributions int ret; 16*8d741a5dSApple OSS Distributions 17*8d741a5dSApple OSS Distributions ret = mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_PORT_SET, &port_set); 18*8d741a5dSApple OSS Distributions T_ASSERT_MACH_SUCCESS(ret, "mach_port_allocate MACH_PORT_RIGHT_PORT_SET"); 19*8d741a5dSApple OSS Distributions 20*8d741a5dSApple OSS Distributions ret = mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &port); 21*8d741a5dSApple OSS Distributions T_ASSERT_MACH_SUCCESS(ret, "mach_port_allocate MACH_PORT_RIGHT_RECEIVE"); 22*8d741a5dSApple OSS Distributions 23*8d741a5dSApple OSS Distributions /* 24*8d741a5dSApple OSS Distributions * Disable [optional] Mach port guard exceptions to avoid fatal crash 25*8d741a5dSApple OSS Distributions */ 26*8d741a5dSApple OSS Distributions ret = task_get_exc_guard_behavior(mach_task_self(), &old); 27*8d741a5dSApple OSS Distributions T_ASSERT_MACH_SUCCESS(ret, "task_get_exc_guard_behavior"); 28*8d741a5dSApple OSS Distributions new = (old & ~TASK_EXC_GUARD_MP_DELIVER); 29*8d741a5dSApple OSS Distributions ret = task_set_exc_guard_behavior(mach_task_self(), new); 30*8d741a5dSApple OSS Distributions T_ASSERT_MACH_SUCCESS(ret, "task_set_exc_guard_behavior new"); 31*8d741a5dSApple OSS Distributions 32*8d741a5dSApple OSS Distributions /* 33*8d741a5dSApple OSS Distributions * Test all known variants of port rights on each type of port 34*8d741a5dSApple OSS Distributions */ 35*8d741a5dSApple OSS Distributions 36*8d741a5dSApple OSS Distributions /* can't subtract a send right if it doesn't exist */ 37*8d741a5dSApple OSS Distributions ret = mach_port_mod_refs(mach_task_self(), port, MACH_PORT_RIGHT_SEND, -1); 38*8d741a5dSApple OSS Distributions T_ASSERT_EQ(ret, KERN_INVALID_RIGHT, "mach_port_mod_refs SEND: -1 on a RECV right"); 39*8d741a5dSApple OSS Distributions 40*8d741a5dSApple OSS Distributions /* can't subtract a send once right if it doesn't exist */ 41*8d741a5dSApple OSS Distributions ret = mach_port_mod_refs(mach_task_self(), port, MACH_PORT_RIGHT_SEND_ONCE, -1); 42*8d741a5dSApple OSS Distributions T_ASSERT_EQ(ret, KERN_INVALID_RIGHT, "mach_port_mod_refs SEND_ONCE: -1 on a RECV right"); 43*8d741a5dSApple OSS Distributions 44*8d741a5dSApple OSS Distributions /* can't subtract a PORT SET right if it's not a port set */ 45*8d741a5dSApple OSS Distributions ret = mach_port_mod_refs(mach_task_self(), port, MACH_PORT_RIGHT_PORT_SET, -1); 46*8d741a5dSApple OSS Distributions T_ASSERT_EQ(ret, KERN_INVALID_RIGHT, "mach_port_mod_refs PORT_SET: -1 on a RECV right"); 47*8d741a5dSApple OSS Distributions 48*8d741a5dSApple OSS Distributions /* can't subtract a dead name right if it doesn't exist */ 49*8d741a5dSApple OSS Distributions ret = mach_port_mod_refs(mach_task_self(), port, MACH_PORT_RIGHT_DEAD_NAME, -1); 50*8d741a5dSApple OSS Distributions T_ASSERT_EQ(ret, KERN_INVALID_RIGHT, "mach_port_mod_refs DEAD_NAME: -1 on a RECV right"); 51*8d741a5dSApple OSS Distributions 52*8d741a5dSApple OSS Distributions /* can't subtract a LABELH right if it doesn't exist */ 53*8d741a5dSApple OSS Distributions ret = mach_port_mod_refs(mach_task_self(), port, MACH_PORT_RIGHT_LABELH, -1); 54*8d741a5dSApple OSS Distributions T_ASSERT_EQ(ret, KERN_INVALID_RIGHT, "mach_port_mod_refs LABELH: -1 on a RECV right"); 55*8d741a5dSApple OSS Distributions 56*8d741a5dSApple OSS Distributions /* can't subtract an invalid right-type */ 57*8d741a5dSApple OSS Distributions ret = mach_port_mod_refs(mach_task_self(), port, MACH_PORT_RIGHT_NUMBER, -1); 58*8d741a5dSApple OSS Distributions T_ASSERT_EQ(ret, KERN_INVALID_VALUE, "mach_port_mod_refs NUMBER: -1 on a RECV right"); 59*8d741a5dSApple OSS Distributions 60*8d741a5dSApple OSS Distributions /* can't subtract an invalid right-type */ 61*8d741a5dSApple OSS Distributions ret = mach_port_mod_refs(mach_task_self(), port, MACH_PORT_RIGHT_NUMBER + 1, -1); 62*8d741a5dSApple OSS Distributions T_ASSERT_EQ(ret, KERN_INVALID_VALUE, "mach_port_mod_refs NUMBER+1: -1 on a RECV right"); 63*8d741a5dSApple OSS Distributions 64*8d741a5dSApple OSS Distributions 65*8d741a5dSApple OSS Distributions /* can't subtract a send right if it doesn't exist */ 66*8d741a5dSApple OSS Distributions ret = mach_port_mod_refs(mach_task_self(), port_set, MACH_PORT_RIGHT_SEND, -1); 67*8d741a5dSApple OSS Distributions T_ASSERT_EQ(ret, KERN_INVALID_RIGHT, "mach_port_mod_refs SEND: -1 on a PORT_SET right"); 68*8d741a5dSApple OSS Distributions 69*8d741a5dSApple OSS Distributions /* can't subtract a send once right if it doesn't exist */ 70*8d741a5dSApple OSS Distributions ret = mach_port_mod_refs(mach_task_self(), port_set, MACH_PORT_RIGHT_SEND_ONCE, -1); 71*8d741a5dSApple OSS Distributions T_ASSERT_EQ(ret, KERN_INVALID_RIGHT, "mach_port_mod_refs SEND_ONCE: -1 on a PORT_SET right"); 72*8d741a5dSApple OSS Distributions 73*8d741a5dSApple OSS Distributions /* can't subtract a receive right if it's a port set */ 74*8d741a5dSApple OSS Distributions ret = mach_port_mod_refs(mach_task_self(), port_set, MACH_PORT_RIGHT_RECEIVE, -1); 75*8d741a5dSApple OSS Distributions T_ASSERT_EQ(ret, KERN_INVALID_RIGHT, "mach_port_mod_refs RECV: -1 on a PORT_SET right"); 76*8d741a5dSApple OSS Distributions 77*8d741a5dSApple OSS Distributions /* can't subtract a dead name right if it doesn't exist */ 78*8d741a5dSApple OSS Distributions ret = mach_port_mod_refs(mach_task_self(), port_set, MACH_PORT_RIGHT_DEAD_NAME, -1); 79*8d741a5dSApple OSS Distributions T_ASSERT_EQ(ret, KERN_INVALID_RIGHT, "mach_port_mod_refs DEAD_NAME: -1 on a PORT_SET right"); 80*8d741a5dSApple OSS Distributions 81*8d741a5dSApple OSS Distributions /* can't subtract a LABELH right if it doesn't exist */ 82*8d741a5dSApple OSS Distributions ret = mach_port_mod_refs(mach_task_self(), port_set, MACH_PORT_RIGHT_LABELH, -1); 83*8d741a5dSApple OSS Distributions T_ASSERT_EQ(ret, KERN_INVALID_RIGHT, "mach_port_mod_refs LABELH: -1 on a PORT_SET right"); 84*8d741a5dSApple OSS Distributions 85*8d741a5dSApple OSS Distributions /* can't subtract an invalid right-type */ 86*8d741a5dSApple OSS Distributions ret = mach_port_mod_refs(mach_task_self(), port_set, MACH_PORT_RIGHT_NUMBER, -1); 87*8d741a5dSApple OSS Distributions T_ASSERT_EQ(ret, KERN_INVALID_VALUE, "mach_port_mod_refs NUMBER: -1 on a PORT_SET right"); 88*8d741a5dSApple OSS Distributions 89*8d741a5dSApple OSS Distributions /* can't subtract an invalid right-type */ 90*8d741a5dSApple OSS Distributions ret = mach_port_mod_refs(mach_task_self(), port_set, MACH_PORT_RIGHT_NUMBER + 1, -1); 91*8d741a5dSApple OSS Distributions T_ASSERT_EQ(ret, KERN_INVALID_VALUE, "mach_port_mod_refs NUMBER+1: -1 on a PORT_SET right"); 92*8d741a5dSApple OSS Distributions 93*8d741a5dSApple OSS Distributions /* restore the old guard behavior */ 94*8d741a5dSApple OSS Distributions ret = task_set_exc_guard_behavior(mach_task_self(), old); 95*8d741a5dSApple OSS Distributions T_ASSERT_MACH_SUCCESS(ret, "task_set_exc_guard_behavior old"); 96*8d741a5dSApple OSS Distributions 97*8d741a5dSApple OSS Distributions /* 98*8d741a5dSApple OSS Distributions * deallocate the ports/sets 99*8d741a5dSApple OSS Distributions */ 100*8d741a5dSApple OSS Distributions ret = mach_port_mod_refs(mach_task_self(), port_set, MACH_PORT_RIGHT_PORT_SET, -1); 101*8d741a5dSApple OSS Distributions T_ASSERT_MACH_SUCCESS(ret, "mach_port_mod_refs(PORT_SET, -1)"); 102*8d741a5dSApple OSS Distributions 103*8d741a5dSApple OSS Distributions ret = mach_port_mod_refs(mach_task_self(), port, MACH_PORT_RIGHT_RECEIVE, -1); 104*8d741a5dSApple OSS Distributions T_ASSERT_MACH_SUCCESS(ret, "mach_port_mod_refs(RECV_RIGHT, -1)"); 105*8d741a5dSApple OSS Distributions } 106