;;; Copyright (c) 2021 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)))

(define (darwinuserdir-subpath darwinuserdir-relative-subpath)
    (subpath (string-append (param "DARWIN_USER_DIR") darwinuserdir-relative-subpath)))

(allow process-info* (target self))
(allow process-info-pidinfo)
(allow process-fork)
(allow process-exec
    (subpath "/bin")
    (subpath "/usr/bin"))

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

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

;; For sample/leaks
(allow mach-task-read mach-task-name
    (target others))
	   
;; Allow UNIX signals and process rusage
(allow signal)
(allow process-info-rusage)
(allow sysctl-read sysctl-write (sysctl-name "vm.self_region_page_size"))
(allow sysctl-read sysctl-write (sysctl-name "vm.self_region_footprint"))
(allow system-fsctl (fsctl-command APFSIOC_GET_PURGEABLE_FILE_FLAGS))

;; preference domain(s)
(allow user-preference-read
        (preference-domain "com.apple.photos.shareddefaults")
        (preference-domain "kCFPreferencesAnyApplication"))
        
(allow user-preference-read user-preference-write
       (preference-domain "com.apple.Photos.CPLDiagnose"))

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

;; Read access for helper tasks and $HOME/Library data
(allow file-read*
    (subpath "/bin")
    (subpath "/usr/bin")
    (subpath "/usr/libexec")
    (subpath "/.fseventsd")
    (home-subpath "/Library/Caches/com.apple.cache_delete")
    (home-subpath "/Library/Containers")
    (home-subpath "/Library/Logs")
    (home-subpath "/Library/MediaAnalysis")
    (home-subpath "/Library/Photos")
    (home-subpath "/Library/Preferences")
    (home-subpath "/.CFUserTextEncoding")
    (darwinuserdir-subpath "/com.apple.LaunchServices.dv"))

;; support app-sandbox semantics for passed in URLs with security scope
(with-filter (extension "com.apple.app-sandbox.read")
  (allow file-read*)
  (allow file-issue-extension (extension-class "com.apple.app-sandbox.read")))
(with-filter (extension "com.apple.app-sandbox.read-write")
  (allow file-read* file-write*)
  (allow file-issue-extension (extension-class "com.apple.app-sandbox.read" "com.apple.app-sandbox.read-write")))

;; Read/write cache access
(let ((cache-path-filter (home-subpath "/Library/Caches/com.apple.Photos.CPLDiagnose")))
  (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.photos.service")
       (global-name "com.apple.cloudphotod")
       (global-name "com.apple.cloudphotod.daemon")
       (global-name "com.apple.photoanalysisd")
       (global-name "com.apple.accountsd.accountmanager")
       (global-name "com.apple.sysdiagnose.service.xpc")
       (global-name "com.apple.lsd.mapdb")
       (global-name "com.apple.lsd.modifydb")
       (global-name "com.apple.securityd.xpc")
       (global-name "com.apple.coreservices.quarantine-resolver")
       (global-name "com.apple.coreservices.launchservicesd")
       (global-name "com.apple.coresymbolicationd")
       (global-name "com.apple.metadata.mds")
       (global-name "com.apple.system.opendirectoryd.api")
       (global-name "com.apple.cache_delete.public"))

;; We need read/write access to our xpcroleaccountd staging areas and global prefs
(allow file-read*
    (prefix "/private/var/db/com.apple.xpc.roleaccountd.staging/com.apple.Photos.CPLDiagnose")
    (prefix "/private/var/root/Library/Preferences/ByHost/.GlobalPreferences.")
    (literal "/private/var/root/Library/Preferences/.GlobalPreferences.plist")
    (literal "/private/var/root/Library/Preferences/.GlobalPreferences_m.plist")
    (literal "/Library/Preferences/.GlobalPreferences.plist"))

;; Internal rules
(with-filter (system-attribute apple-internal)
      (allow process-exec (with no-sandbox) (subpath "/usr/local/bin"))
      (allow file-read-data (literal "/Library/Preferences/com.apple.security.xpc.plist"))
      (allow file-read-data file-write-data (home-subpath "/Library/Developer/Xcode/")))
