Vulnerabilities > CVE-2017-0412 - Time-of-check Time-of-use (TOCTOU) Race Condition vulnerability in Google Android 7.0/7.1.0/7.1.1

047910
CVSS 7.8 - HIGH
Attack vector
LOCAL
Attack complexity
LOW
Privileges required
NONE
Confidentiality impact
HIGH
Integrity impact
HIGH
Availability impact
HIGH
local
low complexity
google
CWE-367
exploit available

Summary

An elevation of privilege vulnerability in the Framework APIs could enable a local malicious application to execute arbitrary code within the context of a privileged process. This issue is rated as High because it could be used to gain local access to elevated capabilities, which are not normally accessible to a third-party application. Product: Android. Versions: 7.0, 7.1.1. Android ID: A-33039926.

Vulnerable Configurations

Part Description Count
OS
Google
3

Common Attack Pattern Enumeration and Classification (CAPEC)

  • Leveraging Race Conditions via Symbolic Links
    This attack leverages the use of symbolic links (Symlinks) in order to write to sensitive files. An attacker can create a Symlink link to a target file not otherwise accessible to her. When the privileged program tries to create a temporary file with the same name as the Symlink link, it will actually write to the target file pointed to by the attackers' Symlink link. If the attacker can insert malicious content in the temporary file she will be writing to the sensitive file by using the Symlink. The race occurs because the system checks if the temporary file exists, then creates the file. The attacker would typically create the Symlink during the interval between the check and the creation of the temporary file.
  • Leveraging Time-of-Check and Time-of-Use (TOCTOU) Race Conditions
    This attack targets a race condition occurring between the time of check (state) for a resource and the time of use of a resource. The typical example is the file access. The attacker can leverage a file access race condition by "running the race", meaning that he would modify the resource between the first time the target program accesses the file and the time the target program uses the file. During that period of time, the attacker could do something such as replace the file and cause an escalation of privilege.

Exploit-Db

  • fileexploits/android/dos/41355.txt
    idEDB-ID:41355
    last seen2018-11-30
    modified2017-02-14
    platformandroid
    port
    published2017-02-14
    reporterExploit-DB
    sourcehttps://www.exploit-db.com/download/41355
    titleGoogle Android - android.util.MemoryIntArray Ashmem Race Conditions
    typedos
  • descriptionAndroid - Inter-Process munmap due to Race Condition in ashmem. CVE-2017-13216. Dos exploit for Android platform
    fileexploits/android/dos/43464.txt
    idEDB-ID:43464
    last seen2018-01-24
    modified2018-01-08
    platformandroid
    port
    published2018-01-08
    reporterExploit-DB
    sourcehttps://www.exploit-db.com/download/43464/
    titleAndroid - Inter-Process munmap due to Race Condition in ashmem
    typedos

Seebug

bulletinFamilyexploit
descriptionThe 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).
idSSV:92902
last seen2017-11-19
modified2017-04-05
published2017-04-05
reporterRoot
titleAndroid: Ashmem race conditions in android.util.MemoryIntArray (CVE-2017-0412)