Repository navigation
Conversation
inverseDCT4 multiplied coefficients by 65536*cos(pi/8)*sqrt(2), or 85627, in int32. The product overflows once a coefficient's magnitude exceeds math.MaxInt32/85627, or 25079, flipping the sign of the term and corrupting the decoded block. Dequantized coefficients are stored as an int16, so a crafted bitstream can reach that magnitude: an AC token value of 89 with a dequantization factor of 284 gives 25276. Split the multiplication into 20091 + 65536, as libwebp's TransformOne does. That gives the same result whenever the original does not overflow, and keeps the intermediate product within int32 for any int16 input. The second pass multiplies the first pass's results, which can still exceed int32 for extreme inputs. libwebp has the same behaviour there. Fixes golang/go#81659 Change-Id: I5e5a9fee76e9f8ec61dfebae1af8b2747d94479c
|
This PR (HEAD: b3c8262) has been imported to Gerrit for code review. Please visit Gerrit at https://go-review.googlesource.com/c/image/+/837286. Important tips:
|
|
Message from Gopher Robot: Patch Set 1: Congratulations on opening your first change. Thank you for your contribution! Next steps: Most changes in the Go project go through a few rounds of revision. This can be Please don’t reply on this GitHub thread. Visit golang.org/cl/837286. |
The previous patch set split the multiplication by 85627 into 20091 + 65536, which keeps the first pass inside an int32 but leaves the second pass, which multiplies the first pass's results, still overflowing. Multiplying by 35468 wraps once its input exceeds 60548, and two maximal coefficients in one row already produce 75577. Widen both multiplications to an int64 instead, which is exact for every int16 input in both passes, and add a test case for the second pass.
|
This PR (HEAD: eb1be93) has been imported to Gerrit for code review. Please visit Gerrit at https://go-review.googlesource.com/c/image/+/837286. Important tips:
|
|
Message from Ahmad Wyne: Patch Set 2: (1 comment) Please don’t reply on this GitHub thread. Visit golang.org/cl/837286. |
inverseDCT4 multiplied coefficients by 65536*cos(pi/8)sqrt(2), or 85627,
and by 65536sin(pi/8)*sqrt(2), or 35468, in int32. Both products can
overflow, which flips the sign of a term and corrupts the decoded block.
Dequantized coefficients are stored as an int16, so the first pass wraps
once a coefficient's magnitude exceeds math.MaxInt32/85627, or 25079. A
crafted bitstream can reach that: an AC token value of 89 with a
dequantization factor of 284 gives 25276.
The second pass multiplies the first pass's results, which range up to
about 126000, so it wraps at much smaller coefficients. Multiplying by
35468 overflows once its input exceeds 60548, and two maximal coefficients
in one row already produce 75577.
Widen both multiplications to an int64. That is exact for every int16
input in both passes, and gives the same results as the int32 arithmetic
wherever that did not overflow.
The added test covers both passes. Its want values were computed with the
same transform in int64 arithmetic, and every case fails before this
change.
Fixes golang/go#81659