KindleDrip — From Your Kindle’s Email Address to Using Your Credit Card
Three vulnerabilities in Amazon Kindle allowed an attacker who knew the device's email address to send a malicious e-book, execute code as a weak user when a link in the book was opened, and then escalate to root. This could let the attacker access device credentials and make purchases using the victim's credit card.
Published on medium.com
For any questions and inquiries about KindleDrip, you may contact me at [email protected].
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.
Or the story of how I received an 18K$ bug bounty for a critical Amazon Kindle vulnerability.

Context
Amazon Kindle has a feature called "Send to Kindle". It lets a Kindle user send e-books to their device as email attachments. Amazon generates each user a special kindle.com email address used specifically for this feature. Users can send an e-book as an attachment to this email address from a predefined list of approved emails, and the book will appear on their device.

The feature accepts MOBI e-books. The question that started this research was simple: what if we can send malicious e-books to unsuspecting users?
Three vulnerabilities were found which together allow an attacker to remotely execute code on the Kindle. The code runs as root, and the attacker only needs to know the email address assigned to the victim's device. The attacker could access device credentials and make purchases on the Kindle store using the victim's credit card. Attackers could sell an e-book on the store and transfer money to their account. At least the confirmation email would make the victim aware of the purchase.
The first vulnerability allowed an attacker to send an e-book to the victim's Kindle device. The second vulnerability was used to run arbitrary code while the e-book is parsed, under the context of a weak user. The third vulnerability allows the attacker to escalate privileges and run code as root.

KindleDrip attack flow
Setup
The research was done at Realmode Labs, with help from Yaar Hahn. The tools and environment used were:
- KindleTool to extract the filesystem from the firmware update file.
- An old jailbroken Kindle PaperWhite 3, in addition to a new Kindle 10.
- The gdb binary that comes with the firmware, used to debug any process.
- Ghidra to reverse-engineer the arm32 binaries.
- An email spoofing service to test the email side.
The firmware version analysed was the then newest, 5.13.2. The proof of concept was demonstrated on a new Kindle 10 with firmware version 5.13.2.
Finding it
Book Injection
A predefined list of approved addresses sounds like a control, but email authentication is still not as widespread as you may think. Since many email servers still don't support authentication, it is not unreasonable to assume that Amazon will not verify the authenticity of the sender.
To test this, an email spoofing service was used to spoof an email message and send an e-book to the researcher's own device. The e-book appeared on the device. To make matters worse, there is no indication that the e-book was received from an email message. It also appeared on the home page of the Kindle with a cover image of the attacker's choice, which makes phishing attacks much easier.
One thing that does provide security for this feature is the destination email address. The attacker must predict this email address. However, it is just the user's regular email under the kindle.com domain (e.g. [redacted] turns into [redacted]). Some users have a random string appended to their name (name_<random_string>@kindle.com), but the entropy on most of the addresses seems to be very low, and it can be bruteforced. The newest users have an address with a maximal entropy of 32 bits, which is still not very good.
Reverse-engineering the firmware
Now that there was a way to send e-books to any Kindle device, the next step was to find a way to execute code from the e-book. For this the book parsing code on firmware 5.13.2 was reverse-engineered. While looking at the various libraries present on the firmware, one library stood out: libjpegXR.so. It seemed to parse an obscure image format called jpegXR, a perfect attack surface.
JPEG XR is an image format standard originally developed by Microsoft. The Kindle supports this format when parsing the newest Kindle file format, KFX. Since only MOBI files can be sent using the previous vulnerability, it seemed irrelevant. Looking for other code that supports JPEG XR showed that the integrated web browser supports it as well. The web browser fits, since MOBI files support web links (and they are even visually identical to TOC links), so a click on a link in the book will open the browser.
Quantization parameters buffer overflow
Reading the official reference code of the JPEG XR standard revealed the following in the jxr_image struct defined in jxr_priv.h:
unsigned char dc_quant_ch[MAX_CHANNELS];unsigned char lp_quant_ch[MAX_CHANNELS][MAX_LP_QPS];unsigned char hp_quant_ch[MAX_CHANNELS][MAX_HP_QPS];
Those are called the "quantization parameters". The function _jxr_r_TILE_HEADER_HIGHPASS implemented in r_parse.c fills hp_quant_ch with data provided in the image file. The length of the copied data is defined by image->num_channels, which is also provided in the image file. The size of the buffer is constant (MAX_CHANNELS=16), so there seems to be potential for a buffer overflow.
The reference code prevents this issue with an assert that checks num_channels is less than MAX_CHANNELS. Popping libjpegXR.so in Ghidra showed that it was heavily based on the reference code, but the assert was not there. Maybe it was removed in the many edits, or simply compiled out in production. Either case, there was a buffer overflow while parsing JPEG XR in the Kindle, with controlled bytes from the JPEG XR image file.
Exploitation
Step 1: deliver the e-book
The attacker spoofs the sender address and attaches a MOBI e-book; the researcher tested this by sending to their own device. The e-book appears on the device with no indication that it arrived by email, and with a cover image of the attacker's choice. The attacker needs to know the destination kindle.com address, which is derived from the user's regular email address and can be bruteforced for many users.
Step 2: get the victim to open the browser
The MOBI file contains web links, visually identical to TOC links. When the victim opens the innocent-looking book and touches one of the links in the table of contents, the browser opens an HTML page that contains a malicious JPEG XR image.
Step 3: overflow into tile_quant
In the jxr_image struct, immediately following the overflowed buffer, there is the pointer struct jxr_tile_qp *tile_quant. The function _jxr_r_TILE_HEADER_HIGHPASS copies hp_quant_ch to this pointer:
memcpy(image->tile_quant[ty*(image->tile_columns) + tx].hp_quant_ch, image->hp_quant_ch, MAX_CHANNELS*MAX_HP_QPS);
Hence, using the overflow, tile_quant could have been overridden to obtain an absolute-write primitive (write attacker-controlled data to an attacker-controlled address). It gets better: the amount of times _jxr_r_TILE_HEADER_HIGHPASS is called is defined by image->tile_columns and image->tile_row, which are also provided in the image file. Thus, the absolute-write primitive could be used many times with a single image file.
Step 4: write and trigger shellcode in mesquite
The memory mapping of the web browser process mesquite looked like this:
00008000–000a0000 r-xp 00000000 b3:01 1939 /usr/bin/mesquite000a0000–000a2000 rwxp 00098000 b3:01 1939 /usr/bin/mesquite000a2000–002b8000 rwxp 00000000 00:00 0 [heap]…
The binary is loaded to a constant address, and it has an executable and writable section. Together with the absolute-write primitive, exploitation was easy. Using the absolute-write primitive, a shellcode could be written to the executable section. Then, the primitive could be used again to "spray" the Global Offset Table (GOT) with the address of the shellcode. The mesquite process is multi-threaded, so one of the other threads would inevitably call a function from the GOT, causing the shellcode to execute.
Step 5: escalate from framework to root
The mesquite process is run under chroot with a weak user called framework. So the previous vulnerability couldn't be used to even reboot the device. Privilege escalation was needed.
Looking for root processes that listen on a local socket:
[root@kindle root]# netstat -ntpl | tail -n +3 | awk '{ print $7 }' | awk -F / '{ print $1 }' | xargs -I {} ps -o uname=,cmd= -p {}9000 /app/bin/AaMenuApplication9000 webreaderroot stackdumpd9000 kfxreader -l9000 /usr/java/bin/cvm -Xmx49m -Xms49m ...root fastmetrics9000 kfxview9000 /usr/java/bin/cvm -Xmx49m -Xms49m ...
stackdumpd seemed promising. From what I understand, this process is responsible for generating stack dumps of crashed processes. It receives information like the crashed process id and thread id, and passes it to /usr/bin/dump-stack. This is a shell script that connects to the crashed process with gdb (so this is why it is present in the firmware!), and like the name suggests, dumps the stack. From the shell script:
${GDB} --quiet \ --se "${PROCESS_EXE}" \ --pid ${CURRENT_TID} \ --batch \ -ex "bt" \ -ex detach
Notice that CURRENT_TID was not quoted, so it may be possible to inject parameters to gdb. Since gdb can run arbitrary commands given in the command argument, it could have been used to run arbitrary code as root.
The situation was a little more complicated. It seems that the developers were aware of this issue, and tried to prevent it by making sure that CURRENT_TID is a number. The first check happens in dumpstackd and it is checking that atoi does not return 0:

The second check is in dump-stack and it uses regex:
“$(echo “$CURRENT_TID” | grep ‘^[0–9]*$’)” != “”
The atoi check can be bypassed simply by starting our string with digits. The regex check is bypassed by putting a new-line character after the digits. Hence a simple string like “1\nsome string” will bypass both checks. Thus, we had a vulnerability that allowed us to execute arbitrary code under the context of the root user. Chaining it with our previous two vulnerabilities, we got a root RCE on the Kindle.
Proof of concept
A short video demonstrated the full exploit chain on a new Kindle 10 with firmware version 5.13.2:
- The attack starts by sending a malicious e-book from the approved email address, using email spoofing.
- Then, the victim enters the innocent-looking book and touches one of the links in the table of contents.
- The link opens the browser with an HTML page that contains a malicious JPEG XR image.
- The image is parsed and malicious code now runs as root. The payload changes the boot background and restarts the device.
- The attacker receives private credentials from the device and logs in to the victim's account.
Root cause
Three separate mistakes combine into the chain.
- Book injection. The approved-sender list is only meaningful if the sender address can be authenticated. Because many email servers still do not support authentication, it is not unreasonable to assume Amazon will not verify the authenticity of the sender, and a spoofed message was accepted. The only remaining barrier is the destination address, which is derived from the user's regular email and has low entropy for most users, up to a maximum of 32 bits for the newest users.
- JPEG XR overflow.
_jxr_r_TILE_HEADER_HIGHPASScopiesimage->num_channelsentries into the fixed-sizehp_quant_chbuffer (MAX_CHANNELS=16), with both the length and the data coming from the image file. The reference implementation guards this with an assert thatnum_channelsis less thanMAX_CHANNELS; the Kindle build was based on the reference code but the assert was not present, likely removed during edits or compiled out in production. The overflow lands on the adjacenttile_quantpointer, which the same function then dereferences in amemcpy, giving an absolute write that can be repeated as many times as the attacker-chosentile_columnsandtile_rowdictate. - Privilege escalation.
/usr/bin/dump-stackinterpolatesCURRENT_TIDinto a gdb command line without quoting it. The two guards that were added, anatoinon-zero check indumpstackdand a^[0-9]*$grep in the shell script, are both defeated by a string that starts with digits and continues after a newline, such as1\nsome string.
Impact
Given only the email address assigned to the device, an attacker could execute arbitrary code on the Kindle as root. From there the attacker could access device credentials and make purchases on the Kindle store using the victim's credit card, and could sell an e-book on the store and transfer money to their own account. The chain could also have been used to jailbreak the newest Kindle devices. The attack requires the victim to open the book and touch a link in the table of contents, and the attacker must know or guess the destination kindle.com address.
Fix
Amazon released firmware version 5.13.4 on Dec 10th 2020 with fixes for the JPEG XR overflow and the privilege escalation. The fixes were reviewed at Amazon's request on Dec 29th 2020.
Book Injection
In cases where Amazon cannot authenticate the sending email address, they now send a verification link to the approved address:

JPEG XR overflow
Amazon fixed the overflow with a simple check that the number of channels does not exceed MAX_CHANNELS.
Privilege escalation
The code in dumpstackd changed to:

They replaced the atoi call with a new function that specifically checks that the string contains only digits.
Regarding /usr/bin/dump-stack, the file diff between the firmwares speaks for itself. The regex check is now more robust:
< PROCESS_PID=$1< PROCESS=$2< CURRENT_TID=$3---> PROCESS_PID=$(echo $1 | head -n 1 | grep '^[[:digit:]]*$')> PROCESS=$(echo $2 | head -n 1 | grep '^[[:alnum:]_]*$')> CURRENT_TID=$(echo $3 | head -n 1 | grep '^[[:digit:]]*$')
Variants to test
- Spoofed sender against other email-ingest features. Send a message with a spoofed From address to any service that accepts content by email from an allow-list. A positive result is the content appearing in the target account with no verification step and no indication of the delivery channel.
- Predictable per-user ingest addresses. Enumerate the address scheme (local part derived from the user's email, optional short random suffix). A positive result is a valid address found by guessing with low entropy, up to 32 bits.
- Reference-code asserts compiled out. Diff vendor binaries of parsers against their upstream reference implementations and look for missing bounds asserts around attacker-controlled length fields. A positive result is a length field from the file reaching a fixed-size buffer unchecked.
- Adjacent pointer after an overflowed buffer. In any parser struct, check what follows a fixed-size array that is filled from file data. A positive result is a pointer that is later dereferenced in a copy or write, giving a repeatable absolute write.
- Fixed-address, writable and executable mappings. Inspect process maps for binaries loaded at constant addresses with
rwxsections. A positive result is a location where shellcode can be placed and reached through a GOT entry from another thread. - Unquoted variables in root shell scripts. Look for variables interpolated into command lines of tools that accept command arguments, such as gdb. A positive result is argument injection that runs a command as root.
- Numeric validation with
atoior anchored regex. Try inputs that begin with digits and continue after a newline, such as1\nsome string. A positive result is the check passing while the rest of the string reaches the command line.
What happened, in plain words
Amazon Kindle is an e-reader: a small tablet made for reading books. Amazon gives every Kindle owner a special email address that ends in kindle.com. If you send an e-book file as an attachment to that address, the book shows up on your Kindle. The feature is called "Send to Kindle".
This report is about what happens when someone abuses that feature. The researcher found three separate mistakes in Amazon's systems. Used one after another, they let an attacker run their own code on a stranger's Kindle, with the highest level of permission on the device (called root). The attacker only needed to know the email address that belongs to the victim's Kindle. From there the attacker could read the victim's stored account credentials and buy things in the Kindle store with the victim's credit card.

The image above is the title card of the write-up: it presents the story as one where an 18,000 US dollar bug bounty was paid for a critical Kindle vulnerability.
A bug bounty is a programme where a company pays people who find and report security problems in its products. The researcher worked at Realmode Labs, with help from Yaar Hahn. Amazon fixed the problems and paid 12,000 dollars, then an extra 6,000 dollars through a holiday reward promotion, for 18,000 dollars in total.
The three mistakes, in the order they were chained:
- Book injection. Amazon's "approved sender" list for Send to Kindle could be fooled, so an attacker could make an e-book appear on someone else's Kindle.
- A bug in an image parser. The Kindle's web browser reads a rare image format called JPEG XR. A malicious image could overflow a buffer and let the attacker run code inside the browser, but only as a low-privilege user called
framework. - A privilege escalation. A background service that runs as root passed a value into a command without proper checking, so the attacker could jump from
frameworkto root.

The diagram above is captioned "KindleDrip attack flow".
Amazon released a software update that fixes the issues. Users do not need to take any action: their devices update automatically once connected to the internet.
The feature that started it
"Send to Kindle" lets a Kindle user send MOBI e-books to their device as email attachments. MOBI is an older e-book file format. Amazon generates each user a special kindle.com email address used only for this feature. Users can send an e-book as an attachment to this address from a predefined list of approved emails, and the book appears on their device.

A predefined list of approved addresses sounds like a lock on the door. The question the researcher asked was simple: what if we can send malicious e-books to unsuspecting users? If the lock can be picked, the whole feature becomes a delivery channel for attacker files.
Step 1: Book injection
The idea
Email has a field called the sender address (the "From" line). In the early design of email, nothing stopped a sender from writing any address they liked in that field. Later standards were invented to let receiving servers check that the sender really owns the address. These checks are called email authentication. The problem is that email authentication is still not as widespread as you may think. Many email servers still do not support it. So it is not unreasonable to assume that Amazon will not verify the authenticity of the sender.
If Amazon does not verify the sender, then the approved-sender list means nothing: an attacker can simply claim to be one of the approved addresses.
The test
To test this, the researcher used an email spoofing service to spoof an email message and send an e-book to their own device. Spoofing means sending a message that claims to come from an address you do not control. The e-book appeared on the device.
Two details made the result worse:
- There is no indication that the e-book was received from an email message. On the device it looks like any other book.
- It appeared on the home page of the Kindle with a cover image of the attacker's choice. A convincing cover makes phishing attacks much easier. Phishing means tricking a person into trusting something that is not genuine.
The remaining barrier: guessing the address
One thing does provide security for this feature: the destination email address. The attacker must predict the address that belongs to the victim's device. But the address is just the user's regular email under the kindle.com domain. For example, [redacted] turns into [redacted]. Some users have a random string appended to their name (name_<random_string>@kindle.com), but the entropy on most of the addresses seems to be very low, and it can be bruteforced.
Entropy, here, means the amount of randomness in the address: how many guesses an attacker needs on average. Bruteforcing means trying many possibilities automatically until one works. The newest users have an address with a maximal entropy of 32 bits, which is still not very good. 32 bits means about four billion combinations, which sounds like a lot but is well within reach of automated guessing.
So the first vulnerability is complete: an attacker who knows or guesses the victim's kindle.com address can put a file of their choosing on the victim's Kindle, with a cover of their choosing, and the victim has no way to tell it arrived by email.
Step 2: Getting inside the Kindle's software
Now that there was a way to send e-books to any Kindle device, the next step was to find a way to execute code from the e-book. Executing code means making the device run instructions the attacker wrote, rather than the instructions the manufacturer intended.
For this the researcher reverse-engineered the book parsing code on firmware 5.13.2, which was the newest version at the time. Firmware is the software built into a device. Parsing means reading a file and turning its contents into something the program can use; the parser is the part of the program that does this reading.
The tools and setup used were:
- KindleTool, to extract the filesystem from the firmware update file. The filesystem is the collection of files and programs stored on the device.
- An old jailbroken Kindle PaperWhite 3, in addition to a new Kindle 10. Jailbroken means modified so that the owner can run their own programs on it.
- The gdb binary that comes with the firmware, used to debug any process. gdb is a debugger: a tool that lets you pause a running program and inspect what it is doing.
- Ghidra, to reverse-engineer the arm32 binaries. A binary is a compiled program. arm32 is the family of processor instructions used by these devices.
- An email spoofing service, to test the email side.
While looking at the various libraries present on the firmware, one library stood out: libjpegXR.so. A library is a bundle of code that other programs can reuse. This one seemed to parse an obscure image format called jpegXR, which the researcher calls a perfect attack surface. Attack surface means all the places where an attacker can feed input into a program; the more parsers a device has, the more doors there are to try.
Why JPEG XR, and why the browser
JPEG XR is an image format standard originally developed by Microsoft. The Kindle supports this format when parsing the newest Kindle file format, KFX. Since only MOBI files can be sent using the previous vulnerability, it seemed irrelevant at first. So the researcher looked for other code that supports JPEG XR, and found that the integrated web browser supports it as well.
The web browser fits, because MOBI files support web links, and they are even visually identical to table-of-contents links. So a click on a link in the book will open the browser. That gives the attacker a path from the e-book to the browser, and the browser is where the JPEG XR parser lives.
Step 3: The JPEG XR buffer overflow
Reading the reference code
The researcher started reading the official reference code of the JPEG XR standard. Reference code is the sample implementation published alongside a standard, which device makers often copy and adapt. In the jxr_image struct defined in jxr_priv.h, the following appears:
unsigned char dc_quant_ch[MAX_CHANNELS];unsigned char lp_quant_ch[MAX_CHANNELS][MAX_LP_QPS];unsigned char hp_quant_ch[MAX_CHANNELS][MAX_HP_QPS];
Line by line, in plain words:
- A struct is a way of grouping several pieces of data together under one name, like a form with several fields.
jxr_imageis the form that describes one image. unsigned char dc_quant_ch[MAX_CHANNELS];declares an array (a fixed row of slots) nameddc_quant_ch, with room forMAX_CHANNELSvalues. Each slot holds one small number.unsigned char lp_quant_ch[MAX_CHANNELS][MAX_LP_QPS];declares a two-dimensional array: a grid ofMAX_CHANNELSrows andMAX_LP_QPScolumns.unsigned char hp_quant_ch[MAX_CHANNELS][MAX_HP_QPS];is the same idea, a grid ofMAX_CHANNELSrows andMAX_HP_QPScolumns.
These are called the "quantization parameters". They are numbers that describe how the image is compressed.
Where the overflow happens
The function _jxr_r_TILE_HEADER_HIGHPASS implemented in r_parse.c fills hp_quant_ch with data provided in the image file. A function is a named block of code that does one job. The length of the copied data is defined by image->num_channels, which is also provided in the image file. The size of the buffer is constant (MAX_CHANNELS=16), so there seems to be potential for a buffer overflow.
A buffer is a fixed-size area of memory reserved for data. A buffer overflow happens when a program writes more data into that area than it can hold. The extra data does not disappear: it spills into whatever sits next to the buffer in memory, overwriting it. That is why a buffer overflow is dangerous: the attacker chooses the spilled bytes, and those bytes land on top of other data the program relies on.
Here, the number of channels comes from the image file, and the buffer has room for 16. If the file says there are more than 16 channels, the copy writes past the end of the buffer.
The missing guard
The reference code prevents this issue with an assert that checks num_channels is less than MAX_CHANNELS. An assert is a check a program makes about itself, which stops it if the assumption is wrong. Popping libjpegXR.so in Ghidra showed that it was heavily based on the reference code, but the assert was not there. Maybe it was removed in the many edits, or simply compiled out in production. Compiling out means the build process leaves the check out of the finished program, often to save space or time.
Either way, there was a buffer overflow while parsing JPEG XR in the Kindle, with controlled bytes from the JPEG XR image file. Controlled bytes means the attacker decides exactly what the spilled data is.
Step 4: Turning the overflow into a write primitive
The neighbour in memory
In the jxr_image struct, immediately following the overflowed buffer, there is the pointer struct jxr_tile_qp *tile_quant. A pointer is a value that holds the address of something else in memory, like a note that says where to find a box rather than the box itself. The function _jxr_r_TILE_HEADER_HIGHPASS copies hp_quant_ch to this pointer:
memcpy(image->tile_quant[ty*(image->tile_columns) + tx].hp_quant_ch, image->hp_quant_ch, MAX_CHANNELS*MAX_HP_QPS);
Line by line:
memcpyis a standard function that copies a block of bytes from one place to another.- The first argument is the destination:
image->tile_quant[ty*(image->tile_columns) + tx].hp_quant_ch. The program usestile_quantas the address of a structure, picks one element of it using the row and column numbers, and writes into that element'shp_quant_chfield. - The second argument is the source:
image->hp_quant_ch, the buffer that was just filled from the image file. - The third argument is the length:
MAX_CHANNELS*MAX_HP_QPSbytes.
Because the overflow spills into tile_quant, the attacker can overwrite the pointer itself. The program then uses that overwritten pointer as the destination of the copy. In other words, the attacker chooses both the address that gets written to and the data that gets written there. This is called an absolute-write primitive: a reliable way to write attacker-controlled data to an attacker-controlled address.
It gets better: the amount of times _jxr_r_TILE_HEADER_HIGHPASS is called is defined by image->tile_columns and image->tile_row, which are also provided in the image file. So the absolute-write primitive can be used many times with a single image file. The attacker is not limited to one write; they can write as many times as they like, just by describing more tiles in the image.
Step 5: Running code in the browser
The memory map
Now, the memory mapping of the web browser process mesquite:
00008000–000a0000 r-xp 00000000 b3:01 1939 /usr/bin/mesquite000a0000–000a2000 rwxp 00098000 b3:01 1939 /usr/bin/mesquite000a2000–002b8000 rwxp 00000000 00:00 0 [heap]…
Line by line:
- Each line describes one region of memory used by the process. A process is a running program;
mesquiteis the name of the Kindle's web browser process. - The first column is the range of addresses, for example
00008000–000a0000. - The letters are permissions:
rmeans readable,wmeans writable,xmeans executable (the processor may run the bytes there as instructions), andpmeans the region is backed by a file on disk. - The first line,
r-xp, is the program's code: readable and executable, not writable. - The second line,
rwxp, is a section of the same binary that is readable, writable and executable at the same time. - The third line,
rwxpwith[heap], is the heap: the general-purpose memory area a program uses for data while it runs.
The important facts are that the binary is loaded to a constant address, and it has an executable and writable section. Constant address means the program is always placed at the same place in memory, so the attacker can hard-code addresses in the image. Executable and writable means the attacker can put bytes there and the processor will run them.
Shellcode and the GOT
Together with the absolute-write primitive, exploitation was easy:
- Using the absolute-write primitive, a shellcode could be written to the executable section. Shellcode is a small piece of attacker-written code whose only job is to start something bigger, such as a command shell.
- Then, the primitive could be used again to "spray" the Global Offset Table (GOT) with the address of the shellcode. The GOT is a table the program uses to find the addresses of functions in libraries. When the program calls a library function, it looks the address up in this table first. Spraying means filling many entries with the same value.
- The
mesquiteprocess is multi-threaded, so one of the other threads would inevitably call a function from the GOT, causing the shellcode to execute. A thread is one line of execution inside a process; a multi-threaded program has several running at once. Because many threads keep calling functions, one of them will soon look up a sprayed entry and jump to the shellcode.
At this point the attacker has code running inside the browser. But the browser is not all-powerful on the device.
Step 6: From framework to root
The mesquite process is run under chroot with a weak user called framework. Two concepts here:
- A user is an account on the device, with its own permissions.
frameworkis a low-privilege account: it can run the browser but not much else. - chroot is a way of confining a program to a section of the filesystem, so it cannot see or touch the rest.
So the previous vulnerability could not be used to even reboot the device. Privilege escalation was needed. Privilege escalation means going from a low-privilege account to a more powerful one, in this case root, the account that can do anything on the device.
Looking for root services
The researcher started looking for root processes that listen on a local socket. A socket is an endpoint a program uses to receive messages, and a local socket is one that only programs on the same device can reach. A root process listening on a socket is interesting because it accepts input from other programs, and it runs with full power.
[root@kindle root]# netstat -ntpl | tail -n +3 | awk '{ print $7 }' | awk -F / '{ print $1 }' | xargs -I {} ps -o uname=,cmd= -p {}9000 /app/bin/AaMenuApplication9000 webreaderroot stackdumpd9000 kfxreader -l9000 /usr/java/bin/cvm -Xmx49m -Xms49m ...root fastmetrics9000 kfxview9000 /usr/java/bin/cvm -Xmx49m -Xms49m ...
Line by line:
[root@kindle root]#is the shell prompt: the researcher is logged in as root on the Kindle.netstat -ntpllists network sockets. The flags ask for numeric addresses (n), TCP sockets (t), the owning process (p), and listening sockets only (l).tail -n +3drops the first two lines, which are headers.awk '{ print $7 }'keeps only the seventh column, which holds the process information.awk -F / '{ print $1 }'splits that column on the slash and keeps the first part, which is the process id.xargs -I {} ps -o uname=,cmd= -p {}runspsfor each process id, printing the user name and the command line.- The output lists services such as
AaMenuApplication,webreader,stackdumpd,kfxreader,fastmetricsandkfxview, with the user they run as.stackdumpdruns as root.
What stackdumpd does
stackdumpd seemed promising, so the researcher opened it in Ghidra to see what it does. From what I understand, this process is responsible for generating stack dumps of crashed processes. A stack dump is a snapshot of what a program was doing when it crashed, used for debugging. It receives information like the crashed process id and thread id, and passes it to /usr/bin/dump-stack. This is a shell script that connects to the crashed process with gdb (so this is why it is present in the firmware!), and like the name suggests, dumps the stack. From the shell script:
${GDB} --quiet \ --se "${PROCESS_EXE}" \ --pid ${CURRENT_TID} \ --batch \ -ex "bt" \ -ex detach
Line by line:
${GDB}is the path to the gdb program, taken from a variable.--quiettells gdb not to print its usual banner.--se "${PROCESS_EXE}"tells gdb which executable file to load. The value is in double quotes.--pid ${CURRENT_TID}tells gdb to attach to a running process by its thread id. This value is not in quotes.--batchtells gdb to run the given commands and then exit, without an interactive prompt.-ex "bt"tells gdb to run thebtcommand, which prints the backtrace (the list of function calls that led to the crash).-ex detachtells gdb to detach from the process afterwards.
Notice that CURRENT_TID was not quoted, so it may be possible to inject parameters to gdb. Parameter injection means slipping extra command-line options into a command by putting them inside a value the program trusts. Since gdb can run arbitrary commands given in the command argument, it could have been used to run arbitrary code as root.
The two guards
The situation was a little more complicated. It seems that the developers were aware of this issue, and tried to prevent it by making sure that CURRENT_TID is a number. The first check happens in dumpstackd and it is checking that atoi does not return 0:

The second check is in dump-stack and it uses regex:
“$(echo “$CURRENT_TID” | grep ‘^[0–9]*$’)” != “”
Line by line:
$( ... )runs the command inside and substitutes its output.echo "$CURRENT_TID"prints the value.| grep '^[0-9]*$'pipes that output into grep, which keeps only lines matching the pattern. The pattern^[0-9]*$means: the start of the line, then any number of digits, then the end of the line. So it matches only strings made entirely of digits.!= ""compares the result with an empty string. If grep found a match, the result is not empty and the check passes.
A regex (regular expression) is a pattern used to describe what a piece of text should look like.
Bypassing both
The atoi check can be bypassed simply by starting our string with digits. atoi reads digits from the start and stops at the first character that is not a digit, so 1 followed by anything still returns 1, which is not zero. The regex check is bypassed by putting a new-line character after the digits. grep works line by line, so it sees a first line made only of digits and matches it, while the rest of the string sits on the next line. Hence a simple string like "1\nsome string" will bypass both checks. \n is the notation for a new-line character.
Thus, we had a vulnerability that allowed us to execute arbitrary code under the context of the root user. Chaining it with our previous two vulnerabilities, we got a root RCE on the Kindle. RCE stands for remote code execution: running your own code on someone else's device over a network.
Proof of concept
A short video demonstrated the full exploit chain on a new Kindle 10 with firmware version 5.13.2:
- The attack starts by sending a malicious e-book from the approved email address, using email spoofing.
- Then, the victim enters the innocent-looking book and touches one of the links in the table of contents.
- The link opens the browser with an HTML page that contains a malicious JPEG XR image.
- The image is parsed and malicious code now runs as root. The payload changes the boot background and restarts the device.
- The attacker receives private credentials from the device and logs in to the victim's account.
A proof of concept is a demonstration that the attack really works, not just in theory. The payload is the part of the attack that does the attacker's bidding once code execution is achieved.
Root cause
Three separate mistakes combine into the chain.
- Book injection. The approved-sender list is only meaningful if the sender address can be authenticated. Because many email servers still do not support authentication, Amazon did not verify the authenticity of the sender, so a spoofed message from an approved address was accepted. The only remaining barrier is the destination address, which is derived from the user's regular email and has low entropy for most users, up to a maximum of 32 bits for the newest users.
- JPEG XR overflow.
_jxr_r_TILE_HEADER_HIGHPASScopiesimage->num_channelsentries into the fixed-sizehp_quant_chbuffer (MAX_CHANNELS=16), with both the length and the data coming from the image file. The reference implementation guards this with an assert thatnum_channelsis less thanMAX_CHANNELS; the Kindle build was based on the reference code but the assert was not present, likely removed during edits or compiled out in production. The overflow lands on the adjacenttile_quantpointer, which the same function then dereferences in amemcpy, giving an absolute write that can be repeated as many times as the attacker-chosentile_columnsandtile_rowdictate. - Privilege escalation.
/usr/bin/dump-stackinterpolatesCURRENT_TIDinto a gdb command line without quoting it. The two guards that were added, anatoinon-zero check indumpstackdand a^[0-9]*$grep in the shell script, are both defeated by a string that starts with digits and continues after a newline, such as1\nsome string.
Impact
Given only the email address assigned to the device, an attacker could execute arbitrary code on the Kindle as root. From there the attacker could access device credentials and make purchases on the Kindle store using the victim's credit card, and could sell an e-book on the store and transfer money to their own account. At least the confirmation email would make the victim aware of the purchase. The chain could also have been used to jailbreak the newest Kindle devices. The attack requires the victim to open the book and touch a link in the table of contents, and the attacker must know or guess the destination kindle.com address.
Fix
Amazon released firmware version 5.13.4 on Dec 10th 2020 with fixes for the JPEG XR overflow and the privilege escalation. The fixes were reviewed at Amazon's request on Dec 29th 2020.
Book injection
In cases where Amazon cannot authenticate the sending email address, they now send a verification link to the approved address:

JPEG XR overflow
Amazon fixed the overflow with a simple check that the number of channels does not exceed MAX_CHANNELS.
Privilege escalation
The code in dumpstackd changed to:

They replaced the atoi call with a new function that specifically checks that the string contains only digits.
Regarding /usr/bin/dump-stack, the file diff between the firmwares speaks for itself. A diff is a side-by-side comparison of two versions of a file, where lines starting with < are the old version and lines starting with > are the new one. The regex check is now more robust:
< PROCESS_PID=$1< PROCESS=$2< CURRENT_TID=$3---> PROCESS_PID=$(echo $1 | head -n 1 | grep '^[[:digit:]]*$')> PROCESS=$(echo $2 | head -n 1 | grep '^[[:alnum:]_]*$')> CURRENT_TID=$(echo $3 | head -n 1 | grep '^[[:digit:]]*$')
Line by line:
< PROCESS_PID=$1,< PROCESS=$2,< CURRENT_TID=$3are the old lines: the script simply took the first, second and third arguments and stored them in variables, with no checking.---separates the old version from the new one.> PROCESS_PID=$(echo $1 | head -n 1 | grep '^[[:digit:]]*$')is the new version: it prints the first argument, keeps only the first line withhead -n 1, and keeps it only if it is made entirely of digits.> PROCESS=$(echo $2 | head -n 1 | grep '^[[:alnum:]_]*$')does the same for the second argument, allowing only letters, digits and underscores.> CURRENT_TID=$(echo $3 | head -n 1 | grep '^[[:digit:]]*$')does the same for the third argument, allowing only digits.
The regex check is now more robust.
Variants to test
These are patterns worth trying on other products, written as things to look for rather than instructions.
- Spoofed sender against other email-ingest features. Send a message with a spoofed From address to any service that accepts content by email from an allow-list. A positive result is the content appearing in the target account with no verification step and no indication of the delivery channel.
- Predictable per-user ingest addresses. Enumerate the address scheme (local part derived from the user's email, optional short random suffix). A positive result is a valid address found by guessing with low entropy, up to 32 bits.
- Reference-code asserts compiled out. Diff vendor binaries of parsers against their upstream reference implementations and look for missing bounds asserts around attacker-controlled length fields. A positive result is a length field from the file reaching a fixed-size buffer unchecked.
- Adjacent pointer after an overflowed buffer. In any parser struct, check what follows a fixed-size array that is filled from file data. A positive result is a pointer that is later dereferenced in a copy or write, giving a repeatable absolute write.
- Fixed-address, writable and executable mappings. Inspect process maps for binaries loaded at constant addresses with
rwxsections. A positive result is a location where shellcode can be placed and reached through a GOT entry from another thread. - Unquoted variables in root shell scripts. Look for variables interpolated into command lines of tools that accept command arguments, such as gdb. A positive result is argument injection that runs a command as root.
- Numeric validation with
atoior anchored regex. Try inputs that begin with digits and continue after a newline, such as1\nsome string. A positive result is the check passing while the rest of the string reaches the command line.
Timeline
- Oct 17th 2020: report sent through the Amazon Vulnerability Research Program.
- Oct 19th 2020: Amazon acknowledges the issues.
- Nov 4th 2020: rewarded 12K$ bounty.
- Nov 11th 2020: rewarded an additional 6K$ bounty from the Amazon Vulnerability Research Program holiday reward promotion.
- Dec 10th 2020: firmware version 5.13.4 is released with fixes for the JPEG XR overflow and the privilege escalation.
- Dec 29th 2020: reviewed fixes deployed by Amazon, per their request.
- Jan 19th 2021: this publication is approved.
Glossary
- Absolute-write primitive: a reliable way to make a program write attacker-chosen data to an attacker-chosen memory address.
- Approved sender list: the set of email addresses a service accepts content from.
- Array: a fixed row of slots in memory, each holding one value.
- Assert: a check a program makes about itself, which stops it if the assumption is wrong.
- Attack surface: all the places where an attacker can feed input into a program.
- Binary: a compiled program, made of instructions the processor can run.
- Buffer: a fixed-size area of memory reserved for data.
- Buffer overflow: writing more data into a buffer than it can hold, so the extra data spills into neighbouring memory.
- Bug bounty: a programme where a company pays people who find and report security problems in its products.
- chroot: a way of confining a program to a section of the filesystem.
- Compiled out: left out of the finished program by the build process.
- Constant address: a memory address that is the same every time a program runs.
- Controlled bytes: data whose exact contents the attacker decides.
- Credentials: the information a device or person uses to prove identity, such as account tokens.
- Debugger: a tool that lets you pause a running program and inspect what it is doing.
- Diff: a side-by-side comparison of two versions of a file.
- Email authentication: standards that let a receiving mail server check that the sender really owns the address in the From line.
- Entropy: the amount of randomness in a value, which determines how hard it is to guess.
- Executable: memory the processor is allowed to run as instructions.
- Firmware: the software built into a device.
- Function: a named block of code that does one job.
- Global Offset Table (GOT): a table a program uses to find the addresses of functions in libraries.
- gdb: a debugger commonly used on Linux systems.
- Heap: the general-purpose memory area a program uses for data while it runs.
- Jailbreak: modifying a device so the owner can run their own programs on it.
- JPEG XR: an image format standard originally developed by Microsoft.
- KFX: the newest Kindle e-book file format.
- Library: a bundle of code that other programs can reuse.
- Local socket: a communication endpoint that only programs on the same device can reach.
- Memory map: the list of memory regions a process is using, with their permissions.
- MOBI: an older e-book file format.
- Multi-threaded: a program with several lines of execution running at once.
- Parameter injection: slipping extra command-line options into a command by putting them inside a value the program trusts.
- Parser: the part of a program that reads a file and turns its contents into something usable.
- Payload: the part of an attack that does the attacker's bidding once code execution is achieved.
- Phishing: tricking a person into trusting something that is not genuine.
- Pointer: a value that holds the address of something else in memory.
- Privilege escalation: going from a low-privilege account to a more powerful one.
- Process: a running program.
- Proof of concept: a demonstration that an attack really works.
- Quantization parameters: numbers in an image file that describe how the image is compressed.
- RCE (remote code execution): running your own code on someone else's device over a network.
- Reference code: the sample implementation published alongside a standard.
- Regex (regular expression): a pattern used to describe what a piece of text should look like.
- Reverse engineering: taking software apart to understand how it works without the source code.
- Root: the account on a device that can do anything.
- Shellcode: a small piece of attacker-written code whose job is to start something bigger.
- Socket: an endpoint a program uses to receive messages.
- Spoofing: sending a message that claims to come from an address you do not control.
- Stack dump: a snapshot of what a program was doing when it crashed.
- Struct: a way of grouping several pieces of data together under one name.
- Thread: one line of execution inside a process.
- User: an account on a device, with its own permissions.