• Stars
    star
    188
  • Rank 198,802 (Top 5 %)
  • Language
    C
  • License
    MIT License
  • Created about 3 years ago
  • Updated over 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Lib kernel r/w

libkrw

An attempt at standardising an iOS kernel r/w API.

Background

Under Mach/Darwin/XNU, task ports are an API that, among other things, let you read and write memory of other processes. The idea is that every process has such a task port, even the kernel - referred to as tfp0 (from the task_for_pid(0) call that would historically yield that port).
On macOS this port always been available to root processes in some form, whereas on iOS you were never able to obtain it. But since iOS jailbreaks needed to gain kernel r/w anyway, most of them would use that to defeat this restriction and set up a well-known interface by which root processes could once more obtain tfp0.

Starting with iOS 10.3, Apple began combatting this from a different angle: rather than prevent the lookup or creation of a task port pointing at the kernel's process struct, they would make the interface detect these capabilities and refuse to operate on them. For a long time, bypassing those checks were as simple as swapping out some pointers or creating a second virtual mapping for the same physical memory, but with iOS 14 it seems Apple's had enough: they are now going so far as to panic the kernel if they detect use of a task port whose task->map->pmap == kernel_pmap. This is non-trivial to bypass on A12 and later due to PPL.

On checkra1n we of course patched this back into place as if nothing was amiss, but other jailbreaks don't have this luxury.
Given that any bypass is likely to get patched once it's used publicly, I think the time has come to abstract away the internal workings or kernel r/w and export a new API.

The role of libkrw

Libkrw serves two purposes:

  • Defining a common API (via header file and .tbd linkable)
  • Providing a default drop-in implementation around tfp0

This means that:

  • On any existing jailbreak that supports tfp0, you should be able to install this library and things should just work.
  • On any jailbreak going forward, the jailbreak author(s) would ship their own version of libkrw with a custom implementation, should they choose to conform to this API.

Structure

See include/libkrw.h for function declarations and API specification.

For building against libkrw:
  1. Copy include/libkrw.h and libkrw.tbd to your project.
  2. Compile with -I. -L. -lkrw.
  3. Don't forget the task_for_pid-allow entitlement.
  4. If you're building a deb file, add this to your control:
    Depends: libkrw (>= 1.0.0)
    
For writing your own implementation of libkrw:

Starting with version 1.1.0, libkrw supports a plugin interface so you no longer need to create a package to replace it.

  1. See include/libkrw_plugin.h for documentation.
  2. Implement and export either a function called krw_initializer or kcall_initializer that takes a krw_handlers_t argument.
  3. Install all handlers that you support.
  4. Compile with -Wl,-bundle.
  5. Name it /usr/lib/libkrw/[name].dylib.
  6. Add this to the control of your deb file:
    Depends: libkrw
    
For using the default implementation:

The default implementation resides in src/libkrw_tfp0.c.
It is automatically selected as a fallback when no plugins are present.
If you want to build it yourself, you can do so with:

make all    # builds the dylib and tbd
make deb    # builds the deb packages

The binary release is available from apt.bingner.com.
But you're free to rebuild and host this library wherever you please.

License

MIT.

More Repositories

1

ios-resources

Useful resources for iOS hacking
1,579
star
2

IOHIDeous

IOHIDFamily 0day
C
443
star
3

psychicpaper

iOS <13.5 sandbox escape/entitlement 0day
C
327
star
4

iometa

arm64 IOKit class dumper
C
256
star
5

v0rtex

IOSurface exploit
Objective-C
215
star
6

iokit-utils

Dev tools for probing IOKit
C
192
star
7

imobax

iOS Mobile Backup Extractor
C
178
star
8

cl0ver

tfp0 for iOS 9.0-9.3.4
C
154
star
9

APRR

Apple hardware secrets
Assembly
111
star
10

PhoenixNonce

64-bit nonce setter for iOS 9.3.4-9.3.5
Objective-C
96
star
11

hsp4

macOS kext for host_special_port(4) patch
C
89
star
12

tbdump

Utility to create tbd's off dylibs
C
77
star
13

dt

DeviceTree
C
73
star
14

IOKernelRW

Insecurity as an IOService
C++
63
star
15

cuck00

Twenty-twenty, bugs aplenty!
C
53
star
16

ios-scripts

iOS-related command line goodies
Shell
50
star
17

nordump

Apple Silicon NOR dumper
C++
41
star
18

lz4dec

Tiny arm64 LZ4 decompressor
C
40
star
19

ld64

Apple ld64 for Debian
Makefile
40
star
20

ios-build

Build files for things related to iOS
C
36
star
21

misc

C
34
star
22

siguza.github.io

Siguza's Blog
HTML
28
star
23

fscmp

CLI frontend for com.apple.decmpfs / AppleFSCompression.framework
C
27
star
24

UserScripts

My Tampermonkey scripts
JavaScript
14
star
25

libprovision

Library for dealing with Apple provisioning profiles and code signatures
10
star
26

StackScripts

My Tampermonkey scripts I use on StackExchange sites
JavaScript
7
star
27

VirtualPack

Bukkit Plugin "VirtualPack"
Java
7
star
28

recfg

C
6
star
29

Stash

Random stuff
Shell
6
star
30

lz4hc

C
3
star
31

libcrippy-1

Forked from openjailbreak.org
Makefile
2
star
32

libpartialzip-1

Forked from openjailbreak.org
C
1
star
33

NBTLib

A version-presistent bridge between Bukkit and Minecraft
Java
1
star
34

InvisiNOT

Bukkit Plugin "InvisiNOT"
Java
1
star