;;; Copyright (c) 2020 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.
;;;
;;; https://confluence.sd.apple.com/display/OSSEC/How+To+Sandbox+a+Daemon+on+macOS
;;;

(version 1)

;;; These 4 lines configure the sandbox for tracing mode (no enforcement)
;; (allow (with report) default)
;; (allow (with report) file-map-executable iokit-get-properties process-info* nvram*)
;; (allow (with report) dynamic-code-generation)
;; (deny mach-priv-host-port)

;;;; These 3 lines enforce the profile
(deny default)
(deny file-map-executable iokit-get-properties process-info* nvram*)
(deny dynamic-code-generation)

(import "system.sb")
(system-network) ;; Allow access to the network
(import "com.apple.corefoundation.sb")
(corefoundation)

;; Contacts
(import "contacts.sb")
(contacts-client (param "HOME") (param "TMPDIR"))

;;; 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 cache access
(let ((cache-path-filter (home-subpath "/Library/Caches/com.apple.icloud.searchpartyuseragent")))
    (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
        )
    )
)

;;;;
;;;; Security.framework section
;;;;

;; Security.framework read-write access
(allow file-read* file-write*
    (subpath "/private/var/db/mds/system")
)

;; Security.framework read-only access
(allow file-read*
    (literal (param "SECURITY_MESSAGES_DIR"))
    (home-subpath "/Library/Preferences/com.apple.security.plist")
)

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

;;;;
;;;; Preferences
;;;;

;; Preferences (read-write)
(allow user-preference-read user-preference-write
    (preference-domain "kCFPreferencesAnyApplication")
    (preference-domain "com.apple.icloud.searchpartyuseragent")
    (preference-domain "com.apple.CloudKit")
    (preference-domain "systemgroup.com.apple.icloud.searchpartyd.sharedsettings")
)

;; Preferences (read-only)
(allow user-preference-read
    (preference-domain "com.apple.FindMyMac")
    (preference-domain "com.apple.security")
)

;;;;
;;;; Filesystem
;;;;

;; Filesystem read-write access
(allow file-read* file-write*
    (extension "com.apple.identityservices.deliver")
    (extension "com.apple.sandbox.application-group")
    (home-subpath "/Library/Caches/CloudKit/com.apple.icloud.searchpartyuseragent")
    (home-subpath "/Library/IdentityServices/files")
    (home-subpath "/Library/Keychains")
    (home-subpath "/Library/com.apple.icloud.searchpartyd")
    (literal "/private/var/tmp/com.apple.icloud.searchpartyd/")
    (mount-relative-regex (string-append "^/\.TemporaryItems/folders\." (param "UID") "(/|$)"))
    (subpath "/private/var/db/searchparty/sharedVault")
    (subpath (param "DARWIN_CACHE_DIR"))
    (subpath (param "DARWIN_USER_DIR"))
    (subpath (param "TMPDIR"))
)

;; Filesystem read-only access
(allow file-read*
    (literal "/usr/libexec")
    (literal "/usr/libexec/searchpartyuseragent")
)

;;;;
;;;; Mach services
;;;;

;; 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.AddressBook.ContactsAccountsService")
    (global-name "com.apple.ak.anisette.xpc")
    (global-name "com.apple.apsd")
    (global-name "com.apple.cloudd")
    (global-name "com.apple.cmfsyncagent.embedded.auth")
    (global-name "com.apple.coreservices.quarantine-resolver")
    (global-name "com.apple.familycircle.agent")
    (global-name "com.apple.findmy.findmybeaconingd.push")
    (global-name "com.apple.findmy.findmylocate.fenceservice")
    (global-name "com.apple.findmy.findmylocate.friendshipservice")
    (global-name "com.apple.findmy.findmylocate.locationservice")
    (global-name "com.apple.findmy.findmylocate.settings")
    (global-name "com.apple.icloud.findmydeviced")
    (global-name "com.apple.icloud.fmfd")
    (global-name "com.apple.icloud.searchpartyd.beaconmanager.agentdaemoninternal")
    (global-name "com.apple.identityservicesd.desktop.auth")
    (global-name "com.apple.identityservicesd.idquery.desktop.auth")
    (global-name "com.apple.locationd.desktop.registration")
    (global-name "com.apple.locationd.desktop.synchronous")
    (global-name "com.apple.logind")
    (global-name "com.apple.lsd.mapdb")
    (global-name "com.apple.metadata.mds")
    (global-name "com.apple.searchparty.BeaconKeyService")
    (global-name "com.apple.searchparty.ObservationStoreService")
    (global-name "com.apple.securityd.ckks")
    (global-name "com.apple.securityd.xpc")
    (global-name "com.apple.server.bluetooth.general.xpc")
    (global-name "com.apple.server.bluetooth.le.att.xpc")
    (global-name "com.apple.bluetooth.xpc")
    (global-name "com.apple.system.opendirectoryd.api")
    (global-name "com.apple.tccd")
    (global-name "com.apple.timed.xpc")
    (global-name "com.apple.usernotifications.listener")
    (global-name "com.apple.usernotifications.usernotificationservice")
    (global-name "com.apple.windowserver.active")
    (global-name "com.apple.wirelessproxd")
    (global-name "com.apple.chronoservices")
    (global-name "com.apple.mobileactivationd")
    (global-name "com.apple.CoreAuthentication.agent")
    (global-name "com.apple.CoreAuthentication.agent.libxpc")
    (global-name "com.apple.containermanagerd")
)

(deny mach-lookup
    (xpc-service-name-prefix "com.apple.WebKit")
)

;;;;
;;;; IOKit
;;;;

;; IOKit
(allow iokit-open
    (iokit-user-client-class "RootDomainUserClient")
    (iokit-user-client-class "AppleKeyStoreUserClient")
)

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

;;;;
;;;; Miscellaneous
;;;;

; For sending files via -[IDSService sendResourceAtURL:metadata:toDestinations:priority:options:identifier:error:]
(allow file-issue-extension
    (require-all
        (extension-class "com.apple.app-sandbox.read-write")
        (subpath (param "TMPDIR"))
    )
)

;; Allow outbound network access
(allow network-outbound
    (literal "/private/var/run/mDNSResponder")
    (remote tcp)
    (remote udp)
)
