Skip to content

C#: unmanaged-code queries flag runtime-provided UnsafeAccessor methods and calls #23370

Backlog

Description

@MartinHock

Query IDs

  • cs/unmanaged-code
  • cs/call-to-unmanaged-code

Observed behavior

In a .NET 11 build of the public CleanArchitecture fork (commit 2750ea8d7047e33feaad747773f0a8abb611cb4a), CodeQL CLI 2.27.6 with the csharp security-and-quality suite reports eight findings on Vogen 8.0.7 generated EF Core converters: four declarations of extern methods annotated with System.Runtime.CompilerServices.UnsafeAccessorAttribute, and four calls to them.

Run: https://github.com/MartinHock/CleanArchitecture/actions/runs/47557255428

The generated methods access managed private fields and managed deserialization methods. One representative declaration is:

[global::System.Runtime.CompilerServices.UnsafeAccessor(
  global::System.Runtime.CompilerServices.UnsafeAccessorKind.Field, Name = _value)]
static extern ref System.Int32 UnsafeValueField(ref Clean.Architecture.Core.ContributorAggregate.ContributorId @this);

The corresponding managed call is:

private static System.Int32 UnderlyingValue(Clean.Architecture.Core.ContributorAggregate.ContributorId i)
  => UnsafeValueField(ref i);

All 15 generated-code alert locations were inspected against locally regenerated sources from the same commit, using dotnet build Clean.Architecture.slnx -c Release -p:EmitCompilerGeneratedFiles=true.

Expected behavior

Do not classify runtime-provided UnsafeAccessor methods targeting managed members, or calls to them, as native unmanaged code merely because of the extern modifier. The runtime provides the implementation; this is not P/Invoke.

Microsoft documentation: https://learn.microsoft.com/dotnet/api/system.runtime.compilerservices.unsafeaccessorattribute

The current queries UnmanagedCodeCheck.ql and CallsUnmanagedCode.ql appear to rely on m.isExtern() without distinguishing UnsafeAccessor.

Scope

This is a quality-query false-positive candidate, not a vulnerability report. A separate minimal CodeQL database was not built; the observations come from the actual hosted scan and the corresponding regenerated C# files. No query suppression or local generator modification was applied.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions