86
SUBVERTING APPLE GRAPHICS: PRACTICAL APPROACHES TO REMOTELY GAINING ROOT Liang Chen (@chenliang0817) Qidan He (@flanker_hqd) Marco Grassi (@marcograss) Yubin Fu (@fuyubin1993)

Us 16-subverting apple-graphics_practical_approaches_to_remotely_gaining_root_chen_he_grassi_fu

Embed Size (px)

Citation preview

Page 1: Us 16-subverting apple-graphics_practical_approaches_to_remotely_gaining_root_chen_he_grassi_fu

SUBVERTINGAPPLEGRAPHICS:PRACTICALAPPROACHESTOREMOTELYGAININGROOT

LiangChen(@chenliang0817)Qidan He(@flanker_hqd)

Marco Grassi (@marcograss)Yubin Fu(@fuyubin1993)

Page 2: Us 16-subverting apple-graphics_practical_approaches_to_remotely_gaining_root_chen_he_grassi_fu

Aboutus

• Tencent KEENSecurityLab (PreviouslyknownasKeenTeam)

• 8Pwn2Ownwinnersin3years• MobilePwn2Own2013iOS,Pwn2Own2014OSX,Pwn2Own2014Flash,Pwn2Own2015Flash,Pwn2Own2015AdobeReader,Pwn2Own2016Edge,Pwn2Own2016OSX*2

• Wepwn OSXtwiceinPwn2Own2016withrootprivilegeescalation

• KeenLab withTencentPCManager(Tencent SecurityTeamSniper)won“MasterofPwn”inPwn2Own2016

Page 3: Us 16-subverting apple-graphics_practical_approaches_to_remotely_gaining_root_chen_he_grassi_fu

Agenda

• Apple GraphicsOverview

• Userland AttackSurface

• KernelAttackSurface

Page 4: Us 16-subverting apple-graphics_practical_approaches_to_remotely_gaining_root_chen_he_grassi_fu

Apple GraphicsOverview

Page 5: Us 16-subverting apple-graphics_practical_approaches_to_remotely_gaining_root_chen_he_grassi_fu

Applegraphicsarchitecture

SandboxedApp WindowServer Service

Userland

Kernelland

UserlandGraphics

IGAccelSurface IGAccelGLContext IGAccelVideoContext…

IOAcceleratorFamily2

Nvidia GraphicsImplementation

IntelGraphicsImplementation

Page 6: Us 16-subverting apple-graphics_practical_approaches_to_remotely_gaining_root_chen_he_grassi_fu

Whygraphics?

• OnOSX,storedin/System/Library/Frameworks/We-bKit.framework/Versions/A/Resources/com.apple.WebProcess.sb

• OniOS,binaryfileembedinkernel:• Sandbox_toolkit:https://github.com/sektioneins/sandbox_toolkit

• What’s in sandbox profile:• File opration• IPC• IOKit• Sharedmem• Etc.

Page 7: Us 16-subverting apple-graphics_practical_approaches_to_remotely_gaining_root_chen_he_grassi_fu

Graphiccomponentsallowedin Safari sandbox profile

• Userland:Com.apple.windowserver.active• Apple Graphics usermode daemon• Managewindow/shape/session/workspace, etc.• Running as _windowserver context

Page 8: Us 16-subverting apple-graphics_practical_approaches_to_remotely_gaining_root_chen_he_grassi_fu

Graphiccomponentsallowedin Safari sandbox profile

• Kernel• (iokit-connection"IOAccelerator")• iokit-connectionallowsthesandboxedprocesstoopenalltheuserclientunderthetargetIOService(muchlessrestrictivethaniokit-user-client-class)

UserClient Name TypeIGAccelSurface 0IGAccelGLContext 1IGAccel2DContext 2IOAccelDisplayPipeUserClient2

4

IGAccelSharedUserClient 5IGAccelDevice 6IOAccelMemoryInfoUserClient

7

IGAccelCLContext 8IGAccelCommandQueue 9IGAccelVideoContext 0x100

Page 9: Us 16-subverting apple-graphics_practical_approaches_to_remotely_gaining_root_chen_he_grassi_fu

Userland AttackSurface

Page 10: Us 16-subverting apple-graphics_practical_approaches_to_remotely_gaining_root_chen_he_grassi_fu

MIG overview

• Apple’sIPCimplementation

Page 11: Us 16-subverting apple-graphics_practical_approaches_to_remotely_gaining_root_chen_he_grassi_fu

mach_msg_header_tmsg

• msg isthekeytosendmessagetoanotherprocess

• Msgh_bits• Simplemessageif0x00xxxxxx• Complex(descriptor)messageif0x8xxxxxxx

Page 12: Us 16-subverting apple-graphics_practical_approaches_to_remotely_gaining_root_chen_he_grassi_fu

Simple message + 3 types of descriptor

• Simple message• Easy to understand

• Port descriptor• Send a port to the remote process• Similar to DuplicateHandle inWindows (can be seen in Chrome sandbox)

• OOL descriptor• Send a pointerto the remote process

• OOL Port descriptor• Send a pointercontainingan array of ports to the remote process

Page 13: Us 16-subverting apple-graphics_practical_approaches_to_remotely_gaining_root_chen_he_grassi_fu

WindowServer overview

• Two private framework:• CoreGraphics• QuartzCore

• Safari sandbox allows to open com.apple.windowserver.active service• Implemented by CoreGraphics framework• QuartzCore framework not allowed by Safari sandbox, but…

Page 14: Us 16-subverting apple-graphics_practical_approaches_to_remotely_gaining_root_chen_he_grassi_fu

CoreGraphics API

• Client side API• Starts with CGSxxxx

• Service side API• Starts with __X

Page 15: Us 16-subverting apple-graphics_practical_approaches_to_remotely_gaining_root_chen_he_grassi_fu

CoreGraphics API grouping

• Workspace• Window• Transitions• Session• Region• Surface• Notifications

• Hotkeys• Display• Cursor• Connection• CIFilter• Event Tap• Misc

Page 16: Us 16-subverting apple-graphics_practical_approaches_to_remotely_gaining_root_chen_he_grassi_fu

Thinking as a hacker

• Before OS X Lion, no apple sandbox• But there is WindowServer• From OS X Lion, apple sandbox is introduced• What we can do to WindowServer service with sandbox by easythinking?• Movemouse position– Yes, by calling _XWarpCursorPosition• Click – Yes, by calling event tap APIs like __XPostFilteredEventTapDataSync

• WindowServerwill then call IOKit IOHIDFamily to handle the event• Set hotkey – Yes, by calling _XSetHotKey

Page 17: Us 16-subverting apple-graphics_practical_approaches_to_remotely_gaining_root_chen_he_grassi_fu

Bypass sandbox

• Movemouse + click == bypass sandbox• Set hotkey == bypass sandbox• After Apple sandbox is introduced, whole windowserver.active API isallowed by safari, Apple might forget to enhancewindowserver?

Page 18: Us 16-subverting apple-graphics_practical_approaches_to_remotely_gaining_root_chen_he_grassi_fu

Reality

• You are wrong, Apple is notthatbad• Movemouse – Still allowed• Click – checked, no way from sandbox• SetHotKey – checked, no way fromsandbox

Page 19: Us 16-subverting apple-graphics_practical_approaches_to_remotely_gaining_root_chen_he_grassi_fu

How about Window related API• Why thinking about Window API• Easy to cause UAF issues (in MSWindows)

• Connection_holds_rights_on_windowcheck• Only the window creator holds thiswriter

• Some tricks to bypass this check inhistory• Many other API doesn’t have thischeck, worthwhile for furtherresearch (Fuzzing, code auditing)

Page 20: Us 16-subverting apple-graphics_practical_approaches_to_remotely_gaining_root_chen_he_grassi_fu

Why windowserver?

• Running in root? No• Running in user account? No• It is running in _windowserver• _windowserver is nothing, nothing, nothing

_windowserver 1746.60.8691040067708??Ss 三07下午69:35.83/System/Library/Frameworks/ApplicationServices.framework/Frameworks/CoreGraphics.framework/Resources/WindowServer -daemon

Page 21: Us 16-subverting apple-graphics_practical_approaches_to_remotely_gaining_root_chen_he_grassi_fu

But,WindowServer is privilegechameleon!

Page 22: Us 16-subverting apple-graphics_practical_approaches_to_remotely_gaining_root_chen_he_grassi_fu

CVE-2014-1314: Design issue

• Session related API• _XCreateSession

• Create a new login session

• Fork a new process

• By default is/System/Library/CoreServices/loginwindow.app/Contents/MacOS/loginwindow

• But user can specify the customized loginpath by sending amach message

• The forked process will be setuid to thecurrent user’s context

• Wow, we bypassed sandbox and run a sub-process under user’s context, outsidesandbox!

Page 23: Us 16-subverting apple-graphics_practical_approaches_to_remotely_gaining_root_chen_he_grassi_fu

CVE-2014-1314: the fix

• Deny allrequest from a sandboxed process to call _XCreateSession• Effective, no way to bypass• Sandbox_check everywhere,makesmetired…ItisobviousthatApplerealizeditisdangerousinCoreGraphics

Page 24: Us 16-subverting apple-graphics_practical_approaches_to_remotely_gaining_root_chen_he_grassi_fu

QuartzCore – The hidden interface

• What is QuartzCore?• Also knownas CoreAnimation• Morecomplexgraphicsoperation

• Animation• Multi-layer handling

• But… Safari sandbox doesn’t allow open com.apple.CARenderServer• Challenge? Sandbox doesn’t allow == we cannot open?

• If you think yes, you stop here• If you think no andmake it open, then you own a new territory.

• Chrome JS renderer cannot open any file, but in fact it can do operation incache folder, why?• Duplicatea handle !

Page 25: Us 16-subverting apple-graphics_practical_approaches_to_remotely_gaining_root_chen_he_grassi_fu

QuartzCore – The hidden interface

• Another way is: a port descriptor message!• Yes, that is CGSCreateLayerContext• It sends a mach_msg to WindowServer• __XCreateLayerContext handles the request inWindowServer• Open a port of com.apple.CARenderServer• Send a replymessage with a port descriptor toclient

• Yay, we got the QuartzCore - Running at aseparate and new thread in WindowServer

Sandboxed process calls CGSCreateLayerContext

WindowServer server creates QuartzCore Client Port

Send the message back with port descriptor

Sandboxed process obtains the Port

Page 26: Us 16-subverting apple-graphics_practical_approaches_to_remotely_gaining_root_chen_he_grassi_fu

QuartzCore – a new territory

• No sandbox check• Nothing…• 3 minutes code auditing, I find something…

Page 27: Us 16-subverting apple-graphics_practical_approaches_to_remotely_gaining_root_chen_he_grassi_fu

CVE-????-???? Logic issue

• In _XSetMessageFile• Can specify arbitrary file path• And append content to that file• Content cannot be controlled• No use?

Page 28: Us 16-subverting apple-graphics_practical_approaches_to_remotely_gaining_root_chen_he_grassi_fu

Chameleon – Now I want you to be root!

Page 29: Us 16-subverting apple-graphics_practical_approaches_to_remotely_gaining_root_chen_he_grassi_fu

CVE-2016-1804 :UAFinmulti-touch (Pwnie 2016Nomination)

• Misc APIinCoreGraphics:_XSetGlobalForceConfig• Introducedforforcetouchpurpose• NewlyintroducedAPIiseasiertocauseproblem

• In_mthid_unserializeGestureConfiguration itcalledCFRelease tofreetheCFData

• Afterthat,theCFData isfreedagain• Doublefree

Page 30: Us 16-subverting apple-graphics_practical_approaches_to_remotely_gaining_root_chen_he_grassi_fu

Exploitable?

• Problemstobesolved• FillinthecontrollabledatabetweentwoFREEs

• Especiallythefirst8bytesoftheCFData• Heapsprayingin64bitprocess/infoleak

• First8bytespointingtotheusercontrollabledata(vtable likeobject)• ASLR• ROP

Page 31: Us 16-subverting apple-graphics_practical_approaches_to_remotely_gaining_root_chen_he_grassi_fu

Exploitation of CVE-2016-1804:Fillinthedata

• Lookslikehard• Twofreestooclose• Nowaytofillinbetweenthetwofreesinthesamethread

• Racecondition?• AllCoreGraphics serverAPIrunsinaserverloopatasinglethread(Gatedandqueued)• Whathappenedifracefailed?(Crash?Ofcourse!Ofcourse!Areyousure)

• Giveup?(Yes,wegiveupthisvulnerabilityforquitesomedays)

Page 32: Us 16-subverting apple-graphics_practical_approaches_to_remotely_gaining_root_chen_he_grassi_fu

Aninterestingandlegacydoublefreeproblem• If this is the case

• Result is:

• time window too small, crashed in case of race failure

• If the case is like this

• No crash!• First 8 bytes of CFData unchanged• Windows LFH like

• Means we can try again and again until successful

• CoreGraphics server APIs are all processed in a single thread…• Any other way

Page 33: Us 16-subverting apple-graphics_practical_approaches_to_remotely_gaining_root_chen_he_grassi_fu

QuartzCore - Thehiddeninterface

• Yes,weneedhiddeninterface’shelp• Thatis,QuartzCore• QuartzCore serverAPIsaresingledthreadedalsobutitisaseparatethreadagainstCoreGraphics

Thread1: CoreGraphics Thread2:QuartzCore

First Free

Double Free

Allocate memory with same size

Fill

Page 34: Us 16-subverting apple-graphics_practical_approaches_to_remotely_gaining_root_chen_he_grassi_fu

Nextquestion?WhatserverAPIsyouchoosetofillindata

• APIsmustmeetthefollowingcriteria:• Createsomestructurethatsizeis0x30(sameasCFData)• Everybyteofthe0x30structurecanbecontrolled(Oratleastthefirst8byte)

• Whatkindofmessageyouchoose?• Simplemessage?Ofcoursenot,atleastthefirst8bytecannotbecontrolledfully.• Portdescriptor?Ofcoursenot.• OOLdescriptor?Yes,becauseitallowsspecifyingapointertoabufferandpasstotheremoteprocess.

Page 35: Us 16-subverting apple-graphics_practical_approaches_to_remotely_gaining_root_chen_he_grassi_fu

Badnewsoncemore• HowmanyAPIsinQuartzCore acceptsOOLdescriptor?• Onlyone…• Thatis_XRegisterClientOptions(Itaccepts3portdescriptorfollowedbyanOOLdescriptor)

Page 36: Us 16-subverting apple-graphics_practical_approaches_to_remotely_gaining_root_chen_he_grassi_fu

What’sin_XRegisterClientOptions

• AcceptaserializedPropertyList(SameconceptasListvsJSON)• WhatisCFPropertyList?

• CheckwhatApplesays• CanbeCFData,CFString,CFArray,CFDictionary,CFDate,CFBoolean,andCFNumber

• Whichonewechoose?• OfcourseCFDictionary,becausethisAPIonlyacceptsCFDictionary asvaliddata• SoCFArray alsogood

Page 37: Us 16-subverting apple-graphics_practical_approaches_to_remotely_gaining_root_chen_he_grassi_fu

Again,whatstructuretofillin

• Firstthinking• UseCFDictionary andputmanyCFData/CFString intotheCFDictionary(BecauseyoucancontrolcontentofCFData/CFString)• Badnews:CFData notgoodbecauseitselfis0x30inlength,thefirst8bytesstruct CFData itselfisnotcontrollable,butonlyitscontent.Reducethereliabilitybyhalf• Worsenews:OnlyCFMutableDataandCFMutableStringhaveseparatecontrolledbuffer. Deserialized CFxxxx are not mutable, which the controlleddata is inlined… (Except for large data, but those are not good to fill in 0x30data)

Page 38: Us 16-subverting apple-graphics_practical_approaches_to_remotely_gaining_root_chen_he_grassi_fu

Our last hope

• Rely on CFPropertyListCreateWithData• Cannot rely on CFData/CFString• What if the CFPropertyListCreateWithDatacreates some internal struct and free it• Also useful?

• Ok, let’s focus onCFPropertyListCreateWithDataimplementation• Wow, it is open sourced!

Page 39: Us 16-subverting apple-graphics_practical_approaches_to_remotely_gaining_root_chen_he_grassi_fu

What is CFPropertyListCreateWithData

• Deserialization logic• Parse serialized buffer data andtransform to basic CFxxxx structures• A complicated implementationwithrecursive functions• _CFPropertyListCreateWithData ->__CFTryParseBinaryPlist ->__CFBinaryPlistCreateObjectFiltered

• CFBinaryPlistCreateObjectFiltered• Tokenparsing

Page 40: Us 16-subverting apple-graphics_practical_approaches_to_remotely_gaining_root_chen_he_grassi_fu

Oh,Unicodesavestheworldagain!• CasekCFBinaryPlistMarkerUnicode16String

• Atempbufferisallocatedandfreedafterprocessing

Allocatethebuffer,sizeusercontrolled

Copytheusercontrolleddatatothebuffer

Freethebuffer

Page 41: Us 16-subverting apple-graphics_practical_approaches_to_remotely_gaining_root_chen_he_grassi_fu

Exploitation of CVE-2016-1804:Fillinthedata

• Wrapup:• Createthread1,triggeringthevulnerabilityagainandagain• Createthread2,sendarequestto_XRegisterClientOptions• WithaCFDictionary/CFArray full ofcontrolled UnicodeCFString

• CFStringCreateWithCharacters createsUnicode16CFString

Page 42: Us 16-subverting apple-graphics_practical_approaches_to_remotely_gaining_root_chen_he_grassi_fu

Exploitation of CVE-2016-1804:Fillinthedata

Page 43: Us 16-subverting apple-graphics_practical_approaches_to_remotely_gaining_root_chen_he_grassi_fu

Exploitation of CVE-2016-1804:Heapspray

• Asimpletest

• Runit3times

• The5th byteisrandom..• Itmeansyouneed256*4Gforreliableheapspray• Bad…

Page 44: Us 16-subverting apple-graphics_practical_approaches_to_remotely_gaining_root_chen_he_grassi_fu

Exploitation of CVE-2016-1804:Heapspray

• Anothertest

• Runit3times

• 5th bytealways0x1• Sprayingwillbeveryreliable

Page 45: Us 16-subverting apple-graphics_practical_approaches_to_remotely_gaining_root_chen_he_grassi_fu

Exploitation of CVE-2016-1804:Heapspray

• Strategy is different• Need persistent in memory• Need to allocate large block of memory (Memory is less randomized)• Both CoreGraphics API and QuartzCore API are good candidate

• Something is same• Need to pick up a OOL descriptormessage

Page 46: Us 16-subverting apple-graphics_practical_approaches_to_remotely_gaining_root_chen_he_grassi_fu

Exploitation of CVE-2016-1804:Heapspray

• CGXSetConnectionProperty is agood candidate• Get the CFDictionary object fromglobal, if not exist then create• Set the key/value pair according touser’s input• Can set the valuemany times bysendingmultiplemessages wherekeys are different

Page 47: Us 16-subverting apple-graphics_practical_approaches_to_remotely_gaining_root_chen_he_grassi_fu

Exploitation of CVE-2016-1804: ASLR / Codeexecution• ASLR is easy as it shares the same base address with Safari webkit• Code execution:• http://phrack.org/issues/66/4.html• ROP

Page 48: Us 16-subverting apple-graphics_practical_approaches_to_remotely_gaining_root_chen_he_grassi_fu

Exploitation of CVE-2016-1804: Root?

• Wait wait, we got only _windowserver context?• Really? Nono• We can setuid to current user as we get codeexecution, just similar as CVE-2016-1314• Why not setuid and setgid to 0? Crazy! Let‘stry…• Successful…• Why?

• _windowserver isprocesseuid,uid isstillroot!• Three bugs , three different privilegeobtained…So I call it Chameleon.

Page 49: Us 16-subverting apple-graphics_practical_approaches_to_remotely_gaining_root_chen_he_grassi_fu

Demo

Page 50: Us 16-subverting apple-graphics_practical_approaches_to_remotely_gaining_root_chen_he_grassi_fu

KernelAttackSurface

Page 51: Us 16-subverting apple-graphics_practical_approaches_to_remotely_gaining_root_chen_he_grassi_fu

TheIOAccelSurface Family

• IOAccelSurface familyplaysanimportantroleinApple'sGraphicsDriverSystem• HowevertheinterfacewasoriginallydesignedforWindowServer usesolelyandvulnerabilitiesareintroducedwhennormalprocessescancallintothisinterface• CVE-2016-1815– `Blitzard`ourp2obug

Page 52: Us 16-subverting apple-graphics_practical_approaches_to_remotely_gaining_root_chen_he_grassi_fu

KeyFunctions

• Set_id_mode• Thefunctionisresponsibleininitializationofthesurface.Bitwised presentationtypeflagsarespecified,buffersareallocatedandframebuffers connectedwiththissurfacearereserved.Thisinterfacemustbecalledpriortoallothersurfaceinterfacestoensurethissurfaceisvalidtobeworkedon.

• surface_control• Basicattributesforthecurrentsurfacearespecifiedviathisfunction,i.e.theflushingrectangleofcurrentsurface.

• surface_lock_options• Specifieslockoptionsthecurrentsurfacewhicharerequiredforfollowingoperations.Forexample,asurfacemustfirstbelockedbeforeit'ssubmittedforrendering.

• surface_flush• Exchangebackupandcurrentbuffer.Triplebufferingisenabledforcertainsurfaces.

Page 53: Us 16-subverting apple-graphics_practical_approaches_to_remotely_gaining_root_chen_he_grassi_fu

Basicrenderunit

• ThebasicrepresentingregionunitinIOAccelerator subsystemisa8bytesrectanglestructurewithfieldsspecifiedinsurface_controlfunction.• int16x;• int16y;• int16w;• int16h;

Page 54: Us 16-subverting apple-graphics_practical_approaches_to_remotely_gaining_root_chen_he_grassi_fu

TypicalGraphicsPipeline

Page 55: Us 16-subverting apple-graphics_practical_approaches_to_remotely_gaining_root_chen_he_grassi_fu

set_scale andsubmit_swap

• Thesurface’sholdingdrawingregioncanbescaledandcombinedwiththeoriginalrectangleregiontoformarectanglepair,rect_pair_t• Thedrawingregionspecifiedinsurface_control isrepresentedinint16• Afterscalingit’srepresentedasIEEE.754float.

• Submit_swap submitsthesurfaceforrenderingpurposeanditwillfinallycallsintoblit opertion.

Page 56: Us 16-subverting apple-graphics_practical_approaches_to_remotely_gaining_root_chen_he_grassi_fu

Blit_param_t

• Thepairandblit_param_t fromsubmit_swap willbepassedtoblit3d_submit_commands.• Thetwomostinterestingfieldsaretwoints atoffset0x14and0x34,whichisthecurrentandtarget(physical)surface’swidthandheight.

Page 57: Us 16-subverting apple-graphics_practical_approaches_to_remotely_gaining_root_chen_he_grassi_fu

Blit3d_submit_commands

• Differentincomingsurfacearecroppedandresizedandmergedtomatchthedisplaycoordinatesystemwithcalculatedscalingfactor.• AfternormalizationtwoflushingrectanglesaresubmittedtoGPUviaBlitRectList

Page 58: Us 16-subverting apple-graphics_practical_approaches_to_remotely_gaining_root_chen_he_grassi_fu

Overflowinblit3d_submit_commands

• TheOSXgraphicscoordinatesystemonlyacceptsrectanglesinrange[0,0,0x4000,0x4000]todrawonthephysicalscreen• Howeveralogicalsurfacecanholdrectangleofnegativecoordinateandlength.• representedbyasigned16bitinteger,translatestorange[-0x8000,0x7fff].

• Theblit functionneedstoscalethelogicalrectangletofititinthespecificrange.

Page 59: Us 16-subverting apple-graphics_practical_approaches_to_remotely_gaining_root_chen_he_grassi_fu

blit3d_submit_commandscheckforcurrentsurface'swidthandtargetsurface'sheight.Ifeitherofthemislargerthan0x4000,Hustonweneedtoscaletherectanglesnow.

• avectorarrayisallocatedwithsizeheight/0x4000hopingtostorethescaledoutputvalidrectangles.

• Thetargetsurface'sheightalwayscomesfromafull-screenresource,i.e.thephysicalscreenresolution.Likefornon-retinaMacbook Air,theheightwillbe900.

• Asnonmachasaresolutionoflargerthan0x4000,thevectorarray'slengthisfixedto1.

Page 60: Us 16-subverting apple-graphics_practical_approaches_to_remotely_gaining_root_chen_he_grassi_fu

RectangletransformationsonXaxis

Page 61: Us 16-subverting apple-graphics_practical_approaches_to_remotely_gaining_root_chen_he_grassi_fu

Ibelieveyouwon’twanttoreadthis…

• Decompiledblit3d_submit_commandsfunction

Page 62: Us 16-subverting apple-graphics_practical_approaches_to_remotely_gaining_root_chen_he_grassi_fu

RewrittenasIDAhex-rayscannotproperlyhandleSSEfloatingpointinstructions

Page 63: Us 16-subverting apple-graphics_practical_approaches_to_remotely_gaining_root_chen_he_grassi_fu

RewrittenasIDAhex-rayscannotproperlyhandleSSEfloatingpointinstructions

Page 64: Us 16-subverting apple-graphics_practical_approaches_to_remotely_gaining_root_chen_he_grassi_fu

OOBleadstheway

• Thecodeimplicitlyassumesthatifthewidthissmallerthan0x4000,theincomingsurface'sheightwillalsobesmallerthan0x4000,whichisthecaseforbenignclientlikeWindowServer,butnotsureforfunkyclients.• Bysupplyingasurfacewithrect2.xsettovaluelargerthan0x4000,LINE1willperformaccessatvector_array[1],whichdefinitelygoesout-of-boundwithfunctionIGVector::addcalledonthisoob location,

Page 65: Us 16-subverting apple-graphics_practical_approaches_to_remotely_gaining_root_chen_he_grassi_fu

Determinethesurfaceattributes

• Bysupplyingsize(0x4141,0x4141,0xffff,0xffff)forsurfaceandcarefullyprepareothersurfaceoptions,wehittheabovecodepathwithrectangle(16705,16705,-1,-1).• Theseargumentswillleadtoout-of-boundaccessatvec[1]• Afterpreprocessing,therectangleistransformedtoy16705,x321,height-1,len -1,triggeringoneoob write• Thenbailoutinwhileconditioninnextloop

Page 66: Us 16-subverting apple-graphics_practical_approaches_to_remotely_gaining_root_chen_he_grassi_fu

RevisittheIGVector

• struct IGVector{• int64currentSize;• int64capacity;• void*storage;

• }• Thevulnerableallocationofblit3d_submit_commandsallocationfallsatkalloc.48,whichiscrucialforournextHeapFengShui.

Page 67: Us 16-subverting apple-graphics_practical_approaches_to_remotely_gaining_root_chen_he_grassi_fu
Page 68: Us 16-subverting apple-graphics_practical_approaches_to_remotely_gaining_root_chen_he_grassi_fu

HeapFengshui inkalloc.48

• kalloc.48isazoneusedfrequentlyinKernelwithIOMachPort actingasthemostcommonlyseenobjectinthiszoneandwemustgetridofit• PreviousworkmainlycomesupwithopenServiceExtended andvm_map_copy topreparethekernelheap.• Howeverthesearenotsuitableforourexploitation

Page 69: Us 16-subverting apple-graphics_practical_approaches_to_remotely_gaining_root_chen_he_grassi_fu

HeapFengshui inkalloc.48(cont.)

• ool_msg sprayhassmallheapside-effect• butvm_map_copy’s head0x18bytesisnotcontrollablewhileweneedcontrolof8bytesatthehead0x8position

• io_open_service_extended hasmassivesideeffectinkalloc.48zonebyproducinganIOMachPort ineveryopenedsprayingconnection• io_open_service_extendedhasthelimitationofsprayingatmost37items,constrainedbythemaximumpropertiescountperIOServiceConnection canhold• Themoreitemswecanfill,thelesssideeffectwewillneedtoconsider

Page 70: Us 16-subverting apple-graphics_practical_approaches_to_remotely_gaining_root_chen_he_grassi_fu

IOCatalogueSendData

• TheaddDrivers functionsacceptsanOSArray withthefollowingeasy-to-meetconditions:• OSArray containsanOSDict• OSDict haskeyIOProviderClass• incomingOSDictmustnotbeexactlysameasanyotherpre-existsOSDict inCatalogue

Page 71: Us 16-subverting apple-graphics_practical_approaches_to_remotely_gaining_root_chen_he_grassi_fu

IOCatalogueSendData (cont.)

• prepareoursprayedcontentinthearraypartastheXMLshows,andslightlychangesonecharatendofperspraytosatisfycondition3• Weonlyneedcontrolof+8-+16bytesregion

Page 72: Us 16-subverting apple-graphics_practical_approaches_to_remotely_gaining_root_chen_he_grassi_fu

Finalsprayroutineinkalloc.48

• Spray0x8000combinationof1vm_map_copy and50IOCatalogueSendData contentofwhichtotallycontrollable(bothofsize0x30),pushingallocationstocontinuousstableregion• freevm_map_copys at1/3to2/3part,leavingholesinallocation• triggervulnerablefunction,vulnerableallocationwillfallinholewepreviouslyleft

Page 73: Us 16-subverting apple-graphics_practical_approaches_to_remotely_gaining_root_chen_he_grassi_fu
Page 74: Us 16-subverting apple-graphics_practical_approaches_to_remotely_gaining_root_chen_he_grassi_fu

Inanearly100%chancetheheapwilllayoutasthisfigureillustrated,whichexactlymatchwhatweexpected.Spraying50ormore0x30sizedcontrollablecontentinonerollcanreducethepossibilityofsomeotherirrelevant0x30contentsuchasIOMachPort toaccidentallybejustplacedafterfreeblockoccupiedin.

Page 75: Us 16-subverting apple-graphics_practical_approaches_to_remotely_gaining_root_chen_he_grassi_fu

KALLOC.8192 ZONE

vm_map_copy header

+0x1140

niddle(filled 0x41414141) filled with 0x41414141

+0x1288

IGAccelVideoContext

IGAccelVideoContext vm_map_copy vm_map_copy…

0xff… 623880000xff… bf800000

IntelAccelerator …

+0x1528

vm_map_copy …

0xff… bf801000

0xff… 62389000

+0x528

+0x288

0xff… bf800000

vm_map_copy

0xff… bf7ff000

+0x140

0xff… 6238a000

Page 76: Us 16-subverting apple-graphics_practical_approaches_to_remotely_gaining_root_chen_he_grassi_fu

Exploitation:nowwhat?

• Wehaveanarbitrary-write-wherebutourvaluewrittenisconstrained.• Forexamplewecanusethis4byteoverwritewithvalue“0xbf800000”todoapartialoverwriteofthelesssignificant4bytesofthe“service”pointerofaIOUserClient.• Thisnewoverwrittenpointerwillbe“0xffffff80bf800000”.• Wecontrolthisheaplocationat”0xffffff80bf800000”!

A000ADDE80FFFFFF 000080BF80FFFFFF

BEFOREOOBWRITE AFTEROOBWRITE

Page 77: Us 16-subverting apple-graphics_practical_approaches_to_remotely_gaining_root_chen_he_grassi_fu

Exploitation:kASLR bypassturningthisintoainfoleak

• OnOSXthekernelisrandomized,weneedtobypasskASLR.• OurtargetIOUserclient isoftypeIGAccelVideoContext• Weoverwritethe“accelerator”fieldofthisuserclient (offset0x528),likeexplainedinthepreviousslidepointingittoourcontrolledlocation• WethenabusetheexternalmethodIGAccelVideoContext::get_hw_steppings toleak1bytetouserspace,toreadavtable 1byteatatime.• Withthevtable addresswefollowittoreadaTEXTaddress(OSObject::release)tofinallygetthekASLR slide,bypassingit.

Page 78: Us 16-subverting apple-graphics_practical_approaches_to_remotely_gaining_root_chen_he_grassi_fu

Exploitation:kASLR bypassturningthisintoainfoleak (2)

IGAccelVideoContext::get_hw_steppings( __int64 this, _DWORD *a2) {…__int64 accelerator = *(this + 0x528); // accelerator is 0xffffff80bf800000...a2[3] = *(unsigned __int8 *)(*(_QWORD *)(accelerator + 0x1230) + D0); // this is returned to userspace!…}

Page 79: Us 16-subverting apple-graphics_practical_approaches_to_remotely_gaining_root_chen_he_grassi_fu

Exploitation:1byteinfoleakmemorylayout

Page 80: Us 16-subverting apple-graphics_practical_approaches_to_remotely_gaining_root_chen_he_grassi_fu

Exploitation:rebasingandROPChain

• NowwiththekASLR slidewecandynamicallyrebaseourROPChainthatweuseforkernelcodeexecution.• AttheendoftheROPchainwewillabusekern_return_tKUNCExecute(char executionPath[1024], int uid, int gid) tospawnaarbitraryexecutableasrootinuserspace,bypassingallthemitigations (SMEP/SMAP,SIP)• SpawnarootOSXCalculatorforteh lulz!MicrosoftWindowscalculatorssucks:D

Page 81: Us 16-subverting apple-graphics_practical_approaches_to_remotely_gaining_root_chen_he_grassi_fu

Exploitation:gainingRIPcontrol

• ThelastmissingpieceofthepuzzleistogetRIPcontrolandexecuteourROPpayloadinkernelandgainkernelcodexec• WewillagainabuseaIGAccelVideoContext andhissuperclassIOAccelContext2.• Ifyourecallfromthepreviousslides,wecorruptedapointeratoffset0x528topointtoourcontrolledlocation.• Wechoosethentotargetanothermethod,named“context_finish”,whichwillmakeavirtualfunctioncallthatwecantotallycontrol.• RIPControlisachievedandwestartexecute

Page 82: Us 16-subverting apple-graphics_practical_approaches_to_remotely_gaining_root_chen_he_grassi_fu

Exploitation:gainingRIPcontrol(2)

IOAccelContext2::context_finishpush rbpmov rbp, rsp…mov rbx, rdi //thismov rax, [rbx+528h] // rax is a location with controlled content…call qword ptr [rax+180h] // RIP control…

Page 83: Us 16-subverting apple-graphics_practical_approaches_to_remotely_gaining_root_chen_he_grassi_fu

ExploitationYoucancheckmoredetailsoftheexploitationinourWhitePaperUnfortunatelyherewehavetimeandspaceconstraintsButnow..ACOOLDEMOJ!AninterestingfactisthatyoucannotpoparootCalc bysudoJIfyouseearootCalc onyoursystem,u’re doomedbykernelexploitL

Page 84: Us 16-subverting apple-graphics_practical_approaches_to_remotely_gaining_root_chen_he_grassi_fu

Demo

Page 85: Us 16-subverting apple-graphics_practical_approaches_to_remotely_gaining_root_chen_he_grassi_fu

Acknowledgements

• Wushi• Windknown• LucaTodesco• Ufotalent

Page 86: Us 16-subverting apple-graphics_practical_approaches_to_remotely_gaining_root_chen_he_grassi_fu

Thank you!