Skip to content
Memory corruption Advisory Advanced CVE-2026-87528

Type confusion in Rust (CVE-2026-87528)

Crubit's Rust-to-C++ bindings declare thunks that return C++ class types by value, but under the Microsoft C++ ABI any class with a user-provided constructor is returned through a hidden out-pointer. The C++ caller therefore reads uninitialised stack memory while Rust writes the result into registers, silently corrupting every returned value on Windows targets.

marcobartoli Follow

Originally disclosed on Chrome VRP

  • Fix shipped in 44 days

Summary

cc_bindings_from_rs declares thunks that return Crubit C++ class types directly, e.g.:

C++
extern "C" rs_std::StrRef __crubit_thunk_foo_uas_ustr();

Under the Microsoft C++ ABI, the one used by clang-cl for Chromium on Windows, any class with a user-provided constructor is returned through a hidden out-pointer (x8 on AArch64, rcx on x86-64), even when it is trivially copyable and of register size.

[[clang::trivial_abi]] is ignored when Microsoft C++ ABI is used, as it is only used in case of Itanium ABI.

Rust's extern "C" returns those same values in registers. The result is a silent ABI mismatch: the C++ caller passes a buffer pointer and reads back uninitialized stack memory while Rust writes the result registers. No diagnostic fires, because generated headers #pragma clang diagnostic ignored "-Wreturn-type-c-linkage".

Every rs_std vocabulary type and every Crubit-generated class has user-provided constructors, so this is not a corner case.

Impact

Affects all *-pc-windows-msvc targets. The size of the value determines which ones diverge, which is why this typically surfaces on ARM64 first:

Return type Size rustc C++ (Crubit class) x86_64-msvc aarch64-msvc
&str, &[T] 16 registers indirect OK by coincidence (both indirect) BROKEN
char 4 i32 indirect BROKEN BROKEN
#[repr(transparent)] ADT 4/8/16 registers indirect BROKEN BROKEN

Non-MSVC targets (aarch64-unknown-linux-gnu, aarch64-apple-darwin, x86_64-unknown-linux-gnu) are unaffected, both sides return in registers.

Note that x86_64-pc-windows-msvc only appears healthy for &str: both sides independently choose sret for 16 bytes. Smaller values are broken there too.

Arguments are unaffected on every target. This is strictly a return-position bug.

Root cause

1. What actually triggers indirect return

Classification is by user-provided constructor, not by triviality or size. = default on first declaration is fine:

Class shape (identical 16-byte layout) aarch64-msvc
pure aggregate [2 x i64] (registers)
A() = default; / A(const A&) = default; / ~A() = default; [2 x i64]
user-provided ctor (A() noexcept : ... {}) sret
user-provided ctor + [[clang::trivial_abi]] sret

rs_std::StrRef, rs_std::SliceRef<T> and rs_std::char_ all have user-provided constructors (the consteval validating conversions, the UnsafePromiseUtf8 constructor, the dangling-pointer default constructor). Crubit-generated classes have them too (T(), T(UnsafeRelocateTag, T&&)).

2. Where the two sides diverge

is_c_abi_compatible_by_value (cc_bindings_from_rs/generate_bindings/query_compiler.rs:41) returns true for TyKind::Ref, TyKind::RawPtr, TyKind::Char, and #[repr(transparent)] TyKind::Adt. It is then used to pick both sides of the thunk signature:

  • generate_function_thunk.rs:211: the C++ declaration
  • generate_function_thunk.rs:620: the Rust implementation

The predicate is correct about value ABI compatibility, but it says nothing about whether the C++ type Crubit maps the value to uses the C return convention. On MSVC it does not.

Incidentally, the arm that looks like it should have caught this is unreachable:

Rust
// Slice references (`&[T]`, `&str`) are not guaranteed to be ABI-compatible when passed
// by-value.
ty::TyKind::Slice { .. } | ty::TyKind::Str => false,

&str is Ref(_, str, _), so it is matched by the Ref arm above and returns true. The intent to exclude fat references was already there.

Reproduction

No Windows machine or MSVC toolchain required, clang cross-targets and both compilers can emit LLVM IR.

C++
// t.cpp
using size_t = unsigned long long;
struct Pod { const char* ptr; size_t size; };
struct [[clang::trivial_abi]] StrRefLike {          // shape of rs_std::StrRef
  StrRefLike() noexcept : ptr(nullptr), size(0) {}  // user-provided
  StrRefLike(const StrRefLike&) = default;
  ~StrRefLike() = default;
  const char* ptr; size_t size;
};
extern "C" Pod        r_pod();
extern "C" StrRefLike r_strref();
Pod a() { return r_pod(); }  StrRefLike b() { return r_strref(); }
CONSOLE
$ clang++ --target=aarch64-pc-windows-msvc -std=c++20 -O1 -S -emit-llvm -o - t.cpp | grep '^declare'
declare dso_local [2 x i64] @r_pod()
declare dso_local void @r_strref(ptr inreg sret(%struct.StrRefLike) align 8)
Rust
// s.rs
#![crate_type = "lib"]
#[unsafe(no_mangle)] pub extern "C" fn t_str() -> &'static str { "foo" }
#[unsafe(no_mangle)] pub extern "C" fn t_char() -> char { 'a' }
CONSOLE
$ rustup target add aarch64-pc-windows-msvc
$ rustc --target=aarch64-pc-windows-msvc --emit=llvm-ir -O --crate-type=lib -o out.ll s.rs
$ grep '^define' out.ll
define [2 x i64] @t_str()
define noundef range(i32 0, 1114112) i32 @t_char()

Rust returns [2 x i64]; C++ expects sret. Swapping the triple for aarch64-unknown-linux-gnu makes both [2 x i64], which is why this is invisible on Linux CI.

To confirm against real generated bindings, run cc_bindings_from_rs over a crate exposing pub fn f() -> &'static str and compile the emitted header with --target=aarch64-pc-windows-msvc.

The original report as published on the Chromium issue tracker, by marcobartoli. Images and videos are our own copies; raw HTML is shown as text. View it on Chromium issue tracker.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

How it worked

Root cause

The compatibility check only considers whether the Rust value is ABI-compatible by value, ignoring that the C++ class Crubit maps it to has a user-provided constructor, which forces indirect return under the Microsoft C++ ABI.

Impact

On all Windows MSVC targets, returned values from Crubit-generated bindings could be read as uninitialised stack memory, with string and slice references only accidentally working on x86-64; arguments are unaffected.

What the attacker needed

Windows MSVC target; Crubit-generated bindings; Return value of class type

The fix

The compatibility predicate should also account for whether the mapped C++ type uses the C return convention, so thunks are declared with an indirect return where the Microsoft ABI requires it.

How it was found

  1. Compile a small C++ file with a class that has a user-provided constructor and a plain struct, both returned from extern C functions, targeting aarch64-pc-windows-msvc.
  2. Inspect the emitted LLVM IR to see the plain struct returned in registers and the class returned via a hidden sret pointer.
  3. Compile a Rust file returning a string reference and a char with extern C, targeting the same Windows target.
  4. Inspect the Rust LLVM IR to confirm both values are returned in registers.
  5. Compare the two sets of declarations to see the mismatch, and note that switching to a Linux target makes both sides agree.

Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

When two programs written in different languages call each other, they must agree on how a value is handed back. Here the Rust side puts the answer in a processor register, while the C++ side expects the caller to supply a memory buffer and looks there instead. The C++ code then reads whatever happened to be in that memory, so it gets garbage rather than the real value.

What is Memory corruption?

Native code reads or writes memory it should not: overflows, use-after-free, type confusion. The core of browser, kernel and parser bugs.

More from Google Chrome

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Memory corruption

The lesson behind this report.

Native code reads or writes memory it should not: overflows, use-after-free, type confusion. The core of browser, kernel and parser bugs.