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