Skip to content

[release/10.0] [mono] Fix SizeConst array marshalling overflows - #137215

Open
github-actions[bot] wants to merge 1 commit into
release/10.0from
backport/pr-137082-to-release/10.0
Open

github-actions[bot] wants to merge 1 commit into
release/10.0from
backport/pr-137082-to-release/10.0

Conversation

@github-actions

@github-actions github-actions Bot commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

Backport of #137082 to release/10.0

/cc @BrzVlad

Customer Impact

  • Customer reported
  • Found internally

On all mono targets, when using MarshalAsAttribute with the ByValArray option, we would fail to check at runtime whether the array to marshal is matching the SizeConst from the attribute. If the application has such a bug in the marshalling code, it could accidentally do unsafe buffer overflow accesses, rather than throwing an exception.

Regression

  • Yes
  • No

Testing

Reproduced the issue locally with a simple test.

Risk

Low. Fix is small and targeted, we add a check for the array size which throws an ArgumentException, same way as coreclr does it.

For example a type declared as

```
[StructLayout(LayoutKind.Sequential)]
internal struct MyStruct
{
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 20)]
    public int[] Values;
}
```

can be marshalled with the `Marshal.StructureToPtr(str, buffer, false);`
to buffer. Mono had problems if the struct to marshal contained an array
that didn't match the `SizeConst` attribute argument. For blittable
types, like in the above definition, if the array was smaller, then we
would copy more bytes than the array holds. For non-blittable types, if
the array was larger, we would write more bytes, overflowing the
destination buffer. The fix throws exception at runtime if the array
doesn't contain enough elements and limits the copying to the
`SizeConst` amount, preventing both source read and destination write
overflows.
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).
12 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @dotnet/interop-contrib
See info in area-owners.md if you want to be subscribed.

@BrzVlad BrzVlad added the Servicing-consider Issue for next servicing release review label Nov 9, 2026
@ViveliDuCh

Copy link
Copy Markdown
Member

Hi,

the code complete date for 10.0.16 (the December 2026 release) is Monday 16 November. Make sure to merge this PR on that date at the latest, or it won't make it into that release.

As a reminder, if this is a product change, you also need Tactics approval before merging this PR (test-only or infra-only changes don't require Tactics approval).

Happy to help with the merge if needed!

@BrzVlad BrzVlad added Servicing-approved Approved for servicing release and removed Servicing-consider Issue for next servicing release review labels Nov 20, 2026

This branch has not been deployed

No deployments
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.

2 participants