logo

Database

C Sharp Create Span Unvalidated Length

Description

Detects potentially dangerous usage of MemoryMarshal.CreateSpan and MemoryMarshal.CreateReadOnlySpan methods in C# code. These methods can lead to buffer overflows or unauthorized memory access if used with unvalidated length parameters, potentially exposing sensitive data or causing application crashes.

Weakness:

111 - Out-of-bounds read

Category: Data Manipulation

Detection Strategy

    Check for direct calls to MemoryMarshal.CreateSpan or MemoryMarshal.CreateReadOnlySpan methods in C# code

    Report any usage of these methods as potentially vulnerable since they could allow access to arbitrary memory locations

    Each direct invocation of these memory manipulation methods triggers a security alert due to the risk of buffer overflows

Vulnerable code example

using System;
using System.Runtime.InteropServices;

public class UnsafeMemoryAccess
{
    private byte _data;

    public Span<byte> GetUnsafeSpan()...

✅ Secure code example

using System;

public class SafeMemoryAccess
{
    private byte[] _data = new byte[100]; // Use byte array instead of single byte

    public Span<byte> GetSafeSpan()
    {...