;;; Copyright (c) 2023 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)

(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-read user-preference-write
    (preference-domain "com.apple.seserviced")
    (preference-domain "com.apple.seserviced.shared")
)

;; Read only prefs - not ours
(allow user-preference-read
    (preference-domain "kCFPreferencesAnyApplication")
    (preference-domain "com.apple.CFNetwork")
)

;; Need read / write / create
(allow file-read* file-write*
    (home-prefix "/Library/Preferences/com.apple.seserviced.plist")
    (home-prefix "/Library/Preferences/com.apple.seserviced.shared.plist")
    (home-subpath "/Library")
    (home-subpath "/Library/Preferences")
    (home-prefix "/Library/Application Support")
    (home-prefix "/Library/Application Support/SESStorage")
)

;; SLAM Directory
(allow file-read*
    (subpath "/usr/standalone/firmware/SLAM")
)

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

;; XPC connections
(allow mach-lookup
    (global-name "com.apple.nfcd.hwmanager")
    (global-name "com.apple.securityd.octagon")
    (global-name "com.apple.security.octagon")
    (global-name "com.apple.securityd.ckks")
    (global-name "com.apple.ak.anisette.xpc")
    (global-name "com.apple.ak.auth.xpc")
    (global-name "com.apple.ak.authorizationservices.xpc")
    (global-name "com.apple.accountsd.accountmanager")
    (global-name "com.apple.security.xpc")
    (global-name "com.apple.securityd.xpc")
    (global-name "com.apple.CoreAuthentication.agent.libxpc")
    (global-name "com.apple.CoreAuthentication.agent")
    (global-name "com.apple.seserviced")
    (global-name "com.apple.AppSSO.service-xpc")
    (global-name "com.apple.metadata.mds")
    (global-name "com.apple.remoted")
    (global-name "com.apple.duetactivityscheduler")
    (global-name "com.apple.seld.tsmmanager")
)

;; Self lookup
(allow file-read*
    (literal "/usr/libexec")
    (literal "/usr/libexec/seserviced")
)

;; Logs
(allow file-read* file-write*
    (literal "/Library/Logs/AppleSSE.log")
)

;; Process info lookup
(allow process-info-pidinfo)

(allow mach-priv-host-port)

(allow iokit-open
    (iokit-user-client-class "RootDomainUserClient")
    (iokit-user-client-class "AppleCredentialManagerUserClient")
    (iokit-user-client-class "AppleSSEUserClient")
    (iokit-user-client-class "AppleKeyStoreUserClient")
    (iokit-user-client-class "BootPolicyUserClient")
)

;; Network stuff
(system-network)

(allow network-outbound
    (remote tcp "*:443")
    (path "/private/var/run/mDNSResponder")
)

;; SEP needs this for some key related stuff in their library
(allow sysctl-read
    (sysctl-name-prefix "net.routetable.")
)

(allow iokit-get-properties)

;; MachServices
(allow mach-register
    (global-name "com.apple.seserviced.sereservation.client")
)
