;;; 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)
(import "system.sb")

(deny default)
(deny file-map-executable process-info* nvram*)
(deny dynamic-code-generation)

;;; 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-pidinfo)
(allow process-info-dirtycontrol)
(allow process-info-setcontrol)

(allow network-outbound)

(allow system-socket)
(allow system-fsctl)

(allow sysctl-read)

(allow file-map-executable
    (literal "/usr/lib/libobjc-trampolines.dylib"))

(allow ipc-posix-shm-read-data
    (ipc-posix-name-prefix "apple.cfprefs.")
    (ipc-posix-name "apple.cfprefs.daemonv1"))

(allow file-read* file-write*
    (extension "com.apple.sandbox.application-group"))
(allow mach-lookup 
    (global-name "com.apple.containermanagerd"))

(allow mach-lookup
    (global-name "com.apple.AppSSO.service-xpc"
                 "com.apple.CoreServices.coreservicesd"
                 "com.apple.SecurityServer"
                 "com.apple.SystemConfiguration.DNSConfiguration"
                 "com.apple.SystemConfiguration.configd"
                 "com.apple.accountsd.accountmanager"
                 "com.apple.adid"
                 "com.apple.ak.anisette.xpc"
                 "com.apple.analyticsd"
                 "com.apple.apsd"
                 "com.apple.cfprefsd.agent"
                 "com.apple.cfprefsd.daemon"
                 "com.apple.coreservices.quarantine"
                 "com.apple.distributed"
                 "com.apple.dnssd.service"
                 "com.apple.locationd.desktop.synchronous"
                 "com.apple.logd.events"
                 "com.apple.lsd.mapdb"
                 "com.apple.lsd.modifydb"
                 "com.apple.mediaremoted.xpc"
                 "com.apple.runningboard"
                 "com.apple.symptom"
                 "com.apple.system.opendirectoryd.api"
                 "com.apple.trustd.agent"
                 "com.apple.usernotifications.usernotificationservice"
                 "com.apple.usernotifications.listener"
                 "com.apple.usymptomsd"
                 "com.apple.windowserver.active"
                 "com.apple.xpc.amsengagementd"
                 "com.apple.xpc.amstoold"
                 "com.apple.symptom_analytics"
                 "com.apple.distributed_notifications@Uv3"
                 "com.apple.watchlistd.aps"
                 "com.apple.watchlistd.xpc"
                 "com.apple.system.opendirectoryd.libinfo"
                 "com.apple.diagnosticd"
                 "com.apple.FileCoordination"
                 "com.apple.xpc.amsaccountsd"))

(allow user-preference-read
    (preference-domain "com.apple.AppleMediaServices")
    (preference-domain "com.apple.AppleMediaServices.notbackedup")
    (preference-domain "com.apple.TV")
    (preference-domain "com.apple.tv")
    (preference-domain "com.apple.WatchListKit")
    (preference-domain "com.apple.itunesstored")
    (preference-domain "kCFPreferencesAnyApplication"))

(allow user-preference-write
    (preference-domain "com.apple.AppleMediaServices")
    (preference-domain "com.apple.AppleMediaServices.notbackedup")
    (preference-domain "com.apple.WatchListKit")
    (preference-domain "com.apple.TV"))

(allow file-read-metadata)

(allow process-info-pidinfo)

(allow distributed-notification-post)

(allow file-read* file-write* 
    (literal "/dev/urandom")
    (home-subpath "/Library/com.apple.WatchListKit")
    (home-subpath "/Library/Caches/com.apple.watchlistd")
    (home-subpath "/Caches/com.apple.AppleMediaServices")
    (home-subpath "/Library/Caches/com.apple.AppleMediaServices/Metrics/com.apple.AppleMediaServices"))
 
(allow file-read-data
    (literal "/private/var/folders/lp")
    (literal "/usr/share/icu/icudt70l.dat")
    (literal "/var/db/timezone/icutz/icutz44l.dat")
    (literal "/usr/local/lib/log")
    (literal "/System/Library/OpenDirectory/request-schema.plist")
    (subpath "/System/Library/CoreServices")
    (subpath "/usr/lib")
    (subpath "/Library/Preferences/Logging")
    (subpath "/System/Library/Frameworks")
    (subpath "/System/Library/PrivateFrameworks"))
