Skip to content

fix(retinaface): reject malformed RLE counts before native decode - #2908

Open
huangkongsheng wants to merge 1 commit into
deepinsight:masterfrom
huangkongsheng:fix/rle-overflow
Open

huangkongsheng wants to merge 1 commit into
deepinsight:masterfrom
huangkongsheng:fix/rle-overflow

Conversation

@huangkongsheng

Copy link
Copy Markdown

Fixes #2889

The vendored pycocotools accepted compressed RLE strings whose decoded run lengths did not sum to the declared h*w. rleDecode() then trusted the counts and wrote past the end of the mask allocation - a native heap overflow reachable from untrusted COCO annotations. The ASan PoC in the issue (_mask.decode([{size:[1,1], counts:b'02'}]) - 2 bytes written into a 1-byte mask) confirms it.

Changes:

  • rleFrString() verifies the decoded run-length total equals h*w and produces an empty RLE otherwise (also catches delta values that wrap into huge uint counts, and failed mallocs for non-empty strings)
  • _frString() turns that empty RLE into a ValueError so callers get a Python exception instead of silent corruption
  • decode() additionally rejects a list of RLEs with inconsistent sizes (the buffer is sized from the first one)

Valid encodings are unaffected - their run lengths total h*w by definition. Note I could not compile locally (no C toolchain on this machine), so the Cython build and the issue's PoC should be re-run by CI or a maintainer environment; the fix is deliberately small and localised to the two decode entry points.

The vendored pycocotools accepted compressed RLE strings whose decoded
run lengths did not sum to the declared h*w. rleDecode() then trusted
the counts and wrote past the end of the mask allocation - a native
heap overflow reachable from untrusted COCO annotations
(_mask.decode([{size:[1,1], counts:b'02'}]) writes 2 bytes into a
1-byte mask, ASan-confirmed in issue deepinsight#2889).

rleFrString() now verifies the run-length total and produces an empty
RLE on mismatch; _frString() turns that into a ValueError, and
decode() additionally rejects RLEs with inconsistent sizes. Valid
encodings are unaffected (their total is h*w by definition).

Issue deepinsight#2889 has the full analysis and an ASan PoC.
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.

[BUG] Malformed COCO RLE counts can overflow the vendored pycocotools decode buffer

1 participant