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

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

;; Preference domain
(allow user-preference-read user-preference-write
    (preference-domain  "com.apple.AppleMediaServices"
                        "com.apple.airplay"
                        "com.apple.itunescloud"
                        "com.apple.itunescloudd"
                        "com.apple.itunescloud.daemon"
                        "com.apple.mobileipod"
                        "com.apple.PlayActivityFeed"
    )
)

(allow user-preference-read
    (preference-domain  "com.apple.coreduetd"
                        "com.apple.itunesstored"
                        "com.apple.security"
                        "kCFPreferencesAnyApplication"
                        "com.apple.mediaplaybackcore"
                        "com.apple.commerce"
    )
)

;; Read/write access to a temporary directory.
(allow file-read* file-write*
       (subpath (param "TMPDIR"))
       (subpath (param "DARWIN_CACHE_DIR"))
       (mount-relative-regex #"^/\.TemporaryItems(/|$)"))

;; Read/write cache access
(let ((cache-path-filter
    (home-subpath "/Library/Caches/com.apple.itunescloudd")))
  (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)))
      
(let ((cache-path-filter (home-subpath "/Library/com.apple.iTunesCloud")))
  (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 mach-lookup
    (global-name    "com.apple.accountsd.accountmanager"
                    "com.apple.adid"
                    "com.apple.ak.anisette.xpc"
                    "com.apple.ak.auth.xpc"
                    "com.apple.AppSSO.service-xpc"
                    "com.apple.aps.itunescloudd"
                    "com.apple.coreservices.quarantine-resolver"
                    "com.apple.private.corewifi-xpc"
                    "com.apple.dnssd.service"
                    "com.apple.identityservicesd.desktop.auth"
                    "com.apple.logind"
                    "com.apple.lsd.mapdb"
                    "com.apple.metadata.mds"
                    "com.apple.nehelper"
                    "com.apple.PowerManagement.control"
                    "com.apple.securityd.xpc"
                    "com.apple.SecurityServer"
                    "com.apple.symptom_diagnostics"
                    "com.apple.system.opendirectoryd.api"
                    "com.apple.SystemConfiguration.configd"
                    "com.apple.SystemConfiguration.DNSConfiguration"
                    "com.apple.tccd"
                    "com.apple.tccd.system"
                    "com.apple.usernotifications.usernotificationservice"
                    "com.apple.usernotifications.listener"
                    "com.apple.usymptomsd"
                    "com.apple.windowserver.active"
                    "com.apple.xpc.amsaccountsd"
                    "com.apple.xpc.amsengagementd"
                    "com.apple.xpc.amstoold"
                    "com.apple.fpsd"
                    "com.apple.apsd"
    )
)

(allow file-read* file-write*
    (home-subpath "/Library/HTTPStorages/com.apple.itunescloudd")
    (home-subpath "/Library/com.apple.iTunesCloud")
    (home-subpath "/Library/Caches/com.apple.iTunesCloud")
    (home-subpath "/Library/Caches/com.apple.AppleMediaServices")
)

(allow file-read*
    (subpath "/private/var/db/mds/system")
    (literal "/Library/Preferences/com.apple.networkd.plist")
    (literal "/private/var/run/resolv.conf")
    (literal (param "_SECURITY_MESSAGES_FILE"))
    (literal "/private/var/db/nsurlstoraged/dafsaData.bin")
)

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

;; Networking
(allow network-outbound)

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

(allow network-inbound
       (remote tcp "*:*")
)

(allow system-socket
    (socket-domain AF_SYSTEM)
)

(allow iokit-get-properties)

(allow iokit-open
    (iokit-user-client-class "RootDomainUserClient")
)
