{"exploit":{"id":"EDB-39595","name":"Apple Mac OSX/iOS - SUID Binary Logic Error Kernel Code Execution","description":"Apple Mac OSX/iOS - SUID Binary Logic Error Kernel Code Execution","cves":["CVE-2016-1757"],"author":"Google Security Research","published":"2016-03-23","platform":"multiple","type":"local","language":"Text","source":"exploit-db","sourceUrl":"https://www.exploit-db.com/exploits/39595","path":"exploits/multiple/local/39595.txt","revision":"8a926a9b85beb3bccd6e8016211fdb73e904b52b","filename":"EDB-39595.txt","vulnerabilities":[{"id":"CVE-2016-1757","state":"PUBLISHED","title":null,"titleSource":null,"description":"Race condition in the kernel in Apple iOS before 9.3 and OS X before 10.11.4 allows attackers to execute arbitrary code in a privileged context via a crafted app.","descriptionSource":"CVE Program","descriptionUrl":"https://www.cve.org/CVERecord?id=CVE-2016-1757","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-2016-1757"},"updatedAt":"2024-08-05T23:10:38.966Z","retrievedAt":"2026-11-27T02:58:56.128Z","stale":false,"error":null}],"imageUrl":"https://exploitbin.com/social/exploits/EDB-39595.png?v=2","url":"https://exploitbin.com/exploits/EDB-39595","downloadUrl":"https://exploitbin.com/api/v1/exploits/EDB-39595/download","codeUrl":"https://exploitbin.com/api/v1/exploits/EDB-39595/code","licenseUrl":"https://exploitbin.com/sources#exploit-db"},"code":"Source: https://bugs.chromium.org/p/project-zero/issues/detail?id=676\n\ntl;dr\nThe code responsible for loading a suid-binary following a call to the execve syscall invalidates\nthe task port after first swapping the new vm_map into the old task object leaving a short race window\nwhere we can manipulate the memory of the euid(0) process before the old task port is destroyed.\n\n******************\n\n__mac_execve calls exec_activate_image which calls exec_mach_imgact via the image activator table execsw.\n\nIf we were called from a regular execve (not after a vfork or via posix_spawn) then this calls load_machfile\nwith a NULL map argument indicating to load_machfile that it should create a new vm_map for this process:\n\n  if (new_map == VM_MAP_NULL) {\n    create_map = TRUE;\n    old_task = current_task();\n  }\n\nit then creates a new pmap and wraps that in a vm_map, but doesn't yet assign it to the task:\n\n    pmap = pmap_create(get_task_ledger(ledger_task),\n           (vm_map_size_t) 0,\n           ((imgp->ip_flags & IMGPF_IS_64BIT) != 0));\n    pal_switch_pmap(thread, pmap, imgp->ip_flags & IMGPF_IS_64BIT);\n    map = vm_map_create(pmap,\n        0,\n        vm_compute_max_offset(((imgp->ip_flags & IMGPF_IS_64BIT) == IMGPF_IS_64BIT)),\n        TRUE)\n\nthe code then goes ahead and does the actual load of the binary into that vm_map:\n\n  lret = parse_machfile(vp, map, thread, header, file_offset, macho_size,\n                        0, (int64_t)aslr_offset, (int64_t)dyld_aslr_offset, result);\n\nif the load was successful then that new map will we swapped with the task's current map so that the task now has the\nvm for the new binary:\n\n    old_map = swap_task_map(old_task, thread, map, !spawn);\n\n    vm_map_t\n    swap_task_map(task_t task, thread_t thread, vm_map_t map, boolean_t doswitch)\n    {\n      vm_map_t old_map;\n\n      if (task != thread->task)\n        panic(\"swap_task_map\");\n\n      task_lock(task);\n      mp_disable_preemption();\n      old_map = task->map;\n      thread->map = task->map = map;\n\nwe then return from load_machfile back to exec_mach_imgact:\n\n  lret = load_machfile(imgp, mach_header, thread, map, &load_result);\n\n  if (lret != LOAD_SUCCESS) {\n    error = load_return_to_errno(lret);\n    goto badtoolate;\n  }\n\n  ...\n\n  error = exec_handle_sugid(imgp);\n\nafter dealing with stuff like CLOEXEC fds we call exec_handle_sugid.\nIf this is indeed an exec of a suid binary then we reach here before actually setting\nthe euid:\n\n       * Have mach reset the task and thread ports.\n       * We don't want anyone who had the ports before\n       * a setuid exec to be able to access/control the\n       * task/thread after.\n      ipc_task_reset(p->task);\n      ipc_thread_reset((imgp->ip_new_thread != NULL) ?\n           imgp->ip_new_thread : current_thread());\n\nAs this comment points out, it probably is quite a good idea to reset the thread, task and exception ports, and\nthat's exactly what they do:\n\n  ...\n  ipc_port_dealloc_kernel(old_kport);\n  etc for the ports\n  ...\n\n\nThe problem is that between the call to swap_task_map and ipc_port_dealloc_kernel the old task port is still valid, even though the task isn't running.\nThis means that we can use the mach_vm_* API's to manipulate the task's new vm_map in the interval between those two calls. This window is long enough\nfor us to easily find the load address of the suid-root binary, change its page protections and overwrite its code with shellcode.\n\nThis PoC demonstrates this issue by targetting the /usr/sbin/traceroute6 binary which is suid-root. Everything is tested on OS X El Capitan 10.11.2.\n\nIn our parent process we register a port with launchd and fork a child. This child sends us back its task port, and once we ack that we've got\nits task port it execve's the suid-root binary.\n\nIn the parent process we use mach_vm_region to work out when the task's map gets switched, which also convieniently tells us the target binary's load\naddress. We then mach_vm_protect the page containing the binary entrypoint to be rwx and use mach_vm_write to overwrite it with some shellcode which\nexecve's /bin/zsh (because bash drops privs) try running id in the shell and note your euid.\n\nEverything is quite hardcoded for the exact version of traceroute6 on 10.11.2 but it would be easy to make this into a very universal priv-esc :)\n\nNote that the race window is still quite tight so you may have to try a few times.\n\n\nProof of Concept:\nhttps://gitlab.com/exploit-database/exploitdb-bin-sploits/-/raw/main/bin-sploits/39595.zip","sha256":"742b2a6ecbf34202eaff4da99aafd59e47625dbc793539c89b8a1d2697e7d515","bytes":4499,"fetchedAt":"2026-11-27T10:07:29.687Z","encoding":"utf-8 (display only; download retains original bytes)"}