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.
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()
{...Search for vulnerabilities in your apps for free with Fluid Attacks' automated security testing! Start your 21-day free trial and discover the benefits of the Continuous Hacking Essential plan. If you prefer the Advanced plan, which includes the expertise of Fluid Attacks' hacking team, fill out this contact form.