;;; Copyright (c) 2017 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 iokit-get-properties 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*)
(allow mach-task-name)

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

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

;; Preference domain
(allow user-preference*
       (preference-domain "com.apple.speakerrecognition")
       (preference-domain "com.apple.voicetrigger")
       (preference-domain "com.apple.voicetrigger.notbackedup")
       (preference-domain "com.apple.CoreSpeech")
       (preference-domain "com.apple.coreaudio")
       (preference-domain "kCFPreferencesAnyApplication")
       (preference-domain "com.apple.assistant.backedup")
       (preference-domain "com.apple.assistant.support")
       (preference-domain "com.apple.avfaudio")
       (preference-domain "com.apple.avfoundation.avvc")
       (preference-domain "com.apple.Siri")
       (preference-domain "com.apple.UnifiedAssetFramework")
       (preference-domain "com.apple.TelephonyUtilities")
       (preference-domain "com.apple.assistant")
)

;; Read / Execute access
(allow file-read* file-map-executable
       (subpath "/System/Library/Components/AudioCodecs.component/")
       (subpath "/System/Library/Components/AudioDSP.component/")
       (subpath "/System/Library/Components/CoreAudio.component/")
       (subpath "/System/Library/Audio/Plug-Ins/RemoteInput/")
)

;; Read access
(allow file-read*
       (subpath "/Library/Application Support/CrashReporter/DiagnosticMessagesHistory.plist")
       (subpath "/System/Library/Audio/Plug-Ins/RemoteInput/")
       (subpath "/private/var/MobileAsset/")
       (subpath "/private/var/db/assetsubscriptiond/")
       (home-subpath "/Library/Trial/")
       (home-subpath "/Library/UnifiedAssetFramework/")
       (home-subpath "/Library/ASRAssetOverrides/")
       (home-subpath "/Library/Biome/sets/")
)

;; Read/write access
(allow file-read* file-write*
       (extension "com.apple.sandbox.application-group")
       (subpath (param "TMPDIR"))
       (subpath (param "DARWIN_CACHE_DIR"))
       (subpath (param "DARWIN_USER_DIR"))
       (subpath "/private/var/tmp/siriBC")
       (subpath "/private/var/tmp/SiriMessages/")
       (home-subpath "/Library/VoiceTrigger/")
       (home-subpath "/Library/Caches/CoreSpeech")
       (home-subpath "/Library/Caches/VoiceTrigger/")
       (home-subpath "/Library/Caches/com.apple.speech.siri/")
       (home-subpath "/Library/Logs/VoiceTrigger/")
       (home-subpath "/Library/Logs/Assistant/")
       (home-subpath "/Library/Logs/Assistant/AVVCCapture/")
       (home-subpath "/Library/Logs/Assistant/ssr/")
       (home-subpath "/Library/Logs/Assistant/TTRAudio/")
       (home-subpath "/Library/Assistant/DictationSampled/")
       (home-subpath "/Library/Assistant/AssistantSampled/")
       (home-subpath "/Library/Assistant/SiriVocabulary/")
)

(allow file-issue-extension
       (home-subpath "/Library/VoiceTrigger/"))

;; mach xpc service lookup
(allow mach-lookup
       (global-name "com.apple.assistant.analytics")
       (global-name "com.apple.audio.SystemSoundServer-OSX")
       (global-name "com.apple.mobileassetd.v2")
       (global-name "com.apple.remoted")
       (global-name "com.apple.audio.audiohald")
       (global-name "com.apple.audio.AudioComponentRegistrar")
       (global-name "com.apple.PowerManagement.control")
       (global-name "com.apple.siri.activation")
       (global-name "com.apple.siri.activation.horseman")
       (global-name "com.apple.siri.activation.blackbird")
       (global-name "com.apple.BTLEAudioController.xpc")
       (global-name "com.apple.securityd.xpc")
       (global-name "com.apple.corespeech.corespeechd.activation.xpc")
       (global-name "com.apple.tccd.system")
       (global-name "com.apple.windowserver.active")
       (global-name "com.apple.systemstatus.activityattribution")
       (global-name "com.apple.powerlog.plxpclogger.xpc")
       (global-name "com.apple.systemstatus.publisher")
       (global-name "com.apple.siri.analytics.assistant")
       (global-name "com.apple.sirittsd")
       (global-name "com.apple.audio.AudioSession")
       (global-name "com.apple.server.bluetooth.general.xpc")
       (global-name "com.apple.server.bluetooth.le.att.xpc")
       (global-name "com.apple.bluetooth.system")
       (global-name "com.apple.biome.access.user")
       (global-name "com.apple.biome.access.system")
       (global-name "com.apple.coreduetd.people")
       (global-name "com.apple.containermanagerd")
       (global-name "com.apple.duetactivityscheduler")
       (global-name "com.apple.corespeech_launchagent.xpc")
       (global-name "com.apple.system.opendirectoryd.api")
       (global-name "com.apple.intelligenceflow.context")
       (global-name "com.apple.intelligenceflow.querydecoration")
       (global-name "com.apple.mobileasset.autoasset")
       (global-name "com.apple.AudioAccessoryServices")
       (global-name "com.apple.siri.uaf.subscription.service")
       (global-name "com.apple.audioanalyticsd")
)

;; iokit
(allow iokit-open
       (iokit-user-client-class "AppleKeyStoreUserClient")
       (iokit-user-client-class "RootDomainUserClient")
)

;; iokit-get-property
(allow iokit-get-properties
        (iokit-property "AppleClamshellState")
        (iokit-property "IOConsoleUsers")
        (iokit-property "Wake Reason")
        (iokit-property "IOPMSystemSleepType")
        (iokit-property "DesktopMode")
        (iokit-property "IORegistryEntryPropertyKeys")
        (iokit-property "product-id")
        (iokit-property "platform-name")
        (iokit-property "role")
        (iokit-property "supports-always-listening")
        (iokit-property "acoustic-id")
        (iokit-property "supports-barge-in")
        (iokit-property "ane-type")
        (iokit-property "chip-id")
)

(allow ipc-posix-shm-read-data
       (ipc-posix-name-regex #"^AudioIO")
)

(allow ipc-posix-shm-write-data
       (ipc-posix-name-regex #"^AudioIO")
)

(allow ipc-posix-shm-read-metadata
       (ipc-posix-name-regex #"^AudioIO")
)

(allow device-microphone)

;; Read/write cache access
(let ((cache-path-filter (home-subpath "/Library/Caches/com.apple.corespeechd")))
  (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)))

;;; Audio Diagnostics
(with-filter (system-attribute apple-internal)
  (allow file-read* file-map-executable
    (literal "/usr/local/lib/libAudioDiagnostics.dylib")))
