task_t pid_to_task(int pid) { static task_t old_pid = -1; static task_t old_task = -1; task_t task = -1; int err; /* xlr8! */ if (old_task != -1 && old_pid == pid) return old_task; err = task_for_pid (mach_task_self (), (pid_t)pid, &task); if ((err != KERN_SUCCESS) || !MACH_PORT_VALID (task)) { task = task_for_pid_workaround (pid); if (task == -1) { eprintf ("Failed to get task %d for pid %d.\n", (int)task, (int)pid); eprintf ("Reason: 0x%x: %s\n", err, (char *)MACH_ERROR_STRING (err)); eprintf ("You probably need to run as root or sign the binary.\n" " Read doc/ios.md || doc/osx.md\n" " make -C binr/radare2 ios-sign || osx-sign\n"); return -1; } } old_pid = pid; old_task = task; return task; }
static task_t pid_to_task(int pid) { task_t task = 0; static task_t old_task = 0; static int old_pid = -1; kern_return_t kr; if (old_task != 0 && old_pid == pid) { return old_task; } else if (old_task != 0 && old_pid != pid) { //we changed the process pid so deallocate a ref from the old_task //since we are going to get a new task kr = mach_port_deallocate (mach_task_self (), old_task); if (kr != KERN_SUCCESS) { eprintf ("pid_to_task: fail to deallocate port\n"); return 0; } } int err = task_for_pid (mach_task_self (), (pid_t)pid, &task); if ((err != KERN_SUCCESS) || !MACH_PORT_VALID (task)) { task = task_for_pid_workaround (pid); if (task == MACH_PORT_NULL) { task = task_for_pid_ios9pangu (pid); if (task != MACH_PORT_NULL) { //eprintf ("Failed to get task %d for pid %d.\n", (int)task, (int)pid); //eprintf ("Missing priviledges? 0x%x: %s\n", err, MACH_ERROR_STRING (err)); return -1; } } } old_task = task; old_pid = pid; return task; }
static task_t pid_to_task(int pid) { task_t task = -1; int err = task_for_pid (mach_task_self (), (pid_t)pid, &task); if ((err != KERN_SUCCESS) || !MACH_PORT_VALID (task)) { task = task_for_pid_workaround (pid); if (task == -1) { eprintf ("Failed to get task %d for pid %d.\n", (int)task, (int)pid); eprintf ("Missing priviledges? 0x%x: %s\n", err, MACH_ERROR_STRING (err)); #if 0 eprintf ("You probably need to add user to procmod group.\n" " Or chmod g+s radare && chown root:procmod radare\n"); eprintf ("FMI: http://developer.apple.com/documentation/Darwin/Reference/ManPages/man8/taskgated.8.html\n"); #endif return -1; } } return task; }
static task_t pid_to_task(RIODesc *fd, int pid) { task_t task = 0; static task_t old_task = 0; static int old_pid = -1; kern_return_t kr; RIODescData *iodd = fd? (RIODescData *)fd->data: NULL; RIOMach *riom = NULL; if (iodd) { riom = iodd->data; if (riom && riom->task) { old_task = riom->task; riom->task = 0; old_pid = iodd->pid; } } if (old_task != 0) { if (old_pid == pid) { return old_task; } //we changed the process pid so deallocate a ref from the old_task //since we are going to get a new task kr = mach_port_deallocate (mach_task_self (), old_task); if (kr != KERN_SUCCESS) { eprintf ("pid_to_task: fail to deallocate port\n"); return 0; } } int err = task_for_pid (mach_task_self (), (pid_t)pid, &task); if ((err != KERN_SUCCESS) || !MACH_PORT_VALID (task)) { task = task_for_pid_workaround (pid); if (task == MACH_PORT_NULL) { task = task_for_pid_ios9pangu (pid); if (task != MACH_PORT_NULL) { //eprintf ("Failed to get task %d for pid %d.\n", (int)task, (int)pid); //eprintf ("Missing priviledges? 0x%x: %s\n", err, MACH_ERROR_STRING (err)); return -1; } } } old_task = task; old_pid = pid; return task; }
mach_port_t get_kernel_task(void) { if (getuid() != 0) { __dbg("Program should be run as root."); return 0; } mach_port_t ktask = task_for_pid_workaround(0); if (!ktask) { __dbg("processor_set_tasks() failed. is SIP enabled?"); return 0; } int32_t pid; pid_for_task(ktask, &pid); if (pid!=0) { __dbg("kernel task was returned by processor_set_tasks(), but it appears to have the wrong PID. If you are reading this, the universe is probably collapsing."); return 0; } return ktask; }