# felix-pb/kfd architecture

> Architecture diagram of the GitHub repository felix-pb/kfd, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2024-12-28). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/felix-pb/kfd
- Repository: https://github.com/felix-pb/kfd

## Overview

##### Main Components

a) Core Library Layer (libkfd):
- This as the central component
- Three main exploitation methods (PUAF primitives):
- PhysPuppet (CVE-2023-23536)
- Smith (CVE-2023-32434)
- Landa (CVE-2023-41974)
- KRKW (Kernel Read/Write) primitives:
- kread methods (kqueue_workloop_ctl, sem_open)
- kwrite methods (dup, sem_open)

b) Interface Layer:
- iOS Interface (Swift-based UI)
- macOS Interface (C-based CLI)

c) Kernel Interaction Layer:
- Physical Memory Management
- Virtual Memory System
- Kernel Objects Manipulation

**[User Interface Layer]**
- ↓   ↑
**[libkfd Core Layer]**
- ↓   ↑
**[Kernel Interaction Layer]**

##### Component Relationships

- Data flow between components
- Exploitation chain progression
- API calls between layers
- Memory access patterns

##### Specific Elements

**kopen()**
**kread()**
**kwrite()**
**kclose()**

- physpuppet
- smith
- landa

c) Memory Operation Primitives:
- kread methods
- kwrite methods

##### Technical Details to Highlight

- Physical page manipulation
- Dangling PTE handling
- Kernel object reallocation
- Memory primitive bootstrapping process

Remember to add notes about:
- Security implications
- Sandbox restrictions
- Version compatibility
- Success rate factors

## Diagram (Mermaid)

```mermaid
graph TB
    subgraph UI["User Interface Layer"]
        iOS["iOS Interface (Swift)"]:::ui
        macOS["macOS Interface (CLI)"]:::ui
    end

    subgraph Core["Core Library Layer (libkfd)"]
        API["API Interface<br/>kopen()<br/>kread()<br/>kwrite()<br/>kclose()"]:::core
        
        subgraph PUAF["PUAF Primitives"]
            direction TB
            PhysPuppet["PhysPuppet<br/>(CVE-2023-23536)"]:::exploit
            Smith["Smith<br/>(CVE-2023-32434)"]:::exploit
            Landa["Landa<br/>(CVE-2023-41974)"]:::exploit
        end
        
        subgraph KRKW["KRKW Primitives"]
            direction TB
            subgraph KR["Kread Methods"]
                KQW["Kqueue Workloop"]:::memory
                SOR["Sem Open Read"]:::memory
            end
            
            subgraph KW["Kwrite Methods"]
                DUP["Dup"]:::memory
                SOW["Sem Open Write"]:::memory
            end
        end
        
        subgraph Infrastructure["Common Infrastructure"]
            direction TB
            Common["Common Utilities"]:::infra
            Perf["Performance Monitor"]:::infra
            Info["System Information"]:::infra
        end
    end

    subgraph Kernel["Kernel Interaction Layer"]
        PhysMem["Physical Memory<br/>Management"]:::kernel
        VirtMem["Virtual Memory<br/>System"]:::kernel
        KernObj["Kernel Objects<br/>Manipulation"]:::kernel
    end

    %% Relationships
    iOS --> API
    macOS --> API
    API --> PUAF
    PUAF --> KRKW
    KRKW --> PhysMem
    KRKW --> VirtMem
    KRKW --> KernObj
    Infrastructure --> API

    %% Click Events
    click iOS "https://github.com/felix-pb/kfd/blob/main/kfd/ContentView.swift"
    click macOS "https://github.com/felix-pb/kfd/blob/main/macos_kfd.c"
    click API "https://github.com/felix-pb/kfd/blob/main/kfd/libkfd.h"
    click PhysPuppet "https://github.com/felix-pb/kfd/blob/main/kfd/libkfd/puaf/physpuppet.h"
    click Smith "https://github.com/felix-pb/kfd/blob/main/kfd/libkfd/puaf/smith.h"
    click Landa "https://github.com/felix-pb/kfd/blob/main/kfd/libkfd/puaf/landa.h"
    click KQW "https://github.com/felix-pb/kfd/blob/main/kfd/libkfd/krkw/kread/kread_kqueue_workloop_ctl.h"
    click SOR "https://github.com/felix-pb/kfd/blob/main/kfd/libkfd/krkw/kread/kread_sem_open.h"
    click DUP "https://github.com/felix-pb/kfd/blob/main/kfd/libkfd/krkw/kwrite/kwrite_dup.h"
    click SOW "https://github.com/felix-pb/kfd/blob/main/kfd/libkfd/krkw/kwrite/kwrite_sem_open.h"
    click Common "https://github.com/felix-pb/kfd/blob/main/kfd/libkfd/common.h"
    click Perf "https://github.com/felix-pb/kfd/blob/main/kfd/libkfd/perf.h"
    click Info "https://github.com/felix-pb/kfd/blob/main/kfd/libkfd/info/dynamic_info.h"

    %% Styles
    classDef ui fill:#3498db,stroke:#2980b9,color:white
    classDef core fill:#95a5a6,stroke:#7f8c8d,color:black
    classDef exploit fill:#e74c3c,stroke:#c0392b,color:white
    classDef memory fill:#f1c40f,stroke:#f39c12,color:black
    classDef kernel fill:#2ecc71,stroke:#27ae60,color:white
    classDef infra fill:#bdc3c7,stroke:#95a5a6,color:black

    %% Legend
    subgraph Legend
        UI_L["User Interface"]:::ui
        Core_L["Core Components"]:::core
        Exploit_L["Exploit Primitives"]:::exploit
        Memory_L["Memory Operations"]:::memory
        Kernel_L["Kernel Components"]:::kernel
        Infra_L["Infrastructure"]:::infra
    end
```
