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

;; -------------------- From template --------------------

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

;; Prod
(deny default)
(deny file-map-executable iokit-get-properties 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)

;; 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.diagnosticextensionsd")))
  (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)))

;; -------------------- Not from template  --------------------

(allow mach-lookup
    (global-name "com.apple.nesessionmanager.content-filter")
    (global-name "com.apple.bird.token")
    (global-name "com.apple.identityservicesd.desktop.auth")
    ;; for embedded use com.apple.identityservicesd.embedded.auth if needed
    (global-name "com.apple.dnssd.service")
    (global-name "com.apple.AppSSO.service-xpc")
    (global-name "com.apple.logind")
    (global-name "com.apple.awdd")
    (global-name "com.apple.pluginkit.pkd")
    (global-name "com.apple.nsurlsessiond")
    (global-name "com.apple.metadata.mds")
    (global-name "com.apple.usernoted.daemon_client")
    (global-name "com.apple.nehelper")
    (global-name "com.apple.containermanagerd")
    (global-name "com.apple.lsd.mapdb")
    (global-name "com.apple.logd.admin")
    (global-name "com.apple.usernotifications.listener")
    (global-name "com.apple.usernotifications.usernotificationservice")
    (global-name "com.apple.cloudd")
    (global-name "com.apple.corefollowup.agent")
    ;; rdar://111156538 proxy access
    (global-name "com.apple.cfnetwork.cfnetworkagent")
    (global-name "com.apple.cfnetwork.AuthBrokerAgent")
    (global-name "com.apple.duetactivityscheduler")
    (global-name "com.apple.enhancedloggingd.bug-session-status.xpc")
)

(allow network-outbound
    (remote ip)
    (literal "/private/var/run/mDNSResponder")
)

;; CFE systems
(allow user-preference-read user-preference-write
      (preference-domain "group.com.apple.feedback")
      (preference-domain "com.apple.diagnosticextensionsd")
      (preference-domain "com.apple.DiagnosticExtensions.extensionTracker")
)

;; Other systems
(allow user-preference-read
    (preference-domain "com.apple.logging")
    (preference-domain "com.apple.security")
    (preference-domain "com.apple.ids")
    (preference-domain "com.apple.CFNetwork")
    (preference-domain "com.apple.AppleServiceToolkit")
)

(allow file-issue-extension
    (home-subpath "/Library/Logs/com.apple.diagnosticextensionsd")
)

;; Allow distributed notifications
(allow distributed-notification-post)

;; -------------------- Sandpile  --------------------

(allow file-write* file-read*
    (subpath "/")
)

(allow file-read-metadata
    (subpath "/")
)

(allow file-clone
    (subpath "/")
)

(allow iokit-get-properties)
(allow iokit-open
    (iokit-user-client-class "AppleNVMeEANUC")
    (iokit-user-client-class "AppleSMCClient")
    (iokit-user-client-class "RootDomainUserClient")
)

(allow user-preference-read user-preference-write
    (preference-domain "diagnosticextensionsd")
    (preference-domain "kCFPreferencesAnyApplication")
)

(allow mach-lookup
    (global-name #"com\.apple\.*")
)

(allow user-preference-read
    (preference-domain #"com\.apple\..*")
)
