;;; Copyright (c) 2021 Apple Inc.  All Rights reserved.
;;;
;;; WARNING: The sandbox rules in this file currently constitute
;;; Apple System Private Interface and are subject to change at any time and
;;; without notice.
;;;
(version 1)

(deny default)
(deny file-map-executable process-info* nvram*)
(deny dynamic-code-generation)

(deny mach-priv-host-port)

(import "system.sb")
(import "com.apple.corefoundation.sb")
(corefoundation)

;;; Homedir-relative path filters
(define (home-regex home-relative-regex)
    (regex (string-append "^" (regex-quote (param "HOME")) home-relative-regex)))

(define (home-subpath home-relative-subpath)
    (subpath (string-append (param "HOME") home-relative-subpath)))

(define (home-prefix home-relative-prefix)
    (prefix (string-append (param "HOME") home-relative-prefix)))

(define (home-literal home-relative-literal)
    (literal (string-append (param "HOME") home-relative-literal)))

(allow process-info* (target self))

;; For resolving symlinks, realpath(3), and equivalents.
(allow file-read-metadata)

;; For validating the entitlements of clients.
(allow process-info-codesignature)

;; Mach lookup
(allow mach-lookup
    (global-name "com.apple.accessibility.voices")
    (global-name "com.apple.accessibility.LiveSpeech")
    (global-name "com.apple.accessibility.livespeech")
    (global-name "com.apple.assistant.analytics")
    (global-name "com.apple.audio.audiohald")
    (global-name "com.apple.audio.coreaudiod")
    (global-name "com.apple.audio.AudioComponentRegistrar")
    (global-name "com.apple.BluetoothServices")
    (global-name "com.apple.CARenderServer")
    (global-name "com.apple.callkit.callcontrollerhost")
    (global-name "com.apple.cmio.registerassistantservice.system-extensions")
    (global-name "com.apple.coreservices.appleevents")
    (global-name "com.apple.coreservices.launchservicesd")
    (global-name "com.apple.coreservices.sharedfilelistd.xpc")
    (global-name "com.apple.DiskArbitration.diskarbitrationd")
    (global-name "com.apple.dock.fullscreen")
    (global-name "com.apple.dock.server")
    (global-name "com.apple.inputmethodkit.getxpcendpoint")
    (global-name "com.apple.inputmethodkit.launchagent")
    (global-name "com.apple.inputmethodkit.launcher")
    (global-name "com.apple.iohideventsystem")
    (global-name "com.apple.lsd.mapdb")
    (global-name "com.apple.lsd.modifydb")
    (global-name "com.apple.mobileassetd.v2")
    (global-name "com.apple.naturallanguaged")
    (global-name "com.apple.pasteboard.1")
    (global-name "com.apple.pluginkit.pkd")
    (global-name "com.apple.speech.recognition.AppleSpeechRecognition.prefs")
    (global-name "com.apple.speech.speechsynthesisd")
    (global-name "com.apple.speech.speechsynthesisd.arm64")
    (global-name "com.apple.speech.speechsynthesisd.x86_64")
    (global-name "com.apple.sysmond")
    (global-name "com.apple.tccd")
    (global-name "com.apple.tccd.system")
    (global-name "com.apple.touchbarserver.mig")
    (global-name "com.apple.tsm.uiserver")
    (global-name "com.apple.window_proxies")
    (global-name "com.apple.windowserver.active")
    (global-name "com.apple.windowmanager.server")
    (global-name "com.apple.uiintelligencesupport.agent")
    (global-name "com.apple.controlcenter.statusitems"))

;; Read/write access to preferences
(allow user-preference-read
    (preference-domain "com.apple.Accessibility")
    (preference-domain "com.apple.Accessibility.Assets")
    (preference-domain "com.apple.accessibility.LiveSpeech")
    (preference-domain "com.apple.accessibility.livespeech")
    (preference-domain "com.apple.assistant.backedup")
    (preference-domain "com.apple.CoreGraphics")
    (preference-domain "com.apple.coremedia")
    (preference-domain "com.apple.recognition.AppleSpeechRecognition.prefs")
    (preference-domain "com.apple.ServicesMenu.Services")
    (preference-domain "com.apple.SpeakSelection")
    (preference-domain "com.apple.speech.recognition.AppleSpeechRecognition.prefs")
    (preference-domain "com.apple.speech.voice.prefs")
    (preference-domain "com.apple.universalaccess")
    (preference-domain "com.apple.universalaccess.debug")
    (preference-domain "pbs")
    (preference-domain "kCFPreferencesAnyApplication"))

(allow user-preference-write
    (preference-domain "com.apple.accessibility.LiveSpeech")
    (preference-domain "com.apple.accessibility.livespeech")
    (preference-domain "com.apple.recognition.AppleSpeechRecognition.prefs")
    (preference-domain "com.apple.universalaccess"))

;; Read/write access to a temporary directory.
(allow file-read* file-write*
    (subpath (param "TMPDIR"))
    (subpath (param "DARWIN_CACHE_DIR")))

;; Audio device
(allow file-map-executable
    (literal "/System/Library/Components/CoreAudio.component/Contents/MacOS/CoreAudio")
    (literal "/System/Library/Extensions/AppleGFXHDA.kext/Contents/PlugIns/AppleGFXHDAHALPlugIn.bundle/Contents/MacOS/AppleGFXHDAHALPlugIn"))

(allow file-read* file-map-executable
    (subpath "/System/Library/TTSPlugins")
    (subpath "/System/Library/AccessibilityBundles"))
    
;; Read/write cache access
(let ((cache-path-filter (home-subpath "/Library/Caches/com.apple.accessibility.LiveSpeech")))
  (allow file-read* file-write* cache-path-filter)
  (allow file-issue-extension
    (require-all
      (extension-class "com.apple.app-sandbox.read" "com.apple.app-sandbox.read-write")
      cache-path-filter)))
(allow file-issue-extension
    (require-all
        (extension-class "com.apple.app-sandbox.read-write")
        (subpath "/")))
(allow file-issue-extension (extension-class "com.apple.app-sandbox.read"))

;; sandbox extension
(allow file-read* (extension "com.apple.app-sandbox.read"))

(allow ipc-posix-shm*)

;; Must be able to open user-clients for IOHID events
(allow iokit-open
    (iokit-user-client-class "IOAudioControlUserClient")
    (iokit-user-client-class "IOAudioEngineUserClient")
    (iokit-user-client-class "IOHIDParamUserClient"))
