;;; 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.
;;;
(version 1)

(allow (with report) default)
(allow (with report) file-map-executable process-info* nvram*)
(allow (with report) 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 distributed-notification-post)

;; For resolving symlinks, realpath(3), and equivalents.
(allow file-read-metadata)
(allow file-read-data)

;; For validating the entitlements of clients.
(allow process-info-codesignature)

;; Your preference domain
(allow user-preference-read user-preference-write
    (preference-domain  "com.apple.cloud.quota"
                        "com.apple.icloud.gm"
                        "com.apple.FeatureAccessAgent"
                        "com.apple.CloudSubscriptionFeatures"
                        "com.apple.CloudSubscriptionFeatures.cache"
                        "com.apple.CloudSubscriptionFeatures.config"
                        "com.apple.CloudSubscriptionFeatures.geoCache"
                        "com.apple.CloudSubscriptionFeatures.gmCache"
                        "com.apple.CloudSubscriptionFeatures.datadetectors"
                        "com.apple.CloudSubscriptionFeatures.ticket.cache"
                        "com.apple.CloudSubscriptionFeatures.migrations"
                        "com.apple.CloudSubscriptionFeatures.gm.bypass"
                        "com.apple.CloudSubscriptionFeatures.followup.engagement"
                        "com.apple.CloudSubscriptionFeatures.optIn"
                        "com.apple.CloudSubscriptionFeatures.gmBypass"
                        "com.apple.CloudSubscriptionFeatures.diagnostic"
                        "com.apple.CloudSubscriptionFeatures.waitlist"
                        "kCFPreferencesAnyApplication"
    )
)

;; Read/write access to a temporary directory.
(allow file-read* file-write*
        (subpath (param "TMPDIR"))
        (subpath (param "DARWIN_CACHE_DIR"))
        (literal        "/Library/Keychains/System.keychain")
        (home-prefix   "/Library/Keychains/login.keychain-db")
        (home-subpath   "/Library/Application Support/com.apple.FeatureAccessAgent")
)

;; Read/write cache access
(let ((cache-path-filter (home-subpath "/Library/Caches/com.apple.FeatureAccessAgent")))
  (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 CloudSubscriptionFeatures framework
(let ((cache-path-filter (literal "/System/Library/PrivateFrameworks/CloudSubscriptionFeatures.framework")))
  (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 httpStorages access
(let ((cache-path-filter (home-subpath "/Library/HTTPStorages/com.apple.FeatureAccessAgent")))
  (allow file-read* file-write* cache-path-filter))
      
;; Mach-lookup
(allow mach-lookup
    (global-name "com.apple.AppSSO.service-xpc"
                 "com.apple.accountsd.accountmanager"
                 "com.apple.ak.anisette.xpc"
                 "com.apple.system.opendirectoryd.api"
                 "com.apple.metadata.mds"
                 "com.apple.cloudd"
                 "com.apple.dnssd.service"
                 "com.apple.windowserver.active"
                 "com.apple.apsd"
                 "com.apple.SystemConfiguration.configd"
                 "com.apple.securityd.xpc"
                 "com.apple.SecurityServer"
                 "com.apple.lsd.mapdb"
                 "com.apple.cache_delete"
                 "com.apple.cache_delete.public"
                 "com.apple.photos.service"
                 "com.apple.corefollowup.agent"
                 "com.apple.usymptomsd"
                 "com.apple.assetsd"
                 "com.apple.DiskArbitration.diskarbitrationd"
                 "com.apple.tccd.system"
                 "com.apple.tccd"
                 "com.apple.duetactivityscheduler"
                 "com.apple.modelcatalog.catalog"
                 "com.apple.generativeexperiences.availabilityService"
                 "com.apple.ctkd.token-client"
                 "com.apple.mobileactivationd"
                 "com.apple.aa.daemon.xpc"
                 "com.apple.ind.cloudfeatures"
                 "com.apple.ind.xpc"
                 "com.apple.nesessionmanager.content-filter"
                 "com.apple.commcenter.coretelephony.xpc"
                 "com.apple.TapToRadarKit.service"
    )
)

;; Mach-register
;; (allow mach-register
;;    (global-name    "com.apple.ind.aps")
;; )

;; IOKit
(allow iokit-get-properties)
(allow iokit-open
    (iokit-registry-entry-class    "AppleKeyStoreUserClient")
)

;; IPC
(allow ipc-posix-shm-read-data ipc-posix-shm-write-create ipc-posix-shm-write-data
       (ipc-posix-name  "com.apple.AppleDatabaseChanged")
)

;; Networking
(allow system-socket)
(allow network-outbound)

;; Opening pref pane
(allow lsopen)

;; NVRAM
(allow nvram-get
    (nvram-variable "4D1EDE05-38C7-4A6A-9CC6-4BCCA8B38C14:MLB"
                    "4D1EDE05-38C7-4A6A-9CC6-4BCCA8B38C14:ROM"
    )
)

;; fsctl
(allow system-fsctl
    (fsctl-command HFSIOC_GET_LOW_DISK)
    (fsctl-command APFSIOC_GET_NEAR_LOW_DISK)
)

;; Common apple internal rules
(with-filter (system-attribute apple-internal)
    (allow sysctl-read sysctl-write
           (sysctl-name "vm.footprint_suspend"               ; dyld performance reporting
                        "vm.task_no_footprint_for_debug")))  ; for malloc stack logging and other debugging tools

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

