;;; macOS sandbox profile for /usr/libexec/replayd.
;;; Copyright (c) 2019 Apple Inc.  All Rights reserved.
(version 1)

(deny default)
(deny file-map-executable process-info* nvram*)
(deny dynamic-code-generation)

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

(allow lsopen)
(system-graphics)


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


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

; And override everything else if we get given an extension
(allow file-read-data (extension "com.apple.app-sandbox.read"))
(allow file-read* file-write* (extension "com.apple.app-sandbox.read-write"))

(allow file-read*
       (subpath "/Applications")
       (subpath "/Library/Application Support/CrashReporter")
       (subpath "/Library/MessageTracer")
       (literal "/usr/libexec")
       (literal "/usr/libexec/replayd")
       (subpath (param "DARWIN_TRANSLOCATION_DIR")))
       
(allow file-write-create (home-subpath "/Library/Application Support/com.apple.replayd"))

(allow file-read* file-write*
       (home-subpath "/Library/Application Support/com.apple.replayd")
       (subpath (param "TMPDIR"))
       (subpath (param "DARWIN_CACHE_DIR"))
       (home-subpath "/Library/Biome"))

;; Cache rules
(let ((cache-path-filter (home-subpath "/Library/Caches/com.apple.replayd")))
    (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)))


(allow iokit-get-properties)

(allow iokit-open)

(allow device-camera)
(allow device-microphone)

;;; Process info
(deny process-info*)
(allow process-info* (target self))
(allow process-info-codesignature)
(allow process-info-pidinfo)

(allow file-read* file-map-executable
       (subpath "/Library/Video/Plug-Ins")
       (subpath "/System/Library/Components")
       (subpath "/System/Library/Extensions")
       (subpath "/System/Library/Video/Plug-Ins")
       (subpath "/usr/local/lib"))

(allow ipc-posix-shm-read* ipc-posix-shm-write-create ipc-posix-shm-write-data (ipc-posix-name "com.apple.AppleDatabaseChanged"))
(allow ipc-posix-shm (ipc-posix-name-prefix "AudioIO"))

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

(allow mach-lookup
       (global-name "com.apple.SecurityServer"
                    "com.apple.UNCUserNotification"
                    "com.apple.audio.AudioComponentRegistrar"
                    "com.apple.audio.audiohald"
                    "com.apple.cmio.AppleCameraAssistant"
                    "com.apple.cmio.VDCAssistant"
                    "com.apple.cmio.registerassistantservice.system-extensions"
                    "com.apple.cmio.registerassistantservice"
                    "com.apple.coremedia.endpoint.xpc"
                    "com.apple.airplay.endpoint.xpc"
                    "com.apple.mediaexperience.endpoint.xpc"
                    "com.apple.usernotifications.usernotificationservice"
                    "com.apple.usernotifications.listener"
                    "com.apple.coreservices.launchservicesd"
                    "com.apple.lsd.modifydb"
                    "com.apple.ocspd"
                    "com.apple.pluginkit.pkd"
                    "com.apple.rtcreportingd"
                    "com.apple.runningboard"
                    "com.apple.tccd"
                    "com.apple.tccd.system"
                    "com.apple.windowserver.active"
                    "com.apple.biome.PublicStreamAccessService"
                    "com.apple.biome.compute.source.user"
                    "com.apple.callkit.callcontrollerhost"
                    "com.apple.replaykit.sharingsession"
                    "com.apple.coreservices.launchservicesd"
                    "com.apple.coreservices.quarantine-resolver"
                    "com.apple.appleneuralengine"
                    "com.apple.systemstatus.publisher"
                    "com.apple.containermanagerd"))

;; Preferences
(allow user-preference-read user-preference-write
       (preference-domain "com.apple.AppleGVA"
                          "com.apple.GVAEncoder"
                          "com.apple.avfaudio"
                          "com.apple.avfoundation"
                          "com.apple.cmio"
                          "com.apple.coreaudio"
                          "com.apple.coremedia"
                          "com.apple.corevideo"
                          "com.apple.replayd"
                          "com.apple.security"
                          "kCFPreferencesAnyApplication"
                          "replayd"))

; LaunchServices, Security read preferences in-process
(allow file-read*
       (home-literal "/Library/Preferences/com.apple.LaunchServices/com.apple.launchservices.secure.plist")
       (literal      "/Library/Preferences/com.apple.security.plist"))
