Skip to content

[XRay][compiler-rt] Fix oob memory access in FDR BufferQueue iterator #90940

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 27, 2024

Conversation

rickyz
Copy link
Member

@rickyz rickyz commented May 3, 2024

Before this change, the FDR BufferQueue iterator could access oob memory
due to checks of the form !Buffers[Offset].Used && Offset != Max. This
allows access to Buffers[Max], which is past the end of the Buffers
array. This can lead to crashes when that memory is not mapped. Fix this
by testing Offset != Max first.

@llvmbot
Copy link
Member

llvmbot commented May 3, 2024

@llvm/pr-subscribers-xray

Author: Ricky Zhou (rickyz)

Changes

Before this change, the FDR BufferQueue iterator could access memory out
of due to checks of the form !Buffers[Offset].Used && Offset != Max,
which can potentially access Buffers[Max], which is past the end of
the Buffers. Fix this by testing Offset != Max first.


Full diff: https://github.com/llvm/llvm-project/pull/90940.diff

1 Files Affected:

  • (modified) compiler-rt/lib/xray/xray_buffer_queue.h (+2-2)
diff --git a/compiler-rt/lib/xray/xray_buffer_queue.h b/compiler-rt/lib/xray/xray_buffer_queue.h
index e1739d050f3d0d..8d33f73576b5e2 100644
--- a/compiler-rt/lib/xray/xray_buffer_queue.h
+++ b/compiler-rt/lib/xray/xray_buffer_queue.h
@@ -87,7 +87,7 @@ class BufferQueue {
       DCHECK_NE(Offset, Max);
       do {
         ++Offset;
-      } while (!Buffers[Offset].Used && Offset != Max);
+      } while (Offset != Max && !Buffers[Offset].Used);
       return *this;
     }
 
@@ -107,7 +107,7 @@ class BufferQueue {
           Max(M) {
       // We want to advance to the first Offset where the 'Used' property is
       // true, or to the end of the list/queue.
-      while (!Buffers[Offset].Used && Offset != Max) {
+      while (Offset != Max && !Buffers[Offset].Used) {
         ++Offset;
       }
     }

@rickyz rickyz force-pushed the xray_fdr_iterator_oob branch from ea7b6c3 to fa41e53 Compare May 3, 2024 06:08
Before this change, the FDR BufferQueue iterator could access oob memory
due to checks of the form `!Buffers[Offset].Used && Offset != Max`. This
allows access to `Buffers[Max]`, which is past the end of the `Buffers`
array. This can lead to crashes when that memory is not mapped. Fix this
by testing `Offset != Max` first.
@rickyz rickyz force-pushed the xray_fdr_iterator_oob branch from fa41e53 to c267705 Compare May 3, 2024 06:20
@rickyz rickyz changed the title [xray] Fix oob memory access in FDR BufferQueue iterator. [XRay][compiler-rt] Fix oob memory access in FDR BufferQueue iterator. May 10, 2024
@MaskRay MaskRay changed the title [XRay][compiler-rt] Fix oob memory access in FDR BufferQueue iterator. [XRay][compiler-rt] Fix oob memory access in FDR BufferQueue iterator May 27, 2024
@MaskRay MaskRay merged commit 21a39df into llvm:main May 27, 2024
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants