LibAtrac9: a malformed block writes past Block::Gradient[31] (gradient end unit 32..47) #2345

Closed
opened 2026-10-09 16:04:17 +00:00 by Dextroid17 · 1 comment
Dextroid17 commented 2026-10-09 16:04:17 +00:00 (Migrated from github.com)
Contributor

Imported from boykopovar/AnyPS5. Originally posted by Dextroid17 on 2026-10-09T16:04:17Z.


What

The vendored 3rdparty/LibAtrac9 (shadps4-emu/ext-LibAtrac9, pinned at 946e05a = upstream master) writes out of bounds for a block whose gradient end unit is 32..47.

Block::Gradient is int Gradient[31] (C/src/structures.h:114), but ReadGradientParams validates the gradient units against 48, not 31:

  • C/src/unpack.c:175 — block->GradientEndUnit = ReadInt(br, 6) + 1; → 1..64
  • C/src/unpack.c:189 — if (block->GradientEndUnit < 0 || block->GradientEndUnit >= 48) → accepts up to 47
  • C/src/bit_allocation.c:13 — for (int i = 0; i < block->GradientEndUnit; i++) block->Gradient[i] = block->GradientStartValue; → for GradientEndUnit in [32,47] this writes Gradient[31..46], past the array and onto GradientMode and the fields after it.

The 48 bound matches GradientCurves[48][48]; it does not match the 31-element Gradient array.

Reachable from title data

libSceAjm.native/src/Ajm.cpp:646 hands the guest buffer straight to the decoder (DecodeAt9Frame(instance, input.data() + consumed, …) → UnpackFrame → ReadGradientParams → CreateGradient). A streamed/corrupt AT9 block with GradientMode == 0 and a 6-bit end unit ≥ 31 reaches the overrun; nothing on the AnyPS5 side validates those units first.

Evidence

  • Static: the three lines above (Gradient[31] vs the < 48 guard vs the i < GradientEndUnit loop).
  • Independent, already on record: #2163's body reports -fsanitize=bounds,address catching the Gradient overrun for its 0x07-filled block, and that the overrun is in Block in this library. #2163 only changes the test input so it no longer reaches the overrun; the overrun itself is untouched.
  • Behavioural, this box (gcc 16.2.1, base d70b8998): at Debug/-O0 guest_ajm_tests aborts (exit 134) at GuestAjm.cpp:597; with #2163 (block byte 0xA5) it exits 0. Release passes on both arms, so this is -O0-only for the test, but the out-of-bounds write exists at every optimisation level.

Why filed here, not upstream

Upstream has issues disabled (hasIssuesEnabled: false), and the pinned commit is already upstream master, so there is no newer commit to bump to. A fix has to be a fork/pin or a patch carried against the submodule.

Suggested fix

Bound the gradient units to the array in ReadGradientParams (reject GradientEndUnit > 31), or size Gradient to match GradientCurves (48). GradientStartUnit <= GradientEndUnit is already checked, so bounding the end unit is sufficient.

Written with AI assistance.

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5781058392 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/2345). Originally posted by [Dextroid17](https://github.com/Dextroid17) on 2026-10-09T16:04:17Z. --- ### What The vendored `3rdparty/LibAtrac9` (`shadps4-emu/ext-LibAtrac9`, pinned at `946e05a` = upstream master) writes out of bounds for a block whose gradient end unit is 32..47. `Block::Gradient` is `int Gradient[31]` (`C/src/structures.h:114`), but `ReadGradientParams` validates the gradient units against 48, not 31: - `C/src/unpack.c:175` — `block->GradientEndUnit = ReadInt(br, 6) + 1;` → 1..64 - `C/src/unpack.c:189` — `if (block->GradientEndUnit < 0 || block->GradientEndUnit >= 48)` → accepts up to 47 - `C/src/bit_allocation.c:13` — `for (int i = 0; i < block->GradientEndUnit; i++) block->Gradient[i] = block->GradientStartValue;` → for `GradientEndUnit` in **[32,47]** this writes `Gradient[31..46]`, past the array and onto `GradientMode` and the fields after it. The 48 bound matches `GradientCurves[48][48]`; it does not match the 31-element `Gradient` array. ### Reachable from title data `libSceAjm.native/src/Ajm.cpp:646` hands the guest buffer straight to the decoder (`DecodeAt9Frame(instance, input.data() + consumed, …)` → `UnpackFrame` → `ReadGradientParams` → `CreateGradient`). A streamed/corrupt AT9 block with `GradientMode == 0` and a 6-bit end unit ≥ 31 reaches the overrun; nothing on the AnyPS5 side validates those units first. ### Evidence - Static: the three lines above (`Gradient[31]` vs the `< 48` guard vs the `i < GradientEndUnit` loop). - Independent, already on record: #2163's body reports `-fsanitize=bounds,address` catching the `Gradient` overrun for its `0x07`-filled block, and that the overrun is in `Block` in this library. #2163 only changes the *test* input so it no longer reaches the overrun; the overrun itself is untouched. - Behavioural, this box (gcc 16.2.1, base `d70b8998`): at Debug/`-O0` `guest_ajm_tests` aborts (exit 134) at `GuestAjm.cpp:597`; with #2163 (block byte `0xA5`) it exits 0. Release passes on both arms, so this is `-O0`-only for the *test*, but the out-of-bounds **write** exists at every optimisation level. ### Why filed here, not upstream Upstream has issues disabled (`hasIssuesEnabled: false`), and the pinned commit is already upstream `master`, so there is no newer commit to bump to. A fix has to be a fork/pin or a patch carried against the submodule. ### Suggested fix Bound the gradient units to the array in `ReadGradientParams` (reject `GradientEndUnit > 31`), or size `Gradient` to match `GradientCurves` (48). `GradientStartUnit <= GradientEndUnit` is already checked, so bounding the end unit is sufficient. Written with AI assistance.
ArchitDewan commented 2026-10-09 17:44:30 +00:00 (Migrated from github.com)
Author
Contributor

Imported from boykopovar/AnyPS5. Originally posted by ArchitDewan on 2026-10-09T17:44:30Z.


I’d like to take on #2345 and submit a PR to fix the LibAtrac9 gradient bounds issue, with a regression test, if no one else is working on it.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6086159719 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/2345#issuecomment-6086159719). Originally posted by [ArchitDewan](https://github.com/ArchitDewan) on 2026-10-09T17:44:30Z. --- I’d like to take on #2345 and submit a PR to fix the LibAtrac9 gradient bounds issue, with a regression test, if no one else is working on it.
Sign in to join this conversation.
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5#2345
No description provided.