




























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

(import "system.sb")


(deny process-info*)
(allow process-info-pidinfo)
(allow process-info-setcontrol (target self))

(deny sysctl*)
(allow sysctl-read
    (sysctl-name
        "hw.availcpu"
        "hw.ncpu"
        "hw.model"
        "kern.memorystatus_level"
        "vm.footprint_suspend"))

(deny iokit-get-properties)
(allow iokit-get-properties
    (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 "AGCInfo")
    (iokit-property-regex #"^ATY,(cbits|fb_(linebytes|offset|size)|intrev)")
    (iokit-property-regex #"^Accel(Caps|NativeDMARowByteAlignment)")
    (iokit-property "ActuationSupported")
    (iokit-property "AllowDisplaySleep")
    (iokit-property "AlwaysNeedsVelocityCalculated")
    (iokit-property "Apple(DisplayType|GVAKeyDoesNotExist|IntelMEVABundleName|Sense)")
    (iokit-property "CFBundleIdentifier")
    (iokit-property "CFBundleIdentifierKernel")

    (iokit-property-regex #"^Display(ParameterHandlerUsesCharPtr|ProductID|Routing|SerialNumber|VendorID)")
    (iokit-property "Development")
    (iokit-property "DeviceEqID")
    (iokit-property "Driver is Ready")
    (iokit-property "EnableLPVP")
    (iokit-property "Endianness")
    (iokit-property "Family ID")
    (iokit-property "ForceSupported")
    (iokit-property "GPUConfigurationVariable")
    (iokit-property "GpuDebugPolicy")
    (iokit-property "HIDPointerAccelerationType")
    (iokit-property-regex #"^IOAV(.*)(De|En)code$")
    (iokit-property-regex #"^IOAccel(DisplayPipeCapabilities|Index|Revision|Types)")
    (iokit-property-regex #"^IOAudioControl(ChannelID|ID|SubType|Usage)")
    (iokit-property "IOAudioControlValue")
    (iokit-property-regex #"^IOAudioDevice(CanBeDefaults|TransportType)")
    (iokit-property-regex #"^IOAudioEngine(ChannelNames|ClientDescription|CoreAudioPlugIn|(|Device)Description|Flavor|GlobalUniqueID|OutputChannelLayout|SampleOffset|State)")
    (iokit-property-regex #"^IOAudioEngineClock(Domain|IsStable)")
    (iokit-property "IOAudioEngineDisableClockBoundsCheck")
    (iokit-property-regex #"^IOAudioEngine(Input|Output)Sample(Latency|Offset)")
    (iokit-property "IOAudioEngineIsHidden")
    (iokit-property-regex #"^IOAudioEngineNum(ActiveUserClients|SampleFramesPerBuffer)")
    (iokit-property "IOAudioSampleRate")
    (iokit-property "IOAudioStreamSampleFormatByteOrder")
    (iokit-property "IOBacklightHandlerID")
    (iokit-property "IOClassNameOverride")
    (iokit-property "IOCFPlugInTypes")
    (iokit-property "IOClass")
    (iokit-property "IOConsoleUsers")
    (iokit-property "IODVDBundleName")
    (iokit-property-regex #"^IODisplay(Attributes|CapabilityString|ConnectFlags|ControllerID|EDID|FirmwareLevel|MCCSVersion|Parameters|PrefsKey|TechnologyType|UsageTime)")
    (iokit-property-regex #"^IOFB(CLUTDefer|Config|CurrentPixelClock|CurrentPixelCount|CursorInfo|DependentID|DependentIndex|DetailedTimings|GammaCount|GammaHeaderSize|GammaWidth|I2CInterfaceIDs|I2CInterfaceInfo|Integrated|MemorySize|NeedsRefresh|ProbeOptions|ScalerInfo|TimingRange|Transform|UIScale|WaitCursorFrames|WaitCursorPeriod)")
    (iokit-property-regex #"^IOFBCurrentPixelCount(Real)")
    (iokit-property "IOFramebufferOpenGLIndex")
    (iokit-property "IOGeneralInterest")
    (iokit-property "IOGLBundleName")
    (iokit-property-regex #"^IOGVA(BGRAEnc|Codec|EncoderRestricted|Scaler|VTCapabilities)")
    (iokit-property-regex "^IOGVA[A-Z]+(De|En)code")
    (iokit-property "IOGVAH264EncodeCapabilities")

    (iokit-property "IOI2CTransactionTypes")
    (iokit-property "IOKitDebug")
    (iokit-property "IOMACAddress")

    (iokit-property "IOMatchCategory")
    (iokit-property-regex #"^IOName(Match|Matched)")
    (iokit-property "IOOCDBundleName")
    (iokit-property-regex #"^IOPCI((Class|Primary|Property|)Match|Express(Capabilities|Link(Status|Capabilities))|MSIMode|Resourced|Tunnelled)")
    (iokit-property "IOPMStrictTreeOrder")
    (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 "IOVABundleName")
    (iokit-property "IOVARendererID")
    (iokit-property "IOVARendererSubID")
    (iokit-property-regex #"^InternalStatistics(|Accm)")
    (iokit-property-regex #"^MT( Built-In|HIDDevice)")
    (iokit-property "MaintainPowerInUILock")
    (iokit-property "Max Packet Size")
    (iokit-property-regex #"^MetalPlugin(Name|ClassName)")
    (iokit-property "MetalStatisticsName")
    (iokit-property-regex #"^Multitouch (ID|Serial Number|Subdevice ID)")
    (iokit-property "NVDA,mm-version")
    (iokit-property "NVDA,NVPresentment-version")
    (iokit-property "NVDA,Enable-A2R10G10B10Format")
    (iokit-property "NXSystemInfo")
    (iokit-property "NoAutoRoute")
    (iokit-property-regex #"^PerformanceStatistics(|Accum)")
    (iokit-property "Protocol Characteristics")
    (iokit-property "SafeEjectRequested")
    (iokit-property-regex #"^Sensor (Columns|Region (Descriptor|Param)|Rows|Surface (Descriptor|Height|Width))")
    (iokit-property "SupportAudioAUUC")
    (iokit-property "SurfaceList")
    (iokit-property "TimeStampFiltering")
    (iokit-property "Transport")
    (iokit-property "VRAM,totalMB")
    (iokit-property "WANTS_FRAMES_IGNORED")
    (iokit-property "board-id")
    (iokit-property-regex #"^audio-(codec-info|device-(mvalue|nvalue))")
    (iokit-property "av-signal-type")
    (iokit-property "bcdVersion")
    (iokit-property "boot-gamma-restored")
    (iokit-property "built-in")
    (iokit-property "cail_properties")
    (iokit-property "connector-type")
    (iokit-property "device-colors")
    (iokit-property "dpm")
    (iokit-property "graphic-options")
    (iokit-property "idProduct")
    (iokit-property "idVendor")
    (iokit-property "iofb_version")
    (iokit-property-regex #"^parser-(options|type)")
    (iokit-property "port-number")
    (iokit-property "startup-timing")
    (iokit-property "touch-size-id")
)

(deny mach-lookup (xpc-service-name-prefix ""))
(allow mach-lookup
    (xpc-service-name "com.apple.ViewBridgeAuxiliary")
    (xpc-service-name "com.apple.accessibility.mediaaccessibilityd")
    (xpc-service-name "com.apple.appkit.xpc.openAndSavePanelService")
    (xpc-service-name "com.apple.appstore.PluginXPCService")

    (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.print.normalizerd")
)



(allow mach-lookup (xpc-service-name "com.apple.MTLCompilerService"))



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

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

(define (home-library-subpath home-library-relative-subpath)
    (subpath (string-append (param "HOME_LIBRARY_DIR") home-library-relative-subpath)))

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

(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-subpath home-library-preferences-relative-subpath)
    (subpath (string-append (param "HOME_LIBRARY_PREFERENCES_DIR") home-library-preferences-relative-subpath)))

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


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

(define (shared-preferences-read-write . domains)
  (for-each (lambda (domain)
              (begin
                (if (defined? `user-preference-write)
                    (allow user-preference-read user-preference-write (preference-domain domain)))


                (allow file-read* file-write*
                       (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))


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


(define (uuid-HEX-pattern-match-string)
    (HEX-pattern-match-generator '(8 0 4 0 4 0 4 0 12)))


(define *uuid-pattern* "")

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

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



(if (not (defined? 'os-version))
    (define os-version (param "_OS_VERSION")))

(system-graphics)


(shared-preferences-read
    ".GlobalPreferences"
    "com.apple.ATS"
    "com.apple.Bluetooth"
    "com.apple.CoreGraphics"
    "com.apple.HIToolbox"
    "com.apple.QuickTime"
    "com.apple.driver.AppleBluetoothMultitouch.mouse"
    "com.apple.driver.AppleBluetoothMultitouch.trackpad"
    "com.apple.driver.AppleHIDMouse"
    "com.apple.inputmethodkit"
    "com.apple.iWork.Pages"
    "com.apple.LaunchServices"
    "com.apple.MultitouchSupport"
    "com.apple.security"
    "com.apple.security.revocation"
    "com.apple.security_common"
    "com.apple.speech.voice.prefs"
    "com.apple.speech.synthesis.general.prefs"
    "com.apple.speech.recognition.AppleSpeechRecognition.prefs"
    "com.apple.systemsound"
    "com.apple.universalaccess"
    "com.apple.WebFoundation"
    "com.apple.WebKit.PluginProcess"
    "pbs"
    "com.apple.ServicesMenu.Services")


(allow file-read*
    (literal "/")
    (literal "/private/etc/hosts")
    (literal "/private/etc/protocols")
    (literal "/private/etc/services")
    (literal "/private/etc/resolv.conf")
    (literal "/private/var/run/resolv.conf")

    (subpath "/Library/Frameworks")
    (subpath "/Library/Managed Preferences")
    (subpath "/private/var/db/mds")

    (regex #"^/Library/Preferences/com\.apple\.security")
    (home-literal "/.CFUserTextEncoding")
    (home-library-subpath "/Audio")
    (home-library-subpath "/ColorPickers")
    (home-library-subpath "/ColorSync")
    (subpath "/Library/Components")
    (home-library-subpath "/Components")
    (subpath "/Library/Contextual Menu Items")
    (subpath "/Library/Input Methods")
    (home-library-subpath "/Input Methods")
    (subpath "/Library/InputManagers")
    (home-library-subpath "/InputManagers")
    (home-library-subpath "/KeyBindings")
    (subpath "/Library/Keyboard Layouts")
    (home-library-subpath "/Keyboard Layouts")
    (subpath "/Library/Fonts")
    (home-library-subpath "/Fonts")
    (subpath "/Library/Spelling")
    (home-library-subpath "/Spelling")
    (subpath "/Library/PDF Services")
    (home-library-subpath "/PDF Services")
    (home-library-preferences-literal "/QuickTime Preferences")

    (home-library-literal "/Caches/com.apple.coreaudio.components.plist")

    (subpath "/Library/Audio/Sounds")
    (subpath "/Library/Audio/Plug-Ins/Components")
    (home-library-subpath "/Audio/Plug-Ins/Components")
    (subpath "/Library/Audio/Plug-Ins/HAL")
    (subpath "/Library/CoreMediaIO/Plug-Ins/DAL")
    (subpath "/Library/QuickTime")
    (home-library-subpath "/QuickTime")
    (subpath "/Library/Video/Plug-Ins")
    (home-library-subpath "/Caches/QuickTime")

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

    (home-library-literal "/Application Support/SyncServices/Local/ClientsWithChanges/com.apple.Keychain")

    (subpath "/Library/ColorSync")

    (home-literal "/Library/Preferences/com.apple.lookup.shared.plist"))


(allow mach-lookup (global-name "com.apple.ocspd"))
(allow file-read*
       (subpath "/private/var/db/mds")
       (literal "/private/var/db/DetachedSignatures"))
(allow ipc-posix-shm-read* ipc-posix-shm-write-data
       (ipc-posix-name "com.apple.AppleDatabaseChanged"))


(allow system-fsctl (fsctl-command (_IO "h" 47)))


(allow iokit-open
    (iokit-user-client-class "IOAudioControlUserClient")
    (iokit-user-client-class "IOAudioEngineUserClient")
    (iokit-user-client-class "IOHIDParamUserClient")
    (iokit-user-client-class "RootDomainUserClient"))


(allow mach-lookup
    (global-name "com.apple.audio.AudioComponentRegistrar")
    (global-name "com.apple.assertiond.processassertionconnection")
    (global-name "com.apple.CoreServices.coreservicesd")
    (global-name "com.apple.DiskArbitration.diskarbitrationd")
    (global-name "com.apple.FileCoordination")
    (global-name "com.apple.FontObjectsServer")
    (global-name "com.apple.ImageCaptureExtension2.presence")
    (global-name "com.apple.PowerManagement.control")
    (global-name "com.apple.SecurityServer")
    (global-name "com.apple.SystemConfiguration.PPPController")
    (global-name "com.apple.SystemConfiguration.configd")
    (global-name "com.apple.UNCUserNotification")
    (global-name "com.apple.analyticsd")
    (global-name "com.apple.audio.audiohald")
    (global-name "com.apple.audio.coreaudiod")
    (global-name "com.apple.cfnetwork.AuthBrokerAgent")
    (global-name "com.apple.cmio.VDCAssistant")
    (global-name "com.apple.cookied")

    (global-name "com.apple.coreservices.launchservicesd")
    (global-name "com.apple.fonts")
    (global-name "com.apple.lsd.mapdb")
    (global-name "com.apple.ocspd")
    (global-name "com.apple.pasteboard.1")
    (global-name "com.apple.pbs.fetch_services")
    (global-name "com.apple.tccd")
    (global-name "com.apple.tccd.system")
    (global-name "com.apple.tsm.uiserver")
    (global-name "com.apple.window_proxies")
    (global-name "com.apple.windowserver.active")
    (local-name "com.apple.tsm.portname")
    (global-name-regex #"_OpenStep$"))


(allow file-read* (subpath (param "PLUGIN_PATH")))
(allow file-read* (subpath (param "WEBKIT2_FRAMEWORK_DIR")))
(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")))
(if (positive? (string-length (param "NSURL_CACHE_DIR")))
    (allow-read-write-directory-and-issue-read-write-extensions (param "NSURL_CACHE_DIR")))


(if (defined? 'mach-register)
    (allow mach-register (global-name-regex #"^_oglprof_attach_<[0-9]+>$")))

(system-network)

(allow network-outbound


    (literal "/private/var/run/mDNSResponder")
    (remote tcp)
    (remote udp))
(allow network-inbound
    (local udp))


(define (webkit-powerbox)
    (allow file-read* (literal "/Library/Preferences/com.apple.ViewBridge.plist"))
    (allow file-read* file-write* (extension "com.apple.app-sandbox.read-write"))
    (allow file-issue-extension
        (require-all
            (extension-class "com.apple.app-sandbox.read")
            (extension "com.apple.app-sandbox.read-write"))
        (require-all
            (extension-class "com.apple.app-sandbox.read-write")
            (extension "com.apple.app-sandbox.read-write"))))


(define (webkit-printing)
    (if (defined? 'authorization-right-obtain)
        (allow authorization-right-obtain
               (right-name "system.print.operator")
               (right-name "system.printingmanager")))
    (if (defined? 'mach-register)
        (deny mach-register (with no-log)
               (global-name-regex #"^com\.apple\.ICA-[0-9]+$")))
    (if (defined? 'mach-task-name)
        (allow mach-task-name))
    (allow network-outbound (literal "/private/var/run/cupsd"))
    (allow mach-lookup
        (global-name "com.apple.printuitool.agent")
        (global-name "com.apple.printtool.agent")
        (global-name "com.apple.printtool.daemon"))
    (allow file-read*
        (subpath "/Library/Printers")
        (home-literal "/.cups/lpoptions")
        (home-literal "/.cups/client.conf")
        (literal "/private/etc/cups/client.conf")
        (literal "/private/etc/cups/lpoptions")
        (subpath "/private/etc/cups/ppd")
        (literal "/private/var/run/cupsd"))
    (shared-preferences-read "org.cups.PrintingPrefs"))


(with-filter (iokit-registry-entry-class "IOHIDEventDriver")
    (allow iokit-set-properties (iokit-property "CapsLockDelayOverride")))


(define (webkit-imagecapture)
    (allow appleevent-send (appleevent-destination "com.apple.imagecaptureextension2")))


(define (webkit-camera)
    (shared-preferences-read "com.apple.coremedia")
    (allow mach-lookup (extension "com.apple.app-sandbox.mach"))
    (allow mach-lookup
        (global-name "com.apple.cmio.AppleCameraAssistant")


        (global-name "com.apple.cmio.VDCAssistant")
        (global-name "com.apple.cmio.AVCAssistant")
        (global-name "com.apple.cmio.IIDCVideoAssistant")


        (global-name "com.apple.IIDCAssistant"))
    (allow iokit-open


        (iokit-user-client-class "IOUSBDeviceUserClientV2")
        (iokit-user-client-class "IOUSBInterfaceUserClientV2"))
    (allow device-camera))


(define (webkit-microphone)
    (allow device-microphone))

(allow ipc-posix-shm*
    (ipc-posix-name-regex #"^AudioIO")
    (ipc-posix-name-regex #"^CFPBS:")
    (ipc-posix-name-regex #"^com\.apple\.cs\.")
    (ipc-posix-name-regex #"^ls\."))
(allow ipc-posix-shm-read*
    (ipc-posix-name-regex #"^/tmp/com\.apple\.csseed\.")
    (ipc-posix-name "FNetwork.defaultStorageSession")
    (ipc-posix-name "apple.shm.notification_center"))


(deny file-read* file-write* (with no-log)
    (subpath "/Network/Library")
    (subpath "/Network/Applications")
    (home-library-preferences-regex #"/com\.apple\.internetconfig(priv)?\.plist")



    (home-library-preferences-literal "/com.apple.LaunchServices.QuarantineEventsV2")
    (home-library-preferences-literal "/com.apple.LaunchServices.QuarantineEventsV2-journal"))

(deny mach-lookup (with no-log)
    (global-name "com.apple.FSEvents")
    (global-name "com.apple.coreservices.appleevents")
    (global-name "com.apple.dock.server")
    (global-name-regex #"^com\.apple\.distributed_notifications"))



(letrec
   ((original-%finalize %finalize)
    (webkit-%finalize
        (lambda ()
            (if (not (defined? 'allow-symlinks))
                (if (defined? 'vnode-type)
                    (deny file-write-create
                        (vnode-type SYMLINK))))


            (deny file-read-xattr file-write-xattr (xattr #"^com\.apple\.security\.private\."))


            (if (defined? 'xattr-regex)
                (deny file-read-xattr file-write-xattr (xattr-regex #"^com\.apple\.security\.private\."))
                (deny file-read-xattr file-write-xattr (xattr #"^com\.apple\.security\.private\.")))
            (original-%finalize))))
   (set! %finalize webkit-%finalize))
