Skip to content
Memory corruption Write-up Advanced

CVE-2018-9539: Use-after-free vulnerability in privileged Android service

A use-after-free vulnerability in Android's MediaCasService allowed an attacker to race descramble operations against session replacement, causing the service to use a freed session object. On Android 9, this resulted in a crash due to a destroyed mutex, while older versions had no noticeable effect.

Tamir Zahavi-Brunner Follow

Published on blog.zimperium.com

{{media:0}}As part of our platform research in Zimperium zLabs, I have recently discovered a vulnerability in a privileged Android service called MediaCasService and reported it to Google. Google designated it as CVE-2018-9539 and patched it in the November security update (2018-11-01 patch level).

Tamir Zahavi-Brunner, on blog.zimperium.com

The full write-up lives on its author's site

It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.

Written with AI from the full original report and checked against it passage by passage; anything it could not back up was removed or toned down. The original is the record. 5 min read.

Context

MediaCasService (also known as android.hardware.cas) is a privileged Android service that lets apps descramble protected media streams. Apps talk to it through two interfaces: Cas, which manages keys (the MediaCas Java API), and Descrambler, which performs the actual descramble operation (the MediaDescrambler Java API).

Beneath the API, the real work is done by a plugin library loaded by the service. The relevant one here is the ClearKey plugin, libclearkeycasplugin.so. To descramble data, an app uses both objects: the Descrambler performs the descramble, but it must first be linked to a session that holds a key, and sessions and keys are managed through the Cas object.

Internally the ClearKey plugin keeps sessions in ClearKeySessionLibrary, essentially a hash table whose keys are session IDs and whose values are the session objects. Apps receive the session IDs and use them to refer to session objects in the service. After creating a session and attaching a key, the app links it to a Descrambler object. The Descrambler has a member called mCASSession, a reference to its session object used during descramble operations. There is no obligation to keep the session in the library: once a Descrambler is linked to a session, the app can remove that session from the library, leaving the Descrambler's mCASSession as the only reference.

References to session objects are held through strong pointers (the sp class), so each session object has a reference count and is released when that count reaches zero. References come either from the session library or from a Descrambler's mCASSession.

From the original on blog.zimperium.com

Setup

The work was platform research in Zimperium zLabs. The target is the ClearKey plugin shipped with Android 9 (the code referenced is from the android-9.0.0_r3 tree), and the proof of concept is mainly intended to run on Android 9. The researcher wrote a proof of concept that triggers the bug; its full source is on GitHub, and the details of the actions it performs are described below.

Finding it

The starting point is ClearKey's descramble method in frameworks/av/drm/mediacas/plugins/clearkey/ClearKeyCasPlugin.cpp:

From the original on blog.zimperium.com

The session object referenced by mCASSession is used in order to decrypt, but its reference count is not increased while it is being used. That means the decrypt function can run with a session object that has already been released, because its reference count was decreased to zero.

Exploitation

The bug is a use-after-free reachable through a race condition. Before running descramble, the attacker removes the reference to the session object from the session library, leaving the Descrambler's mCASSession as the only reference to the session. Then the attacker runs descramble at the same time as setting the session of the Descrambler to another session. Setting a different session releases the original session object, dropping its reference count to zero; if that happens in the middle of mCASSession->decrypt, decrypt is using a freed session object.

Step 1: Initialise Cas and Descrambler objects

The PoC first creates the Cas and Descrambler objects it will use for the rest of the run.

Step 2: Create two sessions

Using the Cas object, it creates two sessions, session1 and session2. Both are referenced from the session library at this point.

Step 3: Link session1 and drop the library reference

session1 is linked to the Descrambler object, and then the Cas object is used to remove it from the session library. session1 now has a reference only from the Descrambler object; its reference count is one.

Step 4: Race descramble against a session swap

At the same time, the PoC does two things:

  1. Runs multiple threads which perform descramble through the Descrambler object.
  2. Sets the session of the Descrambler object to session2.

The session swap releases session1 while the descramble threads may still be inside mCASSession->decrypt.

Step 5: Detect success and retry

If running descramble in one of the threads did not return, the PoC was successful and the service crashed. If not, it retries from step 2.

What the freed object is used for

In this PoC nothing is allocated in place of the released session object; the decrypt function is simply allowed to use a freed object. One of the members of the session object that decrypt uses is a mKeyLock, essentially a mutex that decrypt attempts to lock:

From the original on blog.zimperium.com

When a session object is released, the mutex of its mKeyLock is destroyed, so when the use-after-free is triggered, decrypt attempts to use an already destroyed mutex. This is where a recent change matters. Up until Android 8.1, attempting to use a destroyed mutex returned an error, which in this case was simply ignored. Since Android 9, attempting to use a destroyed mutex results in an abort, which crashes the process:

From the original on blog.zimperium.com

So while the PoC should always cause a use-after-free, only Android 9 has a way to detect whether it worked. On older versions there is no noticeable effect, which is why the PoC is mainly intended for Android 9.

Root cause

The descramble path uses the session object held in mCASSession without taking a strong reference for the duration of the operation. Because references are counted strong pointers, the object can be released by another thread while decrypt is still running: removing the session from the library leaves the Descrambler as the sole owner, and reassigning the Descrambler's session drops that last reference to zero. The decrypt code then operates on freed memory, including the destroyed mutex inside mKeyLock.

Impact

The result is a use-after-free in a privileged Android service, reachable by an app that uses the MediaCasService API. The report describes the concrete effect as a crash of the process on Android 9, where using a destroyed mutex aborts; on versions up to Android 8.1 the error is ignored and there is no noticeable effect. The report does not go beyond this to describe other consequences of the use-after-free.

Fix

Google designated the issue CVE-2018-9539 and patched it in the November security update (2018-11-01 patch level). The report does not describe the contents of the patch.

  • Look for other service methods that read an object through a member pointer (like mCASSession) without holding a strong reference for the duration of the call. A positive result is a code path where another thread can drop the last reference mid-call.
  • Look for APIs that let a caller detach an object from its owning container while another reference remains elsewhere, then reassign or destroy that remaining reference concurrently. A positive result is a reproducible crash or sanitizer report under concurrent use.
  • On Android 9 and later, target destroyed-mutex use as an oracle: if a race on a lock inside a freed object aborts the process, the use-after-free is confirmed even when nothing is reallocated in its place.
  • On older releases, where the destroyed-mutex error is ignored, treat the absence of a crash as inconclusive and look for other observable effects of the freed object being used.

Written with AI from the full original report and checked against it passage by passage; anything it could not back up was removed or toned down. The original is the record. 14 min read.

What happened, in plain words

A security researcher working in Zimperium zLabs found a bug in a part of Android that most people never see: a background service called MediaCasService. This service is used by apps that play protected video or audio, for example paid streaming content that the app is only allowed to show if it holds the right key. The bug meant that, under the right timing, the service could be made to use a piece of memory after it had already been thrown away. That kind of bug is called a use-after-free, and in this case it crashes the service on Android 9.

The researcher reported the bug to Google. Google gave it the identifier CVE-2018-9539 and fixed it in the November 2018 security update, which is the update with the 2018-11-01 patch level. The researcher also wrote a small program, a proof of concept, that triggers the bug, and published its source code.

This article walks through the whole story from the beginning: what the service does, what the bug is, how the proof of concept makes it happen, and why the bug only shows a visible effect on Android 9.

From the original on blog.zimperium.com

Some background you need first

Before the bug makes sense, a few ideas need to be clear. None of them require any security knowledge, only the kind of thing you already know from using a computer.

What a service is

On a computer, a service (sometimes called a daemon) is a program that runs in the background, without a window, and does work on behalf of other programs. On Android, many services run with high privileges, meaning they are allowed to do things that ordinary apps are not. MediaCasService is one of those privileged services. Because it is privileged, a bug inside it is more serious than a bug in an ordinary app: the attacker is reaching into code that has more power than their own app.

What an API is

An API (application programming interface) is the agreed way for one program to ask another program to do something. A simple comparison: a restaurant menu. You do not walk into the kitchen and cook; you pick an item from the menu, and the kitchen does the work. The menu is the API. It lists what you can ask for and what you have to provide, and it hides how the kitchen actually works.

MediaCasService offers an API to apps. Apps do not touch the service's internals directly; they call the API, and the service does the rest.

What descrambling is

When a video is sent to your device for a paid service, it is usually scrambled: the data is mixed up so that it looks like noise. Only a device that holds the right key can descramble it, that is, put it back into a form the player can show. The key is a secret value, and the service is the part of Android that holds keys and applies them.

What a session is

A session is a short-lived working context. Think of a hotel room: you check in, you get a room number, you use the room for a while, and then you check out and the room is cleaned and given to someone else. In MediaCasService, a session holds a key and is identified by a session ID. The app is given the ID and uses it to refer to the session later.

What a reference count is

When several parts of a program need to use the same object, the program has to know when nobody needs it any more, so it can free the memory. A common technique is a reference count: a counter that says how many places are currently holding the object. Each new holder increases the count by one; each holder that lets go decreases it by one. When the count reaches zero, the object is freed. This is like a library book with several readers: the book stays on the shelf until the last reader returns it.

In Android, this counting is done by a class called a strong pointer, abbreviated sp. Every session object is held through strong pointers, so it has a reference count, and it is released when that count reaches zero.

What a hash table is

A hash table is a way of storing things so you can find them quickly by a label. Think of a cloakroom: you hand in your coat, you get a ticket with a number, and later you hand back the number and get the same coat. The number is the key, the coat is the value. The ClearKey plugin stores its sessions in a hash table called ClearKeySessionLibrary, where the session IDs are the keys and the session objects are the values.

What a mutex is

A mutex (short for mutual exclusion) is a lock that stops two threads from using the same thing at the same time. Picture a single-stall toilet with a lock on the door: only one person can be inside at a time, and everyone else waits. In a program, a mutex makes sure that when one thread is working on a shared object, another thread waits its turn.

What a thread is

A thread is one line of execution inside a program. A program can have several threads running at once, each doing a different piece of work. This is what makes a computer feel like it is doing many things simultaneously.

What a race condition is

A race condition is a bug that only appears when two threads do things in a particular order, and the order is not guaranteed. It is like two people reaching for the same door handle at the same moment: most of the time nothing bad happens, but occasionally one pushes while the other pulls, and something breaks. Race conditions are hard to reproduce because they depend on exact timing.

What a use-after-free is

A use-after-free is a bug where a program keeps using a piece of memory after it has been freed. The memory may have been handed back to the system, or given to something else entirely. Using it is like reading a letter that has already been shredded and the paper recycled into someone else's notebook: you might get nonsense, you might get someone else's data, or the whole thing might fall over. In a privileged service, this is a serious class of bug, because an attacker who can control what ends up in that reused memory may be able to influence what the program does next.

The service and its parts

MediaCasService is also known as android.hardware.cas. Apps talk to it through two interfaces:

  • Cas, which manages keys. The public documentation is the MediaCas Java API.
  • Descrambler, which performs the actual descramble operation. The public documentation is the MediaDescrambler Java API.

Beneath those interfaces, the real work is done by a plugin: a library that the service loads. The plugin relevant here is the ClearKey plugin, whose library file is libclearkeycasplugin.so.

To descramble data, an app uses both objects. The Descrambler does the descrambling, but it must first be linked to a session that holds a key. Sessions and keys are managed through the Cas object. So the flow is: create a session with Cas, put a key in it, link it to a Descrambler, then ask the Descrambler to descramble.

Inside the ClearKey plugin, sessions live in ClearKeySessionLibrary, the hash table described above. The app receives session IDs and uses them to refer to session objects inside the service.

After creating a session and attaching a key, the app links it to a Descrambler object. The Descrambler has a member called mCASSession, which is a reference to its session object, used during descramble operations. Nothing forces the app to keep the session in the library: once a Descrambler is linked to a session, the app can remove that session from the library, and the Descrambler's mCASSession becomes the only reference to the session.

Because references are held through strong pointers, each session object has a reference count and is released when the count reaches zero. References come from either the session library or a Descrambler's mCASSession.

The vulnerability

The starting point is ClearKey's descramble method, in the file frameworks/av/drm/mediacas/plugins/clearkey/ClearKeyCasPlugin.cpp:

From the original on blog.zimperium.com

The code uses the session object referenced by mCASSession in order to decrypt, but it does not increase that object's reference count while it is being used. In plain terms: the descramble code borrows the session object without telling the reference counter that it is holding it. That means the decrypt function can run with a session object that has already been released, because its reference count was decreased to zero by someone else.

Why does that matter? Because the reference count is the only thing that decides when the object is freed. If the descramble code does not register itself as a holder, another part of the program can legitimately decide that nobody needs the object any more and free it, while the descramble code is still in the middle of using it.

How the bug is triggered

The bug is a use-after-free reachable through a race condition. The sequence is:

  1. Before running descramble, the attacker removes the reference to the session object from the session library. Now the Descrambler's mCASSession is the only reference to the session.
  2. The attacker runs descramble at the same time as setting the session of the Descrambler to another session.
  3. Setting a different session releases the original session object, dropping its reference count to zero.
  4. If that release happens in the middle of mCASSession->decrypt, then decrypt is using a freed session object.

Step 2 is the race: the descramble and the session swap have to overlap in time. Most of the time they will not overlap, which is why the proof of concept retries.

The proof of concept

The researcher wrote a proof of concept, a small program whose only job is to trigger the bug. Its full source code is on GitHub. The proof of concept is mainly intended to run on Android 9, for a reason explained further down.

Here is a high-level overview of the actions it performs, in order.

Step 1: Initialise Cas and Descrambler objects

The proof of concept first creates the Cas and Descrambler objects it will use for the rest of the run. This is the equivalent of opening the two doors it needs: one to manage keys and sessions, one to do the descrambling.

Step 2: Create two sessions

Using the Cas object, it creates two sessions, session1 and session2. Both are referenced from the session library at this point. Two sessions are needed because the trick relies on swapping one for the other.

Step 3: Link session1 and drop the library reference

session1 is linked to the Descrambler object, and then the Cas object is used to remove it from the session library. session1 now has a reference only from the Descrambler object; its reference count is one.

This step is deliberate. By removing the library reference, the researcher makes the Descrambler the sole owner of the session. That sets up the situation where a single action, the session swap, can drop the count to zero.

Step 4: Race descramble against a session swap

At the same time, the proof of concept does two things:

  1. Runs multiple threads which perform descramble through the Descrambler object.
  2. Sets the session of the Descrambler object to session2.

The session swap releases session1 while the descramble threads may still be inside mCASSession->decrypt.

Step 5: Detect success and retry

If running descramble in one of the threads did not return, the proof of concept was successful and the service crashed. If not, it retries from step 2. The retry is necessary because a race condition does not happen every time; the program keeps trying until the timing lines up.

What the freed object is used for

In this proof of concept, nothing is allocated in place of the released session object. The decrypt function is simply allowed to use a freed object.

One of the members of the session object that decrypt uses is a mKeyLock, essentially a mutex that decrypt attempts to lock:

From the original on blog.zimperium.com

When a session object is released, the mutex inside its mKeyLock is destroyed. So when the use-after-free is triggered, decrypt attempts to use an already destroyed mutex. In the toilet comparison, it is as if the lock on the door has been removed while someone is still trying to turn it.

This is where a recent change in Android matters. Up until Android 8.1, attempting to use a destroyed mutex returned an error, and in this case that error was simply ignored. Since Android 9, attempting to use a destroyed mutex results in an abort, which crashes the process:

From the original on blog.zimperium.com

So while the proof of concept should always cause a use-after-free, only Android 9 has a way to detect whether it worked. On older versions there is no noticeable effect, which is why the proof of concept is mainly intended for Android 9. In other words, the bug is present on older versions too, but the crash that makes it visible only happens from Android 9 onwards.

Root cause

The descramble path uses the session object held in mCASSession without taking a strong reference for the duration of the operation. Because references are counted strong pointers, the object can be released by another thread while decrypt is still running. Removing the session from the library leaves the Descrambler as the sole owner, and reassigning the Descrambler's session drops that last reference to zero. The decrypt code then operates on freed memory, including the destroyed mutex inside mKeyLock. The report does not describe any additional defence that would have prevented the race.

In everyday terms: the descramble code treated the session as if it were guaranteed to stay alive for the duration of the call, but nothing enforced that guarantee.

Impact

The result is memory corruption in a privileged Android service, reachable by an app that uses the MediaCasService API. The report describes the concrete effect as a crash of the process on Android 9, where using a destroyed mutex aborts. On versions up to Android 8.1 the error is ignored and there is no noticeable effect. The report does not go beyond this to describe other consequences of the use-after-free.

Fix

Google designated the issue CVE-2018-9539 and patched it in the November security update, with the 2018-11-01 patch level. The report does not describe the contents of the patch.

Timeline

  • 08.2018: vulnerability discovered.
  • 08.2018: vulnerability details and proof of concept sent to Google.
  • 11.2018: Google distributed patches.

Variants to test

  • Look for other service methods that read an object through a member pointer (like mCASSession) without holding a strong reference for the duration of the call. A positive result is a code path where another thread can drop the last reference mid-call.
  • Look for APIs that let a caller detach an object from its owning container while another reference remains elsewhere, then reassign or destroy that remaining reference concurrently. A positive result is a reproducible crash or sanitizer report under concurrent use.
  • On Android 9 and later, target destroyed-mutex use as an oracle: if a race on a lock inside a freed object aborts the process, the use-after-free is confirmed even when nothing is reallocated in its place.
  • On older releases, where the destroyed-mutex error is ignored, treat the absence of a crash as inconclusive and look for other observable effects of the freed object being used.

Glossary

  • API: the agreed way for one program to ask another program to do something, like a menu of allowed requests.
  • Cas: the interface in MediaCasService that manages keys and sessions.
  • ClearKey plugin: the library, libclearkeycasplugin.so, that does the real work underneath the MediaCasService API.
  • ClearKeySessionLibrary: the hash table inside the ClearKey plugin that stores sessions, keyed by session ID.
  • CVE: a public identifier for a known security vulnerability, here CVE-2018-9539.
  • Descramble: to turn scrambled media data back into a form a player can show, using a key.
  • Descrambler: the interface in MediaCasService that performs the descramble operation.
  • Hash table: a storage structure that finds values quickly by a label, like a cloakroom ticket.
  • Key: a secret value used to scramble or descramble media data.
  • mCASSession: the member of a Descrambler object that refers to its linked session object.
  • MediaCasService: the privileged Android service, also known as android.hardware.cas, that lets apps descramble protected media streams.
  • mKeyLock: a member of the session object that is essentially a mutex used during decrypt.
  • Mutex: a lock that stops two threads from using the same thing at the same time.
  • Patch level: the date label of a security update, here 2018-11-01.
  • Plugin: a library loaded by a service to do part of its work.
  • Privileged service: a background program that runs with more permissions than an ordinary app.
  • Proof of concept (PoC): a small program written to demonstrate that a bug can be triggered.
  • Race condition: a bug that only appears when two threads act in a particular order, which is not guaranteed.
  • Reference count: a counter of how many places currently hold an object; the object is freed when the count reaches zero.
  • Scramble: to mix up media data so that only a device holding the right key can read it.
  • Service: a program that runs in the background and does work for other programs.
  • Session: a short-lived working context that holds a key, identified by a session ID.
  • Session ID: the label an app uses to refer to a session inside the service.
  • sp (strong pointer): the Android class used to hold objects with a reference count.
  • Thread: one line of execution inside a program; several can run at once.
  • Use-after-free: a bug where a program keeps using memory after it has been freed.

More by Tamir Zahavi-Brunner

View all

4 more disclosed reports, best first.

More from Google

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Memory corruption

The lesson behind this report.

Native code reads or writes memory it should not: overflows, use-after-free, type confusion. The core of browser, kernel and parser bugs.