Task/pid for port?

Questions and Answers about all things *OS (macOS, iOS, tvOS, watchOS)

Task/pid for port?

Postby LIJI » Sat Jun 25, 2016 11:56 pm

I know this is probably far fetched, but still:

Let's say my process has send rights to a Mach port — is there a way to get the PID (or even better, the task port) of the process that has the receive rights?
LIJI
 
Posts: 6
Joined: Sat Sep 12, 2015 5:43 pm

Re: Task/pid for port?

Postby morpheus » Sun Jun 26, 2016 12:18 pm

Hardly far fetched.

Code: Select all
procexp <pid> ports


will do that for you.
morpheus
Site Admin
 
Posts: 530
Joined: Thu Apr 11, 2013 6:24 pm

Re: Task/pid for port?

Postby LIJI » Sun Jun 26, 2016 12:42 pm

I ment from inside my own process, but I guess I can figure out how procexp does that, thanks! :)
LIJI
 
Posts: 6
Joined: Sat Sep 12, 2015 5:43 pm

Re: Task/pid for port?

Postby morpheus » Mon Jun 27, 2016 3:11 pm

Oh. Your question wasn't clear - Anyway - The simple version of it is - you get all the processor_set_tasks, you then call mach_port_space_info for each task. Get their ports. Each port has an opaque, but unique kobject. You then start matching the unique IDs. Turns out that Apple has an open source to do that - called lsmp - which is in system_cmds, so it's easier I guess to look at that. Note that doesn't get you the port names. That only procexp gets, due to its close liaison with launchd :-)

And note you will need an entitlement as of 10.11 w/SIP. Still works on iOS since com.apple.system-task-ports isn't enforced there, though. q.v. the "Task for pid workaround" article.
morpheus
Site Admin
 
Posts: 530
Joined: Thu Apr 11, 2013 6:24 pm

Re: Task/pid for port?

Postby LIJI » Mon Jun 27, 2016 5:47 pm

I figured (most) of it out by looking at procexp (Thanks for keeping the symbols and debug info in! :P)

Apparently there's a simpler solution that works in my case – I use xpc_dictionary_get_audit_token on an XPC dictionary originating from that port, then audit_token_to_pid and task_for_pid.
LIJI
 
Posts: 6
Joined: Sat Sep 12, 2015 5:43 pm


Return to Questions and Answers

Who is online

Users browsing this forum: No registered users and 1 guest