AppleMacDeveloper tools5 min· Alexey Bulatnikov

What a sandboxed Mac app can and cannot do with a USB drive

We wanted to put Restore My USB on the Mac App Store. Its job is to bring a broken USB stick back to life: unmount it, wipe it, format it again. Apps on the store must run inside the App Sandbox, so before writing the app we wrote small test apps and measured what the sandbox allows. The answer changed how the app is distributed.

Everything below was measured on macOS 26 with sandboxed test apps, on a disk image and on a real SanDisk stick.

With the default sandbox

A sandboxed app that has only the entitlement for files the user selects:

  • diskutil info -plist <disk> works and returns full data.
  • diskutil list -plist exits with status 0 and returns an empty list. Code that checks only the exit status concludes that the Mac has no drives.
  • Opening /dev/rdiskN for reading or writing fails with "Operation not permitted".
  • diskutil unmountDisk force fails.
  • diskutil eraseDisk fails with "unable to communicate with storagekitd (-69812)".
  • The DiskArbitration calls work: DADiskUnmount, and the callback that reports a newly connected drive.
  • Reading and writing files on a volume the user has chosen works.

With a temporary exception

One entitlement lets a sandboxed app talk to named system services: com.apple.security.temporary-exception.mach-lookup.global-name. We listed the storage services in it and measured again.

com.apple.storagekitd
com.apple.storagekitd.dm
com.apple.DiskArbitration.diskarbitrationd
  • diskutil list -plist returns real data.
  • diskutil zeroDisk works.
  • diskutil eraseDisk works on a disk image.
  • diskutil unmountDisk force works on a disk image and fails on a physical drive. DADiskUnmount with the whole-disk and force options works on both.
  • diskutil eraseDisk on a physical drive fails with "Insufficient privileges (-69863)".
  • The raw device is still closed.

The limit that decides everything

Outside the sandbox, an ordinary user erases a USB stick with diskutil eraseDisk and is never asked for a password. Inside the sandbox the same command is refused, with every exception we could list. Adding the security and authorisation services to the exception did not help, because the sandbox denies the authorisation right itself. We searched the macOS SDK for an entitlement that grants it and found none.

So a sandboxed app can list drives and force them to unmount, and it cannot format or erase a physical drive. This is a technical wall, and no review note gets an app past it. Root is not the question either: the user needs no password for the command, and the sandbox still refuses it.

Three traps along the way

  1. Test on physical media. A disk image is more permissive than a real drive. Our first results came from an image and were wrong.
  2. Check the output, and not only the exit status. The empty list with status 0 is easy to miss.
  3. Drive DiskArbitration with a dispatch queue (DASessionSetDispatchQueue). With a run loop it deadlocks as soon as your code blocks the main thread while it waits for the answer.

What we did with the result

Restore My USB is distributed outside the Mac App Store, signed with a Developer ID and notarised by Apple, so macOS opens it without warnings and it can do the whole job. An app that only deletes files in the user's home folder, such as a disk cleaner, has none of these problems and fits the sandbox well.

If you plan a Mac tool that touches drives, measure the sandbox in the first week, before the design work.

Read: what to do when an external drive will not erase on a Mac

See Restore My USB