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

;; Your preference domain
(allow user-preference-read user-preference-write
       (preference-domain "com.apple.mlruntimed"))

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

;; Read/write cache access
(let ((cache-path-filter (home-subpath "/Library/Caches/com.apple.mlruntimed")))
  (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 access to ~/Library/DES/*
(allow file-read* file-write*
       (home-subpath "/Library/DES/")
)

;; Read access to CrashReporter
(allow file-read*
       (subpath "/Library/Application Support/CrashReporter")
)
       
;; Read access to /usr/libexec/mlruntimed
(allow file-read*
       (literal "/usr/libexec")
       (literal "/usr/libexec/mlruntimed")
)


;; Read access to other preference domains
(allow user-preference-read
       (preference-domain "com.apple.assistant.backedup")
       (preference-domain "com.apple.assistant.support")
       (preference-domain "com.apple.CFNetwork")
       (preference-domain "com.apple.DataDeliveryServices")
       (preference-domain "com.apple.homed")
       (preference-domain "com.apple.LanguageModeling")
       (preference-domain "com.apple.mobileslideshow")
       (preference-domain "com.apple.NLPLearner")
       (preference-domain "com.apple.nsurlcache")
       (preference-domain "com.apple.parsecd")
       (preference-domain "com.apple.security")
       (preference-domain "com.apple.siri.morphunassetsupdaterd")
       (preference-domain "com.apple.speech.speechsynthesis")
       (preference-domain "com.apple.SubmitDiagInfo")
       (preference-domain "kCFPreferencesAnyApplication")
)

;;; Security.framework
; mds: mds.lock, mdsDirectory.db, mdsObject.db
; 1. extension "mds"
;    uid == 0: r+w /private/var/db/mds/system
;    uid  > 0: r+w <_DARWIN_USER_CACHE_DIR>/mds
; 2. /private/var/db/mds/system/{mdsDirectory.db,mdsObject.db}
;    uid == 0: r+w (already covered by (extension "mlruntimed:mds"))
;    uid  > 0: r
(allow file-read* file-write* (extension "mlruntimed:mds"))
(allow file-read*
       (literal "/private/var/db/mds/system/mdsDirectory.db")
       (literal "/private/var/db/mds/system/mdsObject.db"))
; 3. se_SecurityMessages:
;    uid  < 500: /private/var/db/mds/messages/se_SecurityMessages
;    uid >= 500: /private/var/db/mds/messages/<uid>/se_SecurityMessages
(allow file-read*
       (literal (param "SECURITY_MESSAGES")))

;; Execute plugins
(allow file-map-executable
    (subpath "/System/Library/DistributedEvaluation/Plugins"))

;; Networking
(system-network)
(allow network-outbound)
(allow ipc-posix-shm-read-data
       (ipc-posix-name "FNetwork.defaultStorageSession"))

;; mach xpc service lookup
(allow mach-lookup
       (global-name "com.apple.adprivacyd.pfl")
       (global-name "com.apple.assistant.analytics")
       (global-name "com.apple.corespeech.speechmodeltraining.xpc")
       (global-name "com.apple.lsd.mapdb")
       (global-name "com.apple.lsd.modifydb")
       (global-name "com.apple.parsecd")
       (global-name "com.apple.photoanalysisd")
       (global-name "com.apple.pluginkit.pkd")
       (global-name "com.apple.siri.analytics.assistant")
       (global-name "com.apple.siriinferenced")
       (global-name "com.apple.suggestd.fides")
       (global-name "com.apple.trial.status")
)

;; iokit-open AppleAPFSUserClient
(allow iokit-open
       (iokit-user-client-class "AppleAPFSUserClient")
)

;; allow issue file-extension 
(allow file-issue-extension
    (require-all
      	(extension-class "com.apple.app-sandbox.read" "com.apple.app-sandbox.read-write")
    )
)

;; allow read/write for granted extension
(with-filter (extension "com.apple.app-sandbox.read")
    (allow file-read*))

(with-filter (extension "com.apple.app-sandbox.read-write")
    (allow file-read* file-write*))
