;;; 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 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 iokit-get-properties)
;; Allow setting these properties - needed to kickoff device rematching
(allow iokit-set-properties
    (iokit-property "GameControllerSupportedHIDDevice"
                    "IOHIDDeviceForceInterfaceRematch"))
;; Allow opening IOHIDDevice connections
;; Allow opening IOUSBHostDevice/Interface connections
;; Allow opening AppleGCResource in the kernel
  (allow iokit-open-service
         (iokit-registry-entry-class "AppleGCResource"))
  (allow iokit-open-user-client
         (iokit-user-client-class "IOHIDLibUserClient"
                                  "AppleUSBHostUserClient"
                                  "AppleGCResourceDeviceUserClient"))
;;; Allowing loading IOKit Plugins
(allow file-map-executable
       (subpath "/System/Library/Extensions"))

(allow process-info* (target self))

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

;; File reads
(allow file-read*
    (literal "/usr/libexec")
    (literal "/usr/libexec/gamecontrollerd"))
      
;; Extensions for ReplayKit recordings on macOS
(allow file-read*
    (literal "/private/var/tmp"))
(let ((tmp-path-filter (subpath "/private/var/tmp/com.apple.gamecontrollerd")))
  (allow file-read* file-write* tmp-path-filter)
  (allow file-issue-extension
    (require-all
      (extension-class "com.apple.app-sandbox.read" "com.apple.app-sandbox.read-write")
      tmp-path-filter)))
      
;; For validating the entitlements of clients.
(allow process-info-codesignature)

;; gamecontrollerd preference domain
(allow user-preference-read user-preference-write
       (preference-domain "com.apple.GameController.gamecontrollerd"
                          "com.apple.gamecontroller.settings"))
       
;; Preferences
(allow user-preference-read
    (preference-domain  "kCFPreferencesAnyApplication"
                        "gamecontrollerd"))

;; 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.GameController.gamecontrollerd")))
  (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 process-info-pidinfo)
      
(allow mach-lookup
    (global-name    "com.apple.tccd"
                    "com.apple.tccd.system"
                    "com.apple.windowserver.active"
                    "com.apple.coreservices.launchservicesd"
                    ;; rdar://90412426 (Add mach-lookup com.apple.accessories.externalaccessory-server to gamecontrollerd's macOS sandbox)
                    "com.apple.accessories.externalaccessory-server"
                    "com.apple.iohideventsystem"
                    "com.apple.GameController.gamecontrollerd.driver"
                    "com.apple.SystemConfiguration.configd"
                    ;; rdar://107325810 (Allow Mach service lookup for com.apple.usernotifications.listener in CoreController)
                    "com.apple.usernotifications.listener"
                    "com.apple.bluetooth.xpc"
                    "com.apple.gamepolicyd.tool.privileged"
    )
)
