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

;; Your preference domain
(allow user-preference-read user-preference-write
       (preference-domain "com.apple.managedappdistributiond"))

;; Read/write access to a temporary directory.
(allow file-read* file-write*
       (subpath (param "_DARWIN_CACHE_DIR"))
       (subpath (param "_DARWIN_TEMP_DIR")))

;; Read/write cache access
(let ((cache-path-filter (home-subpath "/Library/Caches/com.apple.managedappdistributiond")))
  (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)))

;; End of template

(allow network-outbound)
(allow system-socket)

(allow ipc-posix-sem)

(allow iokit-open*
    (iokit-user-client-class "AppleKeyStoreUserClient")
    (iokit-user-client-class "AppleNVMeEANUC"))

(allow user-preference-read
    (preference-domain "kCFPreferencesAnyApplication")
    (preference-domain "com.apple.commerce")
    (preference-domain "com.apple.mdmclient")
    (preference-domain "com.apple.universalaccess"))

(allow authorization-right-obtain
    (right-name "system.install.apple-config-data")
    (right-name "system.install.apple-software")
    (right-name "system.install.apple-software.standard-user")
    (right-name "system.install.app-store-software")
    (right-name "system.install.app-store-software.standard-user")
    (right-name "system.install.software")
    (right-name "system.install.software.iap")
    (right-name "system.install.software.mdm-provided"))

(allow mach-lookup
    (global-name "com.apple.appinstalld")
    (global-name "com.apple.AssetCacheLocatorService")
    (global-name "com.apple.commcenter.coretelephony.xpc")
    (global-name "com.apple.containermanagerd")
    (global-name "com.apple.dnssd.service")
    (global-name "com.apple.duetactivityscheduler")
    (global-name "com.apple.fpsd")
    (global-name "com.apple.FSEvents")
    (global-name "com.apple.installcoordinationd")
    (global-name "com.apple.installd")
    (global-name "com.apple.lsd.advertisingidentifiers")
    (global-name "com.apple.lsd.encryption")
    (global-name "com.apple.lsd.mapdb")
    (global-name "com.apple.lsd.modifydb")
    (global-name "com.apple.metadata.mds")
    (global-name "com.apple.mobile.usermanagerd.xpc")
    (global-name "com.apple.nehelper")
    (global-name "com.apple.ProgressReporting")
    (global-name "com.apple.SecurityServer")
    (global-name "com.apple.uninstall.service")
    (global-name "com.apple.usymptomsd"))

(allow file-issue-extension
    (extension-class "com.apple.StreamingUnzipService"))

(allow file-read*
    (literal "/Applications")
    (literal "/Library/Preferences/com.apple.networkextension.uuidcache.plist")
    (literal "/Library/Preferences/com.apple.security.plist")
    (literal "/private/var/db/nsurlstoraged/dafsaData.bin"))

(allow file-read* file-write*
    (extension "com.apple.sandbox.application-group")
    (extension "com.apple.sandbox.container")
    (home-subpath "/Library/Group Containers/com.apple.managedappdistributiod")
    (home-subpath "/Library/HTTPStorages/com.apple.managedappdistributiond")
    (home-subpath "/Library/Logs/com.apple.StoreServices/HTTPArchives")
    (literal "/private/var/db/mds/system/mds.lock")
    (subpath "/private/var/db/installcoordinationd"))

; Allow access to application receipts and download placeholders
(allow file-read* file-write*
    (require-all
        (vnode-type DIRECTORY)
        (require-any
            (mount-relative-regex #"^/Applications/.*\.appdownload(/Contents)?$")
            (regex #"\.app/Contents/_MASReceipt$")))
    (require-all
        (vnode-type REGULAR-FILE)
        (require-any
            (mount-relative-regex #"\.appdownload/Contents/placeholderinfo$")
            (mount-relative-regex #"\.appdownload/Icon")
            (regex #"\.app/Contents/_MASReceipt/receipt$")))
    (literal "/Applications"))
; Allow access to read applications for code signature verification
(allow file-read*
    (subpath "/Applications"))

;; The following mach-lookups are not actually reachable because they are for agents and this is a daemon.
;; But they are here in an attempt to lessen the sandbox violations we have. And in at least some of these
;; cases, they have checks that gracefully recover or fail when the service cannot be reached.
;;
;; Use "(with send-signal SIGINT)" to cause debugger signals.
(deny (with no-report) mach-lookup
    (global-name "com.apple.AppSSO.service-xpc")
    (global-name "com.apple.securityd.xpc")
    (global-name "com.apple.symptom_analytics")
    (global-name "com.apple.accountsd.accountmanager")
    (global-name "com.apple.cfnetwork.AuthBrokerAgent")
    (global-name "com.apple.coreservices.quarantine-resolver")
    (global-name "com.apple.xpc.amsaccountsd")
    (global-name "com.apple.xpc.amsengagementd")
    (global-name "com.apple.xpc.amstoold"))

;; AMS

(allow mach-lookup
    (global-name "com.apple.accountsd.accountmanager")
    (global-name "com.apple.adid")
    (global-name "com.apple.securityd.xpc")
    (global-name "com.apple.SystemConfiguration.DNSConfiguration")
    (global-name "com.apple.xpc.amsaccountsd")
    (global-name "com.apple.xpc.amsengagementd"))

(allow user-preference-read
    (preference-domain "com.apple.AppleMediaServices")
    (preference-domain "com.apple.da"))

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

(allow file-read*
    (literal "/Library/Application Support/CrashReporter/DiagnosticMessagesHistory.plist")
    (literal "/Library/Preferences/com.apple.networkd.plist")
    (literal "/private/var/run/resolv.conf"))

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

;; RemoteManagement

(allow mach-lookup
    (global-name "com.apple.remotemanagementd.store")
    (global-name "com.apple.mdmclient.daemon.unrestricted"))

(allow file-map-executable
    (literal "/System/Library/CoreServices/ManagedClient.app/Contents/PlugIns/MCXToolsInterface.bundle/Contents/MacOS/MCXToolsInterface"))

;; ???

; mds: mds.lock, mdsDirectory.db, mdsObject.db
; 1. extension "appstoreagent:mds"
;    uid == 0: r+w /private/var/db/mds/system
;    uid  > 0: r+w <_DARWIN_USER_CACHE_DIR>/mds
; 2. /private/var/db/mds/system/{mdsDirectory.db,mdsObject.db}
;    uid == 0: r+w (already covered by (extension "appstoreagent:mds"))
;    uid  > 0: r
(allow file-read* file-write* (extension "appstoreagent:mds"))
(allow file-read*
    (literal "/private/var/db/mds/system/mdsDirectory.db")
    (literal "/private/var/db/mds/system/mdsObject.db"))
; 3. se_SecurityMessages:
;    uid  < 500: /private/var/db/mds/messages/se_SecurityMessages
;    uid >= 500: /private/var/db/mds/messages/<uid>/se_SecurityMessages
(allow file-read*
       (literal (param "_SECURITY_MESSAGES_PATH")))
