Skip to content

bmp: validate palette indices when decoding paletted images#30

Open
mohammadmseet-hue wants to merge 1 commit into
golang:masterfrom
mohammadmseet-hue:fix/bmp-palette-index-validation
Open

bmp: validate palette indices when decoding paletted images#30
mohammadmseet-hue wants to merge 1 commit into
golang:masterfrom
mohammadmseet-hue:fix/bmp-palette-index-validation

Conversation

@mohammadmseet-hue
Copy link
Copy Markdown
Contributor

@mohammadmseet-hue mohammadmseet-hue commented Apr 4, 2026

Validate that decoded pixel indices are within palette bounds
in decodePaletted. This is the same fix applied to the TIFF
decoder in commit 3bbf4a6 (tiff: Validate palette indices
when parsing palette-color images), but for BMP.

A crafted BMP with a small palette but out-of-range pixel
indices causes a panic when the decoded image is accessed.
For example, a file with colorUsed=2 (2-color palette) but
pixel data containing index 255 causes:

runtime error: index out of range [255] with length 2

The TIFF decoder was patched to reject invalid palette
indices, but the BMP decoder has the identical vulnerable
pattern. The decoder stores the index without validation.
When any consumer calls At(x, y) on the returned
image.Paletted, it panics.

This affects all paletted BMP modes: 1, 2, 4, and 8 bits
per pixel.

The fix validates each pixel index against the palette
length during decoding and returns an error for any
out-of-range index.

@google-cla
Copy link
Copy Markdown

google-cla Bot commented Apr 4, 2026

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

The TIFF decoder was patched to validate palette indices in commit
3bbf4a6 ("tiff: Validate palette indices when parsing palette-color
images"), but the BMP decoder has the same vulnerable pattern and was
not patched. A crafted BMP file with a small palette (e.g., 2 colors)
but pixel data containing out-of-range indices (e.g., 255) decodes
successfully. When any consumer accesses the pixels via the returned
image.Paletted, it panics with an index out of range error.

This adds the same validation that the TIFF decoder received: check
that each decoded pixel index is within the palette bounds before
storing it.
@mohammadmseet-hue mohammadmseet-hue force-pushed the fix/bmp-palette-index-validation branch from 8225727 to d082a00 Compare April 4, 2026 04:33
@gopherbot
Copy link
Copy Markdown
Contributor

This PR (HEAD: d082a00) has been imported to Gerrit for code review.

Please visit Gerrit at https://go-review.googlesource.com/c/image/+/762680.

Important tips:

  • Don't comment on this PR. All discussion takes place in Gerrit.
  • You need a Gmail or other Google account to log in to Gerrit.
  • To change your code in response to feedback:
    • Push a new commit to the branch used by your GitHub PR.
    • A new "patch set" will then appear in Gerrit.
    • Respond to each comment by marking as Done in Gerrit if implemented as suggested. You can alternatively write a reply.
    • Critical: you must click the blue Reply button near the top to publish your Gerrit responses.
    • Multiple commits in the PR will be squashed by GerritBot.
  • The title and description of the GitHub PR are used to construct the final commit message.
    • Edit these as needed via the GitHub web interface (not via Gerrit or git).
    • You should word wrap the PR description at ~76 characters unless you need longer lines (e.g., for tables or URLs).
  • See the Sending a change via GitHub and Reviews sections of the Contribution Guide as well as the FAQ for details.

@gopherbot
Copy link
Copy Markdown
Contributor

Message from Gopher Robot:

Patch Set 1:

(1 comment)


Please don’t reply on this GitHub thread. Visit golang.org/cl/762680.
After addressing review feedback, remember to publish your drafts!

@gopherbot
Copy link
Copy Markdown
Contributor

Message from Mohammad Seet:

Patch Set 1:

Done. Updated PR description to remove markdown formatting, wrap lines at ~76 characters, and fix any issues flagged by the bot.


Please don’t reply on this GitHub thread. Visit golang.org/cl/762680.
After addressing review feedback, remember to publish your drafts!

@gopherbot
Copy link
Copy Markdown
Contributor

Message from Mohammad Seet:

Patch Set 2:

(1 comment)


Please don’t reply on this GitHub thread. Visit golang.org/cl/762680.
After addressing review feedback, remember to publish your drafts!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants