size_t-to-int vulnerability in exFAT leads to memory corruption via malformed USB flash drives
An integer truncation in Sony's exFAT up-case table handling causes a heap buffer overflow when a malformed USB flash drive is mounted. An attacker could corrupt heap objects and potentially achieve kernel code execution.
Originally disclosed on HackerOne
- Triaged in 6 days
- Fixed in 3 months
- Paid in 15 days
- Disclosed after 12 months
Summary by the program
Summary
A heap-based buffer overflow can be triggered by a malformed exFAT USB flash drive.
Vulnerability
The vulnerability is in Sony's exFAT implementation where there is an integer truncation from 64bit to 32bit on a size variable that is used to allocate the up-case table:
int UVFAT_readupcasetable(void *unused, void *fileSystem) {
...
size_t dataLength = *(size_t *)(upcaseEntry + 24);
size_t size = sectorSize + dataLength - 1;
size = size - size % sectorSize;
uint8_t *data = sceFatfsCreateHeapVl(0, size);
...
while (1) {
...
UVFAT_ReadDevice(fileSystem, offset, sectorSize, data);
...
data += sectorSize;
...
}
}
Namely, dataLength and size are both 64bit wide, however the size argument of sceFatfsCreateHeapVl() is 32bit wide:
void *sceFatfsCreateHeapVl(void *unused, int size) {
return malloc(size, M_EXFATFSPATH, M_WAITOK | M_ZERO);
}
When using a big size for dataLength, this function will therefore only allocate a small buffer, and as a result overflow and corrupt subsequent objects on the heap when calling UVFAT_ReadDevice().
For example, using sectorSize=0x200 and dataLength=0x100000200 we have:
size = (sectorSize + dataLength - 1) - (sectorSize + dataLength - 1) % sectorSize;
<=> size = (0x200 + 0x100000200 - 1) - (0x200 + 0x100000200 - 1) % 0x200;
<=> size = 0x1000003FF - 0x1FF;
<=> size = 0x100000200;
When passing this size to sceFatfsCreateHeapVl(), the leading 1 is cut off to 0x200.
Exploitation
This vulnerability allows us to allocate any buffer on the heap with size >= 512 and multiple of 512, and allows us to overflow by a multiple of 512. There are interesting objects that one could spray on the heap such as struct usb_endpoint which contain interesting pointers that one could corrupt.
Impact
Jailbreak the PS4/PS5 by plugging in the USB and directly getting kernel code execution.
Context
This report concerns the PlayStation 4 (and, as the impact section states, the PS5) and Sony's implementation of the exFAT file system. exFAT is the file system commonly used on removable media such as USB flash drives. When a USB drive is plugged into the console, the kernel-side exFAT driver parses the file system structures on the device, including the up-case table, a table that maps lower-case characters to upper-case ones for case-insensitive name comparison.
The bug is a heap-based buffer overflow in that parsing path, classified as an incorrect calculation of buffer size. It was reported to the PlayStation program on HackerOne by theflow0, rated high severity, and a bounty of 10,000 USD was awarded. The report was disclosed on 21 September 2022.
Setup
The only precondition described is physical: a malformed exFAT USB flash drive that is plugged into the console. No account, role, tool or software version is given in the report. The trigger is plugging in the crafted drive, which causes the driver to parse the file system.
Finding it
The researcher located an integer truncation in the function that reads the up-case table. A 64-bit size value is computed and then passed to an allocator whose size parameter is only 32 bits wide. Because the value is truncated at the call boundary, the allocation is far smaller than the amount of data the driver subsequently writes into it.
Exploitation
Step 1: the vulnerable read function
The up-case table reader computes the buffer size from the table length stored in the up-case entry, rounds it up to a whole number of sectors, and allocates that many bytes:
int UVFAT_readupcasetable(void *unused, void *fileSystem) {
...
size_t dataLength = *(size_t *)(upcaseEntry + 24);
size_t size = sectorSize + dataLength - 1;
size = size - size % sectorSize;
uint8_t *data = sceFatfsCreateHeapVl(0, size);
...
while (1) {
...
UVFAT_ReadDevice(fileSystem, offset, sectorSize, data);
...
data += sectorSize;
...
}
}
dataLength is read as a size_t from offset 24 of the up-case entry, so it is attacker-controlled through the crafted file system. size is also size_t, that is 64-bit. The loop reads the device sector by sector into data, advancing the pointer by sectorSize each time.
Step 2: the truncating allocator
The allocation goes through sceFatfsCreateHeapVl(), whose second parameter is a plain int:
void *sceFatfsCreateHeapVl(void *unused, int size) {
return malloc(size, M_EXFATFSPATH, M_WAITOK | M_ZERO);
}
A 64-bit size passed into a 32-bit int parameter is truncated: only the low 32 bits survive. The buffer that malloc returns is therefore sized by the truncated value, while the loop in UVFAT_readupcasetable() keeps writing sectors according to the full 64-bit length.
Step 3: the arithmetic, with concrete values
With sectorSize=0x200 and dataLength=0x100000200, the rounding works out as follows:
size = (sectorSize + dataLength - 1) - (sectorSize + dataLength - 1) % sectorSize;
<=> size = (0x200 + 0x100000200 - 1) - (0x200 + 0x100000200 - 1) % 0x200;
<=> size = 0x1000003FF - 0x1FF;
<=> size = 0x100000200;
So the computed 64-bit size is 0x100000200. When that value is passed to sceFatfsCreateHeapVl(), the leading 1 is cut off and the allocator receives 0x200, a single sector. The subsequent UVFAT_ReadDevice() calls overflow the 512-byte buffer and corrupt subsequent objects on the heap.
Step 4: turning the overflow into control
The primitive is described as follows: the bug allows allocation of any heap buffer with a size of at least 512 bytes and a multiple of 512, and allows an overflow by a multiple of 512. Among the objects that can be sprayed on the heap, the researcher singles out struct usb_endpoint, which contains pointers worth corrupting.
Root cause
The mistake is a width mismatch between the size computation and the allocation interface. dataLength and size are 64-bit, but the size argument of sceFatfsCreateHeapVl() is 32-bit, so a computed length such as 0x100000200 is truncated to its low 32 bits.. The driver then trusts the untruncated length when copying data from the device. The report does not describe any check that would have caught the mismatch, and the truncation happens implicitly at the call, so nothing in the shown code stops it.
Impact
As stated in the report, the vulnerability allows a jailbreak of the PS4/PS5 by plugging in the USB drive and directly getting kernel code execution. The attacker needs physical access to insert the crafted drive; the report does not discuss any other precondition.
Fix
The report does not describe a fix or a change made in response. The evident correction is to keep the size 64-bit throughout, either by widening the allocator parameter or by rejecting table lengths that do not fit the allocation interface before allocating.
Variants to test
- Other file system parsers that read a length field from on-disk metadata into a 64-bit variable and pass it to a 32-bit allocation or copy length. A positive result is a computed size above
0xFFFFFFFFthat is accepted and truncated at the call. - Sector-size and length combinations that round up to a small multiple of the sector size after truncation, as
0x200and0x100000200do here. A positive result is an allocation of one or a few sectors followed by a copy of a much larger table. - Heap sprays of structures containing function or data pointers, such as
struct usb_endpointin this report, placed after the undersized buffer. A positive result is corruption of those pointers by the overflow.
Hunting notes
This class of bug hides where on-disk or on-device metadata supplies a length that is used both for allocation and for copying, especially in kernel drivers for removable media. The signal is a size variable declared wider than the parameter it is passed to, or a length field read as size_t and handed to an int. The report names the object to spray and the outcome, kernel code execution from plugging in the drive.
What happened, in plain words
A security researcher found a bug in the PlayStation 4 (and, according to the impact section, the PS5) in the code that reads USB flash drives. By preparing a USB stick with a deliberately malformed file system on it and plugging it into the console, it was possible to make the console write more data into a memory area than that area could hold. That kind of mistake is called a buffer overflow, and in this case it happened deep inside the console's operating system, in the part called the kernel, which is the most privileged layer of the software. The researcher reported it to Sony's PlayStation bug bounty programme on HackerOne, it was rated high severity, a bounty of 10,000 USD was paid, and the report was made public on 21 September 2022.
To follow the rest of this article you only need to know that a computer stores data in memory, that programs ask the operating system for chunks of memory when they need them, and that writing past the end of a chunk can damage whatever sits next to it. Everything else is explained as it comes up.
Context
What exFAT is
A file system is the set of rules a storage device uses to record which files exist, where they start and how big they are. exFAT is one such file system, and it is the one commonly used on removable media such as USB flash drives. Think of it as the index and shelving plan of a library: without it, the shelves are just a pile of paper.
When you plug a USB drive into the console, the console has to read that index before it can show you any files. The code that does this reading is the exFAT driver, and it runs inside the kernel. The kernel is the core of the operating system: it sits below all applications and has direct access to memory and hardware, which is why a bug there is far more serious than a bug in a game.
The up-case table
One of the structures the driver reads is the up-case table. Its job is to map lower-case characters to upper-case ones, so that the file system can compare names without caring about capitalisation: Photo.jpg and photo.jpg are treated as the same name. The table lives on the drive itself, which means its contents, including its length, are supplied by whoever prepared the drive. In security terms, that makes the length an attacker-controlled value.
The bug in one sentence
The bug is a heap-based buffer overflow in that parsing path, classified as an incorrect calculation of buffer size. The heap is the general-purpose pool of memory a program uses for objects it creates while running. A buffer overflow means writing past the end of a reserved area. Here the area reserved was far too small for the data that was then copied into it.
Setup
The only precondition described is physical: a malformed exFAT USB flash drive that is plugged into the console. No account, role, tool or software version is given in the report. The trigger is plugging in the crafted drive, which causes the driver to parse the file system. In plain terms, you do not need to log in, install anything or run a game: inserting the prepared drive is enough for the console to start reading it.
Finding it
The researcher located an integer truncation in the function that reads the up-case table. An integer truncation is what happens when a number that needs a lot of space to be stored is squeezed into a smaller container: the extra digits are simply dropped, without any warning. Here a 64-bit size value is computed and then passed to an allocator whose size parameter is only 32 bits wide. An allocator is the part of the system that hands out memory on request. Because the value is truncated at the call boundary, the allocation is far smaller than the amount of data the driver subsequently writes into it.
A useful comparison is a form that only has room for a two-digit house number. If someone writes 100, the form keeps 00 and throws the rest away. The clerk reading the form then believes the house is number 00, while the person who filled it in meant 100.
Exploitation
Step 1: the vulnerable read function
The up-case table reader computes the buffer size from the table length stored in the up-case entry, rounds it up to a whole number of sectors, and allocates that many bytes:
int UVFAT_readupcasetable(void *unused, void *fileSystem) {
...
size_t dataLength = *(size_t *)(upcaseEntry + 24);
size_t size = sectorSize + dataLength - 1;
size = size - size % sectorSize;
uint8_t *data = sceFatfsCreateHeapVl(0, size);
...
while (1) {
...
UVFAT_ReadDevice(fileSystem, offset, sectorSize, data);
...
data += sectorSize;
...
}
}
Line by line, in plain words:
int UVFAT_readupcasetable(void *unused, void *fileSystem) {declares the function that reads the up-case table. It receives two pointers, one unused and one identifying the mounted file system.size_t dataLength = *(size_t *)(upcaseEntry + 24);reads the table length from the up-case entry, at a fixed offset of 24 bytes into it.size_tis a type designed to hold a size; on this system it is 64 bits wide. Because the value comes from the drive, the person who made the drive chooses it.size_t size = sectorSize + dataLength - 1;adds the sector size and subtracts one. A sector is the smallest unit a storage device reads or writes at a time, typically 512 bytes. This step prepares a rounding-up calculation.size = size - size % sectorSize;rounds the result down to a whole multiple of the sector size. The%operator gives the remainder of a division, so subtracting the remainder leaves an exact multiple.uint8_t *data = sceFatfsCreateHeapVl(0, size);asks the allocator for a buffer ofsizebytes and keeps a pointer to it indata. This is the call where the trouble happens, as the next step shows.while (1) {starts a loop that keeps going until something inside it stops it.UVFAT_ReadDevice(fileSystem, offset, sectorSize, data);reads one sector from the device into the buffer atdata.data += sectorSize;moves the pointer forward by one sector, so the next read lands after the previous one.- The loop keeps reading sectors into the buffer, advancing the pointer each time.
dataLength is read as a size_t from offset 24 of the up-case entry, so it is attacker-controlled through the crafted file system. size is also size_t, that is 64-bit. The loop then reads the device sector by sector into data, advancing the pointer by sectorSize each time, for as long as the table data lasts.
Step 2: the truncating allocator
The allocation goes through sceFatfsCreateHeapVl(), whose second parameter is a plain int:
void *sceFatfsCreateHeapVl(void *unused, int size) {
return malloc(size, M_EXFATFSPATH, M_WAITOK | M_ZERO);
}
Line by line:
void *sceFatfsCreateHeapVl(void *unused, int size) {declares the allocation helper. Its second parameter is anint, a type that is 32 bits wide here, not 64.return malloc(size, M_EXFATFSPATH, M_WAITOK | M_ZERO);callsmalloc, the standard memory allocator, passing the requested size plus some flags that describe the kind of memory wanted and ask for it to be zeroed.mallocreturns the address of the new buffer.
A 64-bit size passed into a 32-bit int parameter is truncated: only the low 32 bits survive. The buffer that malloc returns is therefore sized by the truncated value, while the loop in UVFAT_readupcasetable() keeps writing sectors according to the full 64-bit length. Going back to the form comparison: the request says 100, the form records 00, and the warehouse ships space for 00 while the delivery van keeps unloading 100.
Step 3: the arithmetic, with concrete values
With sectorSize=0x200 and dataLength=0x100000200, the rounding works out as follows:
size = (sectorSize + dataLength - 1) - (sectorSize + dataLength - 1) % sectorSize;
<=> size = (0x200 + 0x100000200 - 1) - (0x200 + 0x100000200 - 1) % 0x200;
<=> size = 0x1000003FF - 0x1FF;
<=> size = 0x100000200;
The numbers are written in hexadecimal, a base-16 notation where each digit stands for a value from 0 to 15 and the prefix 0x marks the notation. 0x200 is 512 in ordinary decimal, which is one sector. The first line is the rounding formula from the code. The second line substitutes the two chosen values. The third line simplifies the arithmetic inside the brackets. The fourth line gives the final 64-bit size, 0x100000200.
So the computed 64-bit size is 0x100000200. When that value is passed to sceFatfsCreateHeapVl(), the leading 1 is cut off and the allocator receives 0x200, a single sector. The subsequent UVFAT_ReadDevice() calls overflow the 512-byte buffer and corrupt subsequent objects on the heap. The heap is laid out as a sequence of objects next to each other, so writing past one object spills into its neighbours, much like pouring too much water into a glass on a crowded table.
Step 4: turning the overflow into control
The primitive is described as follows: the bug allows allocation of any heap buffer with a size of at least 512 bytes and a multiple of 512, and allows an overflow by a multiple of 512. A primitive, in security writing, is a small reliable capability that an attacker can build larger attacks from, the way a single Lego brick is not a house but can become part of one.
Among the objects that can be sprayed on the heap, the researcher singles out struct usb_endpoint, which contains pointers worth corrupting. Spraying means creating many copies of a chosen object so that the heap fills up with them and the odds are good that the vulnerable buffer ends up next to one. A pointer is a value that holds the address of something else in memory; if an attacker can overwrite a pointer, the program may later follow it to a place the attacker chose. A struct is simply a bundle of related fields stored together.
Root cause
The mistake is a width mismatch between the size computation and the allocation interface. dataLength and size are 64-bit, but the size argument of sceFatfsCreateHeapVl() is 32-bit, so a computed length such as 0x100000200 is truncated to its low 32 bits.. Modulo means keeping only the remainder after division, so the higher part of the number disappears. The driver then trusts the untruncated length when copying data from the device. The report does not describe any check that would have caught the mismatch, and the truncation happens implicitly at the call, so nothing in the shown code stops it.
Impact
As stated in the report, the vulnerability allows a jailbreak of the PS4/PS5 by plugging in the USB drive and directly getting kernel code execution. A jailbreak means removing the manufacturer's restrictions on what the console will run. Kernel code execution means getting the console to run code of your choosing at the highest privilege level, where nothing in the operating system can refuse it. The attacker needs physical access to insert the crafted drive; the report does not discuss any other precondition.
Fix
The report does not describe a fix or a change made in response. The evident correction is to keep the size 64-bit throughout, either by widening the allocator parameter or by rejecting table lengths that do not fit the allocation interface before allocating. In everyday terms: either give the form enough boxes for the real number, or refuse any number that does not fit instead of quietly chopping off digits.
Variants to test
- Other file system parsers that read a length field from on-disk metadata into a 64-bit variable and pass it to a 32-bit allocation or copy length. A positive result is a computed size above
0xFFFFFFFFthat is accepted and truncated at the call. - Sector-size and length combinations that round up to a small multiple of the sector size after truncation, as
0x200and0x100000200do here. A positive result is an allocation of one or a few sectors followed by a copy of a much larger table. - Heap sprays of structures containing function or data pointers, such as
struct usb_endpointin this report, placed after the undersized buffer. A positive result is corruption of those pointers by the overflow.
Hunting notes
This class of bug hides where on-disk or on-device metadata supplies a length that is used both for allocation and for copying, especially in kernel drivers for removable media. The signal is a size variable declared wider than the parameter it is passed to, or a length field read as size_t and handed to an int. The report names the object to spray and the outcome, kernel code execution from plugging in the drive.
Glossary
- Allocator: the part of a system that hands out chunks of memory when a program asks for them.
- Buffer: a reserved area of memory used to hold data temporarily.
- Buffer overflow: writing more data into a buffer than it can hold, so the extra data lands in neighbouring memory.
- Bounty: a payment made by a company to a researcher who reports a security bug.
- Driver: software that lets the operating system talk to a piece of hardware or understand a particular format.
- exFAT: a file system commonly used on removable media such as USB flash drives.
- File system: the rules a storage device uses to record which files exist and where they are stored.
- Heap: the general-purpose pool of memory a program uses for objects it creates while running.
- Hexadecimal: a base-16 way of writing numbers, marked with the prefix
0x. - Integer truncation: squeezing a number into a smaller storage type and silently dropping the higher digits.
- Jailbreak: removing a manufacturer's restrictions on what a device will run.
- Kernel: the core of an operating system, with direct access to memory and hardware and the highest privilege.
- Kernel code execution: getting code of your choosing to run at kernel privilege level.
- malloc: the standard function that requests memory from the allocator.
- Modulo: the remainder left after a division; reducing a number modulo 2^32 keeps only its low 32 bits.
- Mounting: the act of reading a storage device's file system so its files become accessible.
- Pointer: a value that holds the memory address of something else.
- Primitive: a small, reliable capability that a larger attack can be built from.
- Sector: the smallest unit a storage device reads or writes at a time, typically 512 bytes.
- size_t: a type designed to hold a size, 64 bits wide on this system.
- Spraying: creating many copies of a chosen object so the heap fills with them and one is likely to sit next to the target.
- struct: a bundle of related fields stored together in memory.
- Up-case table: a table mapping lower-case characters to upper-case ones, used for case-insensitive name comparison.
- Width mismatch: a situation where a value is computed in a wide type but passed to a narrower one, so information is lost.