Skip to content

Avoid passing null pointer from empty span to CreateSymmetricKey#120392

Closed
xtqqczze wants to merge 1 commit intodotnet:mainfrom
xtqqczze:nonnull-cng
Closed

Avoid passing null pointer from empty span to CreateSymmetricKey#120392
xtqqczze wants to merge 1 commit intodotnet:mainfrom
xtqqczze:nonnull-cng

Conversation

@xtqqczze
Copy link
Contributor

@xtqqczze xtqqczze commented Oct 3, 2025

The existing code has an implicit call to symmetricKeyMaterial.GetPinnableReference(), which returns a null reference for _length == 0.

@dotnet-policy-service dotnet-policy-service bot added the community-contribution Indicates that the PR has been added by a community member label Oct 3, 2025
@dotnet-policy-service
Copy link
Contributor

Tagging subscribers to this area: @dotnet/area-system-security, @bartonjs, @vcsjones
See info in area-owners.md if you want to be subscribed.

@xtqqczze xtqqczze marked this pull request as ready for review October 3, 2025 21:51
@vcsjones
Copy link
Member

vcsjones commented Oct 3, 2025

What exactly is this addressing? Is there a missing test?

@vcsjones
Copy link
Member

vcsjones commented Oct 3, 2025

The code above this should guarantee that the Span is never nullptr

else
{
// CNG requires a non-null pointer even when the length is zero.
symmetricKeyMaterial = [0];
symmetricKeyMaterialLength = 0;
}

@xtqqczze
Copy link
Contributor Author

xtqqczze commented Oct 3, 2025

The code above this should guarantee that the Span is never nullptr

We then call GetPinnableReference (implicitly within the fixed statement) which returns NullRef<T>() when the span length is zero:

public ref readonly T GetPinnableReference()
{
// Ensure that the native code has just one forward branch that is predicted-not-taken.
ref T ret = ref Unsafe.NullRef<T>();
if (_length != 0) ret = ref _reference;
return ref ret;
}

@vcsjones
Copy link
Member

vcsjones commented Oct 3, 2025

hen the span length is zero:

The span length is never zero though. On line 85 and 35 the span is set to [0] if it is empty and the length is tracked separately. It is guaranteed to not be empty when it is pinned.

@xtqqczze
Copy link
Contributor Author

xtqqczze commented Oct 3, 2025

hen the span length is zero:

The span length is never zero though. On line 85 and 35 the span is set to [0] if it is empty and the length is tracked separately. It is guaranteed to not be empty when it is pinned.

Yes you are right, in the [0] case the length is actually 1, it is symmetricKeyMaterialLength that is 0.🤦‍♂️

@xtqqczze xtqqczze closed this Oct 3, 2025
@github-actions github-actions bot locked and limited conversation to collaborators Nov 3, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area-System.Security community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants