;;; Copyright (c) 2018 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 iokit-get-properties process-info* nvram*)
(deny dynamic-code-generation)

(import "system.sb")
(import "com.apple.corefoundation.sb")
(corefoundation)
(system-network)

;;; -- DEFINES ---

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

;;; --- RULES ---

(allow process-info* (target self))

;; For bootability volume mounting
(allow process-fork)
(allow process-exec (with no-sandbox)
    (literal "/sbin/mount")
    (literal "/System/Library/Filesystems/apfs.fs/Contents/Resources/mount_apfs")) ;; /sbin/mount_apfs

;; For resolving symlinks, realpath(3), and equivalents.
(allow file-read-metadata)

;; For validating the entitlements of clients.
(allow process-info-codesignature)

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

;; Read-Only preference domains
(allow user-preference-read
    (preference-domain "kCFPreferencesAnyApplication")
    (preference-domain "com.apple.CFNetwork")
    (preference-domain "com.apple.nsurlcache")
    (preference-domain "mobileactivationd")
    (preference-domain "com.apple.security")
    (preference-domain "com.apple.triald"))

;; Read/Write/Mount/Unmount Access (Bootability)
(allow file-read* file-write* file-mount file-unmount
    (subpath (param "TMPDIR")))

;; Read file access
(allow file-read*
    (subpath "/AppleInternal/Library/Bundles/BridgeOSInstallInternal.bundle")
    (subpath "/AppleInternal/Library/Frameworks/AppleConnectClient.framework")
    (subpath "/Applications/AppleConnect.app/Contents/Frameworks/SSOClient.framework")
    (subpath "/Library/Trial/"))

;; Read/Write file access
(allow file-read* file-write*
    (subpath "/System/Volumes/iSCPreboot")
    (subpath "/System/Volumes/Hardware/MobileActivation")
    (subpath "/System/Volumes/Hardware/dcrt")
    (subpath "/System/Volumes/Hardware/sdcrt")
    (subpath "/System/Library/Caches/MobileActivation")
    (subpath "/System/Library/Preferences/Logging")
    (subpath "/AppleInternal/Library/Preferences/Logging")
    (subpath "/Library/Preferences/Logging")
    (subpath "/private/var/tmp/OSPersonalizationTemp")
    (home-subpath "/Library/Caches/mobileactivationd")
    (literal "/usr/standalone/bootcaches.plist")
    (literal "./usr/standalone/bootcaches.plist")
    (literal "/private/var/db/mds/system/mds.lock")
    (literal "/private/var/db/mds/system/mdsObject.db")
    (literal "/private/var/db/mds/system/mdsDirectory.db")
    (file-attribute apfs-preboot-volume)
    (file-attribute apfs-recovery-volume))

;; Read/Write/FileExtension access
(allow file-issue-extension
    (require-all
        (extension-class "com.apple.app-sandbox.read" "com.apple.app-sandbox.read-write")
        (require-any
            (subpath "/System/Library/Caches/MobileActivation")
            (home-subpath "/Library/Caches/mobileactivationd"))))

;; Read-Only file access
(allow file-read*
    (extension "mobileactivationd:securitymessages")
    (home-literal "/Library/Preferences/com.apple.LaunchServices/com.apple.launchservices.secure.plist")
    (literal "/Library/Preferences/com.apple.security.plist")
    (literal "/Library/Application Support/CrashReporter/SubmitDiagInfo.domains")
    (literal "/usr/libexec")
    (literal "/usr/libexec/mobileactivationd")
    (subpath "/Volumes"))

;; Mach Lookup
(allow mach-lookup
    (global-name "com.apple.BootabilityService")
    (global-name "com.apple.InstallerDiagnostics.installerdiagd")
    (global-name "com.apple.AppSSO.service-xpc")
    (global-name "com.apple.SystemConfiguration.configd")
    (global-name "com.apple.installerauthagent")
    (global-name "com.apple.cfnetwork.AuthBrokerAgent")
    (global-name "com.apple.cfnetwork.cfnetworkagent")
    (global-name "com.apple.cookied")
    (global-name "com.apple.CoreAuthentication.daemon")
    (global-name "com.apple.ctkd")
    (global-name "com.apple.ctkd.token-client")
    (global-name "com.apple.lsd.mapdb")
    (global-name "com.apple.lsd.modifydb")
    (global-name "com.apple.nsurlstorage-cache")
    (global-name "com.apple.remoted")
    (global-name "com.apple.SecurityServer")
    (global-name "com.apple.usymptomsd")
    (global-name "com.apple.icloud.findmydeviced")
    (global-name "com.apple.inboxupdaterd")
    (global-name "com.apple.timed.xpc")
    (global-name "com.apple.nesessionmanager.content-filter"))

(allow process-info-pidinfo)
(allow mach-priv-host-port)
(allow iokit-get-properties)

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

(allow ipc-posix-shm-read-data
    (ipc-posix-name "com.apple.AppleDatabaseChanged")
    (ipc-posix-name "apple.shm.cfprefsd.daemon")
    (ipc-posix-name "FNetwork.defaultStorageSession")
    (ipc-posix-name-prefix "apple.shm.cfprefsd.")
    (ipc-posix-name-regex #"/tmp/com\.apple\.csseed\.")
    (ipc-posix-name "/com.apple.AppSSO.version"))

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

;; NVRAM

(allow nvram-get
    (nvram-variable "BYForceCreateLocalPolicy")
    (nvram-variable "BYForcePersonalization")
    (nvram-variable "BYForceInstallBootObjects"))

(allow nvram-set
    (nvram-variable "policy-nonce-digests")
    (nvram-variable "40A0DDD2-77F8-4392-B4A3-1E7304206516:policy-nonce-digests")
    (nvram-variable "boot-volume")
    (nvram-variable "40A0DDD2-77F8-4392-B4A3-1E7304206516:boot-volume")
    (nvram-variable "IONVRAM-FORCESYNCNOW-PROPERTY"))

;; Bootability Filesystem Access
(allow system-fsctl
    (fsctl-command APFSIOC_SNAP_LOOKUP))
