Skip to content
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

New rule S6640: Allowing unsafe code is security-sensitive #7290

Closed
egon-okerman-sonarsource opened this issue May 26, 2023 · 0 comments · Fixed by #7474
Closed

New rule S6640: Allowing unsafe code is security-sensitive #7290

egon-okerman-sonarsource opened this issue May 26, 2023 · 0 comments · Fixed by #7474
Assignees
Labels
Area: C# C# rules related issues. Area: Security Related to Vulnerability and Security Hotspot rules Type: New Rule Implementation for a rule that HAS been specified.
Milestone

Comments

@egon-okerman-sonarsource

Why

C# allows for unsafe code blocks, where it is possible to define pointer types, fixed buffers, to use manual memory allocation,... The CLR cannot verify the safety of this code, therefore it can in some circumstances lead to unexpected behavior and security risks.

Detection logic

All unsafe code blocks should be detected. This can either be a keyword in a method header, or a keyword for a block expression. Examples and more information can be found in the language reference.

Examples

Sensitive

private unsafe int subarraySum(int[] array, int start, int end)  // Sensitive
{
    var sum = 0;

    // Skip array bound checks for extra performance
    fixed (int* firstNumber = array)
    {
        for (int i = start; i < end; i++)
            sum += *(firstNumber + i);
    }

    return sum;
}

Compliant

private int subarraySum(int[] array, int start, int end)
{
    var sum = 0;

    Span<int> span = array.AsSpan();
    for (int i = start; i < end; i++)
        sum += span[i];

    return sum;
}

RSPEC

The relevant RSPEC PR can be found here. It also contains additional information regarding the issue message and highlighting.

@egon-okerman-sonarsource egon-okerman-sonarsource added Area: C# C# rules related issues. Area: Security Related to Vulnerability and Security Hotspot rules Type: New Rule Implementation for a rule that HAS been specified. labels May 26, 2023
egon-okerman-sonarsource added a commit to SonarSource/rspec that referenced this issue Jun 2, 2023
[Specification
ticket](https://sonarsource.atlassian.net/browse/APPSEC-729)
[AppSec PoC](SonarSource/appsec-poc#147)
[Implementation
ticket](SonarSource/sonar-dotnet#7290)

[RSPEC
Preview](https://sonarsource.github.io/rspec/#/rspec/S6640/csharp)

---------

Co-authored-by: egon-okerman-sonarsource <[email protected]>
Co-authored-by: Egon Okerman <[email protected]>
@martin-strecker-sonarsource martin-strecker-sonarsource added this to the 9.5 milestone Jun 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: C# C# rules related issues. Area: Security Related to Vulnerability and Security Hotspot rules Type: New Rule Implementation for a rule that HAS been specified.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants