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

;; Your preference domain
(allow user-preference-write
    (preference-domain "com.apple.SharePlay.GroupSessionService")
)
(allow user-preference-read
    (preference-domain "com.apple.SharePlay.GroupSessionService")
    (preference-domain "com.apple.TelephonyUtilities")
    (preference-domain "com.apple.TelephonyUtilities.sharePlayAppPolicies")
    (preference-domain "com.apple.powerlogd")
)

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

; Support for issuing extensions in our temporary directories.
(allow file-issue-extension
    (require-all
        (extension-class "com.apple.app-sandbox.read" "com.apple.app-sandbox.read-write")
        (require-any
            (subpath (param "TMPDIR"))
            (subpath (param "DARWIN_CACHE_DIR"))
        )
    )
)

; For validating  entitlements and looking up process information of XPC clients
(allow process-info-codesignature)
(allow process-info-pidinfo)
(allow process-info* (target self))

(allow user-preference-read
    (preference-domain "com.apple.assistant" "com.apple.security" ".GlobalPreferences" "com.apple.iChat" "com.apple.Messages" "com.apple.facetime.bag" "com.apple.imessage.bag"
"kCFPreferencesAnyApplication" "com.apple.ncprefs")
)

(allow iokit-open
    (iokit-registry-entry-class "RootDomainUserClient")
)

(allow mach-lookup
    (global-name "com.apple.CoreServices.coreservicesd")
    (global-name "com.apple.PowerManagement.control")
    (global-name "com.apple.analyticsd")
    (global-name "com.apple.apsd")
    (global-name "com.apple.awdd")
    (global-name "com.apple.coreservices.launchservicesd")
    (global-name "com.apple.coreservices.quarantine-resolver")
    (global-name "com.apple.distributed_notifications@Uv3")
    (global-name "com.apple.familycircle.agent")
    (global-name "com.apple.identityservicesd.desktop.auth")
    (global-name "com.apple.identityservicesd.idquery.desktop.auth")
    (global-name "com.apple.imagent.desktop.auth")
    (global-name "com.apple.logind")
    (global-name "com.apple.lsd.mapdb")
    (global-name "com.apple.marco")
    (global-name "com.apple.powerlog.plxpclogger.xpc")
    (global-name "com.apple.rtcreportingd")
    (global-name "com.apple.securityd.xpc")
    (global-name "com.apple.soagent")
    (global-name "com.apple.tccd")
    (global-name "com.apple.usernoted.daemon_client")
    (global-name "com.apple.dmd.policy")
    (global-name "com.apple.dmd.emergency-mode")
    (global-name "com.apple.accountsd.accountmanager")
    (global-name "com.apple.windowserver.active")
    (global-name "com.apple.UNCUserNotification")
    (global-name "com.apple.imtransferservices.IMTransferAgent")
    (global-name "com.apple.SharedWebCredentials")
    (global-name "com.apple.identityservicesd.nsxpc")
    (global-name "com.apple.StatusKit.presence")
    (global-name "com.apple.PowerManagement.control")
    (global-name "com.apple.iokit.powerdxpc")
)

(allow file-read*
    (extension "com.apple.app-sandbox.read")
    (literal "/Library/MessageTracer/SubmitDiagInfo.default.domains.searchtree")
    (literal "/Library/Preferences/com.apple.security.plist")
    (home-literal ".CFUserTextEncoding")
    (home-literal "/Library/Preferences/com.apple.LaunchServices/com.apple.launchservices.secure.plist")
    (home-subpath "/Library/Caches/GeoServices")
)

(allow file-read* file-write*
    (extension "com.apple.identityservices.deliver")
    (extension "com.apple.app-sandbox.read-write")
)

;;; Security.framework files that are dependent on uid. Some paths are covered by extensions that callservicesd issues to itself before entering the sandbox
; 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 "com.apple.SharePlay.GroupSessionService"))
;    uid  > 0: r
; 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* file-write*
    (extension "com.apple.SharePlay.GroupSessionService")
)

(allow file-read*
    (literal (param "SECURITY_MESSAGES_FILE"))
)
(allow file-read*
    (literal "/private/var/db/mds/system/mdsDirectory.db")
    (literal "/private/var/db/mds/system/mdsObject.db")
)

