com.apple.WebProcess.sb.in   [plain text]


; Copyright (C) 2010-2017 Apple Inc. All rights reserved.
;
; Redistribution and use in source and binary forms, with or without
; modification, are permitted provided that the following conditions
; are met:
; 1. Redistributions of source code must retain the above copyright
;    notice, this list of conditions and the following disclaimer.
; 2. Redistributions in binary form must reproduce the above copyright
;    notice, this list of conditions and the following disclaimer in the
;    documentation and/or other materials provided with the distribution.
;
; THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
; AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
; THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
; PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
; BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
; CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
; SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
; INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
; CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
; ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
; THE POSSIBILITY OF SUCH DAMAGE.

(version 1)
(deny default (with partial-symbolication))
(allow system-audit file-read-metadata)

#if __MAC_OS_X_VERSION_MIN_REQUIRED < 101300
(import "system.sb")
#else
;;;
;;; The following rules were originally contained in 'system.sb'. We are duplicating them here so we can
;;; remove unneeded sandbox extensions.
;;;

;;; Allow registration of per-pid services.
(allow mach-register (local-name-prefix ""))

;;; Allow read access to standard system paths.
(allow file-read*
    (require-all (file-mode #o0004)
    (require-any (subpath "/Library/Filesystems/NetFSPlugins")
    (subpath "/Library/Preferences/Logging")      ; Logging Rethink
    (subpath "/System")
    (subpath "/private/var/db/dyld")
    (subpath "/private/var/db/timezone")
    (subpath "/usr/lib")
    (subpath "/usr/share"))))

;;; Allow reading internal profiles on development builds
(allow file-read*
    (require-all (file-mode #o0004)
    (subpath "/AppleInternal/Library/Preferences/Logging")
    (system-attribute apple-internal)))

(allow file-read-metadata
    (literal "/etc")
    (literal "/tmp")
    (literal "/var")
    (literal "/private/etc/localtime"))


;;; Allow access to standard special files.
(allow file-read*
    (literal "/dev/autofs_nowait")
    (literal "/dev/random")
    (literal "/dev/urandom")
    (literal "/private/etc/master.passwd")
    (literal "/private/etc/passwd"))

(allow file-read*
       file-write-data
    (literal "/dev/null")
    (literal "/dev/zero"))

(allow file-read*
       file-write-data
       file-ioctl
    (literal "/dev/dtracehelper"))

(allow network-outbound
    (literal "/private/var/run/asl_input")
    (literal "/private/var/run/syslog"))


;;; Allow creation of core dumps.
(allow file-write-create
    (require-all (prefix "/cores/")
        (vnode-type REGULAR-FILE)))


;;; Allow IPC to standard system agents.
(allow ipc-posix-shm-read*
    (ipc-posix-name "apple.shm.notification_center")
    (ipc-posix-name-prefix "apple.cfprefs."))

;;; (system-graphics) - Allow access to graphics hardware.
(define (system-graphics)
    ;; Preferences
    (allow user-preference-read
        (preference-domain "com.apple.opengl")
        (preference-domain "com.nvidia.OpenGL"))
    ;; OpenGL memory debugging
    (allow mach-lookup
        (global-name "com.apple.gpumemd.source"))
    ;; CVMS
    (allow mach-lookup
        (global-name "com.apple.cvmsServ"))
    ;; OpenCL
    (allow iokit-open
        (iokit-connection "IOAccelerator")
        (iokit-registry-entry-class "IOAccelerationUserClient")
        (iokit-registry-entry-class "IOSurfaceRootUserClient")
        (iokit-registry-entry-class "IOSurfaceSendRight"))
    ;; CoreVideo CVCGDisplayLink
    (allow iokit-open
        (iokit-registry-entry-class "IOFramebufferSharedUserClient"))
    ;; H.264 Acceleration
    (allow iokit-open
        (iokit-registry-entry-class "AppleIntelMEUserClient")
        (iokit-registry-entry-class "AppleSNBFBUserClient"))
    ;; QuartzCore
    (allow iokit-open
        (iokit-registry-entry-class "AGPMClient")
        (iokit-registry-entry-class "AppleGraphicsControlClient")
        (iokit-registry-entry-class "AppleGraphicsPolicyClient"))
    ;; OpenGL
    (allow iokit-open
        (iokit-registry-entry-class "AppleMGPUPowerControlClient"))
    ;; GPU bundles
    (allow file-read*
        (subpath "/Library/GPUBundles"))
    ;; DisplayServices
    (allow iokit-set-properties
        (require-all (iokit-connection "IODisplay")
        (require-any (iokit-property "brightness")
        (iokit-property "linear-brightness")
        (iokit-property "commit")
        (iokit-property "rgcs")
        (iokit-property "ggcs")
        (iokit-property "bgcs")))))


;;; (system-network) - Allow access to the network.
(define (system-network)
    (allow file-read*
        (literal "/Library/Preferences/com.apple.networkd.plist"))
    (allow mach-lookup
        (global-name "com.apple.SystemConfiguration.PPPController")
        (global-name "com.apple.SystemConfiguration.SCNetworkReachability")
        (global-name "com.apple.nehelper")
        (global-name "com.apple.networkd")
        (global-name "com.apple.nsurlstorage-cache")
        (global-name "com.apple.symptomsd")
        (global-name "com.apple.usymptomsd"))
    (allow network-outbound
        (control-name "com.apple.netsrc")
        (control-name "com.apple.network.statistics"))
    (allow system-socket
        (require-all (socket-domain AF_SYSTEM)
        (socket-protocol 2)) ; SYSPROTO_CONTROL
    (socket-domain AF_ROUTE)))

;;;
;;; End rules originally copied from 'system.sb'
;;;
#endif

;;; process-info* defaults to allow; deny it and then allow operations we actually need.
(deny process-info*)
(allow process-info-pidinfo)
(allow process-info-setcontrol (target self))
#if __MAC_OS_X_VERSION_MIN_REQUIRED >= 101200
(allow process-codesigning-status*)
#endif

(deny sysctl*)
(allow sysctl-read
    (sysctl-name
        "hw.byteorder"
        "hw.busfrequency_max"
        "hw.cputype"
        "hw.l2cachesize"
        "hw.machine"
        "hw.memsize"
        "hw.model"
        "hw.ncpu"
        "hw.vectorunit"
        "kern.hostname"
        "kern.maxfilesperproc"
        "kern.memorystatus_level"
        "kern.safeboot"
        "kern.version"
        "security.mac.sandbox.sentinel"
        "vm.footprint_suspend")
    (sysctl-name-regex #"^hw.(active|avail)cpu")
    (sysctl-name-regex #"^hw.(busfrequency|cachelinesize|cpufrequency|pagesize|tbfrequency)_compat")
    (sysctl-name-regex #"^hw.(logical|physical)cpu_max")
    (sysctl-name-regex #"^hw.optional\.")
    (sysctl-name-regex #"^kern.os(release|type|variant_status|version)")
    (sysctl-name-regex #"^net.routetable")
)

(deny iokit-get-properties)
(allow iokit-get-properties
    (iokit-property "AGCInfo")
    (iokit-property "AccelCaps")
    (iokit-property-regex #"^(Accurate|Extended)MaxDigitizerPressureValue")
    (iokit-property-regex #"^(Activation|Animation)Thresholds")
    (iokit-property "ActuationSupported")
    (iokit-property "AllowDisplaySleep")
    (iokit-property "AlwaysNeedsVelocityCalculated")
    (iokit-property-regex #"Apple(GVAKeyDoesNotExist|IntelMEVABundleName)")
    (iokit-property-regex #"^AAPL,(DisplayPipe|OpenCLdisabled|IOGraphics_LER(|_RegTag_1|_RegTag_0|_Busy_2)|alias-policy|boot-display|display-alias|mux-switch-state|ndrv-dev|primary-display|slot-name)")
    (iokit-property-regex #"^ATY,fb_(linebytes|offset|size)")
    (iokit-property "BacklightHandle")
    (iokit-property "BlockSize")
    (iokit-property-regex #"^CEA(ModeID|PixelRepetition)")
    (iokit-property "CFBundleIdentifier")
    (iokit-property "CapsLockDelay")
    (iokit-property "ConfigState")
    (iokit-property "Description")
    (iokit-property-regex #"^Device( Characteristics|EqID)")
    (iokit-property "DisplayRouting")
    (iokit-property "Driver is Ready")
    (iokit-property "EnableLPVP")
    (iokit-property "Endianness")
    (iokit-property "Family ID")
    (iokit-property "ForceSupported")
    (iokit-property "Formats")
    (iokit-property "HIDPointerAccelerationType")
    (iokit-property-regex #"^IOAccel(DisplayPipeCapabilities|Index|Types|Revision)")
    (iokit-property-regex #"^IO(Class|MatchCategory|NameMatch)")
    (iokit-property-regex #"^IOAudioControl(ChannelID|ID|SubType|Usage|Value)")
    (iokit-property-regex #"^IOAudioDevice(CanBeDefaults|TransportType)")
    (iokit-property-regex #"^IOAudioEngine(ChannelNames|ClientDescription|CoreAudioPlugIn|(|Device)Description|Flavor|GlobalUniqueID|IsHidden|OutputChannelLayout|SampleOffset|State)")
    (iokit-property-regex #"^IOAudioEngineClock(Domain|IsStable)")
    (iokit-property "IOAudioEngineDisableClockBoundsCheck")
    (iokit-property-regex #"^IOAudioEngine(Input|Output)Sample(Latency|Offset)")
    (iokit-property-regex #"^IOAudioEngineNum(ActiveUserClients|SampleFramesPerBuffer)")
    (iokit-property "IOAudioSampleRate")
    (iokit-property "IOAudioStreamSampleFormatByteOrder")
    (iokit-property "IOBusyInterest")
    (iokit-property "IOCFPlugInTypes")
    (iokit-property "IOChildIndex")
    (iokit-property-regex #"^IOClass(|NameOverride)")
    (iokit-property "IOConsoleUsers")
    (iokit-property "IODVDBundleName")
    (iokit-property "IODeviceMemory")
    (iokit-property "IODisplayParameters")
    (iokit-property-regex #"^IOFB(CLUTDefer|Config|CursorInfo|Dependent(ID|Index))")
    (iokit-property "IOFBCurrentPixelClock")
    (iokit-property-regex #"^IOFBCurrentPixelCount(|Real)")
    (iokit-property-regex #"^IOFB(DetailedTimings|Gamma(Count|HeaderSize|Width))")
    (iokit-property-regex #"^IOFB(Blue|Green|Red)GammaScale")
    (iokit-property-regex #"^IOFBI2CInterface(IDs|Info)")
    (iokit-property-regex #"^IOFB(HDMIDongleROM|Integrated|MemorySize|NeedsRefresh|ProbeOptions|ScalerInfo|TimingRange|Transform|UIScale|WaitCursor(Frames|Period))")
    (iokit-property "IOFramebufferOpenGLIndex")
    (iokit-property "IOGeneralInterest")
    (iokit-property "IOGLBundleName")
    (iokit-property "IOHibernateState")
    (iokit-property "IOI2CTransactionTypes")
    (iokit-property-regex #"^IOInterrupt(Controllers|Specifiers)")
    (iokit-property "IOGVAVTCapabilities")
    (iokit-property-regex #"^IOGVA(Codec|EncoderRestricted)")
    (iokit-property-regex #"^IOGVA(.*)(De|En)code$")
    (iokit-property "IOMatchCategory")
    (iokit-property "IONDRVFramebufferGeneration")
    (iokit-property "IONVRAMProperty")
    (iokit-property-regex #"^IOName(|Match(|ed))")
    (iokit-property "IOPCITunnelled")
    (iokit-property "IOPMStrictTreeOrder")
    (iokit-property "IOParentMatch")
    (iokit-property-regex #"^IOPCI(Express(Capabilities|Link(Status|Capabilities))|MSIMode|Resourced|Tunnelled)")
    (iokit-property "IOPMIsPowerManaged")
    (iokit-property-regex #"^IOPlatform(SerialNumber|UUID)")
    (iokit-property "IOPowerManagement")
    (iokit-property "IOProbeScore")
    (iokit-property "IOPropertyMatch")
    (iokit-property "IOProviderClass")
    (iokit-property-regex #"^IOReport(Lures|Legend(|Public))")
    (iokit-property "IOScreenRestoreState")
    (iokit-property "IOSourceVersion")
    (iokit-property-regex #"^IOVA(BundleName|Renderer(|Sub)ID)")
    (iokit-property-regex #"^InternalStatistics(|Accm)")
    (iokit-property-regex #"^MetalPlugin(Name|ClassName)")
    (iokit-property "MetalStatisticsName")
    (iokit-property "MTHIDDevice")
    (iokit-property "MT Built-In")
    (iokit-property "MaintainPowerInUILock")
    (iokit-property "Max Packet Size")
    (iokit-property "MinDigitizerPressureValue")
    (iokit-property "Multitouch Serial Number")
    (iokit-property-regex #"^Multitouch (Subdevice |)ID")
    (iokit-property "NVArch")
    (iokit-property-regex #"^NVC(AP|LASS)")
    (iokit-property-regex #"^NVDA,(Features|NVPresentment-version|accel-loaded|invalid-config|mm-version)")
    (iokit-property-regex #"^NVDA(Type|initgl_created)")
    (iokit-property "NVRAMProperty")
    (iokit-property "NXSystemInfo")
    (iokit-property-regex #"^VRAM,(memvendorID|total(MB|size))")
    (iokit-property "NoAutoRoute")
    (iokit-property-regex #"^Num(Blocks|Streams)")
    (iokit-property-regex #"^PerformanceStatistics(|Accum)")
    (iokit-property "Protocol Characteristics")
    (iokit-property "Removable")
    (iokit-property-regex #"^ResetOn(Lock|Unlock)Ms")
    (iokit-property "SafeEjectRequested")
    (iokit-property "SampleRates")
    (iokit-property "Serial Number")
    (iokit-property "StartupDisplay")
    (iokit-property-regex #"^Support(AudioAUUC|sSilentClick|TapToWake)")
    (iokit-property-regex #"^Sensor (Columns|Rows)")
    (iokit-property-regex #"^Sensor Region (Descriptor|Param|Rows)")
    (iokit-property-regex #"^Sensor Surface (Descriptor|Height|Width)")
    (iokit-property "SurfaceList")
    (iokit-property "TimeStampFiltering")
    (iokit-property "Transport")
    (iokit-property "UserClientEnabled")
    (iokit-property "WANTS_FRAMES_IGNORED")
    (iokit-property-regex #"^acpi-(device|path)")
    (iokit-property "assigned-addresses")
    (iokit-property "attached-gpu-control-path")
    (iokit-property-regex #"^audio-(codec-info|device-mvalue|device-nvalue|selector)")
    (iokit-property "av-signal-type")
    (iokit-property "bcdVersion")
    (iokit-property-regex #"^(board|device|revision|subsystem|vendor)-id")
    (iokit-property "boot-gamma-restored")
    (iokit-property "built-in")
    (iokit-property "class-code")
    (iokit-property "compatible")
    (iokit-property "connector-type")
    (iokit-property-regex #"^(device|revision|subsystem-vendor|touch-size)-id")
    (iokit-property "device_type")
    (iokit-property "graphic-options")
    (iokit-property "hda-gfx")
    (iokit-property-regex #"^id(Product|Vendor)")
    (iokit-property "iofb_version")
    (iokit-property "model")
    (iokit-property "mt-device-id")
    (iokit-property "name")
    (iokit-property "nv-stats")
    (iokit-property-regex #"^parser-(options|type)")
    (iokit-property-regex #"^pci(-aspm-default|debug)")
    (iokit-property "port-number")
    (iokit-property "reg")
    (iokit-property "rm_board_number")
    (iokit-property-regex #"^(rom|vbios)-revision")
    (iokit-property "saved-config")
    (iokit-property "startup-timing")
    (iokit-property "touch-size-id")
)

#if __MAC_OS_X_VERSION_MIN_REQUIRED >= 101300
(deny mach-lookup (xpc-service-name-prefix ""))
(allow mach-lookup
    (xpc-service-name "com.apple.PerformanceAnalysis.animationperfd")
    (xpc-service-name "com.apple.accessibility.mediaaccessibilityd")
    (xpc-service-name "com.apple.audio.SandboxHelper")
    (xpc-service-name "com.apple.coremedia.videodecoder")
    (xpc-service-name "com.apple.coremedia.videoencoder")
    (xpc-service-name-regex #"\.apple-extension-service$")
    (xpc-service-name "com.apple.hiservices-xpcservice")
    (xpc-service-name "com.apple.ist.ds.appleconnect2.HelperService")
    (xpc-service-name "com.apple.print.normalizerd")
    (xpc-service-name "com.apple.securityd.xpc")
    (xpc-service-name "com.apple.signpost.signpost-notificationd")
)
#endif

;; Utility functions for home directory relative path filters
(define (home-regex home-relative-regex)
  (regex (string-append "^" (regex-quote (param "HOME_DIR")) home-relative-regex)))

(define (home-subpath home-relative-subpath)
  (subpath (string-append (param "HOME_DIR") home-relative-subpath)))

(define (home-literal home-relative-literal)
  (literal (string-append (param "HOME_DIR") home-relative-literal)))

(define (allow-read-directory-and-issue-read-extensions path)
    (if path
        (begin
            (allow file-read* (subpath path))
            (allow file-issue-extension (require-all (extension-class "com.apple.app-sandbox.read") (subpath path))))))

(define (allow-read-write-directory-and-issue-read-write-extensions path)
    (if path
        (begin
            (allow file-read* file-write* (subpath path))
            (allow file-issue-extension (require-all (extension-class "com.apple.app-sandbox.read") (subpath path)))
            (allow file-issue-extension (require-all (extension-class "com.apple.app-sandbox.read-write") (subpath path))))))

;; Remove when <rdar://problem/29646094> is fixed.
(define (HEX-pattern-match-generator pattern-descriptor)
    (letrec ((pattern-string ""))
        (for-each  (lambda (repeat-count)
            (if (zero? repeat-count)
                (set! pattern-string (string-append  pattern-string "-"))
                (let appender ((count repeat-count))
                    (if (> count 0)
                        (begin
                            (set! pattern-string (string-append  pattern-string "[0-9A-F]"))
                            (appender (- count 1)))))))
            pattern-descriptor)
    pattern-string))

;; return a regex pattern matching string for 8-4-4-4-12 UUIDs:
(define (uuid-HEX-pattern-match-string)
    (HEX-pattern-match-generator '(8 0 4 0 4 0 4 0 12)))

;; global to hold the computed UUID matching pattern.
(define *uuid-pattern* "")

(define (uuid-regex-string)
    (if (zero? (string-length *uuid-pattern*))
        (set! *uuid-pattern* (uuid-HEX-pattern-match-string)))
    *uuid-pattern*)

;; Read-only preferences and data
(allow file-read*
    ;; Basic system paths
    (subpath "/Library/Dictionaries")
    (subpath "/Library/Fonts")
    (subpath "/Library/Frameworks")
    (subpath "/Library/Managed Preferences")
    (subpath "/Library/Speech/Synthesizers")
    (regex #"^/private/etc/(hosts|group|passwd)$")

    (literal "/Library/Application Support/CrashReporter/SubmitDiagInfo.domains")

    ;; System and user preferences
    (home-literal "/.CFUserTextEncoding")

    ;; FIXME: This should be removed when <rdar://problem/8957845> is fixed.
    (home-subpath "/Library/Fonts")

    (subpath "/Library/Audio/Plug-Ins/HAL")

    (home-subpath "/Library/Dictionaries"))

(allow file-read-data
    ;; Needed for AES3 support
    (subpath "/Library/Audio/Plug-Ins/Components"))

;; Preferences support
(allow user-preference-read
    (preference-domain
        "kCFPreferencesAnyApplication"
        "com.apple.ATS"
        "com.apple.CoreGraphics"
        "com.apple.DownloadAssessment"
        "com.apple.HIToolbox"
        "com.apple.LaunchServices"
        "com.apple.MultitouchSupport" ;; FIXME: Remove when <rdar://problem/13011633> is fixed.
        "com.apple.QTKit"
        "com.apple.ServicesMenu.Services" ;; Needed for NSAttributedString <rdar://problem/10844321>
        "com.apple.WebFoundation"
        "com.apple.avfoundation"
        "com.apple.avfoundation.frecents" ;; <rdar://problem/33137029>
        "com.apple.avfoundation.videoperformancehud" ;; <rdar://problem/31594568>
        "com.apple.coremedia"
        "com.apple.crypto"
        "com.apple.driver.AppleBluetoothMultitouch.mouse"
        "com.apple.driver.AppleBluetoothMultitouch.trackpad"
        "com.apple.driver.AppleHIDMouse"
        "com.apple.lookup.shared"
#if __MAC_OS_X_VERSION_MIN_REQUIRED < 101200
        "com.apple.mediaaccessibility"
#endif
        "com.apple.networkConnect"
        "com.apple.security"
        "com.apple.security.common"
        "com.apple.security.revocation"
        "com.apple.speech.voice.prefs"
        "com.apple.systemsound"
        "com.apple.universalaccess"
        "edu.mit.Kerberos"
        "pbs" ;; Needed for NSAttributedString <rdar://problem/10844321>
))

;; On-disk WebKit2 framework location, to account for debug installations outside of /System/Library/Frameworks,
;; and to allow issuing extensions.
(allow-read-directory-and-issue-read-extensions (param "WEBKIT2_FRAMEWORK_DIR"))

;; Allow issuing extensions to system libraries that the Network process can already read.
;; This is to avoid warnings attempting to create extensions for these resources.
(allow-read-directory-and-issue-read-extensions "/System/Library/PrivateFrameworks/WebInspectorUI.framework")

;; Sandbox extensions
(define (apply-read-and-issue-extension op path-filter)
    (op file-read* path-filter)
    (op file-issue-extension (require-all (extension-class "com.apple.app-sandbox.read") path-filter)))
(define (apply-write-and-issue-extension op path-filter)
    (op file-write* path-filter)
    (op file-issue-extension (require-all (extension-class "com.apple.app-sandbox.read-write") path-filter)))
(define (read-only-and-issue-extensions path-filter)
    (apply-read-and-issue-extension allow path-filter))
(define (read-write-and-issue-extensions path-filter)
    (apply-read-and-issue-extension allow path-filter)
    (apply-write-and-issue-extension allow path-filter))
(read-only-and-issue-extensions (extension "com.apple.app-sandbox.read"))
(read-write-and-issue-extensions (extension "com.apple.app-sandbox.read-write"))
(allow mach-lookup (extension "com.apple.app-sandbox.mach")) ;; FIXME: Should be removed when <rdar://problem/13066206> is fixed.

;; Allow the OpenGL Profiler to attach.
(if (defined? 'mach-register)
    (allow mach-register (global-name-regex #"^_oglprof_attach_<[0-9]+>$")))

#if __MAC_OS_X_VERSION_MIN_REQUIRED < 101200
;; MediaAccessibility
(allow user-preference-read user-preference-write
    (preference-domain "com.apple.mediaaccessibility.public"))
#endif

(if (positive? (string-length (param "DARWIN_USER_CACHE_DIR")))
    (allow-read-write-directory-and-issue-read-write-extensions (param "DARWIN_USER_CACHE_DIR")))

(if (positive? (string-length (param "DARWIN_USER_TEMP_DIR")))
    (allow-read-write-directory-and-issue-read-write-extensions (param "DARWIN_USER_TEMP_DIR")))

;; IOKit user clients
(allow iokit-open
    (iokit-user-client-class "AppleMultitouchDeviceUserClient")
    (iokit-user-client-class "AppleUpstreamUserClient")
    (iokit-user-client-class "IOHIDParamUserClient")
    (iokit-user-client-class "RootDomainUserClient")
    (iokit-user-client-class "IOAudioControlUserClient")
    (iokit-user-client-class "IOAudioEngineUserClient")
    ;; Following is needed due to <rdar://problem/10427451> && <rdar://problem/10808817>
    (iokit-user-client-class "AudioAUUC"))

;; cookied.
;; FIXME: Update for <rdar://problem/13642852>.
(allow ipc-posix-shm-read-data
    (ipc-posix-name "FNetwork.defaultStorageSession")
    (ipc-posix-name-regex #"\.PrivateBrowsing-")
    (ipc-posix-name-regex #"^WebKit Test-"))

;; ColorSync
(allow ipc-posix-shm*
    (ipc-posix-name "com.apple.ColorSync.Gen.lock")
    (ipc-posix-name "com.apple.ColorSync.Disp.lock")
    (ipc-posix-name "com.apple.ColorSync.Gray2.2")
    (ipc-posix-name "com.apple.ColorSync.sRGB")
    (ipc-posix-name "com.apple.ColorSync.GenGray")
    (ipc-posix-name "com.apple.ColorSync.GenRGB"))

;; Audio
(allow ipc-posix-shm-read* ipc-posix-shm-write-data
    (ipc-posix-name-regex #"^AudioIO"))

;; Remote Web Inspector
(allow mach-lookup
       (global-name "com.apple.webinspector"))

;; Various services required by AppKit and other frameworks
(allow mach-lookup
       (global-name "com.apple.FileCoordination")
       (global-name "com.apple.FontObjectsServer")
#if __MAC_OS_X_VERSION_MIN_REQUIRED < 101200
       (global-name "com.apple.FontServer")
#endif
       (global-name "com.apple.PowerManagement.control")
       (global-name "com.apple.SystemConfiguration.configd")
       (global-name "com.apple.SystemConfiguration.PPPController")
       (global-name "com.apple.audio.SystemSoundServer-OSX")
#if __MAC_OS_X_VERSION_MIN_REQUIRED >= 101300
       (global-name "com.apple.analyticsd")
       (global-name "com.apple.audio.AudioComponentRegistrar")
#endif
       (global-name "com.apple.audio.audiohald")
       (global-name "com.apple.audio.coreaudiod")
       (global-name "com.apple.awdd")
       (global-name "com.apple.cfnetwork.AuthBrokerAgent")
       (global-name "com.apple.cfprefsd.agent")
       (global-name "com.apple.cfprefsd.daemon")
       (global-name "com.apple.cookied")
       (global-name "com.apple.coreservices.launchservicesd")
       (global-name "com.apple.diagnosticd")
       (global-name "com.apple.dock.server")
       (global-name "com.apple.dyld.closured")
       (global-name "com.apple.fonts")
       (global-name "com.apple.iconservices")
       (global-name "com.apple.iconservices.store")
#if __MAC_OS_X_VERSION_MIN_REQUIRED >= 101200
       (global-name "com.apple.mediaremoted.xpc")
#endif
       (global-name "com.apple.logd")
       (global-name "com.apple.logd.events")
       (global-name "com.apple.lsd.mapdb")
       (global-name "com.apple.mobileassetd")
       (global-name "com.apple.nesessionmanager.flow-divert-token")
       (global-name "com.apple.speech.speechsynthesisd")
       (global-name "com.apple.speech.synthesis.console")
       (global-name "com.apple.system.DirectoryService.libinfo_v1")
       (global-name "com.apple.system.logger")
       (global-name "com.apple.system.opendirectoryd.api")
       (global-name "com.apple.systemstats.analysis")
       (global-name "com.apple.tccd")
       (global-name "com.apple.tccd.system")
       (global-name "com.apple.trustd")
       (global-name "com.apple.trustd.agent")
       (global-name "com.apple.window_proxies")
       (global-name "com.apple.windowserver.active")
       (global-name "com.apple.xpc.activity.unmanaged")
       (global-name "com.apple.xpcd")
)

;; Security framework
(allow mach-lookup
       (global-name "com.apple.ctkd.token-client")
       (global-name "com.apple.ocspd")
       (global-name "com.apple.securityd.xpc") 
       (global-name "com.apple.CoreAuthentication.agent.libxpc")
       (global-name "com.apple.SecurityServer"))

;; FIXME: This should be removed when <rdar://problem/10479685> is fixed.
;; Restrict AppSandboxed processes from creating /Library/Keychains, but allow access to the contents of /Library/Keychains:
(allow file-read-data file-read-metadata file-write*
    (subpath "/Library/Keychains"))

;; Do permit creating per-user keychains
(allow file-read* file-write*
    (home-subpath "/Library/Keychains"))

;; Except deny access to new-style iOS Keychain folders which are UUIDs.
(deny file-read* file-write*
    (regex (string-append "/Library/Keychains/" (uuid-regex-string) "(/|$)"))
    (home-regex (string-append "/Library/Keychains/" (uuid-regex-string) "(/|$)")))

(allow file-read* file-write* (subpath "/private/var/db/mds/system")) ;; FIXME: This should be removed when <rdar://problem/9538414> is fixed.

(allow file-read*
       (subpath "/private/var/db/mds")
       (literal "/private/var/db/DetachedSignatures")
       ; The following are needed until <rdar://problem/11134688> is resolved.
       (literal "/Library/Preferences/com.apple.security.plist")
       (literal "/Library/Preferences/com.apple.security.common.plist")
       (literal "/Library/Preferences/com.apple.security.revocation.plist")
       (home-literal "/Library/Application Support/SyncServices/Local/ClientsWithChanges/com.apple.Keychain")
       (home-literal "/Library/Preferences/com.apple.security.plist")
       (home-literal "/Library/Preferences/com.apple.security.revocation.plist"))

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

;; CoreFoundation. We don't import com.apple.corefoundation.sb, because it allows unnecessary access to pasteboard.
(allow mach-lookup
    (global-name-regex #"^com.apple.distributed_notifications")                                                       
    (global-name "com.apple.CoreServices.coreservicesd"))
(allow file-read-data
    (literal "/dev/autofs_nowait")) ; Used by CF to circumvent automount triggers
(allow ipc-posix-shm
    (ipc-posix-name-regex #"^CFPBS:")) ; <rdar://problem/13757475>
(allow system-fsctl (fsctl-command (_IO "h" 47)))

;; Graphics
(system-graphics)

;; Networking
(system-network)
(allow network-outbound
       ;; Local mDNSResponder for DNS, arbitrary outbound TCP
       (literal "/private/var/run/mDNSResponder")
       (remote tcp))

#if __MAC_OS_X_VERSION_MIN_REQUIRED >= 101300
;; CFNetwork
(allow file-read-data (path "/private/var/db/nsurlstoraged/dafsaData.bin"))
#endif

;; FIXME should be removed when <rdar://problem/9347205> + related radar in Safari is fixed
(allow mach-lookup
       (global-name "org.h5l.kcm")
       (global-name "com.apple.GSSCred")
       (global-name "com.apple.system.logger")
       (global-name "com.apple.system.notification_center"))
(allow network-outbound
       (remote udp))
(allow user-preference-read
    (preference-domain
        "com.apple.Kerberos"
        "com.apple.GSS"))

(allow file-read*
        (literal "/private/etc/krb5.conf")
        (literal "/private/etc/services")
        (literal "/private/etc/host")
        (subpath "/Library/KerberosPlugins/GSSAPI")
        (subpath "/Library/KerberosPlugins/KerberosFrameworkPlugins"))

(if (defined? 'vnode-type)
        (deny file-write-create (vnode-type SYMLINK)))

;; Reserve a namespace for additional protected extended attributes.
(deny file-read-xattr file-write-xattr (xattr-regex #"^com\.apple\.security\.private\."))

(deny file-read* file-write* (with no-log)
       ;; FIXME: Should be removed after <rdar://problem/10463881> is fixed.
       (home-literal "/Library/Preferences/com.apple.LaunchServices.QuarantineEventsV2")
       (home-literal "/Library/Preferences/com.apple.LaunchServices.QuarantineEventsV2-journal"))

;; Deny access needed for unnecessary NSApplication initialization.
;; FIXME: This can be removed once <rdar://problem/13011633> is fixed.
(deny file-read* (with no-log)
       (subpath "/Library/InputManagers")
       (home-subpath "/Library/InputManagers"))
(deny user-preference-read (with no-log)
    (preference-domain "com.apple.speech.recognition.AppleSpeechRecognition.prefs"))
(deny mach-lookup (with no-log)
       (global-name "com.apple.coreservices.appleevents")
       (global-name "com.apple.pasteboard.1")
       (global-name "com.apple.speech.recognitionserver"))
;; Also part of unnecessary NSApplication initialization, but we can't block access to these yet, see <rdar://problem/13869765>.
(allow file-read*
       (subpath "/Library/Components")
       (subpath "/Library/Keyboard Layouts")
       (subpath "/Library/Input Methods")
       (home-subpath "/Library/Components")
       (home-subpath "/Library/Keyboard Layouts")
       (home-subpath "/Library/Input Methods"))

;; AirPlay
(allow mach-lookup
    (global-name "com.apple.coremedia.endpoint.xpc")
    (global-name "com.apple.coremedia.endpointstream.xpc")
    (global-name "com.apple.coremedia.endpointplaybacksession.xpc")
    ; "com.apple.coremedia.endpointpicker.xpc" can be removed when <rdar://problem/30081582> is resolved.
    (global-name "com.apple.coremedia.endpointpicker.xpc")
    ; <rdar://problem/35509194>
    (global-name "com.apple.coremedia.endpointremotecontrolsession.xpc")
#if __MAC_OS_X_VERSION_MIN_REQUIRED >= 101300
    (global-name "com.apple.coremedia.routediscoverer.xpc")
    (global-name "com.apple.coremedia.routingcontext.xpc")
    (global-name "com.apple.coremedia.volumecontroller.xpc")
#endif
)

;; Data Detectors
(allow file-read* (subpath "/private/var/db/datadetectors/sys"))

;; Media capture, utilities
(if (not (defined? 'sbpl-filter?))
  (define (sbpl-filter? x)
      (and (list? x)
           (eq? (car x) 'filter))))

(macro (with-filter form)
   (let* ((ps (cdr form))
          (extra-filter (car ps))
          (rules (cdr ps)))
    `(letrec
        ((collect
             (lambda (l filters non-filters)
                 (if (null? l)
                     (list filters non-filters)
                     (let* 
                         ((x (car l))
                          (rest (cdr l)))
                         (if (sbpl-filter? x)
                             (collect rest (cons x filters) non-filters)
                             (collect rest filters (cons x non-filters)))))))
         (inject-filter
             (lambda args
                 (let* ((collected (collect args '() '()))
                        (filters (car collected))
                        (non-filters (cadr collected)))
                 (if (null? filters)
                     (cons ,extra-filter non-filters)
                     (cons (require-all (apply require-any filters) ,extra-filter) non-filters)))))
         (orig-allow allow)
         (orig-deny deny)
         (wrapper
             (lambda (action)
                 (lambda args (apply action (apply inject-filter args))))))
        (set! allow (wrapper orig-allow))
        (set! deny (wrapper orig-deny))
        ,@rules
        (set! deny orig-deny)
        (set! allow orig-allow))))

(define (home-library-preferences-regex home-library-preferences-relative-regex)
    (regex (string-append "^" (regex-quote (param "HOME_LIBRARY_PREFERENCES_DIR")) home-library-preferences-relative-regex)))

(define (home-library-preferences-literal home-library-preferences-relative-literal)
    (literal (string-append (param "HOME_LIBRARY_PREFERENCES_DIR") home-library-preferences-relative-literal)))

(define (shared-preferences-read . domains)
  (for-each (lambda (domain)
              (begin
                (if (defined? `user-preference-read)
                    (allow user-preference-read (preference-domain domain)))
                ; (Temporary) backward compatibility with non-CFPreferences readers.
                (allow file-read*
                       (literal (string-append "/Library/Preferences/" domain ".plist"))
                       (home-library-preferences-literal (string-append "/" domain ".plist"))
                       (home-library-preferences-regex (string-append #"/ByHost/" (regex-quote domain) #"\..*\.plist$")))))
            domains))

;; Media capture, microphone access
(with-filter (extension "com.apple.webkit.microphone")
    (allow device-microphone))

;; Media capture, camera access
(with-filter (extension "com.apple.webkit.camera")
    (shared-preferences-read "com.apple.coremedia")
    (allow file-read* (subpath "/Library/CoreMediaIO/Plug-Ins/DAL"))
    (allow mach-lookup (extension "com.apple.app-sandbox.mach"))
    (allow mach-lookup
        (global-name "com.apple.cmio.AppleCameraAssistant")
        ;; Apple DAL assistants
        (global-name "com.apple.cmio.VDCAssistant")
        (global-name "com.apple.cmio.AVCAssistant")
        (global-name "com.apple.cmio.IIDCVideoAssistant")
        ;; QuickTimeIIDCDigitizer assistant
        (global-name "com.apple.IIDCAssistant"))
    (allow iokit-open
        ;; QuickTimeUSBVDCDigitizer
        (iokit-user-client-class "IOUSBDeviceUserClientV2")
        (iokit-user-client-class "IOUSBInterfaceUserClientV2"))
    (allow device-camera))