;;; Copyright (c) 2021 Apple Inc.  All Rights reserved.

(version 1)

(deny default)
(deny file-map-executable 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)))

(allow file-read*
    (home-subpath "/Library/UnifiedAssetFramework/")
    (literal "/private/var/db/eligibilityd/eligibility.plist")
)

(allow file-read* file-write*
    (subpath "/private/var/db/com.apple.modelcatalog/")
)

(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))
(allow process-info-dirtycontrol (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.modelcatalog")
       (preference-domain "com.apple.MobileAsset.MC.Test")
       (preference-domain "com.apple.modelcatalog.ajax"))

(allow user-preference-read
       (preference-domain "com.apple.UnifiedAssetFramework")
       (preference-domain "kCFPreferencesAnyApplication"))

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

(allow mach-lookup
    (global-name "com.apple.modelmanager")
    (global-name "com.apple.modelcatalog.catalog")
    (global-name "com.apple.siri.uaf.service")
    (global-name "com.apple.mobileasset.autoasset")
    (global-name "com.apple.mobileassetd.v2")
    (global-name "com.apple.duetactivityscheduler")
    (global-name "com.apple.SystemConfiguration.configd")
    (global-name "com.apple.system.opendirectoryd.api")
    (global-name "com.apple.generativeexperiences.FailureTracking")
    (global-name "com.apple.siri.uaf.subscription.service")
)

;; Read/write access to a temporary directory.
(allow file-read* file-write*
       (subpath "/private/var/tmp")
       (subpath (param "TMPDIR"))
       (subpath (param "DARWIN_CACHE_DIR"))
       (mount-relative-literal "/.TemporaryItems")
       (mount-relative-regex #"^/\.TemporaryItems/folders.[0-9]+(/|$)"))

;; Allow read of AppleIntelligencePlatform availability secure check
(allow file-read*
       (subpath "/private/var/db/AppleIntelligencePlatform")
       (subpath "/System/Library/AssetsV2/locks/com.apple.UnifiedAssetFramework/")
       (subpath "/AppleInternal/Library/UnifiedAssetFramework/AssetSets/")
)

;; Allow read of UAF subscription db
(allow file-read*
       (subpath "/private/var/db/assetsubscriptiond")
)

(allow process-info-listpids)

;; Allow issuing extensions to cfprefsd for AppleIntelligencePlatform availability secure check
(allow file-issue-extension
    (require-all
        (extension-class "com.apple.cfprefsd.read-write")
        (subpath "/private/var/db/AppleIntelligencePlatform")
    )
    (require-all
        (extension-class "com.apple.cfprefsd.read")
        (subpath "/private/var/db/AppleIntelligencePlatform")
    )
)

;; Remove once rdar://141175685 (Move safety sanitization into its own inference provider) lands
;; Regulatory domains
(allow file-read*
    (subpath "/private/var/db/com.apple.countryd/")
)
(allow mach-lookup
    (global-name "com.apple.lsd.mapdb")
)

;; Biome entitlements
(allow mach-lookup
    (global-name
        "com.apple.biome.access.user"
        "com.apple.biome.access.system"
        "com.apple.biome.compute.publisher.service.user"
        "com.apple.biome.compute.publisher.service"
        "com.apple.biome.compute.source"
        "com.apple.biome.compute.source.user"))


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

;; Allow read-only access to /Library/Trial
(allow file-read*
    (subpath "/Library/Trial"))
