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

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

(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 "com.apple.icloud.searchpartyd")
)

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


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

;; Filesystem read-write access
(allow file-read* file-write*
    (extension "com.apple.sandbox.application-group")
    (subpath "/Library/Application Support/com.apple.icloud.searchpartyd/")
    (subpath "/Library/Keychains")
    (subpath (param "DARWIN_CACHE_DIR"))
    (subpath (param "DARWIN_USER_DIR"))
    (subpath (param "TMPDIR"))
    (literal "/private/var/db/searchparty")
    (mount-relative-regex (string-append "^/\.TemporaryItems/folders\." (param "UID") "(/|$)"))
    (subpath "/private/var/db/searchparty")
)

;; Filesystem read-only access
(allow file-read*
    (literal "/Library/Preferences/com.apple.security.plist")
    (literal "/usr/libexec")
    (literal "/usr/libexec/searchpartyd")
)

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

;; Mach services
(allow mach-lookup
    (global-name "com.apple.CoreAuthentication.daemon")
    (global-name "com.apple.CoreAuthentication.daemon.libxpc")
    (global-name "com.apple.PowerManagement.control")
    (global-name "com.apple.SecurityServer")
    (global-name "com.apple.accountsd.accountmanager")
    (global-name "com.apple.coreservices.quarantine-resolver")
    (global-name "com.apple.icloud.fmfd")
    (global-name "com.apple.locationd.desktop.registration")
    (global-name "com.apple.locationd.desktop.synchronous")
    (global-name "com.apple.lsd.mapdb")
    (global-name "com.apple.lsd.modifydb")
    (global-name "com.apple.metadata.mds")
    (global-name "com.apple.mobileactivationd")
    (global-name "com.apple.remoted")
    (global-name "com.apple.searchparty.BeaconScanningSession")
    (global-name "com.apple.securityd.systemkeychain")
    (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.chronoservices")
    (global-name "com.apple.containermanagerd")
)

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

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

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

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

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