Skip to content

About

Tweak injection core for macOS on Apple Silicon (requires SIP disabled)

Resources

Stars

7 stars

Watchers

0 watching

Forks

Repository files navigation

TI_TweakLoader

A tweak loader and injection suite for macOS on Apple silicon, built to work like its iOS counterparts while extending what a tweak can filter on.

Requires SIP to be disabled (csrutil disable from recoveryOS). Nothing here works on a stock system, and that is by design — turning SIP off removes real protections from the whole machine. Use it on a machine you are willing to treat as untrusted.

Components

Component What it is
TweakLoader/ The dylib (TI_TweakLoader.dylib) injected into every process. Enumerates filter plists, decides what loads, and dlopens the matching tweaks. It also hooks posix_spawn, so processes that something other than launchd starts get the loader too.
Shared/ What the three components above have in common: the never-inject process list and the dyld interposing macro.
LaunchdHooks/ Hooks posix_spawn inside launchd so newly spawned processes inherit the loader. Also where per-process disable and Safe Mode are enforced.
XpcProxyHooks/ The same for xpcproxy, which is what actually execs most XPC services.
SafeMode/ Crash tripwire loaded into critical processes, so a broken tweak cannot leave the machine unusable.
SafeModePill/ The menu bar indicator shown while Safe Mode is active, and the way out of it.

Disabling injection for a process, and Safe Mode itself, are enforced in the spawn hooks rather than in the loader: a stripped process never receives TI_TweakLoader.dylib at all, so there is nothing in it to go wrong.

Processes launchd did not start

launchd only arms what it spawns itself. A process started by anything else — a terminal tool, an IDE, a build system — would never see the loader. So the loader interposes posix_spawn and posix_spawnp in the processes it is injected into, and puts itself first in the child's DYLD_INSERT_LIBRARIES, keeping whatever the caller already inserts. The same gates as the other spawn hooks apply, evaluated on every spawn: injection suspended (/var/run/tweakinject.disabled, /Library/TweakInject/.disabled, or the loader missing), Safe Mode, safe boot, the process list, and TweakInjectionDisabled in perProcessTweaks.plist. Disabling or enabling injection therefore applies to the next process spawned, with nothing to restart.

An iOS app running on the Mac (a /Wrapper/ path or LSRequiresIPhoneOS) gets a pass-through hook, the same test the ThemeEngine function hooks use.

Not covered: fork + execve, which is how shells start commands. Only posix_spawn is hooked.

Filters

A tweak ships a plist next to its dylib describing where it should load. The format follows the iOS tweak-filter convention — Bundles, Classes, Executables, combined with OR — plus a few additions:

  • ExcludeBundles — hard veto, evaluated before anything else.
  • Type — App, Binary or Any.
  • Privilege — Any (default), Root, or User. User keeps a tweak out of root processes entirely, which bounds what a package can reach. Absent means Any, so existing filters behave exactly as before.

A filter that declares no Bundles/Classes/Executables applies to every process that passes its gates, as long as it sets at least one of Type or Privilege. A filter that sets neither matches nothing — no criteria and no gate is an incomplete filter, not a request to load everywhere. The iOS loaders differ here: there a criteria-less filter matches nothing at all, and "everywhere" is spelled by naming a universally linked bundle such as com.apple.foundation.

<dict>
  <key>Filter</key>
  <dict>
    <key>Bundles</key>
    <array><string>com.apple.dock</string></array>
    <key>Privilege</key>
    <string>User</string>
  </dict>
</dict>

Preferences

A tweak injected into a sandboxed app cannot use the stock preferences API to reach a shared store: NSUserDefaults hands it that app's container, so the tweak's settings fragment per host process.

TI_Support.dylib reads and writes /Library/TweakInject/Preferences/Defaults directly, at a fixed absolute path, so every process sees the same store regardless of container or uid. The store is root:staff 775, and TI_LaunchdHooks issues a sandbox extension over it per injected process.

Two surfaces, mirroring the shapes they replace:

#import <PSUserDefaults.h>

PSUserDefaults *d = [[PSUserDefaults alloc] initWithSuiteName:@"com.you.tweakname"];
[d registerDefaults:@{ @"Enabled": @YES }];
BOOL on = [d boolForKey:@"Enabled"];

PSPreferences mirrors the CoreFoundation functions (PSPreferencesCopyAppValue, PSPreferencesSetAppValue) for tweaks written against that style.

Link with -L/Library/TweakInject -lTI_Support. The install name is the absolute /Library/TweakInject/Internal/TI_Support.dylib, so dyld resolves it there at load time — no search path, no @rpath.

There is deliberately no standardUserDefaults equivalent. A tweak is a guest in someone else's process and has no "own" domain — the host's is not it. Naming the domain is what makes a tweak's settings the same everywhere it loads. By convention that name is the package identifier, as with Cephei's HBPreferences.

Getting it running

Two steps, because a loader that only affects processes started after it is installed is not much use:

  1. Inject the launchd hook. Dylinject loads TI_LaunchdHooks.dylib into launchd (pid 1). From then on every process launchd spawns inherits TI_TweakLoader.dylib.
  2. Catch what was already running. Everything alive before step 1 has no loader in it. A userspace restart (launchctl reboot userspace) brings those processes back with it, without rebooting the kernel.

The hook has to be re-injected after each boot.

TweakInject is the macOS app built on this and drives all of it from a GUI — install, inject, userspace restart, per-process rules, Safe Mode and tweak management — so none of it needs doing by hand. It ships with a signed package store of its own.

This repository is the engine underneath: the app depends on the loader, the loader does not depend on the app, and nothing here assumes a particular front-end.

Layout on disk

/Library/TweakInject/
├── TI_TweakLoader.dylib
├── TI_Support.dylib
├── TI_Ellekit.dylib
├── Tweaks/
│   ├── DynamicLibraries/      tweak dylibs + their filter plists
│   └── Bundles/               self-contained .bundle tweaks
├── DisabledTweaks/            the same two, for disabled tweaks
├── Preferences/
│   ├── Defaults/              the preference store (root:staff 775)
│   ├── PreferenceBundles/     settings UI shipped by tweaks
│   └── PreferencePanes/
├── Config/
│   ├── perProcessTweaks.plist per-process enable/disable
│   ├── denyInjectionTweaks.plist
│   └── installedPackages.plist
├── SafeMode/                  TI_SafeMode.dylib, TI_SafeModePill.dylib, and .safemode
├── LaunchdHook/               TI_LaunchdHooks.dylib and TI_XpcProxyHooks.dylib
└── logs/

Everything except Preferences/Defaults is root:wheel, directories 755 and files 644. All of it is loaded into other processes — including root ones — so a world-writable directory is a local privilege escalation, not a convenience. Preferences/Defaults is the one exception, and is group-writable on purpose: tweaks write their own preferences as the user.

Building

Open TI_TweakLoader.xcodeproj and set your own development team (the shipped project has it blank). Targets: TI_TweakLoader, TI_LaunchdHooks, TI_XpcProxyHooks, SafeMode. SafeModePill has no Xcode target — it is one file against Cocoa, built directly.

Or build a component by hand:

clang -dynamiclib -arch arm64e -install_name /Library/TweakInject/Internal/TI_TweakLoader.dylib \
  -framework CoreFoundation -lobjc -o TI_TweakLoader.dylib TweakLoader/TweakLoader.c

Logging

Every injected component has ENABLE_LOGS, off by default, and logs through one macro:

TL_LOG("[TweakLoader] %{public}s denied", procName);

The switch itself:

#define ENABLE_LOGS 0

Leave it off unless you are actively debugging. This code runs inside every process that launches, logd included, and os_log() from a constructor there deadlocks logd — after which the rest of the system follows and the machine usually needs a reboot to recover.

Notes

  • The spawn hooks carry a blacklist of processes that must never be injected. It is not a convenience list; each entry is something that broke. logd, syslogd and notifyd, where logging from a constructor deadlocks the system. cfprefsd and launchservicesd, because nearly every launch goes through them and injecting them is circular. amfid, because injecting the validator that authorises injection is a loop, and a wedged amfid stalls every exec on the machine. loginwindow and the authentication stack, which deadlock a userspace restart. MTLCompilerService, where a hooked function in a shared XPC service stalls the GPU pipeline machine-wide rather than for the app that triggered the compile.
  • WindowServer is deliberately not blacklisted.
  • The process list is one file, Shared/ProcessBlacklist.h, included by both spawn hooks and by the loader. Both hook files carry the same gate and plist parser, and those blocks are kept byte-identical between them. xpcproxy does not link CoreFoundation, so the parser in both is libc-only.
  • Code signatures for every injectable dylib are registered with the kernel (F_ADDFILESIGS_RETURN) before a userspace restart. launchctl reboot userspace tears down amfid before PID 1 re-execs, so an uncached signature fails closed and killing a boot-critical process panics the kernel.

About

Tweak injection core for macOS on Apple Silicon (requires SIP disabled)

Resources

Stars

7 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages