cute_aseprite.h: Cel Extra chunk (0x2006) is written to cels[cel_count] instead of the last cel (off-by-one, out-of-bounds write at 64 cels)

Open Beginner friendly
#456 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
72/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
c
Domain
data

Research direction

The bug is in the 0x2006 Cel Extra case of cute_aseprite_load_from_memory in cute_aseprite.h, which indexes frame->cels[frame->cel_count] where it should use cel_count - 1. Read the Cel chunk case just above it to confirm the increment, then check how a frame with 64 cels and a trailing 0x2006 chunk is handled. Done when the extra data attaches to the correct cel, the out-of-bounds write is gone, and a zero-cel frame is rejected or skipped safely.

Written by the indexing model from the issue text.

Description

Summary

When cute_aseprite_load_from_memory parses a Cel Extra chunk (0x2006), it stores the data in frame->cels[frame->cel_count]. The preceding Cel chunk has already incremented cel_count, so this is the slot after the cel the extra data belongs to. This attaches the extra data to the wrong cel and, when a frame has 64 cels (CUTE_ASEPRITE_MAX_LAYERS), writes past the end of the cels[] array.

Affected code

cute_aseprite.h (v1.04, master 476d688):

// line 1056-1057, Cel chunk
CUTE_ASEPRITE_ASSERT(frame->cel_count < CUTE_ASEPRITE_MAX_LAYERS);
ase_cel_t* cel = frame->cels + frame->cel_count++;
...
// line 1101-1111, Cel Extra chunk
case 0x2006:
{
    ase_cel_t* cel = frame->cels + frame->cel_count;   // <-- should be cel_count - 1
    cel->has_extra = 1;
    cel->extra.precise_bounds_are_set = (int)s_read_uint32(s);
    cel->extra.precise_x = s_read_fixed(s);
    ...

The same code is in cute_framework's copy (libraries/cute/cute_aseprite.h, 0x2006 case).

How it is reached

The Aseprite file format spec says the Cel Extra chunk "adds extra information to the latest read cel". In a normal file it directly follows a Cel chunk, so cel_count >= 1 and the right slot is cel_count - 1.

  • Normal files: the extra info lands in an unused slot. The next Cel chunk in the same frame reuses that slot but does not clear has_extra, so the extra info shows up on the wrong cel.
  • A frame with 64 Cel chunks (allowed: the assert checks cel_count < 64 before the increment) followed by a Cel Extra chunk writes to cels[64]. cels[] is the last member of ase_frame_t, so the write lands in the next element of the heap frames array, or past the end of the allocation for the last frame.

No assert guards this path, so it applies to debug and release builds.

Impact

Out-of-bounds heap write of 24 bytes (has_extra plus the extra struct) with values taken from the file, when loading an untrusted .ase/.aseprite file. Also a functional bug: precise bounds are reported on the wrong cel.

Suggested fix

case 0x2006:
{
    if (frame->cel_count == 0) { s_skip(s, (int)chunk_size); break; } // or fail the load
    ase_cel_t* cel = frame->cels + frame->cel_count - 1;
    ...
Dominant language
C
Stars
5.1k
Forks
316
Avg merge
28m
Merged PRs (30d)
1

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from RandyGaul/cute_headers

All issues in RandyGaul/cute_headers

Similar issues

More C issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.