]> git.saurik.com Git - apple/xnu.git/blame - tests/immovable_rights.c
xnu-6153.41.3.tar.gz
[apple/xnu.git] / tests / immovable_rights.c
CommitLineData
cb323159
A
1#include <darwintest.h>
2#include <servers/bootstrap.h>
3#include <mach/mach.h>
4#include <mach/message.h>
5#include <stdlib.h>
6#include <sys/sysctl.h>
7#include <unistd.h>
8#include <mach/port.h>
9#include <mach/mach_port.h>
10
11T_GLOBAL_META(T_META_RUN_CONCURRENTLY(true));
12
13T_DECL(immovable_rights, "Create a port with immovable receive rights") {
14 mach_port_t imm_port;
15 mach_port_options_t opts = {
16 .flags = MPO_CONTEXT_AS_GUARD | MPO_IMMOVABLE_RECEIVE
17 };
18 kern_return_t kr;
19
20 kr = mach_port_construct(mach_task_self(), &opts, 0x10, &imm_port);
21 T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "mach_port_construct");
22
23 mach_port_status_t status;
24 mach_msg_type_number_t status_size = MACH_PORT_RECEIVE_STATUS_COUNT;
25 kr = mach_port_get_attributes(mach_task_self(), imm_port,
26 MACH_PORT_RECEIVE_STATUS, (mach_port_info_t)&status, &status_size);
27 T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "mach_port_get_attributes");
28 T_LOG("Status flags %d", status.mps_flags);
29 T_ASSERT_NE(0, (status.mps_flags & MACH_PORT_STATUS_FLAG_GUARD_IMMOVABLE_RECEIVE), "Imm rcv bit is set");
30
31 mach_port_t imm_port2;
32 mach_port_options_t opts2 = {};
33
34 kr = mach_port_construct(mach_task_self(), &opts2, 0, &imm_port2);
35 T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "mach_port_construct");
36
37 kr = mach_port_guard_with_flags(mach_task_self(), imm_port2, 0x11, (uint64_t)MPG_IMMOVABLE_RECEIVE);
38 T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "mach_port_guard_with_flags");
39
40 kr = mach_port_get_attributes(mach_task_self(), imm_port2,
41 MACH_PORT_RECEIVE_STATUS, (mach_port_info_t)&status, &status_size);
42 T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "mach_port_get_attributes");
43 T_LOG("Status flags %d", status.mps_flags);
44 T_ASSERT_NE(0, (status.mps_flags & MACH_PORT_STATUS_FLAG_GUARD_IMMOVABLE_RECEIVE), "Imm rcv bit is set");
45
46 kr = mach_port_swap_guard(mach_task_self(), imm_port2, 0x11, 0xde18);
47 T_ASSERT_MACH_SUCCESS(kr, "mach_port_swap_guard");
48
49 kr = mach_port_unguard(mach_task_self(), imm_port2, 0xde18);
50 T_ASSERT_MACH_SUCCESS(kr, "mach_port_unguard");
51}