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

(allow iokit-open
        (iokit-user-client-class "AppleNVMeUpdateUC"
                                 "AppleNVMeEANUC"
                                 "AppleAPFSUserClient"
                                 "AppleNVMeSMARTUserClient"
                                 "AppleNVMePassThroughUC"
                                 "RootDomainUserClient"
                                 "IOUSBMassStorageResourceUserClient"
                                 "SCSISubsystemGlobalsUserClient"
                                 "AppleNVMeTranslationSMARTUserClient"))

(allow mach-lookup
        (global-name "com.apple.cache_delete"
                     "com.apple.cache_delete.public"
                     "com.apple.diagnosticpipeline.service"
                     "com.apple.diagnosticpipeline.tasking.service"
                     "com.apple.windowserver.active"
                     "com.apple.mobile.softwareupdated"
                     "com.apple.metadata.mds"
                     "com.apple.accountsd.accountmanager"
                     "com.apple.StorageManagement.Service"
                     "com.apple.SystemConfiguration.configd"
                     "com.apple.TapToRadarKit.service"))

(allow sysctl-read
       (sysctl-name "kern.memorystatus_freeze_pageouts")
       (sysctl-name "kern.osrevision")
       (sysctl-name "kern.kern.hibernatestatistics")
       (sysctl-name "hw.memsize")
       (sysctl-name-prefix "vm.compressor_")
       (sysctl-name "vm.native_hw_pagesize"
                    "vm.pagesize"))

;; Your preference domain
(allow user-preference-read user-preference-write
        (preference-domain "com.apple.ASPCarryLog"
                           "com.apple.nandCarryLogs"))

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

;; Read/write access to a temporary directory.
(allow file-read* file-write*
        (subpath "/private/var/log/iolog_unsent")
        (subpath "/private/var/db/NANDTelemetryServices")
        (subpath (param "TMPDIR"))
        ;; Allow access to ASPCarryLog temporary items directory
        (subpath (string-append (param "TMPDIR") "com.apple.ASPCarryLog/TemporaryItems")))

(allow file-read*
        (literal "/usr/libexec")
        (literal "/usr/libexec/ASPCarryLog"))

(allow file-read* file-write*
    (path "/Library/Application Support/CrashReporter/DiagnosticMessagesHistory.plist")
    (path "/private/var/root/Library/Preferences/com.apple.ASPCarryLog.plist"))

(allow nvram-get
       (nvram-variable "rdma-enable"))

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

;; Customization
(allow file-map-executable
        (subpath "/System/Library/Extensions/NVMeSMARTLib.plugin/Contents/MacOS/NVMeSMARTLib"))

(allow file-issue-extension
    (require-all
        (extension-class "com.apple.app-sandbox.read" "com.apple.app-sandbox.read-write")
        (subpath "/private/var/log/iolog_unsent")))

(allow file-issue-extension
    (require-all
        (extension-class "com.apple.app-sandbox.read" "com.apple.app-sandbox.read-write")
        (subpath "/private/var/db/NANDTelemetryServices")))

(with-filter (system-attribute apple-internal)
  (allow sysctl-write
         (sysctl-name "vm.task_no_footprint_for_debug"))) ; MallocStackLogging

(with-filter (iokit-registry-entry-class "IONVMeController")
    (allow iokit-set-properties
        (iokit-property "NVMe Controller Info")))
