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

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

;; Required for getting information about processes via either libproc or csops.
(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)

;; Access to preferences
(allow user-preference-read user-preference-write
        (preference-domain "com.apple.AdLib")
        (preference-domain "com.apple.AdPlatforms")
        (preference-domain "com.apple.ap.adservicesd")
        (preference-domain "com.apple.ap.promotedcontentd"))

(allow user-preference-read
        (preference-domain "com.apple.security.plist")
        (preference-domain "com.apple.springboard")
        (preference-domain "com.apple.ap.promotedcontentd")
        (preference-domain "com.apple.AppleMediaServices")
        (preference-domain "com.apple.CFNetwork")
        (preference-domain "com.apple.security")
        (preference-domain "com.apple.itunesstored")
        (preference-domain "com.apple.amsengagementd")
        (preference-domain "kCFPreferencesAnyApplication"))

;; Access to the general App Support Storage.
;; This is where all EFS data will be stored.
(let ((appSupport-path-filter (home-subpath "/Library/Application Support/com.apple.ap.promotedcontentd")))
  (allow file-read* file-write* appSupport-path-filter))

;; Temporary folder.
(allow file-read* file-write*
    (subpath (param "DARWIN_USER_TEMP")))

;; Allow to read /usr/libexec and /usr/libexec/promotedcontentd
(allow file-read*
        (literal "/usr/libexec")
        (literal "/usr/libexec/promotedcontentd"))

;; Needed by LaunchServices / CoreServices.
(allow ipc-posix-shm*
       (ipc-posix-name-regex #"^/tmp/com\.apple\.csseed\.[0-9]+$"))
       
;; Security framework database updates
(allow ipc-posix-shm-read-data ipc-posix-shm-write-create ipc-posix-shm-write-data
       (ipc-posix-name "com.apple.AppleDatabaseChanged"))

;; cookied?
(allow ipc-posix-shm-read-data
        (ipc-posix-name "FNetwork.defaultStorageSession"))

;; Mach services
(allow mach-lookup
    (global-name "com.apple.SecurityServer")
    (global-name "com.apple.SystemConfiguration.configd")
    (global-name "com.apple.accountsd.accountmanager")
    (global-name "com.apple.ap.promotedcontentd")
    (global-name "com.apple.commcenter.coretelephony.xpc")
    (global-name "com.apple.cookied")
    (global-name "com.apple.fpsd")
    (global-name "com.apple.lsd.mapdb")
    (global-name "com.apple.securityd.xpc")
    (global-name "com.apple.adid")
    (global-name "com.apple.symptom_diagnostics")
    (global-name "com.apple.xpc.amsaccountsd")
    (global-name "com.apple.fairplaydeviceidentityd")
    (global-name "com.apple.duetactivityscheduler"))

;; 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 "promotedcontentd:mds"))
;    uid  > 0: r
(allow file-read* file-write*
       (extension "promotedcontentd:mds"))
(allow file-read*
       (literal "/private/var/db/mds/system/mdsDirectory.db")
       (literal "/private/var/db/mds/system/mdsObject.db"))
; 3. se_SecurityMessages
(allow file-read*
       (literal (param "SECURITY_MESSAGES")))

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

;; mDNSResponder
(allow network-outbound
       (path "/private/var/run/mDNSResponder")
       (remote ip)
       (control-name "com.apple.net.netagent"))

;; IOKit read access
(allow iokit-get-properties)

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