;;; Copyright (c) 2017-2022 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)

;;; TODO: Change these to deny before finalizing this profile.
(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)

(system-network)
(allow network-outbound)

;;; 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)

(allow file-read* file-read-metadata
    (subpath "/private/var/db/assetsubscriptiond/"))

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

;; Preference domain
(allow user-preference-read user-preference-write
       (preference-domain "com.apple.sirittsd")
       (preference-domain "com.apple.voiceservices")
       (preference-domain "com.apple.osprey")
       (preference-domain "com.apple.coremedia")
       (preference-domain "com.apple.siri.audio")
       (preference-domain "com.apple.SpeakSelection")
       (preference-domain "com.apple.gms.availability")
       (preference-domain "com.apple.assistant.support")
       (preference-domain "com.apple.assistant.backedup")
       (preference-domain "com.apple.GenerativeFunctions.GenerativeFunctionsInstrumentation")
)
(allow user-preference-read
    (preference-domain "kCFPreferencesAnyApplication")
    (preference-domain "com.apple.UnifiedAssetFramework")
)

;; Mach lookups
(allow mach-lookup
(global-name
             "com.apple.appleneuralengine"
             "com.apple.audio.AudioComponentRegistrar"
             "com.apple.audio.audiohald"
             "com.apple.coremedia.systemcontroller.xpc"
             "com.apple.mobileasset.autoasset"
             "com.apple.mobileassetd.v2"
             "com.apple.securityd.xpc"
             "com.apple.siri.analytics.assistant"
             "com.apple.siri.uaf.service"
             "com.apple.siri.uaf.subscription.service"
             "com.apple.sirittsd"
             "com.apple.symptom_diagnostics"
             "com.apple.system.opendirectoryd.api"
             "com.apple.tailspind"
             "com.apple.modelmanager"
             "com.apple.biome.access.user"
             "com.apple.modelcatalog.catalog"
             "com.apple.SystemConfiguration.configd"
))



;; Read/write access to a temporary directory.
(allow file-read* file-write*
       (subpath (param "TMPDIR"))
       (subpath (param "DARWIN_CACHE_DIR"))
       (subpath "/private/tmp/")
)
;; Read access to /private/var/db/mds/messages/*/se_SecurityMessages
(allow file-read*
       (subpath (param "SECURITY_MESSAGES_DIR")))

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

;; Read/write HTTPStorage
(let ((cache-path-filter (home-subpath "/Library/HTTPStorages/com.apple.sirittsd")))
  (allow file-read* file-write* cache-path-filter)
)

;; Read/write Trial
(let ((cache-path-filter (home-subpath "/Library/Trial/")))
  (allow file-read* file-write* cache-path-filter)
)

;; Read UnifiedAssetFramework
(allow file-read*
    (home-subpath "/Library/UnifiedAssetFramework/")
)

;; Read/write Logs
(let ((cache-path-filter (home-subpath "/Library/Logs/SiriTTSService/")))
  (allow file-read* file-write* cache-path-filter)
)

;; Read/write VoiceServices folder
(allow file-read* file-write*
    (home-subpath "/Library/VoiceServices/")
)

;; Read/write SiriTTS cache folder
(allow file-read* file-write*
    (home-subpath "/Library/Caches/SiriTTS/")
)

;; Read/write sandbox-extended request.outputPath
(allow file-read* file-write*
    (extension "com.apple.app-sandbox.read-write")
)

;; Group container access
(allow file-read* file-write*
    (extension "com.apple.sandbox.application-group"))
(allow mach-lookup (global-name "com.apple.containermanagerd"))

;; Allow mmap
(allow file-read* file-map-executable
       (subpath "/System/Library/Components/AudioCodecs.component/")
       (subpath "/System/Library/Components/AudioDSP.component/")
       (subpath "/System/Library/Components/CoreAudio.component/")
)

;; Allow Audio ipc shm read
(allow ipc-posix-shm-read-data ipc-posix-shm-write-data ipc-posix-shm-read-metadata
       (ipc-posix-name-regex #"^AudioIO")
)

;; iokit access, needed by ANE
(allow iokit-open
         (iokit-user-client-class "AGXCommandQueue"
                                  "AGXDevice"
                                  "AGXDeviceUserClient"
                                  "AGXSharedUserClient"
                                  "H11ANEInDirectPathClient"
                                  "IOAccelContext"
                                  "IOAccelContext2"
                                  "IOAccelDevice"
                                  "IOAccelDevice2"
                                  "IOAccelSharedUserClient"
                                  "IOAccelSharedUserClient2"
                                  "IOAccelSubmitter2"
                                  "IOSurfaceRootUserClient"
                                  ;; rdar://84983834 needed by Trial
                                  "AppleKeyStoreUserClient"))
