Vulnerability Classes
Hiểu các loại bugs phổ biến giúp bạn biết tìm gì khi audit code, và biết cách exploit khi tìm được.
1. Use-After-Free (UAF)
Khái niệm: Object bị freed nhưng pointer vẫn được dùng. Khi freed memory bị reallocate cho object khác, dùng dangling pointer = access object mới qua context cũ.
struct ipc_port *port = ipc_port_alloc();
// ... use port ...
ipc_port_release(port); // port freed
// ... later ...
port->ip_messages; // UAF! port memory may now contain different object
Exploitation:
- Trigger free → object returned to zone
- Spray controlled data vào cùng zone → chiếm freed slot
- Dùng dangling pointer → access controlled data as if it were original object
- → Type confusion, controlled reads/writes, code execution
Ví dụ: IOKit clientClose() race — close UserClient trong 1 thread, call external method trong thread khác.
2. Heap Buffer Overflow
Khái niệm: Ghi data vượt quá boundary của allocated buffer trên heap, corrupt object kế tiếp.
char *buf = kalloc(64);
memcpy(buf, user_data, user_size); // Nếu user_size > 64 → overflow!
Exploitation:
- Heap groom: đặt target object ngay sau vulnerable buffer
- Overflow → ghi đè fields quan trọng của target object
- Common targets: function pointers, size fields, linked list pointers
3. Type Confusion
Khái niệm: Object được interpret như type khác. Field X ở offset 0x10 trong type A có ý nghĩa hoàn toàn khác với field Y ở offset 0x10 trong type B.
// Type A: field at offset 0x20 is a size field
struct type_a { ...; uint32_t size; ... };
// Type B: field at offset 0x20 is a pointer
struct type_b { ...; void *ptr; ... };
// Nếu type B object bị interpret as type A:
// "size" = pointer value → huge size → read/write out of bounds
Exploitation via zone transfer:
- Allocate object A → zone X
- Free object A (dangling reference remains)
- Force zone X to release pages
- Allocate object B (different type, same size zone) → same memory
- Access via dangling reference → type A operations on type B data
4. Integer Overflow
Khái niệm: Arithmetic operation produces result larger than storage → wraps around → bypass size checks.
uint32_t total = count * elem_size; // Nếu count * elem_size > 2^32 → overflow!
char *buf = kalloc(total); // Allocate nhỏ hơn expected
memcpy(buf, data, count * elem_size); // Copy nhiều hơn allocated → heap overflow
Thường thấy ở: External method size validation, buffer allocation before copy.
5. Race Condition (TOCTOU)
Khái niệm: Time-of-Check vs Time-of-Use — condition kiểm tra lúc T1 nhưng dùng lúc T2, giữa T1 và T2 state đã thay đổi.
// Thread 1 (kernel):
if (port->ip_object.io_bits & IO_VALID) { // T1: check valid
// ... other thread frees port here ...
use(port); // T2: use freed port!
}
Exploitation:
- Tạo nhiều threads, race để trigger window giữa check và use
- Thường cần chạy exploit nhiều lần (probabilistic)
- Deterministic exploits tìm cách eliminate race (e.g., Trigon)
6. Physical Use-After-Free (PUAF)
Khái niệm: Physical page freed nhưng virtual mapping vẫn tồn tại.
Process maps VA 0x1000 → PA 0x2000
Kernel bug: frees PA 0x2000 without removing VA mapping
Kernel allocates PA 0x2000 for kernel data structure
Process reads/writes VA 0x1000 → reads/writes kernel data!
Đây là basis cho modern iOS exploits (kfd, Trigon, …).
7. Information Leak
Khái niệm: Kernel exposes internal data (addresses, values) to userspace.
Types:
- Uninitialized heap data: kalloc returns memory with stale data → leak addresses
- Out-of-bounds read: Read past buffer boundary → leak adjacent object data
- Side channels: Timing differences reveal kernel state
Dùng cho: KASLR bypass (cần biết kernel base address trước khi exploit).
Tìm Bugs Ở Đâu
| Vulnerability class | Nơi hay tìm thấy |
|---|---|
| UAF | IOKit drivers (lifecycle bugs), IPC port handling |
| Heap overflow | External method struct input parsing, memcpy with user-controlled size |
| Type confusion | Zone allocator (cross-zone), MIG message parsing |
| Integer overflow | Size calculations trước allocation |
| Race condition | Multi-threaded IOKit method access, concurrent IPC |
| PUAF | VM subsystem (vm_map, pmap interactions) |
| Info leak | IOKit properties, sysctl handlers, uninitialized stack/heap |