Android KeyStore Stack Buffer Overflow

Android KeyStore service

UPDATE  (Jun. 30, 2014):
The vulnerability affects Android 4.3 only. Thanks for the Android Security Team for correcting our advisory.

Nine months ago, my team came across a classic stack-based buffer overflow in the Android KeyStore service.

As always, we adhered to our responsible disclosure policy and privately reported this issue to the Android Security Team; the result is a patch that is now available in KitKat. Considering Android’s fragmented nature and the fact that this was a code-execution vulnerability, we decided to wait a bit with the public disclosure.

See the full advisory with more details here.

As an anecdote, the vulnerable source code file contains the following comment:

Though things are simple, buffers are not always larger than the maximum space they needed.

The Android KeyStore Service

Android provides a secure storage service implemented by /system/bin/keystore. In the past, this service was accessible to other applications using a UNIX socket daemon found under /dev/socket/keystore; nowadays, however, it is accessible by the Binder interface. Each Android user receives his or her own secure storage area. A Blob is encrypted with AES using a master key, which is random, and is encrypted on-disk using a key that is derived from a password (the lock screen credentials) by the PKCS5_PBKDF2_HMAC_SHA1 function.

In recent Android versions, credentials such as RSA private keys can be hardware-backed. This basically means that the keystore keys only serve as identifiers for the real keys backed by the hardware. Despite the hardware support, some credentials, such as VPN PPTP credentials, are still stored (encrypted) on-disk.

The following diagram best illustrates the operation of the KeyStore service:

KeyStore Modus Operandi

More internals of KeyStore are available on the excellent series of posts (1, 2, 3, 4, 5) by Nikolay Elenkov.

The Vulnerability

A stack buffer is created by the ‘KeyStore::getKeyForName’ method.

This function has several callers, which are accessible by external applications using the Binder interface (e.g., ‘android::KeyStoreProxy::get’). Therefore, the ‘keyName’ variable can be controllable with an arbitrary size by a malicious application. As you can see, the ‘encode_key’ routine that is called by ‘encode_key_for_uid’ can overflow the ‘filename’ buffer, since bounds checking is absent:


Exploiting this vulnerability can theoretically be done by a malicious application; however, a working exploit needs to overcome a combination of obstacles:

  1. Data Execution Prevention (DEP). This can be bypassed by Return-Oriented Programming (ROP) payloads.
  2. Address Space Layout Randomization (ASLR)
  3. Stack Canaries
  4. Encoding. Characters below 0×30 (’0′) or above 0x7e (‘~’) are encoded before being written on the buffer.

However, the Android KeyStore is respawned every time it terminates. This behavior enables a probabilistic approach; moreover, the attacker may even theoretically abuse ASLR to defeat the encoding.


Successfully exploiting this vulnerability leads to a malicious code execution under the keystore process. Such code can:

  1. Leak the device’s lock credentials. Since the master key is derived by the lock credentials, whenever the device is unlocked, ‘Android::KeyStoreProxy::password’ is called with the credentials.
  2. Leak decrypted master keys, data and hardware-backed key identifiers from the memory.
  3. Leak encrypted master keys, data and hardware-backed key identifiers from the disk for an offline attack.
  4. Interact with the hardware-backed storage and perform crypto operations (e.g., arbitrary data signing) on behalf of the user.

Vulnerable Versions

Android 4.3.

Non-vulnerable Versions

Android 4.4.

Disclosure Timeline

Jun. 23, 2014 Public disclosure.
Nov. 11, 2013 Fix confirmed by Android Security Team.
Oct. 22, 2013 Updates requested from Android Security Team.
Sept. 9, 2013 Vulnerability acknowledged by Android Security Team.
Sept. 9, 2013 Private disclosure to Android Security Team.




We would like to thank Android Security Team for the efficient way in which they handled this security vulnerability.



CREDIT: Roee Hay

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s