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

;; Needed to lookup information about incoming XPC clients
(allow process-info-pidinfo)

;; Contacts uses this
(allow distributed-notification-post)

;; For resolving symlinks, realpath(3), and equivalents.
(allow file-read-metadata)

;; For validating the entitlements of clients.
(allow process-info-codesignature)

(allow user-preference-read user-preference-write
       (preference-domain "com.apple.sociallayerd")
       (preference-domain "com.apple.SocialLayer")
       (preference-domain "com.apple.sociallayerd.CloudKit.ckwriter")
       (preference-domain "com.apple.sociallayerd.CloudKit.ckreader")
       )
       
(allow user-preference-read
       (preference-domain "kCFPreferencesAnyApplication")
       (preference-domain "com.apple.CloudKit")
       )

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

;; FileProvider reads xattr on the Desktop and Documents directories on macOS when loading an FPItem for a file within those directories. See: rdar://98577978
(allow file-read-xattr
        (home-subpath "/Desktop")
        (home-subpath "/Documents"))

;; Read/write cache access
(let ((cache-path-filter (home-subpath "/Library/Caches/com.apple.sociallayerd")))
  (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)))
      
;; Contacts
(import "contacts.sb")
(contacts-client (param "HOME") (param "TMPDIR"))

;; Use the graphics hardware for rendering
(system-graphics)

;; For all kext GPU drivers
(allow file-map-executable
    (subpath "/System/Library/Extensions"))

(allow file-read*
    (literal "/Library/Application Support/CrashReporter/SubmitDiagInfo.domains"))

;; Allow reading files via consumed sandbox extensions from FileProvider/apps
;; Required for SLDCloudDocsService and SLDCollaborationHandshakeService to access FileProvider documents
;; rdar://159819435 (sociallayerd has no read access to files from sandbox extension)
(allow file-read*
    (require-any
        (extension "com.apple.app-sandbox.read")
        (extension "com.apple.app-sandbox.read-write")))

;; Allow writing files via consumed read-write sandbox extensions
(allow file-write*
    (extension "com.apple.app-sandbox.read-write"))

(allow mach-lookup
    (global-name "com.apple.lsd.mapdb")
    (global-name "com.apple.cloudd")
    (global-name "com.apple.proactive.PersonalizationPortrait.SocialHighlight")
    (global-name "com.apple.accountsd.accountmanager")
    (global-name "com.apple.CARenderServer")
    (global-name "com.apple.fonts")
    (global-name "com.apple.system.opendirectoryd.api")
    (global-name "com.apple.tccd")
    (global-name "com.apple.accountsd.accountmanager")
    (global-name "com.apple.SystemConfiguration.configd")
    (global-name "com.apple.coreservices.launchservicesd")
    (global-name "com.apple.coreservices.appleevents")
    (global-name "com.apple.identityservicesd.desktop.auth")
    (global-name "com.apple.identityservicesd.idquery.desktop.auth")
    (global-name "com.apple.imagent.desktop.auth")
    (global-name "com.apple.windowserver.active")
    (global-name "com.apple.apsd")
    (global-name "com.apple.FileProvider")
    (global-name "com.apple.telephonyutilities.callservicesdaemon.conversationmanager")
    (global-name "com.apple.telephonyutilities.callservicesdaemon.callcapabilities")
    (global-name "com.apple.telephonyutilities.callservicesdaemon.callstatecontroller")
    (global-name "com.apple.coreservices.quarantine-resolver")
    (xpc-service-name "com.apple.imdpersistence.IMDPersistenceAgent")
    (global-name "com.apple.securityd.xpc")
    (global-name "com.apple.securityd")
    )

(allow appleevent-send)
(allow lsopen)
