(version 1)

(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)
(system-network)

(import "contacts.sb")
(contacts-client (param "HOME") (param "_TMPDIR"))

(allow syscall-mig)
(allow syscall-mach)
(allow syscall-unix)

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

;; ${PRODUCT_NAME}’s preference domain.
(allow user-preference-read user-preference-write
    (preference-domain "com.apple.transparencyd"))

;; Related product domains (push relay?)
(allow user-preference-read user-preference-write
    (preference-domain "com.apple.facetime.bag")
    (preference-domain "com.apple.imessage.bag"))
    
;; Global and security preferences
(allow user-preference-read
    (preference-domain ".GlobalPreferences")
    (preference-domain "com.apple.CFNetwork")
    (preference-domain "com.apple.nsurlcache")
    (preference-domain "kCFPreferencesAnyApplication")
    (preference-domain "com.apple.ids")
    (preference-domain "com.apple.security")
    (preference-domain "com.apple.CloudKit")
    (literal "transparencyd"))

;; read/write for Analytics DB, Transparency DB, temp dir for sqlite, URL cache, and background download task temporary storage
(allow file-read* file-write*
    (home-regex #"/Library/Keychains/.*/Analytics(/|$)")
    (regex #"^/private/var/protected/sfanalytics(/|$)")
    (home-subpath #"/Library/Application Support/com.apple.transparencyd")
    (home-subpath #"/Library/Application Support/transparencyd")
    (home-subpath #"/Library/Caches/com.apple.nsurlsessiond/Downloads/com.apple.transparencyd/")
    (subpath (param "_TMPDIR")))

(allow file-read*
    (literal "/usr/libexec")
    (literal "/usr/libexec/transparencyd")
    (literal "/Library/Preferences/com.apple.networkd.plist")
    (home-subpath #"Library/Caches/com.apple.setmme"))

(allow file-read* file-write*
    (extension "com.apple.sandbox.application-group"))

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

;; talk to other processes for networking, DeviceIdentity, AuthKit, and PCS
(allow mach-lookup
    (global-name "com.apple.AppSSO.service-xpc")
    (global-name "com.apple.CoreAuthentication.agent")
    (global-name "com.apple.CoreAuthentication.agent.libxpc")
    (global-name "com.apple.CoreServices.coreservicesd")
    (global-name "com.apple.SystemConfiguration.configd")
    (global-name "com.apple.accountsd.accountmanager")
    (global-name "com.apple.ak.anisette.xpc")
    (global-name "com.apple.ak.auth.xpc")
    (global-name "com.apple.apsd")
    (global-name "com.apple.ckdiscretionaryd")
    (global-name "com.apple.cloudd")
    (global-name "com.apple.containermanagerd")
    (global-name "com.apple.cookied")
    (global-name "com.apple.corefollowup.agent")
    (global-name "com.apple.coreservices.quarantine-resolver")
    (global-name "com.apple.ctkd.token-client")
    (global-name "com.apple.distributed_notifications@Uv3")
    (global-name "com.apple.duetactivityscheduler")
    (global-name "com.apple.identityservicesd.nsxpc")
    (global-name "com.apple.identityservicesd.desktop.auth")
    (global-name "com.apple.identityservicesd.idquery.desktop.auth")
    (global-name "com.apple.kvsd")
    (global-name "com.apple.mobileactivationd")
    (global-name "com.apple.nesessionmanager.content-filter")
    (global-name "com.apple.nsurlsessiond")
    (global-name "com.apple.nsurlstorage-cache")
    (global-name "com.apple.protectedcloudstorage.protectedcloudkeysyncing")
    (global-name "com.apple.remoted")
    (global-name "com.apple.security.octagon")
    (global-name "com.apple.securityd.ckks")
    (global-name "com.apple.securityd.general")
    (global-name "com.apple.cdp.daemon")
    (global-name "com.apple.securityd.xpc")
    (global-name "com.apple.UNCUserNotification")
    (global-name "com.apple.transparencyd.ids")
    (global-name "com.apple.system.opendirectoryd.api")
    (global-name "com.apple.windowserver.active"))

;; Access to LaunchServices
(allow mach-lookup
       (global-name "com.apple.coreservices.lsuseractivitymanager.xpc"
                    "com.apple.lsd.mapdb"))

(allow ipc-posix-shm-read-data
    (ipc-posix-name "FNetwork.defaultStorageSession")
    (ipc-posix-name "/com.apple.AppSSO.version"))

;; Legacy SecKey operations
(allow file-read* file-write*
    (regex #"^/private/var/folders/[^/]+/[^/]+/C/mds/mdsDirectory\.db$")
    (regex #"^/private/var/folders/[^/]+/[^/]+/C/mds/mdsObject\.db$")
    (regex #"^/private/var/folders/[^/]+/[^/]+/C/mds/mds\.lock$")
    (regex "/private/var/db/mds/messages/([A-Za-z0-9]+/)?se_SecurityMessages"))
(allow mach-lookup
    (global-name "com.apple.SecurityServer"))

;; AuthKit needs some weird things
(allow iokit-get-properties (iokit-property "device-colors"))
(allow nvram-get
    (nvram-variable "4D1EDE05-38C7-4A6A-9CC6-4BCCA8B38C14:ROM")
    (nvram-variable "4D1EDE05-38C7-4A6A-9CC6-4BCCA8B38C14:MLB"))

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

;; Read IOKit properties
(allow iokit-get-properties
    (iokit-property "osenvironment")
    (iokit-property "boot-ios-diagnostics")
    (iokit-property "model")
    (iokit-registry-entry-class "IOPlatformExpertDevice"))

;; allow network
(allow network-outbound)
(allow system-socket)

(allow distributed-notification-post)

;; TTR support
(with-filter (system-attribute apple-internal)
    (allow user-preference-read user-preference-write
        (preference-domain "com.apple.security"))
    (allow lsopen))
