void guard_mach_port() { mach_port_t port; mach_port_options_t options; mach_port_context_t gval = CONTEXT_VALUE1; int kret; printf("Testing guarding a non-guarded mach port: "); 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, 0); if (kret == KERN_SUCCESS) printf("[PASSED]\n"); else goto failed; printf("Testing guarding a guarded mach port: "); kret = mach_port_guard(mach_task_self(), port, CONTEXT_VALUE2, 0); if (kret != KERN_SUCCESS) printf("[PASSED]\n"); else goto failed; return; failed: printf("[FAILED]\n"); exit(1); }
int _kernelrpc_mach_port_guard_trap(struct _kernelrpc_mach_port_guard_args *args) { task_t task = port_name_to_task(args->target); int rv = MACH_SEND_INVALID_DEST; if (task != current_task()) goto done; rv = mach_port_guard(task->itk_space, args->name, args->guard, args->strict); done: if (task) task_deallocate(task); return (rv); }
void guard_port_with_context() { mach_port_t port; mach_port_options_t options; mach_port_context_t gval = CONTEXT_VALUE1; int kret; printf("Testing mach_port_guard() for a 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_guard(mach_task_self(), port, gval, 0); if (kret != KERN_SUCCESS) printf("[PASSED]\n"); else { 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); }