Skip to content
Info disclosure Write-up Advanced

CVE-2020-24427: Adobe Reader CJK Codecs Memory Disclosure Vulnerability

A memory disclosure vulnerability in Adobe Reader's handling of CJK codecs allowed an attacker to leak memory contents, including JavaScript object addresses, to bypass ASLR. The bug was triggered by passing a crafted object to stringFromStream() that returned invalid Shift JIS bytes.

HaboobSa Follow

Published on blog.haboob.sa

Written By Haboob Research Team

Haboob Research Team (@HaboobSa), on blog.haboob.sa

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. 7 min read.

Context

Adobe Acrobat and Adobe Reader expose JavaScript APIs for working with strings and streams. Two of these functions, streamFromString() and stringFromStream(), are used to encode and decode strings. stringFromStream() expects a ReadStream object, which is obtained by calling streamFromString(). That object is implemented natively in C/C++, and clients of native objects commonly expect a certain behaviour and overlook unexpected cases.

The Haboob Research Team spent part of a year looking into Adobe Acrobat and found multiple vulnerabilities of varying criticality. This write-up covers one of them: a memory disclosure in Adobe Reader 2020.009.20074, tracked as CVE-2020-24427. The bug existed while handling the CJK codecs used to decode Japanese, Chinese and Korean scripts, namely Shift JIS, Big5, GBK and UHC. It was caused by an unexpected program state during the CJK to UCS-2/UTF-16 decoding process. The team studied one execution path where the bug was leveraged to disclose memory, leak JavaScript object addresses and bypass ASLR.

Setup

The analysis targets Adobe Reader 2020.009.20074. The relevant native code lives in the EScript.api plugin, and part of the decoding path is implemented in AcroRd32.dll. The team reverse engineered the internal structures, because the exact layout of the ASText object is undocumented. No accounts, roles or special configuration are described.

Finding it

The starting question was what happens when stringFromStream() receives an object that satisfies the ReadStream interface but behaves unexpectedly, for example by returning invalid data that cannot be decoded back using Shift JIS. That is how the bug was initially discovered.

Exploitation

Step 1: Pass a hostile ReadStream to stringFromStream()

The proof of concept passes an object with a read() method to stringFromStream(). That method returns an invalid Shift JIS byte sequence beginning with the bytes 0xfc and 0x23.

After running the code, some random memory data was dumped to the debug console, which may include some recognisable strings. The output differs on different machines. Surprisingly, the bug does not trigger an access violation or crash the process, and the root cause analysis explains why.

The team notes a possible heuristic for automatically detecting such bugs: measure the entropy of the function output. Typically the output entropy will be high if the input has high entropy, so an output with low entropy could be an indication of a memory disclosure.

Step 2: Trace stringFromStream() into EScript.api

To find the root of the bug, the team traced the call of stringFromStream(), which is implemented natively in the EScript.api plugin. The decompiled pseudocode of the function decodes the hex string returned by ReadStream's read() and checks whether the encoding is a CJK encoding, among other single-byte encodings such as Windows-1256 (Arabic). It then creates an ASText object from the encoded string using ASTextFromSizedScriptText().

Step 3: Reverse the ASText layout

Because the exact layout of ASText is undocumented, the team reverse engineered it. The u_str field is a pointer to a Unicode UCS-2/UTF-16 encoded string, and mb_str stores the non-Unicode encoded string. ASTextFromSizedScriptText() initialises mb_str. The string mb_str points to is lazily converted to u_str only if needed.

ASTextFromSizedScriptText() does not validate the encoded data apart from looking for the end of the string by locating the null byte. This works fine because 0x00 maps to the same codepoint in all the supported encodings, as they are all supersets of ASCII and no multibyte codepoint uses 0x00.

Step 4: Convert the ASText to a JavaScript string

Once the ASText object is created, it is passed to create_JSValue_from_ASText(), which converts the ASText object to SpiderMonkey's string JSValue to pass it to JavaScript. ASTextGetUnicode() is implemented in AcroRd32.dll and lazily converts mb_str first to u_str if u_str is NULL, then returns the value of u_str.

Step 5: Follow the lazy conversion

The conversion happens in the function the team named convert_mb_to_unicode(), which is referenced by many functions to perform the lazy conversion. The initial call to Host2UCS() computes the size of the buffer required to perform the decoding. Then it allocates memory, calls Host2UCS() again for the actual decoding and terminates the decoded string. The function change_u_endianness() swaps the byte order of the decoded data, which matters for exploitation.

Step 6: The first Host2UCS() call fails on invalid input

The initial call to Host2UCS() computes the size of the buffer needed for decoding. First, Host2UCS() calls MultiByteToWideChar() to get the size of the buffer required for decoding with the flag MB_ERR_INVALID_CHARS set. This flag makes MultiByteToWideChar() fail if it encounters an invalid byte sequence. This call fails with the invalid input data. Next, it calls MultiByteToWideChar() again but without this flag, which means the function will successfully return to convert_mb_to_unicode().

Step 7: The second Host2UCS() call falls back to Adobe's decoder

When the first call to Host2UCS() returns, convert_mb_to_unicode() allocates the buffer and calls Host2UCS() again for the actual decoding. In this call, Host2UCS() tries to decode the data with MultiByteToWideChar() again with the flag MB_ERR_INVALID_CHARS set, and this fails as before. This time it does not call MultiByteToWideChar() again because u_str_size is not zero and the if condition is not met. This makes Adobe Reader fall back to its own decoder.

Step 8: The fallback decoder leaves u_str uninitialised

The fallback initially calls PDEncConvAcquire() to allocate a buffer for holding the context data required for decoding. Then it calls PDEncConvSetEncToUCS(), which looks up the character map for the codec. However, this call always fails and returns zero. That means the call to PDEncConvXLateString() is never reached and the function returns with u_str uninitialised.

Step 9: Character map lookup and the exception

The failing function PDEncConvSetEncToUCS() initially maps the codepage number to the name of the Adobe Reader character map in the global array CJK_to_UC2_charmaps. For example, Shift JIS maps to 90ms-RKSJ-UCS2. Once the character map name is resolved, it passes the character map name to sub_6079CCB6(), which calls PDReadCMapResource() with the character map name as an argument inside an exception handler.

PDReadCMapResource() is where the exception is triggered. It fetches a relatively large data structure stored in the current thread's local storage area. It checks for a dictionary within this structure and creates one if it does not exist. Then it checks for a STL-like vector and creates it too if it does not exist. The dictionary stores the decoder data and its entries are looked up by the character map name ASAtom atom string, 90ms-RKSJ-UCS2 in this case. The vector stores the names of the character maps as an ASAtom.

The code that follows is where the exception is triggered. It looks up the dictionary using the character map name. If the character map is not in the dictionary, it is not expected to be in the vector too, otherwise it triggers an exception. In this case the character map 90ms-RKSJ-UCS2, atom 0x1366, is not in the dictionary, so ASDictionaryFind() returns NULL. However, if the vector were dumped, it would be found there, and this is what causes the exception.

Root cause

The bug comes from an unexpected program state during the CJK to UCS-2/UTF-16 decoding process. ASTextFromSizedScriptText() does not validate the encoded data beyond locating the null byte, so an invalid Shift JIS sequence is accepted into mb_str. When the lazy conversion runs, the first Host2UCS() call fails on the invalid bytes with MB_ERR_INVALID_CHARS set, but the second call without the flag succeeds and returns a size. On the second Host2UCS() call, the invalid input makes MultiByteToWideChar() fail again, and because u_str_size is not zero the code does not retry it and falls back to Adobe's own decoder. That fallback calls PDEncConvSetEncToUCS(), which always fails and returns zero, so PDEncConvXLateString() is never reached and u_str is left uninitialised. The function returns with u_str uninitialised, and memory contents end up in the JavaScript string.

Impact

The disclosed memory can leak JavaScript object addresses, which allows ASLR to be bypassed. The proof of concept dumps random memory data to the debug console, and that data may include recognisable strings. The output differs between machines. The report describes this as one execution path where the bug was leveraged to disclose memory; it does not state a severity or CVSS score.

Fix

The report does not describe a fix or a code change. It only notes that the vendor confirmed the vulnerability and issued CVE-2020-24427.

Variants to test

Disclosure timeline

Date Event
10-8-2020 Vulnerability reported to vendor.
31-10-2020 Vendor confirms the vulnerability.
3-11-2020 Vendor issues CVE-2020-24427 for the vulnerability.

Encodings are generally hard to implement for developers, and the constant need for encoders and encodings makes them a ripe area for vulnerability research, as every format has its own encoders.

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. 13 min read.

What happened, in plain words

Adobe Reader is the free program many people use to open PDF files. Like a web browser, it can run small programs written in JavaScript that are embedded in a PDF, so a PDF can do things like fill in a form or show a calculation. To let those programs work with text, Adobe Reader provides built-in functions, and one pair of them turns text into a stream of bytes and back again.

The Haboob Research Team spent part of a year looking at Adobe Acrobat and Adobe Reader and found several bugs of different seriousness. This article is about one of them, in Adobe Reader version 2020.009.20074, which Adobe later tracked as CVE-2020-24427. A CVE is a public catalogue number given to a known security flaw so people can refer to it unambiguously.

The flaw sat in the code that handles CJK encodings. An encoding is a set of rules for turning characters into numbers (bytes) and back. CJK stands for Chinese, Japanese and Korean, and the encodings involved here are Shift JIS, Big5, GBK and UHC. When Adobe Reader converted text from one of these encodings into the internal Unicode form it uses, an unexpected situation arose, and the result was that the program handed back a piece of its own memory as if it were text. That is called a memory disclosure: the program reveals data it was never meant to show.

Why does that matter? Because memory contains addresses, which are the numbered locations where the program keeps its objects. If an attacker learns those addresses, they can defeat ASLR, a defence that deliberately randomises where things sit in memory so an attacker cannot predict them. The team showed one path where the bug leaked JavaScript object addresses and so bypassed ASLR.

Background: the two functions

Before the details, it helps to see how the two functions are normally used. streamFromString() takes a piece of text and produces a ReadStream object. stringFromStream() takes a ReadStream object and gives back the text. Think of a ReadStream as a pipe: you do not get the whole contents at once, you ask the pipe for the next chunk by calling its read() method. A method is simply a function that belongs to an object.

The important point is that the ReadStream object is not written in JavaScript. It is implemented natively, meaning in C or C++, the languages Adobe Reader itself is built from. Programmers who use such native objects usually assume they behave in a certain way, and they often forget to handle the strange cases. The team asked a simple question: what happens if stringFromStream() is given an object that looks like a ReadStream but misbehaves, for example by returning bytes that are not valid Shift JIS? That question is how the bug was found.

Proof of concept

The team wrote a small JavaScript proof of concept. A proof of concept is a minimal piece of code that demonstrates a bug exists.

Line by line:

  1. var s = { ... }; creates a plain JavaScript object and stores it in a variable called s.
  2. read: function() { ... } gives that object a method named read. This is the name stringFromStream() will call when it wants the next chunk of data.
  3. return "fc23"; makes read() hand back the hex string fc23. In hex, fc is the byte 0xfc and 23 is the byte 0x23. This pair is not a valid Shift JIS sequence, which is exactly the point.
  4. var out = stringFromStream(s, "Shift-JIS"); passes the fake stream and the encoding name to the built-in function, and stores whatever comes back in out.
  5. console.println(out); prints the result to the debug console, a text window used for testing.

After running this, some random memory data was dumped to the debug console, and it may include recognisable strings. The output differs from machine to machine, because it depends on what happened to be in memory at that moment.

Surprisingly, the bug does not cause an access violation or crash the process. An access violation is the error a program raises when it touches memory it is not allowed to touch; here no access violation occurred, and the root cause analysis later explains why. The root cause analysis later explains why.

The team also suggested a practical trick for finding bugs like this automatically: measure the entropy of the output. Entropy is a measure of how random or unpredictable data looks. Normally, if you feed in input with high entropy, the output has high entropy too. So an output with low entropy, meaning suspiciously repetitive or structured data, can be a sign that memory was disclosed.

Root cause analysis

To find the root of the bug, the team traced what happens inside stringFromStream(). That function is implemented natively in a plugin file called EScript.api. A plugin is an add-on module that the main program loads.

The decompiled function decodes the hex string returned by the ReadStream's read() and checks whether the encoding is a CJK encoding, among other single-byte encodings such as Windows-1256, which is used for Arabic. It then creates an ASText object from the encoded string using ASTextFromSizedScriptText(). An object here means a structured block of memory with named fields, created by the C++ code.

The exact layout of the ASText object is undocumented, so the team had to reverse engineer it, working out the fields by studying the code. Two fields matter:

  • u_str is a pointer to a Unicode string encoded as UCS-2 or UTF-16. A pointer is a number that says where in memory something lives. Unicode is the standard character set that covers most of the world's writing systems; UCS-2 and UTF-16 are two ways of storing it, using two bytes per character.
  • mb_str stores the original, non-Unicode encoded string, the raw bytes as they arrived.

ASTextFromSizedScriptText() initialises mb_str. The string that mb_str points to is converted to u_str lazily, meaning only when someone actually needs the Unicode version. Lazy work is a common programming habit: do it later, or not at all, to save time.

It is worth noting that ASTextFromSizedScriptText() does not validate the encoded data, apart from looking for the end of the string by locating the null byte. A null byte is the byte 0x00, and by convention it marks the end of a string in C and C++. This shortcut works fine because 0x00 maps to the same character in all the supported encodings: they are all supersets of ASCII, the basic English character set, and no multibyte character uses 0x00. So finding 0x00 really does mean the string has ended.

Once the ASText object is created, it is passed to create_JSValue_from_ASText(), which converts it into SpiderMonkey's string JSValue so it can be handed to JavaScript. SpiderMonkey is the JavaScript engine, the part of the program that runs JavaScript code, and a JSValue is the engine's internal representation of a value. ASTextGetUnicode() is implemented in AcroRd32.dll, a Windows library file that is part of Adobe Reader. It lazily converts mb_str to u_str if u_str is NULL, meaning empty or not yet set, and then returns the value of u_str.

The conversion itself happens in a function the team named convert_mb_to_unicode(), because its real name was not known. Many functions call it to perform the lazy conversion. The first call to Host2UCS() computes the size of the buffer required for the decoding. A buffer is a reserved block of memory used to hold data temporarily. Then the code allocates memory, calls Host2UCS() again for the actual decoding, and terminates the decoded string. The function change_u_endianness() swaps the byte order of the decoded data. Byte order, or endianness, is the convention for whether the most significant byte of a number comes first or last; the team notes this matters for exploitation, meaning for turning the bug into an attack.

The first Host2UCS() call

The initial call to Host2UCS() computes the size of the buffer needed for decoding. First, Host2UCS() calls MultiByteToWideChar(), a standard Windows function that converts bytes into wide characters, with the flag MB_ERR_INVALID_CHARS set. A flag is an option passed to a function to change its behaviour. This particular flag makes MultiByteToWideChar() fail if it encounters an invalid byte sequence. With the invalid input, this call fails. Next, Host2UCS() calls MultiByteToWideChar() again but without the flag, which means the function will successfully return to convert_mb_to_unicode().

The second Host2UCS() call

When the first call to Host2UCS() returns, convert_mb_to_unicode() allocates the buffer and calls Host2UCS() again for the actual decoding. In this call, Host2UCS() tries to decode the data with MultiByteToWideChar() again with the flag MB_ERR_INVALID_CHARS set, and this fails as before. This time it does not call MultiByteToWideChar() again, because u_str_size is not zero and the if condition is not met. In other words, the code remembers that a size was already worked out, so it skips the retry. This makes Adobe Reader fall back to its own decoder, a separate piece of code written by Adobe for the same job.

The fallback decoder

The fallback first calls PDEncConvAcquire() to allocate a buffer for holding the context data required for decoding. Context data is the working state a decoder needs while it runs. Then it calls PDEncConvSetEncToUCS(), which looks up the character map for the codec. A character map is a table that says which byte sequence corresponds to which character. However, this call always fails and returns zero. That means the call to PDEncConvXLateString(), the function that would actually do the translation, is never reached, and the function returns with u_str uninitialised. Uninitialised means the memory was reserved but never filled in, so it still holds whatever was there before.

The character map lookup and the exception

The failing function PDEncConvSetEncToUCS() first maps the codepage number to the name of the Adobe Reader character map in the global array CJK_to_UC2_charmaps. A codepage is a number that identifies an encoding. For example, Shift JIS maps to 90ms-RKSJ-UCS2. Once the character map name is resolved, it passes the name to sub_6079CCB6(), a function whose real name was unknown so the team used its address as a label. That function calls PDReadCMapResource() with the character map name as an argument inside an exception handler. An exception is an error signal that code can catch and handle; an exception handler is the block of code that catches it.

PDReadCMapResource() is where the exception is triggered. It fetches a relatively large data structure stored in the current thread's local storage area. A thread is one sequence of execution inside a program, and thread-local storage is memory private to that thread. It checks for a dictionary within this structure and creates one if it does not exist. A dictionary is a lookup table that maps keys to values. Then it checks for a vector, a growable list, and creates it too if it does not exist. The dictionary stores the decoder data, and its entries are looked up by the character map name as an ASAtom atom string, 90ms-RKSJ-UCS2 in this case. An atom is a short numeric identifier for a string, used so the program can compare names quickly. The vector stores the names of the character maps as atoms.

The code that follows is where the exception is triggered. It looks up the dictionary using the character map name. If the character map is not in the dictionary, it is not expected to be in the vector either; if it is, that triggers an exception. In this case the character map 90ms-RKSJ-UCS2, atom 0x1366, is not in the dictionary, so ASDictionaryFind() returns NULL. However, if the vector were dumped, it would be found there, and this is what causes the exception.

Root cause

The bug comes from an unexpected program state during the CJK to UCS-2/UTF-16 decoding process. ASTextFromSizedScriptText() does not validate the encoded data beyond locating the null byte, so an invalid Shift JIS sequence is accepted into mb_str. When the lazy conversion runs, the first Host2UCS() call fails on the invalid bytes with MB_ERR_INVALID_CHARS set, but the second call without the flag succeeds and returns a size. On the second Host2UCS() call, the invalid input makes MultiByteToWideChar() fail again, and because u_str_size is not zero the code does not retry it and falls back to Adobe's own decoder. That fallback calls PDEncConvSetEncToUCS(), which always fails and returns zero, so PDEncConvXLateString() is never reached and u_str is left uninitialised. The function returns with u_str uninitialised, and memory contents end up in the JavaScript string.

Impact

The disclosed memory can leak JavaScript object addresses, which allows ASLR to be bypassed. The proof of concept dumps random memory data to the debug console, and that data may include recognisable strings. The output differs between machines. The report describes this as one execution path where the bug was leveraged to disclose memory; it does not state a severity or CVSS score.

Fix

The report does not describe a fix or a code change. It only notes that the vendor confirmed the vulnerability and issued CVE-2020-24427.

Variants to test

Disclosure timeline

Date Event
10-8-2020 Vulnerability reported to vendor.
31-10-2020 Vendor confirms the vulnerability.
3-11-2020 Vendor issues CVE-2020-24427 for the vulnerability.

Encodings are generally hard to implement for developers, and the constant need for encoders and encodings makes them a ripe area for vulnerability research, as every format has its own encoders.

Glossary

  • Access violation: the error a program raises when it tries to touch memory it is not allowed to touch.
  • Adobe Reader: the free program used to open and display PDF files.
  • ASLR: Address Space Layout Randomisation, a defence that places things at random addresses in memory so an attacker cannot predict them.
  • ASAtom / atom: a short numeric identifier for a string, used so the program can compare names quickly.
  • ASCII: the basic character set for English letters, digits and punctuation.
  • ASText: Adobe's internal object for holding a string, with fields such as u_str and mb_str.
  • Big5, GBK, UHC, Shift JIS: encodings for Chinese, Korean and Japanese text.
  • Buffer: a reserved block of memory used to hold data temporarily.
  • Byte: a group of eight bits, the basic unit of data storage; one byte can hold a value from 0 to 255.
  • Character map: a table that says which byte sequence corresponds to which character.
  • CJK: Chinese, Japanese and Korean.
  • Codec: a piece of software that encodes or decodes a format.
  • Codepage: a number that identifies an encoding.
  • Context data: the working state a decoder needs while it runs.
  • CVE: a public catalogue number given to a known security flaw.
  • CVSS: a scoring system for rating the severity of a vulnerability.
  • Debug console: a text window used for testing and printing messages.
  • Decompile: turn compiled machine code back into readable pseudocode.
  • Dictionary: a lookup table that maps keys to values.
  • Encoding: a set of rules for turning characters into bytes and back.
  • Endianness (byte order): the convention for whether the most significant byte of a number comes first or last.
  • Entropy: a measure of how random or unpredictable data looks.
  • Exception: an error signal that code can catch and handle.
  • Exception handler: the block of code that catches an exception.
  • Flag: an option passed to a function to change its behaviour.
  • Hex string: a string of characters representing bytes, where each byte is written as two hexadecimal digits.
  • JSValue: the JavaScript engine's internal representation of a value.
  • Lazy conversion: doing work only when it is actually needed, rather than immediately.
  • Memory disclosure: a bug where a program reveals data from its memory that it was never meant to show.
  • Method: a function that belongs to an object.
  • Multibyte: an encoding where one character can take more than one byte.
  • Native code: code written in C or C++ and compiled into the program, as opposed to JavaScript.
  • Null byte: the byte 0x00, used by convention to mark the end of a string.
  • Object: a structured block of memory with named fields, or in JavaScript a collection of named values and methods.
  • Plugin: an add-on module that the main program loads.
  • Pointer: a number that says where in memory something lives.
  • Proof of concept: a minimal piece of code that demonstrates a bug exists.
  • ReadStream: an object that supplies data in chunks when its read() method is called.
  • Reverse engineering: working out how something works by studying it, often without documentation.
  • SpiderMonkey: the JavaScript engine whose string JSValue the ASText is converted to.
  • Superset: a set that contains all the elements of another set.
  • Thread: one sequence of execution inside a program.
  • Thread-local storage: memory private to a particular thread.
  • UCS-2 / UTF-16: two ways of storing Unicode text, using two bytes per character.
  • Unicode: the standard character set covering most of the world's writing systems.
  • Uninitialised: memory that was reserved but never filled in, so it still holds whatever was there before.
  • Vector: a growable list.
  • Windows-1256: a single-byte encoding used for Arabic.

More from Adobe

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Information disclosure

The lesson behind this report.

Data the application should keep private (personal data, internal details, other users' records) reaches someone it should not.