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

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

;; Your preference domain
(allow user-preference-read user-preference-write
       (preference-domain "com.apple.translationd"
                          "com.apple.osprey"
                          "group.com.apple.private.translation"))
(allow user-preference-read
       (preference-domain "kCFPreferencesAnyApplication"
                          "com.apple.UnifiedAssetFramework"))

;; Read/write access to a temporary directory.
(allow file-read* file-write*
       (subpath (param "TMPDIR"))
       (subpath (param "DARWIN_CACHE_DIR"))
       ;; For users with home directories on a different volume
       (mount-relative-regex #"^/\.TemporaryItems(/|$)"))

;; Read/write cache access
(let ((cache-path-filter (home-subpath "/Library/Caches/com.apple.translationd")))
  (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-read* file-write*
       (home-subpath "/Library/Translation")
       (home-subpath "/Library/HTTPStorages/com.apple.translationd"))
       
;; Group container access
(allow file-read* file-write*
    (home-subpath "/Library/Group Containers/group.com.apple.private.translation"))

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

(allow mach-lookup
       (global-name "com.apple.AppSSO.service-xpc"
                    "com.apple.assistant.settings"
                    "com.apple.audio.AudioComponentRegistrar"
                    "com.apple.audio.audiohald"
                    "com.apple.dnssd.service"
                    "com.apple.lsd.mapdb"
                    "com.apple.metadata.mds"
                    "com.apple.mobileassetd.v2"
                    "com.apple.nehelper"
                    "com.apple.securityd.xpc"
                    "com.apple.siri.analytics.assistant"
                    "com.apple.usymptomsd"
                    "com.apple.containermanagerd"
                    "com.apple.pluginkit.pkd"
                    "com.apple.mobileasset.autoasset"
                    "com.apple.triald"
                    "com.apple.triald.namespace-management"
                    "com.apple.sirittsd"
                    "com.apple.siri.uaf.service"
                    "com.apple.system.opendirectoryd.api"
                    "com.apple.siri.uaf.subscription.service"
                    "com.apple.modelmanager"
                    "com.apple.modelcatalog.catalog"
       )
)

(allow file-map-executable
       (literal "/System/Library/Components/AudioCodecs.component/Contents/MacOS/AudioCodecs"
                "/System/Library/Components/AudioDSP.component/Contents/MacOS/AudioDSP"
                "/System/Library/Components/CoreAudio.component/Contents/MacOS/CoreAudio"))

(allow ipc-posix-shm-read-data ipc-posix-shm-write-data (ipc-posix-name-prefix "AudioIO"))

(allow network-outbound
       (remote tcp)
       (remote udp))

(allow file-read*
       (literal "/Library/Preferences/com.apple.networkextension.uuidcache.plist")
       (literal "/private/var/db/mds/messages/se_SecurityMessages")
       (regex #"/private/var/db/mds/messages/.*/se_SecurityMessages")
       (subpath "/private/var/db/com.apple.modelcatalog/sideload/")
       (subpath "/System/Library/AssetsV2/locks/com.apple.UnifiedAssetFramework/")
       (subpath "/System/Library/AssetsV2/com_apple_MobileAsset_UAF_FM_GenerativeModels/purpose_auto/")
       (subpath "/private/var/MobileAsset/PreinstalledAssetsV2/InstallWithOs/com_apple_MobileAsset_UAF_FM_GenerativeModels/")
       (subpath "/System/Library/PreinstalledAssetsV2/RequiredByOs/com_apple_MobileAsset_UAF_FM_GenerativeModels/")
       (subpath "/System/Library/AssetsV2/com_apple_MobileAsset_UAF_FM_Overrides/purpose_auto/")
       (subpath "/private/var/MobileAsset/PreinstalledAssetsV2/InstallWithOs/com_apple_MobileAsset_UAF_FM_Overrides/")
       (subpath "/System/Library/PreinstalledAssetsV2/RequiredByOs/com_apple_MobileAsset_UAF_FM_Overrides/")
)

;; Allow reading TTS files
(allow file-read* file-map-executable
    (subpath "/System/Library/TTSPlugins")
    (subpath "/System/Library/AccessibilityBundles"))

;; Allow reading UAF metadata
(allow file-read* file-read-metadata
    (subpath "/private/var/db/assetsubscriptiond"))
