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

(deny default)
(deny file-map-executable process-info* nvram*)
(allow iokit-get-properties)
(deny dynamic-code-generation)

(deny mach-priv-host-port)

(import "system.sb")
(import "com.apple.corefoundation.sb")
(corefoundation)

(import "contacts.sb")
(contacts-client (param "HOME") (param "TMPDIR"))

;;; 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.gamed")
    (preference-domain "com.apple.gamecenter")
    (preference-domain "com.apple.logging")
    (preference-domain "com.apple.conference")
)

(allow user-preference-read
    (preference-domain "com.apple.MultitouchSupport")
    (preference-domain "com.apple.coreaudio")
    (preference-domain "com.apple.coremedia")
    (preference-domain "com.apple.itunesstored")
    (preference-domain "com.apple.VideoConference")
    (preference-domain "com.apple.springboard")
)

(if (param "_CACHE_DIR")
       (allow file-write* file-read* (subpath (param "_CACHE_DIR"))))

(if (param "_TMP_DIR")
       (allow file-write* file-read* (subpath (param "_TMP_DIR"))))

(if (param "INSTALL_DIR")
    (allow file-read* (subpath (param "INSTALL_DIR"))))

(allow file-write-create file-write-unlink
    (subpath "/file")
)

(allow file-read* file-write*
    (home-literal "/Library/Preferences/com.apple.AppleMediaServices.plist")
    (home-subpath "/Library/Caches/GameKit")
    (home-subpath "/Library/Caches/com.apple.VideoConference")
    (home-subpath "/Library/GameKit")
    (home-subpath "/Library/Caches/Metrics")
    (home-subpath "/Library/Caches/com.apple.AppleMediaServices")
    (home-subpath "/Library/Caches/com.apple.gamed")
    (home-subpath "/Library/Caches/com.apple.nsurlsessiond/Downloads/com.apple.gamed")
    (subpath "/private/var/folders")
    (subpath "/Library/Caches/com.apple.nsurlsessiond")
    (subpath "/private/var/root/Library/Caches/com.apple.nsurlsessiond")
    (extension "com.apple.sandbox.application-group")
)

(allow file-read*
    (literal "/")
    (literal "/Library/Preferences/.GlobalPreferences.plist")
    (literal "/Library/Preferences/SystemConfiguration/preferences.plist")
    (literal "/usr/libexec")
    (literal "/usr/libexec/gamed")
    (literal "/Library/Preferences/com.apple.security.plist")
    (literal "/private/var/run/resolv.conf")
    (subpath "/private/var/db/mds/system")
    (subpath "/private/var/db/mds/messages")
    (subpath "/Applications")
    (subpath "/AppleInternal/Applications")
    (subpath "/AppleInternal/Library/Bundles/GameKitFakeData.bundle")
    (subpath "/Library/Application Support/AppleConnect")
    (home-subpath "/Library/Preferences")
    (home-subpath "/Library/Caches/GeoServices")
)

(allow file-read-data (subpath "/usr/libexec/GKCentralCache.momd"))

(allow mach-lookup
       (global-name "com.apple.SystemConfiguration.SCNetworkReachability")
       (global-name "com.apple.SystemConfiguration.configd")
       (global-name "com.apple.cfnetwork.cfnetworkagent")
       (global-name "com.apple.coreservices.launchservicesd")
       (global-name "com.apple.nsurlsessiond")
       (global-name "com.apple.nsurlstorage-cache")
       (global-name "com.apple.xpc.activity.control")
       (global-name "com.apple.CrashReporterSupportHelper")
       (global-name "com.apple.corerecents.recentsd")
       (global-name "com.apple.coremedia.mediaplaybackd.mutablecomposition.xpc")
       (global-name "com.apple.coremedia.mediaplaybackd.mutablemovie.xpc")
       (global-name "com.apple.coremedia.mediaplaybackd.player.xpc")
       (global-name "com.apple.coremedia.mediaplaybackd.visualcontext.xpc")
       (global-name "com.apple.coremedia.sts")
       (global-name "com.apple.xpc.amsaccountsd")
       (global-name "com.apple.containermanagerd")
       (global-name "com.apple.biome.access.user")
;; TTR
       (global-name "com.apple.TapToRadarKit.service")
       (global-name "com.apple.gamepolicyd.app.privileged")
)

(allow mach-lookup
(global-name "PurpleSystemAppPort"
             "PurpleSystemEventPort"
             "com.apple.accountsd.accountmanager"
             "com.apple.authkit.xpc"
             "com.apple.frontboard.systemappservices"
             "com.apple.commcenter.xpc"
             "com.apple.corerecents.recentsd"
             "com.apple.fairplayd"                ; <rdar://problem/12974324>
             "com.apple.fairplayd.versioned"
             "com.apple.GameController.gamecontrollerd"
             "com.apple.GameController.gamecontrollerd.app"
             "com.apple.GameController.gamecontrollerd.haptics"
             "com.apple.GameOverlayUI"
             "com.apple.GameOverlayViewService"
             "com.apple.GameTrampoline"
             "com.apple.imagent.embedded.auth"
             "com.apple.itunesstored.xpc"
             "com.apple.mediaserverd"
             "com.apple.mobile.installd"
             "com.apple.mobilemail.services.xpc"
             "com.apple.springboard.backgroundappservices"
             "com.apple.gamecenter"
             "com.apple.cloudd"
             "com.apple.windowserver.active"
             "com.apple.lsd.mapdb"
             "com.apple.usernotifications.usernotificationservice"
             "com.apple.usernotifications.listener"
             "com.apple.logind"
             "com.apple.identityservicesd.desktop.auth"
             "com.apple.AppSSO.service-xpc"
             "com.apple.dnssd.service"
             "com.apple.usymptomsd"
             "com.apple.apsd"
             "com.apple.securityd.xpc"
             "com.apple.SecurityServer"
             "com.apple.appstoreagent.xpc"
             "com.apple.SystemConfiguration.DNSConfiguration"
             "com.apple.adid"
             "com.apple.ocspd"
             "com.apple.ak.anisette.xpc"
             "com.apple.spotlight.IndexAgent"
             "com.apple.system.opendirectoryd.api"
             "com.apple.SystemConfiguration.NetworkInformation"
             "com.apple.storeassetd"
             "com.apple.server.bluetooth"
             "com.apple.ak.auth.xpc"
             "com.apple.tccd"
             "com.apple.tccd.system"
             "com.apple.identityservicesd.idquery.desktop.auth"
             "com.apple.AddressBook.ContactsAccountsService"
             "com.apple.AddressBook.abd"
             "com.apple.imagent.desktop.auth"
             "com.apple.coreduetd.people"
             "com.apple.duetactivityscheduler"
             "com.apple.SBUserNotification"
             "com.apple.xpc.amstoold"
             "com.apple.metadata.mds"
             "com.apple.xpc.amsengagementd"
             "com.apple.amsprivateidentifiers"
             "com.apple.ctkd.token-client"
))

(allow file-read*
    (literal "/private/var/db/CoreDuet/People/interactionC.db")
    (literal "/private/var/db/CoreDuet/People/interactionC.db-shm")
    (literal "/private/var/db/CoreDuet/People/interactionC.db-wal"))
(allow file-write*
    (literal "/private/var/db/CoreDuet/People/interactionC.db-shm"))
    
(allow file-map-executable
    (subpath "/System/Library/Frameworks")
    (subpath "/System/Library/PrivateFrameworks")
    (subpath "/System/Library/Address Book Plug-Ins")
)

(allow process-info-pidinfo)

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

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

(with-filter (system-attribute apple-internal)
    (allow nvram-get
           (nvram-variable "AMFITrustedKeys")
    )
)

(allow iokit-open
    (iokit-registry-entry-class "RootDomainUserClient")
    (iokit-user-client-class "AppleKeyStoreUserClient")
)

(system-network)
(allow network-outbound)
(allow network-inbound)

(allow file-read* (extension "com.apple.app-sandbox.read"))
(allow file-read-metadata (extension "com.apple.app-sandbox.read"))
(allow file-issue-extension
    (extension-class "com.apple.app-sandbox.read")
)

(allow file-read* file-write*
    (extension "com.apple.sandbox.container")
)

(allow file-issue-extension*
    (require-all
        (extension-class "com.apple.app-sandbox.read-write")
            (home-subpath "/Library/Caches/com.apple.AppleMediaServices")
    )
    (require-all
        (extension-class "com.apple.app-sandbox.read-write")
            (home-subpath "/library/caches/cloudkit/com.apple.gamed")
    )
)

;; allow launching of games
(allow mach-lookup
    (global-name "com.apple.coreservices.quarantine-resolver")
)
(if (defined? 'lsopen)
    (allow lsopen)
)
    
;; Address book
(allow distributed-notification-post)
(with-filter (extension "com.apple.tcc.kTCCServiceAddressBook")
    (allow mach-lookup
           (global-name "com.apple.AddressBook.abd")
           (global-name "com.apple.AddressBook.AddressBookApplicationFrameworkIPC")
           (global-name "com.apple.AddressBook.AssistantService")
           (global-name "com.apple.AddressBook.ContactsAccountsService")
           (global-name "com.apple.AddressBook.SourceSync")
           (global-name "com.apple.ContactsAgent.addressbook"))
    (allow user-preference-read user-preference-write (preference-domain "com.apple.AddressBook"))
    (allow file-read* file-write*
           (home-subpath "/Library/Application Support/AddressBook")))
