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

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

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

;; Mach lookup
(allow mach-lookup
    (global-name "com.apple.tccd")
    (global-name "com.apple.tccd.system")
    (global-name "com.apple.audio.coreaudiod")
    (global-name "com.apple.cmio.VDCAssistant")
    (global-name "com.apple.windowserver.active")
    (global-name "com.apple.audio.audiohald")
    (global-name "com.apple.cvmsServ")
    (global-name "com.apple.audio.AudioComponentRegistrar")
    (global-name "com.apple.coreservices.launchservicesd")
    (global-name "com.apple.powerlog.plxpclogger.xpc")
    (global-name "com.apple.cmio.AppleCameraAssistant")
    (global-name "com.apple.securityd.xpc")
    (global-name "com.apple.SecurityServer")
    (global-name "com.apple.appleneuralengine")
    (global-name "com.apple.appleh13camerad")
    (global-name "com.apple.cmio.registerassistantservice")
    (global-name "com.apple.cmio.registerassistantservice.system-extensions")
    (subpath "/Library/Video/Plug-Ins"))

;; Must be able to open user-clients for IOHID events
(allow iokit-open)

;; Camera
(allow device-camera)
(allow iokit-get-properties)

(allow file-map-executable
    (path "/System/Library/Components/AudioCodecs.component/Contents/MacOS/AudioCodecs")
    (path "/System/Library/CoreServices/RawCamera.bundle/Contents/MacOS/RawCamera")
    (subpath "/System/Library/Extensions")
    (subpath "/private/var/db/CVMS")
    (subpath "/Library/Video/Plug-Ins"))

(allow nvram-get (nvram-variable "IOGVACodec"))

;; Your preference domain
(allow user-preference-read user-preference-write
       (preference-domain "com.apple.accessibility.MotionTrackingAgent")
       (preference-domain "com.apple.accessibility.MotionTracking")
       (preference-domain "com.apple.Accessibility"))

;; Read/write access to a temporary directory.
(allow file-read* file-write*
       (subpath (param "TMPDIR"))
       (subpath (param "DARWIN_CACHE_DIR")))

;; Read/write cache access
(let ((cache-path-filter (home-subpath "/Library/Caches/com.apple.accessibility.MotionTrackingAgent")))
  (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 file-issue-extension
    (require-all
        (extension-class "com.apple.app-sandbox.read-write")
        (subpath "/")))
(allow file-issue-extension (extension-class "com.apple.app-sandbox.read"))

(allow file-read*
    (literal "/Library/Application Support/CrashReporter/SubmitDiagInfo.domains")
    (subpath "/private/var/db/CVMS")
    (subpath "/private/var/db/mds/system/mdsObject.db")
    
    ;; System and user preferences
    (literal "/Library/Preferences/.GlobalPreferences.plist")
    (home-literal "/Library/Preferences/com.apple.accessibility.MotionTracking.plist")
    (home-literal "/Library/Preferences/.GlobalPreferences.plist")
    (home-regex #"/Library/Preferences/ByHost/\.GlobalPreferences\.")

    ;; Camera
    (subpath "/Library/CoreMediaIO/Plug-Ins/DAL")
    (subpath "/Library/Video/Plug-Ins"))

;; Unit test access for video files (only for internal installs)
(with-filter (system-attribute apple-internal)
    (allow file-read*
        (subpath "/private/var/tmp")
        (subpath "/private/tmp")))

;; sandbox extension
(allow file-read* (extension "com.apple.app-sandbox.read"))
