# size_t-to-int vulnerability in exFAT leads to memory corruption via malformed USB flash drives

Sony's exFAT driver truncates a 64-bit up-case table size to 32-bit before heap allocation, letting a malformed USB drive overflow the heap and gain PS4/PS5 kernel code execution.

- Payout: $10,000
- Severity: high
- Program: PlayStation
- Found by: theflow0
- Disclosed: 2022-09-21
- Write-up quality: 7/10
- Original: https://hackerone.com/reports/1340942
- Library page: https://bugbountylibrary.com/r/h1%3A1340942
- Tags: area:oss domain:oss impact:privesc impact:rce kind:bounty platform:hackerone sev:high tech:linux technique:source-review vuln:dos vuln:memory-corruption

**Target:** exFAT filesystem driver (UVFAT_readupcasetable) parsing USB flash drive up-case table
**Root cause:** dataLength and size are computed as 64-bit size_t, but sceFatfsCreateHeapVl() takes a 32-bit int size, so a large dataLength truncates the allocation to a small buffer while UVFAT_ReadDevice() still writes the full rounded-up length.
**Steps:**
1. Craft exFAT volume with sectorSize=0x200 and up-case table dataLength=0x100000200
2. size = (0x200 + 0x100000200 - 1) - (0x200 + 0x100000200 - 1) % 0x200 = 0x100000200
3. sceFatfsCreateHeapVl() truncates 0x100000200 to 0x200, allocating only 512 bytes
4. UVFAT_ReadDevice() loops writing sectorSize chunks into the undersized buffer, overflowing by a multiple of 512
5. Plug the malformed USB drive into the console to trigger the heap overflow
**How found:** Source review of Sony's exFAT implementation spotted the 64-bit size_t size passed into a 32-bit int parameter of sceFatfsCreateHeapVl(), an integer truncation on the up-case table allocation.
**Exploitation:** Attacker controls allocation size (any multiple of 512 >= 512) and overflow length (multiple of 512). Heap spray objects such as struct usb_endpoint containing useful pointers, then corrupt them via the overflow to achieve kernel code execution and jailbreak the PS4/PS5.
**Escalation:** Chained in real jailbreak chains with a WebKit browser exploit: browser bug gives usermode code and kernel heap spray, then the user plugs in the malicious USB to trigger this heap overflow for kernel code execution.
**Insight:** When auditing filesystem parsers, trace size variables from on-disk fields through every function boundary; a size_t narrowed to int at an allocator call is a truncation primitive giving attacker-controlled allocation size and overflow length.

## Proof of concept (copied exactly from the original report)

**Malformed exFAT up-case table size calculation**

```
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;
```

**32-bit allocation function truncating size**

```
void *sceFatfsCreateHeapVl(void *unused, int size) {
  return malloc(size, M_EXFATFSPATH, M_WAITOK | M_ZERO);
```

**Concrete size truncation arithmetic example**

```
size = (sectorSize + dataLength - 1) - (sectorSize + dataLength - 1) % sectorSize;
<=> size = (0x200 + 0x100000200 - 1) - (0x200 + 0x100000200 - 1) % 0x200;
<=> size = 0x1000003FF - 0x1FF;
<=> size = 0x100000200;
```

