Skip to content

fix: embed Bluetooth entitlements for daemon/CLI, restart-safe pairing scan - #37

Closed
yachoukh wants to merge 1 commit into
HMAKT99:mainfrom
yachoukh:fix/bluetooth-entitlements-macos27
Closed

yachoukh wants to merge 1 commit into
HMAKT99:mainfrom
yachoukh:fix/bluetooth-entitlements-macos27

Conversation

@yachoukh

Copy link
Copy Markdown

Summary

Found and fixed while setting up TouchBridge from source on macOS 27 ("Golden Gate"). Two of these are very likely macOS-version-specific (recent CoreBluetooth/codesign enforcement changes); the third is a pre-existing logic bug independent of macOS version.

1. Missing Bluetooth entitlement on touchbridged / touchbridge-test

Plain SwiftPM executables built without an embedded Info.plist have nowhere for NSBluetoothAlwaysUsageDescription to live. On this macOS version, that caused two different symptoms:

  • touchbridged: BLEServer logged Bluetooth powered off immediately.
  • touchbridge-test pair: CBPeripheralManager never called back with .poweredOn at all (not even .unauthorized) — the process would sit silently forever, with the phone stuck on "Scanning for Mac...".

Fix: embed a minimal Info.plist (with NSBluetoothAlwaysUsageDescription / NSBluetoothPeripheralUsageDescription) into both executables via -sectcreate __TEXT __info_plist linker flags in daemon/Package.swift.

2. Invalid code signature after adding the Info.plist linker flag

Once the daemon is re-linked with the -sectcreate flag above, its own ad-hoc "linker-signed" signature is rejected as Code Signature Invalid (hard SIGKILL) after being copied to /usr/local/bin — reproducible via crash report termination: CODESIGNING / Invalid Page.

Fix: scripts/install.sh now re-signs the installed binary with a plain codesign --force -s - pass after copying it, which reliably resolves this.

3. beginPairing doesn't restart a stuck BLE scan

If a previous pairing attempt used stale/expired pairing data, BLEClient is left mid-scan (or mid-connect) for the old service UUID. startScanning() no-ops while isScanning is already true, so entering fresh, valid pairing data afterward never actually restarted the scan — the app would show "Scanning for Mac..." indefinitely even with a correct, unexpired token.

Fix: CompanionCoordinator.beginPairing() now calls the existing bleClient.resetConnectionAndScan() instead of startScanning(), forcing a clean stop + reset before scanning for the new UUID.

Testing

  • All 112 existing daemon tests pass unchanged (swift test).
  • Verified end-to-end on real hardware: built from this branch, installed via scripts/install.sh, paired a real iPhone via touchbridge-test pair, and ran sudo echo test — authenticated successfully via Face ID.

Scope note

I found a couple of other things while debugging (a stale bundle identifier conflict under my own free Apple Developer account, and a placeholder-only AppIcon.appiconset) but those are specific to my local signing setup / cosmetic, not upstream bugs, so I've deliberately left them out of this PR to keep it focused on the actual functional fixes.

…g scan

On recent macOS (verified on 27 "Golden Gate"), plain SwiftPM executables
built without an embedded Info.plist have CoreBluetooth silently deny
authorization ("Bluetooth powered off" from BLEServer, or the peripheral
manager never reaching .poweredOn at all in touchbridge-test), because
there's nowhere for NSBluetoothAlwaysUsageDescription to live.

- daemon/Package.swift, Sources/{touchbridged,touchbridge-test}/Supporting/Info.plist:
  Embed a minimal Info.plist into both executables via `-sectcreate __TEXT
  __info_plist` linker flags, so CoreBluetooth can find the required usage
  description and macOS actually prompts for/grants Bluetooth access.

- scripts/install.sh:
  Re-sign the installed daemon binary with `codesign --force -s -` after
  copying it. The linker's own ad-hoc "linker-signed" signature (a side
  effect of the sectcreate flag above) is rejected as "Code Signature
  Invalid" (SIGKILL) on some macOS versions; a plain ad-hoc codesign pass
  after install fixes this reliably.

- companion/TouchBridge/Core/CompanionCoordinator.swift:
  beginPairing() now calls bleClient.resetConnectionAndScan() instead of
  startScanning(). If a previous pairing attempt used stale/expired data,
  BLEClient is left mid-scan (or mid-connect) for the *old* service UUID.
  startScanning() no-ops while already scanning, so entering fresh, valid
  pairing data afterward never actually restarted the scan — the app would
  show "Scanning for Mac..." indefinitely even with a correct token.

All 112 existing daemon tests pass unchanged.

Verified end-to-end on a real Mac + iPhone: build, install, pair, and
`sudo` authentication via Face ID all working after these fixes.
@xiawanghao206-cpu

xiawanghao206-cpu commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

Thanks @yachoukh — all three findings are real and were reproduced on a
MacBook Air running macOS 27.0.1 with Xcode 27.1. The Info.plist and
re-signing parts are exactly what unblocked Bluetooth here (before the fix,
touchbridged logged Bluetooth powered off and touchbridge-test pair
never got a CBManagerState callback at all).

I rebased the Mac-side parts onto v2.0.0 (c4c746f) and opened a replacement
PR that credits this work: #41

Notes for review:

  • daemon/Package.swift, the two new Supporting/Info.plist files and the
    scripts/install.sh re-sign step are carried over as-is (the plist path is
    resolved from #filePath, so it works from any build directory).
  • The companion-side beginPairing() change was not carried over: v2.0.0
    rewrote that path so beginPairing calls unpair() first, and unpair()
    stops scanning + disconnects before a new service UUID is used, which fixes
    the same stale-scan failure. Happy to add resetConnectionAndScan() as
    defence in depth if maintainers prefer that.
  • Verified on macOS 27.0.1 / Xcode 27.1: protocol 25 + daemon 115 tests pass,
    PAM native/script tests pass, tests/e2e-validation.sh passes, and the
    installed daemon verifies with codesign --verify --strict. Real-device
    smoke test (iPhone 18 Pro, Personal Team) went through pairing, root
    enrollment and a Face-ID-approved sudo, with deny/timeout falling back to
    password.
  • Side observation from macOS 27 that may matter for install docs: a root shell
    from osascript … with administrator privileges could not create
    /etc/pam.d/sudo_local (EPERM), while an interactive Terminal sudo
    could. Install automation that relies on GUI authorization to write PAM
    files may need to run that step in a Terminal.

HMAKT99 pushed a commit that referenced this pull request Oct 30, 2026
… after install

Plain SwiftPM executables have no Info.plist, so CoreBluetooth cannot read
NSBluetoothAlwaysUsageDescription. On macOS 27 this leaves TCC authorization
incomplete: touchbridged logs 'Bluetooth powered off' and touchbridge-test
never receives a poweredOn callback, silently stalling pairing.

Embed a minimal Info.plist into the touchbridged and touchbridge-test
executables via -sectcreate __TEXT __info_plist. Because the linker-signed
signature of such a binary can be rejected after being copied, install.sh now
runs an ad-hoc codesign pass on the installed daemon.

Mac side of PR #37 by @yachoukh; adapted to v2.0.0 (the companion-side
resetConnectionAndScan change is already covered by beginPairing -> unpair,
which stops scanning before a new service UUID is used).
@HMAKT99

HMAKT99 commented Oct 30, 2026

Copy link
Copy Markdown
Owner

Thank you @yachoukh. This was the first diagnosis of the macOS 27 Bluetooth problem. #41 carries your Info.plist and re-signing fixes forward, rebased onto v2.0.0 with credit to you, and is now merged.

The beginPairing change isn't needed in v2: beginPairing now calls unpair(), which stops scanning and disconnects before the new scan starts. Closing as superseded by #41.

@HMAKT99 HMAKT99 closed this Oct 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants