Home / os / winxp

Google Android android.util.MemoryIntArray Ashmem Race Conditions

Posted on 30 November -0001

<HTML><HEAD><TITLE>Google Android android.util.MemoryIntArray Ashmem Race Conditions</TITLE><META http-equiv="Content-Type" content="text/html; charset=utf-8"></HEAD><BODY>The MemoryIntArray class allows processes to share an in-memory array of integers by transferring an ashmem file descriptor. As the class implements the Parcelable interface, it can be passed within a Parcel or a Bundle and transferred via binder to remote processes. Instead of directly tracking the size of the shared memory region, the MemoryIntArray class calls the ASHMEM_GET_SIZE ioctl on the ashmem descriptor to retrieve it on demand. This opens up a variety of race conditions when using MemoryIntArray, as the size of the ashmem descriptor can be modified (via ASHMEM_SET_SIZE) so long as the descriptor itself has not yet been mapped. To illustrate this, here is a snippet from the native function called when a MemoryIntArray is first mapped in: 1. static jlong android_util_MemoryIntArray_open(JNIEnv* env, jobject clazz, jint fd, 2. jboolean owner, jboolean writable) 3. { 4. if (fd < 0) { 5. jniThrowException(env, "java/io/IOException", "bad file descriptor"); 6. return -1; 7. } 8. 9. int ashmemSize = ashmem_get_size_region(fd); 10. if (ashmemSize <= 0) { 11. jniThrowException(env, "java/io/IOException", "bad ashmem size"); 12. return -1; 13. } 14. 15. int protMode = (owner || writable) ? (PROT_READ | PROT_WRITE) : PROT_READ; 16. void* ashmemAddr = mmap(NULL, ashmemSize, protMode, MAP_SHARED, fd, 0); 17. ... 18.} If an attacker can call ASHMEM_SET_SIZE on the shared ashmem descriptor during the execution of lines 10-15, he may modify the internal size of the descriptor, causing a mismatch between the mapped-in size and the underlying size of the descriptor. As the MemoryIntArray class uses the size reported by the ashmem descriptor to perform all bounds checks (see http://androidxref.com/7.0.0_r1/xref/frameworks/base/core/java/android/util/MemoryIntArray.java#217), this allows an attacker to cause out-of-bounds accesses to the mapped in buffer via subsequent calls to the "get" and "set" methods. Additionally, MemoryIntArray uses the ashmem-reported size when unmapping the shared memory buffer, like so: 1. static void android_util_MemoryIntArray_close(JNIEnv* env, jobject clazz, jint fd, 2. jlong ashmemAddr, jboolean owner) 3. { 4. ... 5. int ashmemSize = ashmem_get_size_region(fd); 6. if (ashmemSize <= 0) { 7. jniThrowException(env, "java/io/IOException", "bad ashmem size"); 8. return; 9. } 10. int unmapResult = munmap(reinterpret_cast<void *>(ashmemAddr), ashmemSize); 11. ... 12.} This allows an attacker to trigger an inter-process munmap with a controlled size by modifying the underlying ashmem size to a size larger than the mapped in buffer's size. Doing so will cause the finalizer to call munmap with the new size, thus forcibly freeing memory directly after the buffer. After the memory is freed, the attacker can attempt to re-capture it using controlled data. I've attached a PoC which triggers this race condition and causes system_server to call munmap on a large memory region. Running it should cause system_server to crash. Note that simply modifying the size of the ashmem file descriptor is insufficient. This is due to the fact that Parcel objects keep track of the size of the ashmem descriptors passed through them using an unsigned variable (http://androidxref.com/7.0.0_r1/xref/frameworks/native/libs/binder/Parcel.cpp#216). When a descriptor object is released, the size variable is decremented according to the reported size of the descriptor. Although this variable is not used in any meaningful way, increasing the size of the ashmem descriptor between the creation and destruction of a Parcel would cause the size variable to underflow. As system_server is compiled with UBSAN, this triggers an abort (thus preventing us from using the exploit). To get around this, I've added an additional descriptor to the Parcel, whose size is appropriately reduced before increasing the size of the MemoryIntArray's descriptor (thus keeping the size variable from underflowing). ################################################################################ Attaching another version of the PoC, adjusted for Android 7.1 (since MemoryIntArray's fields have slightly changed). This version also contains a few additional tricks to make it slightly more reliable: -Waits for the ASHMEM_SET_SIZE ioctl to fail to more closely control the race by waiting for MemoryIntArray's constructor (allowing for a smaller chance of hitting the UBSAN check before the additional descriptor's size is decreased) -Uses an ugly hack to avoid constructing MemoryIntArray instances in the attacking process (modifies the marshalled class in the Parcel manually). This prevents the PoC process from crashing due to the same UBSAN checks. Note that if the race is lost (that is - if Parcel.recycle() is called before we manage to call ASHMEM_SET_SIZE on the additional descriptor), the UBSAN abort will be triggered, resulting in a SIGABRT. If that happens, just try and run the PoC again. Here is a sample crash from a successful execution of the PoC: 11-22 14:38:43.137 9328 10749 F libc : Fatal signal 11 (SIGSEGV), code 1, fault addr 0x7ffedcbfa690 in tid 10749 (RenderThread) 11-22 14:38:43.137 1183 1183 W : debuggerd: handling request: pid=9328 uid=1000 gid=1000 tid=10749 11-22 14:38:43.137 9328 9336 F libc : Fatal signal 11 (SIGSEGV), code 1, fault addr 0x7ffedca00000 in tid 9336 (FinalizerDaemon) 11-22 14:38:43.137 9328 9336 I libc : Another thread contacted debuggerd first; not contacting debuggerd. 11-22 14:38:43.151 10816 10816 F DEBUG : *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** 11-22 14:38:43.152 10816 10816 F DEBUG : Build fingerprint: 'Android/sdk_google_phone_x86_64/generic_x86_64:7.1.1/NPF10D/3354678:userdebug/test-keys' 11-22 14:38:43.152 10816 10816 F DEBUG : Revision: '0' 11-22 14:38:43.152 10816 10816 F DEBUG : ABI: 'x86_64' 11-22 14:38:43.152 10816 10816 F DEBUG : pid: 9328, tid: 10749, name: RenderThread >>> system_server <<< 11-22 14:38:43.152 10816 10816 F DEBUG : signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0x7ffedcbfa690 11-22 14:38:43.152 10816 10816 F DEBUG : rax 00007ffed6f4fce0 rbx 00007ffedcbfa680 rcx 00007ffef5955547 rdx 0000000000000004 11-22 14:38:43.152 10816 10816 F DEBUG : rsi 00007ffed7ad14c4 rdi 00007ffedcbfa680 11-22 14:38:43.152 10816 10816 F DEBUG : r8 00007ffee8801ca0 r9 0000000000000000 r10 00007ffef58eed20 r11 0000000000000206 11-22 14:38:43.152 10816 10816 F DEBUG : r12 00007ffeea5b8598 r13 00007ffedbb30a18 r14 00007ffef66fe610 r15 00007ffedef974a0 11-22 14:38:43.152 10816 10816 F DEBUG : cs 0000000000000033 ss 000000000000002b 11-22 14:38:43.152 10816 10816 F DEBUG : rip 00007ffee88efe87 rbp 00007ffed7ad1760 rsp 00007ffed7ad16d0 eflags 0000000000000202 11-22 14:38:43.156 10816 10816 F DEBUG : 11-22 14:38:43.156 10816 10816 F DEBUG : backtrace: 11-22 14:38:43.157 10816 10816 F DEBUG : #00 pc 0000000000001e87 /system/lib64/libOpenglSystemCommon.so (_ZN14HostConnection10gl2EncoderEv+7) 11-22 14:38:43.157 10816 10816 F DEBUG : #01 pc 0000000000008434 /system/lib64/egl/libGLESv2_emulation.so (glCreateProgram+36) 11-22 14:38:43.157 10816 10816 F DEBUG : #02 pc 000000000007c9ec /system/lib64/libhwui.so 11-22 14:38:43.157 10816 10816 F DEBUG : #03 pc 000000000007d58f /system/lib64/libhwui.so 11-22 14:38:43.157 10816 10816 F DEBUG : #04 pc 000000000005e36e /system/lib64/libhwui.so 11-22 14:38:43.157 10816 10816 F DEBUG : #05 pc 0000000000099ddd /system/lib64/libhwui.so 11-22 14:38:43.157 10816 10816 F DEBUG : #06 pc 00000000000a4674 /system/lib64/libhwui.so 11-22 14:38:43.157 10816 10816 F DEBUG : #07 pc 0000000000037373 /system/lib64/libhwui.so 11-22 14:38:43.157 10816 10816 F DEBUG : #08 pc 0000000000036b5d /system/lib64/libhwui.so 11-22 14:38:43.157 10816 10816 F DEBUG : #09 pc 0000000000039745 /system/lib64/libhwui.so 11-22 14:38:43.157 10816 10816 F DEBUG : #10 pc 000000000003f128 /system/lib64/libhwui.so (_ZN7android10uirenderer12renderthread12RenderThread10threadLoopEv+136) 11-22 14:38:43.157 10816 10816 F DEBUG : #11 pc 0000000000012c39 /system/lib64/libutils.so (_ZN7android6Thread11_threadLoopEPv+313) 11-22 14:38:43.157 10816 10816 F DEBUG : #12 pc 00000000000aa613 /system/lib64/libandroid_runtime.so (_ZN7android14AndroidRuntime15javaThreadShellEPv+99) 11-22 14:38:43.157 10816 10816 F DEBUG : #13 pc 00000000000897b1 /system/lib64/libc.so (_ZL15__pthread_startPv+177) 11-22 14:38:43.157 10816 10816 F DEBUG : #14 pc 0000000000029a6b /system/lib64/libc.so (__start_thread+11) 11-22 14:38:43.157 10816 10816 F DEBUG : #15 pc 000000000001cae5 /system/lib64/libc.so (__bionic_clone+53) </BODY></HTML>

 

TOP