math/rand/v2: ChaCha8.UnmarshalBinary panics on an overlong read buffer · Issue #81488 · golang/go · GitHub
Skip to content You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session. Dismiss alert math/rand/v2: ChaCha8.UnmarshalBinary panics on an overlong read buffer #81488 New issue Copy link New issue Copy link Open #81561 Open math/rand/v2: ChaCha8.UnmarshalBinary panics on an overlong read buffer #81488 #81561 Copy link Labels FixPending Issues that have a fix which has not yet been reviewed or submitted.
Issues that have a fix which has not yet been reviewed or submitted. NeedsFix The path to resolution is known, but the work has not been done. The path to resolution is known, but the work has not been done. Milestone Go1.28 Description filippo-agent opened on Sep 13, 2026 Issue body actions (*ChaCha8).UnmarshalBinary accepts a length-prefixed readbuf: field longer than its eight-byte buffer, then panics while slicing the buffer instead of returning an error.
It also updates the buffered bytes before validating the remaining state, so a failed unmarshal can change subsequent Read output. c := rand . NewChaCha8 ([ 32 ] byte {}) // rand is math/rand/v2 _ = c . UnmarshalBinary ([] byte ( "readbuf: \x09 123456789" )) // slice bounds panic Details In src/math/rand/v2/chacha8.go , UnmarshalBinary executes c.readLen = copy(c.readBuf[len(c.readBuf)-len(buf):], buf) before calling chacha8rand.Unmarshal .
The length-prefix helper accepts lengths from 0 through 255 if enough bytes follow. A nine-byte buffer makes the slice start at −1, before the core state is checked. A shorter buffer avoids the panic but is committed even if the remaining state is invalid. On a fresh generator, the following rejected encoding injects POC into the next read: err := c . UnmarshalBinary ([] byte ( "readbuf: \x03 POCnot-a-valid-chacha8-state" )) var out [ 3 ] byte ; _ , _ = c .
Read ( out [:]) // err != nil; string(out[:]) == "POC" This issue was found by zkao and was triaged by @FiloSottile as a valid bug but not a vulnerability. /cc @golang/security Activity gabyhelp commented on Sep 13, 2026 gabyhelp on Sep 13, 2026 More actions Related Issues math/big: Int.GobDecode accepts negative zero that can panic in arithmetic #81490 math/rand: NewZipf accepts non-finite parameters that prevent Uint64 from returning #81492 (Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion .) cristiangirlea commented on Sep 16, 2026 cristiangirlea on Sep 16, 2026 Contributor More actions I'd like to work on this.
I can send a small fix that rejects oversized read buffers and leaves the generator unchanged when decoding fails, with regression tests for malformed inputs and valid round trips. Would it be OK for me to take this, or is someone already working on it? gopherbot commented on Sep 16, 2026 gopherbot on Sep 16, 2026 Contributor More actions Change https://go.dev/cl/833564 mentions this issue: math/rand/v2: validate ChaCha8 encodings before changing state cherrymui added NeedsFix The path to resolution is known, but the work has not been done.
The path to resolution is known, but the work has not been done. on Sep 18, 2026 cherrymui added this to the Go1.28 milestone on Sep 18, 2026 cherrymui added FixPending Issues that have a fix which has not yet been reviewed or submitted. Issues that have a fix which has not yet been reviewed or submitted. on Sep 18, 2026 Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment Metadata Metadata Assignees No one assigned Labels FixPending Issues that have a fix which has not yet been reviewed or submitted.
Issues that have a fix which has not yet been reviewed or submitted. NeedsFix The path to resolution is known, but the work has not been done. The path to resolution is known, but the work has not been done. Type No type Projects No projects Milestone Go1.28 No due date Relationships None yet Development No branches or pull requests Issue actions You can’t perform that action at this time.