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

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

(allow user-preference-read user-preference-write
       ;; Required by EAR.
       (preference-domain "com.apple.EmojiPreferences")
       ;; Your preference domain
       (preference-domain "com.apple.speech.localspeechrecognition")
)

(allow user-preference-read
       (preference-domain "com.apple.assistant")
       (preference-domain "com.apple.assistant.support")
       (preference-domain "com.apple.assistant.backedup")
       (preference-domain "com.apple.UnifiedAssetFramework")
       (preference-domain "kCFPreferencesAnyApplication")
)

;; Read/write access to a temporary directory.
(allow file-read* file-write*
       (subpath (string-append (param "DARWIN_USER_BASE_DIR") "/T/com.apple.speech.localspeechrecognition"))
       (subpath (string-append (param "DARWIN_USER_BASE_DIR") "/T/SpeechModelCache"))
       (subpath (string-append (param "DARWIN_USER_BASE_DIR") "/C/com.apple.speech.localspeechrecognition")))

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

;; mach service lookup
(allow mach-lookup
       (global-name "com.apple.appleneuralengine")
       (global-name "com.apple.mobileassetd.v2")
       (global-name "com.apple.triald.namespace-management")
       (global-name "com.apple.mobileasset.autoasset")
       (global-name "com.apple.FileCoordination")
       (global-name "com.apple.siri.uaf.service")
       (global-name "com.apple.siri.uaf.subscription.service")
       (global-name "com.apple.system.opendirectoryd.api")
       (global-name "com.apple.powerlog.plxpclogger.xpc")
       (global-name "com.apple.siri.analytics.assistant")
       (global-name "com.apple.symptom_diagnostics")
       (global-name "com.apple.audio.AudioComponentRegistrar")
       (global-name "com.apple.SystemConfiguration.configd")
)

;; Allow read-only access to $HOME/Library/Trial
(allow file-read*
    (home-subpath "/Library/Trial"))

;; Allow read-only access to $HOME/Library/UnifiedAssetFramework
(allow file-read*
    (home-subpath "/Library/UnifiedAssetFramework"))

;; Allow read-only access to $HOME/Library/ASRAssetOverrides
(allow file-read*
    (home-subpath "/Library/ASRAssetOverrides"))

;; Allow read-only access to data in $HOME/Library/Developer/UaaPKit/ASR/ -- needed for UaaP support
(allow file-read*
    (home-subpath "/Library/Developer/UaaPKit/ASR"))

;; Allow read-only access to $HOME/Library/Assistant/SiriVocabulary
(allow file-read*
    (home-subpath "/Library/Assistant/SiriVocabulary"))

;; iokit
(allow iokit-open
       (iokit-user-client-class "IOSurfaceRootUserClient")
       (iokit-user-client-class "H11ANEInDirectPathClient")
       (iokit-user-client-class "AGXDeviceUserClient")
)

;; Allow ANE framework to issue file read extensions for Trial assets
(allow file-issue-extension
       (require-all
           (extension-class "com.apple.aned.read-only" "com.apple.app-sandbox.read")
           (home-subpath "/Library/Trial/Treatments")))
           
           
;; Read access
(allow file-read*
       (subpath "/Library/Application Support/CrashReporter/SubmitDiagInfo.domains")
)

;; Filesystem access for consumed sandbox extensions
(allow file-read*
       (extension "com.apple.app-sandbox.read"))
(allow file-read* file-write*
       (extension "com.apple.app-sandbox.read-write"))
(allow file-read* file-read-metadata
       (subpath "/private/var/db/assetsubscriptiond/"))

;; Allow access to /System/Library/Components/AudioDSP.component/Contents/MacOS/AudioDSP
(allow file-map-executable
       (literal
                "/System/Library/Components/AudioDSP.component/Contents/MacOS/AudioDSP"))
