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