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

;;; log stream --info --debug --style compact --predicate '((message contains "RemoteManagement")) AND (((processID == 0) AND (senderImagePath CONTAINS "/Sandbox")) OR (subsystem == "com.apple.sandbox.reporting"))'

;;; Training wheels on:
;; (allow (with report) default)
;; (allow (with report) file-map-executable process-info* nvram*)
;; (allow (with report) dynamic-code-generation)

;;; Training wheels off:
(deny default)
(deny file-map-executable iokit-get-properties 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)

;; Read/write preference domain
(allow user-preference-read user-preference-write
    (preference-domain "com.apple.RemoteManagementAgent")
)

(allow user-preference-read
    (preference-domain "kCFPreferencesAnyApplication")
)

;; Defaults override
(allow file-read*
    (literal "/private/var/db/.RMDefaultsOverride.plist")
)

;; Read/write access to a temporary directory.
(allow file-read* file-write*
    (subpath (param "TMPDIR"))
    (subpath (param "DARWIN_CACHE_DIR"))
    (regex #"^/Volumes/[^/]+/.TemporaryItems/.*")
)

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

;; Read/write access to Application Support directory.
(allow file-read* file-write*
    (home-subpath "/Library/Application Support/com.apple.RemoteManagementAgent")
)

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

;; Device Properties
(allow iokit-get-properties)

;; Launch Services
(allow file-read*
    (home-literal "/Library/Preferences/com.apple.LaunchServices/com.apple.launchservices.secure.plist")
    (regex "^/private/var/folders/.*/com\.apple\.LaunchServices-.*\.csstore$")
)

(allow mach-lookup
    (global-name "com.apple.coreservices.quarantine-resolver")
    (global-name "com.apple.lsd.mapdb")
    (global-name "com.apple.lsd.modifydb")
)

;; Push Notifications
(allow mach-lookup
    (global-name "com.apple.apsd")
    (global-name "com.apple.SystemConfiguration.configd")
    (global-name "com.apple.windowserver.active")
)

;; AuthKit OTP Headers
(allow mach-lookup
    (global-name "com.apple.ak.anisette.xpc")
)

;; CFNetwork access
(allow mach-lookup
    (global-name "com.apple.cookied")
)

(allow user-preference-read
    (preference-domain "com.apple.CFNetwork")
    (preference-domain "com.apple.nsurlcache")
    (preference-domain "com.apple.security")
)

(allow file-read*
    (literal "/private/var/db/mds/messages/501/se_SecurityMessages")
    (literal "/private/var/db/mds/system/mdsDirectory.db")
)

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

;; Consuming Sandbox Extensions
(allow file-read* (extension "com.apple.app-sandbox.read"))
(allow file-read* file-write* (extension "com.apple.app-sandbox.read-write"))

;; Security - keychain
(allow mach-lookup
    (global-name "com.apple.ctkd.token-client")
    (global-name "com.apple.SecurityServer")
    (global-name "com.apple.securityd")
    (global-name "com.apple.securityd.xpc")
)

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

;; Keychain
(allow file-read* file-write*
    (home-regex #"/Library/Keychains/\.[\w]*")
    (home-literal "/Library/Keychains/login.keychain")
    (home-literal "/Library/Keychains/login.keychain-db")
    (home-regex #"/Library/Keychains/login\.keychain-db\.sb-[-\w]*")
)

;; For validating client authorization to modify trust settings
(allow authorization-right-obtain
    (right-name "com.apple.trust-settings.user"))

;; MCX
(allow file-map-executable
    (literal "/System/Library/CoreServices/ManagedClient.app/Contents/PlugIns/MCXToolsInterface.bundle/Contents/MacOS/MCXToolsInterface")
)
(allow mach-lookup
    (global-name "com.apple.mdmclient.agent")
    (global-name "com.apple.mdmclient.agent.unrestricted")
    (global-name "com.apple.mdmclient.daemon")
    (global-name "com.apple.mdmclient.daemon.unrestricted")
)
(allow user-preference-read
    (preference-domain "com.apple.mdmclient")
)

;; Miscellaneous
(allow mach-lookup
    (global-name "com.apple.accountsd.accountmanager")  ;; Accounts.framework
    (global-name "com.apple.dmd")                       ;; management.test access to DMD
)

;; DeviceIdentity
(allow mach-lookup
    (global-name "com.apple.CoreAuthentication.agent")
    (global-name "com.apple.mobileactivationd")
    (global-name "com.apple.remoted")
)
(allow iokit-open
    (iokit-user-client-class "AppleCredentialManagerUserClient")
    (iokit-user-client-class "AppleKeyStoreUserClient")
)

;; Persona
(allow mach-lookup
    (global-name "com.apple.mobile.keybagd.xpc")
    (global-name "com.apple.mobile.keybagd.UserManager.xpc")
    (global-name "com.apple.mobile.usermanagerd.xpc")
    (global-name "com.apple.system.opendirectoryd.api")
)

;; Read/write access to /tmp/rmd_test for making use of the file conduit
(allow file-read* file-write*
    (subpath "/private/tmp/rmd_test")
)

;; Background Tasks
(allow mach-lookup
    (global-name "com.apple.duetactivityscheduler")
)
