Use of Unsafe Memory Functions

C or C++ code uses functions such as strcpy, strcat, sprintf or gets that do not check buffer sizes, risking buffer overflows when input is longer than expected.

SeverityMedium
CVSS 3.16.5 CVSS:3.1/AV:N/AC:H/PR:N/UI:N/S:U/C:N/I:L/A:H Open in the calculator
CVSS 4.08.3 CVSS:4.0/AV:N/AC:L/AT:P/PR:N/UI:N/VC:N/VI:L/VA:H/SC:N/SI:N/SA:N Open in the calculator
CWECWE-676: Use of Potentially Dangerous Function
OWASP Top 10A06:2025 Insecure Design
MITRE ATT&CKT1190 Exploit Public-Facing Application, T1203 Exploitation for Client Execution, T1068 Exploitation for Privilege Escalation
CategoryInput Validation

Description

The application's C or C++ code, or native code within another application such as a mobile or desktop client, uses library functions that write to memory without checking the size of the destination buffer. The classic examples are gets(), which cannot be used safely at all and has been removed from the C standard, and strcpy(), strcat(), sprintf(), vsprintf() and scanf() with an unbounded %s conversion. Functions such as memcpy() and strncpy() are unsafe when the length passed to them comes from the input or is miscalculated, and strncpy() also leaves the destination without a terminating null character when the source is too long.

Whether a particular call is exploitable depends on whether data from outside the program can reach it and exceed the buffer, and the risk is highest in code that parses network data or files from untrusted sources. Where input an attacker controls overflows the buffer, memory next to it is overwritten: the most likely result is a crash and denial of service, but an overflow can also corrupt data that controls the program's behaviour and, in the worse cases, let an attacker run code with the privileges of the process, for example as a network service or with elevated rights on the local system. Modern compiler and operating system protections, such as stack canaries, address space layout randomisation and non-executable memory, make code execution considerably harder but do not prevent it in every case.

The finding covers both calls where an overflow has been shown to be reachable and calls whose safety rests on assumptions elsewhere in the code, which later changes can break. Unsafe functions are found by source code review and static analysis, such as compiler warnings, clang-tidy, Flawfinder or Cppcheck, which flag each use, and in compiled binaries by listing the functions they import. Each occurrence is then reviewed to judge whether its input is bounded, and fuzzing can confirm whether an overflow can be triggered.

Remediation

Replace unbounded memory operations with bounded ones, and check every length:

  • Remove all uses of gets(), replacing it with fgets().
  • Replace sprintf() and vsprintf() with snprintf() and vsnprintf(), checking the return value for truncation, and give every %s conversion in the scanf() family a field width.
  • Replace strcpy() and strcat() with bounded operations that always terminate the string, such as snprintf(), or strlcpy() and strlcat() where the platform provides them, and validate the length of input before copying it.
  • Check that the length passed to memcpy() and similar functions can never exceed the destination size, and check size calculations for integer overflow.
  • In C++, use std::string, std::vector and other standard containers instead of raw character arrays, and consider writing new components that parse untrusted input in a memory-safe language.

Build with the compiler's hardening options, such as stack protection, _FORTIFY_SOURCE, position-independent executables and full RELRO, and enable warnings and static analysis rules that flag banned functions in the build. These protections reduce the chance of exploitation while code is being fixed, but do not replace the fix. Confirm the fix by rerunning static analysis and fuzzing the affected input paths.

References


Add This Finding to Your Library

In Pental this write-up is in Settings, Library. Add to my library copies it into your own, rated in your firm's CVSS version, ready to put in your own words.

Free trial  ·  No card required

Only need the reporting? Our reporting tool is free and open source, with the same library.


More Source Code and Supply Chain Findings

Every Source Code and Supply Chain finding