drm/amdgpu: fix CPER ring header parsing
Subsystems (2)
PATCH 1/1 drm/amdgpu: fix CPER ring header parsing
amdgpu_cper_ring_get_ent_sz() parses CPER headers directly from the circular ring buffer to determine the current entry size. When the ring is full and the write pointer lands near the end of the buffer, the header can wrap across the ring boundary.
The existing code treats the 4-byte CPER signature as a C string and uses strcmp() on in-ring binary data, then reads record_length through a direct struct pointer cast. Both assumptions are unsafe for wrapped entries and can read past the end of the ring mapping.
Fix the parser by comparing the signature as raw bytes and by copying the header into a local buffer before reading record_length, handling wraparound explicitly in both cases. This avoids out-of-bounds reads in amdgpu_cper_ring_get_ent_sz() when the CPER ring is full or the current entry starts at the tail of the ring.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_cper.c | 34 +++++++++++++++++------- 1 file changed, 25 insertions(+), 9 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_cper.c +25 -9
@@ -426,23 +426,40 @@ int amdgpu_cper_generate_ce_records(struct amdgpu_device *adev,static bool amdgpu_cper_is_hdr (struct amdgpu_ring *ring,u64 pos) { -struct cper_hdr *chdr; +char signature[sizeof (((struct cper_hdr *)0 )->signature)]; - chdr = (struct cper_hdr *)&(ring->ring[pos]); -return strcmp (chdr->signature,"CPER" ) ? false : true; +if ((pos <<2 ) >= ring->ring_size) +return false; + +if ((pos <<2 ) +sizeof (signature) <= ring->ring_size) { +memcpy (signature, &ring->ring[pos],sizeof (signature)); + }else { +u32 chunk = ring->ring_size - (pos <<2 ); + +memcpy (signature, &ring->ring[pos], chunk); +memcpy (signature + chunk, ring->ring,sizeof (signature) - chunk); + } + +return !memcmp (signature,"CPER" ,sizeof (signature)); }static u32 amdgpu_cper_ring_get_ent_sz (struct amdgpu_ring *ring,u64 pos) { -struct cper_hdr *chdr; +struct cper_hdr chdr;u64 p;u32 chunk, rec_len =0 ; - chdr = (struct cper_hdr *)&(ring->ring[pos]); chunk = ring->ring_size - (pos <<2 ); -if (!strcmp (chdr->signature,"CPER" )) { - rec_len = chdr->record_length; +if (amdgpu_cper_is_hdr (ring, pos)) { +if (chunk >=sizeof (chdr)) { +memcpy (&chdr, &ring->ring[pos],sizeof (chdr)); + }else { +memcpy (&chdr, &ring->ring[pos], chunk); +memcpy ((u8 *)&chdr + chunk, ring->ring,sizeof (chdr) - chunk); + } + + rec_len = chdr.record_length; goto calc; }
@@ -451,8 +468,7 @@ static u32 amdgpu_cper_ring_get_ent_sz(struct amdgpu_ring *ring, u64 pos) goto calc;for (p = pos +1 ; p <= ring->buf_mask; p++) { - chdr = (struct cper_hdr *)&(ring->ring[p]); -if (!strcmp (chdr->signature,"CPER" )) { +if (amdgpu_cper_is_hdr (ring, p)) { rec_len = (p - pos) <<2 ; goto calc; }
Signature · 1 line
2.34.1
[AMD Official Use Only - AMD Internal Distribution Only]
Quoted 36 lines
-----Original Message-----
From: Liu, Xiang(Dean) <[email protected]>
Sent: Thursday, April 9, 2026 5:24 PM
To: [email protected]
Cc: Zhang, Hawking <[email protected]>; Zhou1, Tao <[email protected]>; Liu, Xiang(Dean) <[email protected]>
Subject: [PATCH] drm/amdgpu: fix CPER ring header parsing
amdgpu_cper_ring_get_ent_sz()parses CPER headers directly from the circular ring buffer to determine the current entry size. When the ring is full and the write pointer lands near the end of the buffer, the header can wrap across the ring boundary.The existing code treats the 4-byte CPER signature as a C string and uses
strcmp()on in-ring binary data, then readsrecord_lengththrough a direct struct pointer cast. Both assumptions are unsafe for wrapped entries and can read past the end of the ring mapping.Fix the parser by comparing the signature as raw bytes and by copying the header into a local buffer before reading
record_length, handling wraparound explicitly in both cases. This avoids out-of-bounds reads inamdgpu_cper_ring_get_ent_sz()when the CPER ring is full or the current entry starts at the tail of the ring.Signed-off-by Xiang Liu <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_cper.c | 34 +++++++++++++++++----- -- 1 file changed, 25 insertions(+), 9 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu_cper.c +0
-0
drivers/gpu/drm/amd/amdgpu/amdgpu_cper.c int amdgpu_cper_generate_ce_records(struct · line 429
Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_cper.c +1
-1@@ -426,23 +426,40 @@ int amdgpu_cper_generate_ce_records(structamdgpu_device *adev, static bool amdgpu_cper_is_hdr(struct amdgpu_ring *ring, u64 pos) { - struct cper_hdr *chdr; + char signature[sizeof(((struct cper_hdr *)0)->signature)];
[Tao] I prefer to define CPER_SIGNATURE_SZ macro for sizeof(signature), with this fixed, the patch is:
Quoted 57 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_cper.c +24
-9- chdr = (struct cper_hdr *)&(ring->ring[pos]); -return strcmp (chdr->signature,"CPER" ) ? false : true; +if ((pos <<2 ) >= ring->ring_size) +return false; + +if ((pos <<2 ) +sizeof (signature) <= ring->ring_size) { +memcpy (signature, &ring->ring[pos],sizeof (signature)); + }else { +u32 chunk = ring->ring_size - (pos <<2 ); + +memcpy (signature, &ring->ring[pos], chunk); +memcpy (signature + chunk, ring->ring,sizeof (signature) - chunk); + } + +return !memcmp (signature,"CPER" ,sizeof (signature)); }static u32 amdgpu_cper_ring_get_ent_sz (struct amdgpu_ring *ring,u64 pos) { -struct cper_hdr *chdr; +struct cper_hdr chdr;u64 p;u32 chunk, rec_len =0 ; - chdr = (struct cper_hdr *)&(ring->ring[pos]); chunk = ring->ring_size - (pos <<2 ); -if (!strcmp (chdr->signature,"CPER" )) { - rec_len = chdr->record_length; +if (amdgpu_cper_is_hdr (ring, pos)) { +if (chunk >=sizeof (chdr)) { +memcpy (&chdr, &ring->ring[pos],sizeof (chdr)); + }else { +memcpy (&chdr, &ring->ring[pos], chunk); + memcpy((u8 *)&chdr + chunk, ring->ring, sizeof(chdr) - chunk); + } + + rec_len = chdr.record_length; goto calc; }@@ -451,8 +468,7 @@ static u32 amdgpu_cper_ring_get_ent_sz(struct amdgpu_ring *ring, u64 pos) goto calc;for (p = pos +1 ; p <= ring->buf_mask; p++) { - chdr = (struct cper_hdr *)&(ring->ring[p]); -if (!strcmp (chdr->signature,"CPER" )) { +if (amdgpu_cper_is_hdr (ring, p)) { rec_len = (p - pos) <<2 ; goto calc;}--
2.34.1
Liu, Xiang(Dean)
Reviewed-by Tao Zhou[AMD Official Use Only - AMD Internal Distribution Only]
Thanks, will improve this.
Best Regards,
Liu, Xiang
________________________________
From: Zhou1, Tao <[email protected]>
Sent: Friday, April 10, 2026 7:55 PM
To: Liu, Xiang(Dean) <[email protected]>; [email protected] <[email protected]>
Cc: Zhang, Hawking <[email protected]>
Subject: RE: [PATCH] drm/amdgpu: fix CPER ring header parsing
[AMD Official Use Only - AMD Internal Distribution Only]
Quoted 36 lines
-----Original Message-----
From: Liu, Xiang(Dean) <[email protected]>
Sent: Thursday, April 9, 2026 5:24 PM
To: [email protected]
Cc: Zhang, Hawking <[email protected]>; Zhou1, Tao <[email protected]>; Liu, Xiang(Dean) <[email protected]>
Subject: [PATCH] drm/amdgpu: fix CPER ring header parsing
amdgpu_cper_ring_get_ent_sz()parses CPER headers directly from the circular ring buffer to determine the current entry size. When the ring is full and the write pointer lands near the end of the buffer, the header can wrap across the ring boundary.The existing code treats the 4-byte CPER signature as a C string and uses
strcmp()on in-ring binary data, then readsrecord_lengththrough a direct struct pointer cast. Both assumptions are unsafe for wrapped entries and can read past the end of the ring mapping.Fix the parser by comparing the signature as raw bytes and by copying the header into a local buffer before reading
record_length, handling wraparound explicitly in both cases. This avoids out-of-bounds reads inamdgpu_cper_ring_get_ent_sz()when the CPER ring is full or the current entry starts at the tail of the ring.Signed-off-by Xiang Liu <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_cper.c | 34 +++++++++++++++++----- -- 1 file changed, 25 insertions(+), 9 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu_cper.c +0
-0
drivers/gpu/drm/amd/amdgpu/amdgpu_cper.c int amdgpu_cper_generate_ce_records(struct · line 429
Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_cper.c +1
-1@@ -426,23 +426,40 @@ int amdgpu_cper_generate_ce_records(structamdgpu_device *adev, static bool amdgpu_cper_is_hdr(struct amdgpu_ring *ring, u64 pos) { - struct cper_hdr *chdr; + char signature[sizeof(((struct cper_hdr *)0)->signature)];
[Tao] I prefer to define CPER_SIGNATURE_SZ macro for sizeof(signature), with this fixed, the patch is:
Quoted 57 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_cper.c +24
-9- chdr = (struct cper_hdr *)&(ring->ring[pos]); -return strcmp (chdr->signature,"CPER" ) ? false : true; +if ((pos <<2 ) >= ring->ring_size) +return false; + +if ((pos <<2 ) +sizeof (signature) <= ring->ring_size) { +memcpy (signature, &ring->ring[pos],sizeof (signature)); + }else { +u32 chunk = ring->ring_size - (pos <<2 ); + +memcpy (signature, &ring->ring[pos], chunk); +memcpy (signature + chunk, ring->ring,sizeof (signature) - chunk); + } + +return !memcmp (signature,"CPER" ,sizeof (signature)); }static u32 amdgpu_cper_ring_get_ent_sz (struct amdgpu_ring *ring,u64 pos) { -struct cper_hdr *chdr; +struct cper_hdr chdr;u64 p;u32 chunk, rec_len =0 ; - chdr = (struct cper_hdr *)&(ring->ring[pos]); chunk = ring->ring_size - (pos <<2 ); -if (!strcmp (chdr->signature,"CPER" )) { - rec_len = chdr->record_length; +if (amdgpu_cper_is_hdr (ring, pos)) { +if (chunk >=sizeof (chdr)) { +memcpy (&chdr, &ring->ring[pos],sizeof (chdr)); + }else { +memcpy (&chdr, &ring->ring[pos], chunk); + memcpy((u8 *)&chdr + chunk, ring->ring, sizeof(chdr) - chunk); + } + + rec_len = chdr.record_length; goto calc; }@@ -451,8 +468,7 @@ static u32 amdgpu_cper_ring_get_ent_sz(struct amdgpu_ring *ring, u64 pos) goto calc;for (p = pos +1 ; p <= ring->buf_mask; p++) { - chdr = (struct cper_hdr *)&(ring->ring[p]); -if (!strcmp (chdr->signature,"CPER" )) { +if (amdgpu_cper_is_hdr (ring, p)) { rec_len = (p - pos) <<2 ; goto calc;}--
2.34.1