Skip to content

crypto/cipher: AES GCM assembly can write beyond the end of the tag when using non-standard tag sizes [1.26 backport] #82297

Open

Description

@gopherbot

@chrisddwrt requested issue #80288 to be considered for backport to the next 1.26 minor release.

Independent confirmation on real hardware, plus a request to backport the fix to the supported release branches.

Reproduced with the program below on amd64 hardware (Intel Xeon, AES-NI/PCLMULQDQ) with go1.24.7 and go1.25.1, and on arm64 hardware (Apple M-series, AES+PMULL) with go1.24.7 and go1.25.1 under Linux 6.8 and with go1.26.3 under macOS — identical results everywhere. release-branch.go1.25 (go1.25.14) and release-branch.go1.26 (go1.26.9) still contain the unfixed assembly (checked in git, not run). The arm64 path is the same construction (gcm_arm64.s, encReduce: → VST1.P [B0.B16], 16(dstPtr) reached from tailLast:). With -tags purego nothing is written out of bounds.

Exact behaviour on all affected versions (guard bytes behind a destination slice of exact capacity):

tag size plaintext length (mod 16) bytes written past cap(dst)
12 1 / 2 / 3 3 / 2 / 1
13 1 / 2 2 / 1
14 1 1
15, 16 – none

i.e. 16 - (len % 16) - tagSize when positive. The bytes written are always zero (the block is masked with andMask before the full-block store at gcm_amd64.s ptxLoadLoop → MOVOU X0, (DX)). Plaintexts shorter than one block (len 1..3) are affected too. Open only over-reads up to 3 bytes.

Why "nearly always harmless due to allocator semantics" does not hold in practice: the slack argument assumes dst is a freshly allocated slice whose size class leaves padding. In packet-processing code dst is routinely a sub-slice of a larger live buffer — buf[:0:n], ring buffers, sync.Pool buffers, send buffers that already hold the next packet. We hit this in production code: the zero bytes landed in a pooled HMAC state (crypto/sha256: invalid hash state identifier) and, on go1.24, in a SIGSEGV. Any program using NewGCMWithTagSize with 12–14 byte tags on amd64/arm64 with hardware AES can be affected, and the plaintext length is attacker-controlled in most network protocols.

The fix (CL 801600, e36c20c) is currently only on master. Since this is a memory-safety bug in crypto/cipher with an attacker-influenceable trigger, and the fix is small and self-contained (tail load/store only, with new boundary tests), could it please be considered for backport to go1.25 and go1.26?

@gopherbot please consider this for backport to 1.25 and 1.26: memory safety issue in crypto/cipher (out-of-bounds write with short GCM tags), fix is on master.

Minimal reproduction (prints the number of bytes written past cap for each length):

package main

import (
	"crypto/aes"
	"crypto/cipher"
	"fmt"
	"runtime"
)

func main() {
	blk, _ := aes.NewCipher(make([]byte, 16))
	a, _ := cipher.NewGCMWithTagSize(blk, 12)
	nonce := make([]byte, 12)
	for _, l := range []int{1, 17, 18, 19, 20, 33} {
		buf := make([]byte, l+12+8)
		for i := range buf {
			buf[i] = 0xEE
		}
		a.Seal(buf[:0:l+12], nonce, make([]byte, l), nil) // dst has exact capacity
		over := 0
		for _, c := range buf[l+12:] {
			if c != 0xEE {
				over++
			}
		}
		fmt.Printf("%s %s len=%d: %d bytes written past cap(dst)\n", runtime.Version(), runtime.GOARCH, l, over)
	}
}

Activity

  1. modified the milestones: Go1.26.10, Go1.26.11 on Oct 17, 2026
  2. dr2chase commented on Oct 20, 2026

    @dr2chase
    Contributor

    @rolandshoemaker @FiloSottile how do you guys feel about this?

  3. neild commented on Oct 20, 2026

    @neild
    Contributor

    Copying @rolandshoemaker's comment from elsewhere:

    This is causing real crashes/unexpected behavior in prod environments, we should backport as it fixes a known bug.

  4. visiongaiatechnology commented on Oct 23, 2026

    @visiongaiatechnology

    Independent confirmation on Go 1.26.10 (linux/amd64, AES-NI/PCLMULQDQ):

    • cipher.NewGCMWithTagSize with tag 12/13/14 writes 3/2/1 zero bytes past cap(dst) in Seal when dst has exact capacity. Sweep over plaintext lengths 0..64, all dst lengths/caps and four offsets found 1872/1264/640 affected combinations.
    • Open over-read is demonstrable as a fault: place a 13-byte ciphertext (1-byte plaintext, 12-byte tag) so it ends exactly at a PROT_NONE page (mmap + mprotect); Open faults with SIGSEGV in crypto/internal/fips140/aes/gcm.gcmAesDec at gcm_amd64.s:1810.
    • Go 1.27.3: 0 OOB bytes and no fault. Go 1.26.10 with -tags purego: no OOB/fault.

    Go 1.26.10 is a current supported release and still contains the unfixed assembly. Please backport CL 801600 / CL 803120 to the 1.26 branch. I can provide the full standalone programs; no attachments sent here.

    — VisionGaiaTechnology

  5. JunyangShao commented on Oct 27, 2026

    @JunyangShao
    Contributor

    @rolandshoemaker ping on backport CLs, thanks.

  6. FiloSottile commented on Oct 27, 2026

    @FiloSottile
    Contributor

    It would be important for this backport to land after the #82626 ones. It can land in the same release, we just need a commit with #82626 and without this to re-wrap the FIPS 140-3 module at. (This change is too large and would trigger an algorithm re-test.)

  7. modified the milestones: Go1.26.11, Go1.26.12 on Nov 10, 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

    CherryPickApprovedUsed during the release process for point releases

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions