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

(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.healthd")
       (regex #"com\.apple\.private\.health\..*"))

;; 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.healthd")))
  (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/Health")
    (home-subpath "/Library/MedicalID"))
    
(allow file-read*
    (home-subpath "/Library/Caches/CloudKit/com.apple.healthd"))

;; Security.framework
; mds: mds.lock, mdsDirectory.db, mdsObject.db
; 1. extension "security: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 "security:mds"))
;    uid  > 0: r
(allow file-read* file-write* (extension "security: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* (extension "security:messages"))

;; LaunchServices (SQLite3 db)
(allow file-read*
       (home-literal "/Library/Preferences"))
(allow file-read* file-write*
       (home-subpath "/Library/Preferences/com.apple.LaunchServices.QuarantineEventsV2")
       (home-subpath "/Library/Preferences/com.apple.LaunchServices.QuarantineEventsV2-journal")
       (home-subpath "/Library/Preferences/com.apple.LaunchServices.QuarantineEventsV2-shm")
       (home-subpath "/Library/Preferences/com.apple.LaunchServices.QuarantineEventsV2-wal"))

;; Preferences (file read in-process)
(allow file-read*
       (home-subpath "/Library/Preferences/com.apple.security.plist")
       (literal      "/Library/Preferences/com.apple.security.plist"))
         
(allow user-preference-read
    (preference-domain "com.apple.CloudKit")
    (preference-domain "kCFPreferencesAnyApplication"))

(allow file-read* file-map-executable
    (subpath "/System/Library/Health/Plugins"))

(with-filter (system-attribute apple-internal)
    (allow file-read* file-map-executable
        (subpath "/AppleInternal/Library/Health/Plugins")))

(allow mach-lookup
    (global-name "com.apple.accountsd.accountmanager")
    (global-name "com.apple.aa.usernotifications.xpc")
    (global-name "com.apple.ak.anisette.xpc")
    (global-name "com.apple.ak.auth.xpc")
    (global-name "com.apple.apsd")
    (global-name "com.apple.cloudd")
    (global-name "com.apple.coreservices.quarantine-resolver")
    (global-name "com.apple.commcenter.coretelephony.xpc")
    (global-name "com.apple.healthrecordsd")
    (global-name "com.apple.lsd.mapdb")
    (global-name "com.apple.lsd.modifydb")
    (global-name "com.apple.nehelper")
    (global-name "com.apple.securityd.xpc")
    (global-name "com.apple.server.bluetooth.le.att.xpc")
    (global-name "com.apple.SystemConfiguration.configd")
    (global-name "com.apple.tccd")
    (global-name "com.apple.tccd.system")
    (global-name "com.apple.usernotifications.listener")
    (global-name "com.apple.usernotifications.usernotificationservice")
    (global-name "com.apple.windowserver.active"))

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

;; Process info
(allow process-info* process-info-codesignature)

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