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

;;; TODO: Change these to deny before finalizing this profile.
(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)

(allow network-outbound
       (literal "/private/var/run/mDNSResponder")
       (remote ip))

(allow syscall-mig (kernel-mig-routine mach_port_extract_right))

(allow syscall-unix
    (syscall-number SYS_necp_open)
    (syscall-number SYS_change_fdguard_np)
    (syscall-number SYS_necp_client_action)
    (syscall-number SYS_recvfrom_nocancel)
    (syscall-number SYS_select_nocancel)
    (syscall-number SYS_socketpair))

(allow system-necp-client-action
         (necp-client-action NECP_CLIENT_ACTION_ADD)
         (necp-client-action NECP_CLIENT_ACTION_COPY_RESULT)
         (necp-client-action NECP_CLIENT_ACTION_REMOVE))

(allow system-fcntl (fcntl-command F_GETFD))

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

;; Your preference domain
(allow user-preference-read user-preference-write
       (preference-domain "com.apple.wifianalyticsd")
       (preference-domain-prefix "com.apple.wifi.analytics"))

(allow user-preference-read
       (preference-domain "kCFPreferencesAnyApplication"))

(allow managed-preference-read
       (preference-domain "com.apple.wifianalyticsd")
       (preference-domain-prefix "com.apple.wifi.analytics"))

;; Read/write access to a temporary directory.
(allow file-read* file-write*
       (subpath (param "TMPDIR"))
       (subpath (param "DARWIN_CACHE_DIR"))
       (subpath "/private/var/db/mds")
       (subpath "/private/var/folders")
       (subpath "/private/var/root/Library/com.apple.wifianalyticsd")
       (home-subpath "/Library/Keychains/")
       (subpath "/Library/Keychains/")
       (literal "/Library/Preferences/com.apple.security.plist")
       (literal "/Library/Preferences/com.apple.wifianalyticsd.plist")
       (home-subpath "/Library/Application Support/com.apple.wifianalyticsd")
       (home-subpath "/Library/com.apple.wifi.analytics")
       (home-subpath "/Library/com.apple.wifianalyticsd")
       (subpath "/private/var/log/com.apple.wifi.analytics/")
)

;; Read/write cache access
(let ((cache-path-filter (home-subpath "/Library/Caches/com.apple.wifianalyticsd")))
  (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 sysctl-read
       (sysctl-name-prefix "net.routetable."))     ; [libinfo] getifaddrs()

(allow iokit-open
       (iokit-user-client-class "IO80211APIUserClient"
                                "IOUserUserClient"
                                "AppleKeyStoreUserClient"
                                "IOReportUserClient")) ;; expect_iokit_open(true, "IOReportHub");

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

(allow mach-lookup
    (global-name "com.apple.SystemConfiguration.configd")
    (global-name "com.apple.SecurityServer")
    (global-name "com.apple.private.corewifi.internal-xpc")
    (global-name "com.apple.WiFiVelocityAgent")
    (global-name "com.apple.securityd.xpc")
    (global-name "com.apple.symptom_diagnostics")
    (global-name "com.apple.awdd")
    (global-name "com.apple.securityd")
    (global-name "com.apple.usymptomsd")
    (global-name "com.apple.wifivelocityd")
    (global-name "com.apple.symptoms.symptomsd.managed_events")
    (global-name "com.apple.networkd")
    (global-name "com.apple.dnssd.service")
)

(allow file-read*
       (subpath "/usr/libexec")
       (literal "/private/var/Managed Preferences/mobile/com.apple.wifianalyticsd.plist")
)

;; Keychain
(allow file-read* file-write*
    (home-regex #"/Library/Keychains/\.[\w]*")
    (home-literal "/Library/Keychains/System.keychain")
    (home-literal "/Library/Keychains/System.keychain-db")
    (home-regex #"/Library/Keychains/System\.keychain-db\.sb-[-\w]*")
)



