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

;; (allow (with report) default)
;; (allow (with report) file-map-executable process-info* nvram*)
;; (allow (with report) dynamic-code-generation)

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

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

;; Read-write for our preference domain
(allow user-preference-read
    (preference-domain "com.apple.ImageIO")
    (preference-domain "com.apple.coremedia")
    (preference-domain "com.apple.ImageIOXPCService"))
       
;; Read-only for global prefs
(allow user-preference-read (preference-domain ".GlobalPreferences"))
(allow file-read*
    (literal "/Library/Preferences/.GlobalPreferences.plist")
    (home-literal "/Library/Preferences/.GlobalPreferences.plist"))

;; 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.ImageIOXPCService")))
  (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)))
      
;; Read from our bundle
(allow file-read* (subpath (param "XPC_SERVICE_BUNDLE")))

;; Read from ImageIO bundle
(allow file-read* (subpath (param "IMAGEIO_BUNDLE")))
(allow file-map-executable (subpath (param "IMAGEIO_BUNDLE")))

;; (allow allow file-read*
;;     (regex #"^/System/Library/Extensions/AGXMetal[A-Za-z0-9_]+\.bundle/"))
;; (allow file-map-executable
;;     (regex #"^/System/Library/Extensions/AGXMetal[A-Za-z0-9_]+\.bundle/"))
     
;; For development only PRODUCTS_DIR is undefined for release builds
(when (param "PRODUCTS_DIR")
    (allow file-read* (subpath (param "PRODUCTS_DIR")))
    (allow file-map-executable (subpath (param "PRODUCTS_DIR"))))

;;; -------------------------------------------------------------------------------------------- ;;;

(allow iokit-open
    (iokit-user-client-class
        "IOSurfaceRootUserClient"
        "AGXCommandQueue"
        "AGXDevice"
        "AGXDeviceUserClient"
        "AGXGLContext"
        "AGXSharedUserClient"
        "AppleJPEGDriverUserClient"
        "H11ANEInDirectPathClient"
        "H2JPEGDeviceInterface"
        "IOAccelContext"
        "IOAccelContext2"
        "IOAccelDevice"
        "IOAccelDevice2"
        "IOAccelSharedUserClient"
        "IOAccelSharedUserClient2"
        "IOAccelSubmitter2"
        "IOSurfaceAcceleratorClient"))

(allow iokit-get-properties)

(allow mach-lookup
    (global-name "com.apple.tccd.system")
    (global-name "com.apple.windowserver.active")
    )

(allow file-read-data
    (subpath "/Library/Preferences/com.apple.coremedia.plist")
    (subpath "/Library/Preferences/com.apple.imageio.plist")
    )
