;;; 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.
;;;
;;; Template copied from: https://at.apple.com/G0Cw8Q
(version 1)

;;; DEV ONLY
;(allow (with report) default)
;(allow (with report) file-map-executable process-info* nvram*)
;(allow (with report) dynamic-code-generation)

;; Taking the training wheel off!
(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.assistant_cdmd")
       (preference-domain "com.apple.siri.cdm"))

(allow user-preference-read
       (preference-domain "com.apple.assistant.backedup")
       (preference-domain "com.apple.assistant.support")
       (preference-domain "com.apple.UnifiedAssetFramework")
       (preference-domain "com.apple.siri.vocabulary")
       (preference-domain "com.apple.internal.ck")
       (preference-domain "kCFPreferencesAnyApplication")
       (preference-domain "com.apple.gms.availability"))

(allow file-read*
    (subpath "/private/tmp")
    (literal "/private/var/db/eligibilityd/eligibility.plist")
)

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

(allow file-read* file-write*
       (home-subpath "/Library/Caches/com.apple.e5rt.e5bundlecache/")
       (home-subpath "/Library/Assistant/SiriNLUOverrides/")
       (home-subpath "/Library/Assistant/SiriVocabulary")
       (home-subpath "/Library/Assistant/ContinuousDialogManager")
       (home-subpath "/Library/Caches/ssu")
       (home-subpath "/Library/Assistant/SiriVocabulary-Staged"))

(allow file-read* file-read-metadata
       (home-subpath "/Library/UnifiedAssetFramework/")
       (home-subpath "/Library/Trial/")
       (subpath "/private/var/db/assetsubscriptiond/"))

;; Add internal only directories for testing
(with-filter (system-attribute apple-internal)
    (allow file-read* file-read-metadata
        (subpath "/private/tmp/CDM")
        (path "/AppleInternal/Library/Assistant/InternalConfig.plist")))

;; Read/write cache access
(let ((cache-path-filter (home-subpath "/Library/Caches/com.apple.assistant_cdmd")))
  (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 iokit-open
       (iokit-user-client-class "IOSurfaceRootUserClient"))

;; Allow mach lookup
; Keep case-sensitive alphabetically sorted
(allow mach-lookup
    (global-name "com.apple.siri.uaf.service"
                 "com.apple.coreduetd.people"
                 "com.apple.coreservices.quarantine-resolver"
                 "com.apple.lsd.mapdb"
                 "com.apple.lsd.modifydb"
                 "com.apple.mobileasset.autoasset"
                 "com.apple.modelmanager"
                 "com.apple.modelcatalog.catalog"
                 "com.apple.siri.analytics.assistant"
                 "com.apple.triald.namespace-management"
                 "com.apple.generativeexperiences.availabilityService"
                 "com.apple.system.opendirectoryd.api"
                 "com.apple.siri.uaf.subscription.service"))
