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

(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 user-preference-read
    (preference-domain "kCFPreferencesAnyApplication"))

(allow user-preference-read user-preference-write
       (preference-domain "com.apple.sportsd")
       (preference-domain "group.com.apple.sports")
       (preference-domain "com.apple.AppleMediaServices"))

;; 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/sportsd")))
  (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)))
 
(system-network)
 
(allow file-read-metadata)
(allow file-test-existence)

(allow iokit-get-properties)

(allow network-outbound)

(allow syscall-mach)
(allow syscall-unix)
(allow system-fcntl)
(allow system-mac-syscall)
   
(allow file-read*
    (literal (param "SECURITY_MESSAGES"))
    (subpath "/usr/libexec")
    (literal "/Library/Preferences/com.apple.networkd.plist"))

(allow file-read* file-write*
    (home-subpath "/Library/Group Containers/group.com.apple.sports")
    (home-subpath "/Library/HTTPStorages/sportsd")
    (home-subpath "/Library/Caches/com.apple.AppleMediaServices/Engagement")
    (home-subpath "/Library/Caches/com.apple.AppleMediaServices/Metrics/com.apple.AppleMediaServices")
    (home-subpath "/Library/Caches/com.apple.AppleMediaServices/Metrics/com.apple.AppleMediaServices.meta")
    (home-subpath "/Library/Caches/com.apple.AppleMediaServices/Metrics/sportsd")
    (home-literal "/Library/Caches/com.apple.AppleMediaServices/sap-setup-cert.txt"))
 
;; IO
(allow socket-ioctl
       (ioctl-command CTLIOCGINFO))
                      
;; IOKit
(allow iokit-open-service
    (iokit-user-client-class "IOMobileFramebuffer"))
    
(allow iokit-open-user-client
    (iokit-user-client-class "IOMobileFramebufferUserClient"))
 
;; NECP
(allow necp-client-open)
;;(allow system-necp-client-action)
(allow system-necp-client-action
    (necp-client-action NECP_CLIENT_ACTION_ADD
                        NECP_CLIENT_ACTION_ADD_FLOW
                        NECP_CLIENT_ACTION_COPY_AGENT
                        NECP_CLIENT_ACTION_COPY_INTERFACE
                        NECP_CLIENT_ACTION_COPY_RESULT
                        NECP_CLIENT_ACTION_COPY_ROUTE_STATISTICS
                        NECP_CLIENT_ACTION_COPY_UPDATED_RESULT
                        NECP_CLIENT_ACTION_MAP_SYSCTLS
                        NECP_CLIENT_ACTION_REMOVE
                        NECP_CLIENT_ACTION_REMOVE_FLOW))
                           
;; Socket Option
(allow socket-option-set
       (socket-option-name SO_NOSIGPIPE
                           SO_RCVBUF
                           SO_NECP_CLIENTUUID
                           SO_TRAFFIC_CLASS))
(allow socket-option-get
       (socket-option-name SO_NREAD
                           SO_REUSEPORT))
 
;; Mach Service Lookups
(allow mach-lookup
    (global-name "com.apple.accountsd.accountmanager"
                 "com.apple.adid"
                 "com.apple.apsd"
                 "com.apple.containermanagerd"
                 "com.apple.coreservices.quarantine-resolver"
                 "com.apple.lsd.mapdb"
                 "com.apple.metadata.mds"
                 "com.apple.SystemConfiguration.DNSConfiguration"
                 "com.apple.watchlistd.xpc"
                 "com.apple.windowserver.active"
                 "com.apple.xpc.amsaccountsd"
                 "com.apple.xpc.amsengagementd"
                 "com.apple.xpc.amstoold"))
