int _kernelrpc_mach_port_construct_trap(struct _kernelrpc_mach_port_construct_args *args) { task_t task = port_name_to_task(args->target); mach_port_name_t name; int rv = MACH_SEND_INVALID_DEST; mach_port_options_t options; if (copyin(args->options, (char *)&options, sizeof (options))) { rv = MACH_SEND_INVALID_DATA; goto done; } if (task != current_task()) goto done; rv = mach_port_construct(task->itk_space, &options, args->context, &name); if (rv == KERN_SUCCESS) rv = copyout(&name, args->name, sizeof (name)); done: if (task) task_deallocate(task); return (rv); }
void unguard_mach_port() { mach_port_t port; mach_port_options_t options; mach_port_context_t gval = CONTEXT_VALUE1; int kret; printf("Testing unguard with correct guard: \n"); options.flags = (MPO_CONTEXT_AS_GUARD); kret = mach_port_construct(mach_task_self(), &options, gval, &port); if (kret != KERN_SUCCESS) exit(1); kret = mach_port_unguard(mach_task_self(), port, gval); if (kret == KERN_SUCCESS) printf("[PASSED]\n"); else goto failed; printf("Testing unguard with incorrect guard (Expecting Exception)... \n"); kret = mach_port_construct(mach_task_self(), &options, gval, &port); if (kret != KERN_SUCCESS) exit(1); kret = mach_port_unguard(mach_task_self(), port, CONTEXT_VALUE2); if (kret == KERN_SUCCESS) goto failed; return; failed: printf("[FAILED]\n"); exit(1); }
void set_context_mach_port() { mach_port_t port; mach_port_options_t options; mach_port_context_t gval = CONTEXT_VALUE1; mach_port_context_t g; int kret; printf("Testing set_context() with non-guarded port: "); kret = mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &port); if (kret != KERN_SUCCESS) exit(1); kret = mach_port_set_context(mach_task_self(), port, gval); if (kret != KERN_SUCCESS) goto failed; else printf("[PASSED]\n"); printf("Testing setting context on non-guarded port with pre-existing context: "); kret = mach_port_set_context(mach_task_self(), port, CONTEXT_VALUE2); if (kret != KERN_SUCCESS) goto failed; else printf("[PASSED]\n"); printf("Testing setting context on strict guarded port (Expecting Exception)...\n"); options.flags = (MPO_CONTEXT_AS_GUARD|MPO_STRICT); kret = mach_port_construct(mach_task_self(), &options, gval, &port); if (kret != KERN_SUCCESS) exit(1); kret = mach_port_set_context(mach_task_self(), port, CONTEXT_VALUE2); if (kret == KERN_SUCCESS) goto failed; return; failed: printf("[FAILED]\n"); exit(1); }
void unguard_nonguarded_mach_port() { mach_port_t port; mach_port_options_t options; mach_port_context_t gval = CONTEXT_VALUE1; int kret; printf("Testing mach_port_unguard() for non-guarded port (Expecting exception)...\n"); options.flags = 0; kret = mach_port_construct(mach_task_self(), &options, 0, &port); if (kret != KERN_SUCCESS) exit(1); kret = mach_port_unguard(mach_task_self(), port, gval); if (kret == KERN_SUCCESS) { printf("[FAILED]\n"); exit(1); } return; }
void mod_ref_guarded_mach_port() { mach_port_t port; mach_port_options_t options; mach_port_context_t gval = CONTEXT_VALUE1; int kret; printf("mach_port_mod_refs() guarded mach port (Expecting exception)...\n"); options.flags = (MPO_CONTEXT_AS_GUARD); kret = mach_port_construct(mach_task_self(), &options, gval, &port); if (kret != KERN_SUCCESS) exit(1); kret = mach_port_mod_refs(mach_task_self(), port, MACH_PORT_RIGHT_RECEIVE, -1); if (kret == KERN_SUCCESS) { printf("[FAILED]\n"); exit(1); } return; }
void destroy_guarded_mach_port() { mach_port_t port; mach_port_options_t options; mach_port_context_t gval = CONTEXT_VALUE1; int kret; printf("Destroying guarded mach port (Expecting exception)...\n"); options.flags = (MPO_CONTEXT_AS_GUARD); kret = mach_port_construct(mach_task_self(), &options, gval, &port); if (kret != KERN_SUCCESS) exit(1); kret = mach_port_destroy(mach_task_self(), port); if (kret == KERN_SUCCESS) { printf("[FAILED]\n"); exit(1); } return; }
void get_context_mach_port() { mach_port_t port; mach_port_options_t options; mach_port_context_t gval = CONTEXT_VALUE1; mach_port_context_t g; int kret; options.flags = (MPO_CONTEXT_AS_GUARD); printf("Testing get_context() for non-strict guarded port: "); kret = mach_port_construct(mach_task_self(), &options, gval, &port); if (kret != KERN_SUCCESS) exit(1); kret = mach_port_get_context(mach_task_self(), port, &g); if (kret != KERN_SUCCESS || g != gval) goto failed; else printf("[PASSED]\n"); printf("Testing get_context() for strict guarded port: "); options.flags = (MPO_CONTEXT_AS_GUARD|MPO_STRICT); kret = mach_port_construct(mach_task_self(), &options, gval, &port); if (kret != KERN_SUCCESS) exit(1); kret = mach_port_get_context(mach_task_self(), port, &g); if (kret != KERN_SUCCESS || g != 0) goto failed; else printf("[PASSED]\n"); printf("Testing get_context() for strict guard port (guarded using mach_port_guard): "); kret = mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &port); if (kret != KERN_SUCCESS) exit(1); kret = mach_port_guard(mach_task_self(), port, gval, 1); if (kret != KERN_SUCCESS) exit(1); kret = mach_port_get_context(mach_task_self(), port, &g); if (kret != KERN_SUCCESS || g != 0) goto failed; else printf("[PASSED]\n"); printf("Testing get_context() for non-guarded port with context: "); kret = mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &port); if (kret != KERN_SUCCESS) exit(1); kret = mach_port_set_context(mach_task_self(), port, gval); if (kret != KERN_SUCCESS) exit(1); kret = mach_port_get_context(mach_task_self(), port, &g); if (kret != KERN_SUCCESS || g != gval) goto failed; else printf("[PASSED]\n"); return; failed: printf("[FAILED]\n"); exit(1); }
void destruct_guarded_mach_port() { mach_port_t port; mach_port_options_t options; mach_port_context_t gval = CONTEXT_VALUE1; int kret; printf("Destructing guarded mach port with correct guard: "); options.flags = (MPO_CONTEXT_AS_GUARD); kret = mach_port_construct(mach_task_self(), &options, gval, &port); if (kret != KERN_SUCCESS) exit(1); kret = mach_port_destruct(mach_task_self(), port, 0, gval); if (kret == KERN_SUCCESS) printf("[PASSED]\n"); else goto failed; printf("Destructing guarded mach ports with incorrect send right count: "); options.flags = (MPO_CONTEXT_AS_GUARD); kret = mach_port_construct(mach_task_self(), &options, gval, &port); if (kret != KERN_SUCCESS) exit(1); kret = mach_port_destruct(mach_task_self(), port, -1, gval); if (kret != KERN_SUCCESS) printf("[PASSED]\n"); else goto failed; printf("Destructing guarded mach ports with correct send right and correct guard: "); options.flags = (MPO_CONTEXT_AS_GUARD|MPO_INSERT_SEND_RIGHT); kret = mach_port_construct(mach_task_self(), &options, gval, &port); if (kret != KERN_SUCCESS) exit(1); kret = mach_port_destruct(mach_task_self(), port, -1, gval); if (kret == KERN_SUCCESS) printf("[PASSED]\n"); else goto failed; printf("Destructing guarded mach port with incorrect guard (Expecting exception)...\n"); kret = mach_port_construct(mach_task_self(), &options, gval, &port); if (kret != KERN_SUCCESS) exit(1); kret = mach_port_destruct(mach_task_self(), port, 0, 0); if (kret == KERN_SUCCESS) goto failed; return; failed: printf("[FAILED]\n"); exit(1); }
void construct_mach_port() { mach_port_t port; mach_port_options_t options; mach_port_context_t gval = CONTEXT_VALUE1; mach_port_context_t g; int kret; printf("Testing All mach_port_construct() options...\n"); printf("No options specified: "); options.flags = 0; kret = mach_port_construct(mach_task_self(), &options, 0, &port); if (kret == KERN_SUCCESS) printf("[PASSED]\n"); else goto failed; printf("Options MPO_GUARD: "); options.flags = MPO_CONTEXT_AS_GUARD; kret = mach_port_construct(mach_task_self(), &options, gval, &port); if (kret == KERN_SUCCESS) printf("[PASSED]\n"); else goto failed; printf("Options MPO_GUARD|MPO_STRICT: "); options.flags = MPO_CONTEXT_AS_GUARD|MPO_STRICT; kret = mach_port_construct(mach_task_self(), &options, gval, &port); if (kret == KERN_SUCCESS) { kret = mach_port_get_context(mach_task_self(), port, &g); if (kret != KERN_SUCCESS || g != 0) goto failed; else printf("[PASSED]\n"); } else goto failed; printf("Options MPO_QLIMIT: "); options.flags = MPO_QLIMIT; mach_port_limits_t limits = { MACH_PORT_QLIMIT_SMALL }; options.mpl = limits; kret = mach_port_construct(mach_task_self(), &options, 0, &port); if (kret == KERN_SUCCESS) printf("[PASSED]\n"); else goto failed; printf("Options MPO_TEMPOWNER: "); options.flags = MPO_TEMPOWNER; kret = mach_port_construct(mach_task_self(), &options, 0, &port); if (kret == KERN_SUCCESS) printf("[PASSED]\n"); else goto failed; printf("Options MPO_IMPORTANCE_RECEIVER: "); options.flags = MPO_IMPORTANCE_RECEIVER; kret = mach_port_construct(mach_task_self(), &options, 0, &port); if (kret == KERN_SUCCESS) printf("[PASSED]\n"); else goto failed; printf("Options MPO_INSERT_SEND_RIGHT: "); options.flags = MPO_INSERT_SEND_RIGHT; kret = mach_port_construct(mach_task_self(), &options, 0, &port); if (kret == KERN_SUCCESS) printf("[PASSED]\n"); else goto failed; printf("No options specified (Construct Port-Set): "); options.flags = 0; kret = mach_port_construct(mach_task_self(), &options, 0, &port); if (kret == KERN_SUCCESS) printf("[PASSED]\n"); else goto failed; printf("...Complete\n"); return; failed: printf("[FAILED %d]\n", kret); exit(1); }