;;; Copyright (c) 2021 Apple Inc.  All Rights reserved.

(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))
(allow process-info-dirtycontrol (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.intelligenceplatform"))

(allow managed-preference-read (preference-domain "kCFPreferencesAnyApplication"))

(allow user-preference-read
    (preference-domain "kCFPreferencesAnyApplication")
    (preference-domain "com.apple.suggestions")
)

;; Allow access to GPU for CoreML
(allow iokit-open
    (iokit-user-client-class "AppleKeyStoreUserClient")
    (iokit-user-client-class "AGXDeviceUserClient")
    (iokit-user-client-class "AGXCommandQueue")
    (iokit-user-client-class "AGXDevice")
    (iokit-user-client-class "AGXDeviceUserClient")
    (iokit-user-client-class "AGXSharedUserClient")
    (iokit-user-client-class "IOAccelContext")
    (iokit-user-client-class "IOAccelDevice")
    (iokit-user-client-class "IOAccelSharedUserClient")
    (iokit-user-client-class "IOAccelSubmitter2")
    (iokit-user-client-class "IOAccelContext2")
    (iokit-user-client-class "IOAccelDevice2")
    (iokit-user-client-class "IOAccelSharedUserClient2")
    (iokit-user-client-class "IOSurfaceRootUserClient")
    (iokit-user-client-class "H11ANEInDirectPathClient")
)

(allow mach-lookup
    (global-name "com.apple.appleneuralengine")
    (global-name "com.apple.biome.access.system")
    (global-name "com.apple.biome.access.user")
    (global-name "com.apple.biome.compute.source.user")
    (global-name "com.apple.biomesyncd.sync")
    (global-name "com.apple.coreduetd.knowledge")
    (global-name "com.apple.coreservices.quarantine-resolver")
    (global-name "com.apple.intelligenceplatform.Internal")
    (global-name "com.apple.intelligenceplatform.EventLog")
    (global-name "com.apple.intelligenceplatform.InferenceSupport")
    (global-name "com.apple.intelligenceplatform.AssetRegistry")
    (global-name "com.apple.linkd.registry")
    (global-name "com.apple.linkd.transcript")
    (global-name "com.apple.locationd.registration")
    (global-name "com.apple.lsd.mapdb")
    (global-name "com.apple.lsd.modifydb")
    (global-name "com.apple.photos.service")
    (global-name "com.apple.proactive.PersonalizationPortrait.Location.readOnly")
    (global-name "com.apple.proactive.PersonalizationPortrait.Topic.readOnly")
    (global-name "com.apple.proactive.PersonalizationPortrait.NamedEntity.readOnly")
    (global-name "com.apple.remindd")
    (global-name "com.apple.routined.registration")
    (global-name "com.apple.siriknowledged.koa.donate")
    ;; Allow connections to PowerLog service
    (global-name "com.apple.powerlog.plxpclogger.xpc")
    (global-name "com.apple.PerfPowerTelemetryClientRegistrationService")
    (global-name "com.apple.mediaanalysisd.service.public")
    (global-name "com.apple.SetStoreUpdateService")
)

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

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

(allow mach-lookup
    (global-name "com.apple.coreduetd.people"))
(allow file-read*
    (literal "/private/var/db/CoreDuet/People/interactionC.db")
    (literal "/private/var/db/CoreDuet/People/interactionC.db-shm")
    (literal "/private/var/db/CoreDuet/People/interactionC.db-wal"))
(allow file-write*
    (literal "/private/var/db/CoreDuet/People/interactionC.db-shm"))

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

;; Allow BehaviorWindowLimiter to write to HU defaults
(allow user-preference-read user-preference-write
       (preference-domain "com.apple.humanunderstanding"))

;; Contacts
(import "contacts.sb")
(contacts-client (param "HOME") (param "TMPDIR"))

;; Sandbox Extensions
(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*))
