Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

This code example is noncompliant because the character sequence c_str will not be null-terminated when passed as an argument to printf(). (see See STR11-C. Do not specify the bound of a character array initialized with a string literal on how to properly initialize character arrays.).

Code Block
bgColor#FFcccc
langc
#include <stdio.h>
 
void func(void) {
  char c_str[3] = "abc";
  printf("%s\n", c_str);
}

...

Code Block
bgColor#FFcccc
langc
#include <string.h>
 
enum { STR_SIZE = 32 };
 
size_t func(const char *source) {
  char c_str[STR_SIZE];
  size_t ret = 0;

  if (source) {
    c_str[sizeof(c_str) - 1] = '\0';
    strncpy(c_str, source, sizeof(c_str));
  return  ret = strlen(c_str);
  } else {
    /* Handle null pointer */
  }
  return ret;
}

Compliant Solution (Truncation)

...

Code Block
bgColor#ccccff
langc
#include <string.h>
 
enum { STR_SIZE = 32 };
 
size_t func(const char *source) {
  char c_str[STR_SIZE];
  size_t ret = 0;

  if (source) {
    strncpy(c_str, source, sizeof(c_str) - 1);
    c_str[sizeof(c_str) - 1] = '\0';
    ret return= strlen(c_str);
  } else {
    /* Handle null pointer */
  }
  return ret;
}

Compliant Solution (Truncation, strncpy_s())

...

Code Block
bgColor#ccccff
langc
#define __STDC_WANT_LIB_EXT1__ 1
#include <string.h>

enum { STR_SIZE = 32 };

size_t func(const char *source) {
  char ac_str[STR_SIZE];
  size_t ret = 0;

  if (source) {
    errno_t err = strncpy_s(
      ac_str, sizeof(ac_str), source, strlenstrnlen(source, sizeof(c_str))
    );
    if (err != 0) {
      /* Handle error */
    } else {
      ret = strnlen(c_str, sizeof(c_str));
    }
  } else {
     /* Handle null pointer */
  }
  return strlen_s(s, sizeof(a))ret;
}

Compliant Solution (Copy without Truncation)

...

Code Block
bgColor#ccccff
langc
#include <string.h>
 
enum { STR_SIZE = 32 };
 
size_t func(const char *source) {
  char c_str[STR_SIZE];
  size_t ret = 0;

  if (source) {
    if (strlenstrnlen(source, sizeof(c_str)) < sizeof(c_str)) {
      strcpy(c_str, source);
      ret = strlen(c_str);
    } else {
      /* Handle string-too-large */
    }
  } else {
    /* Handle null pointer */
  }
  return strlen(c_str);
}ret;
}

Note that this code is not bulletproof. It gracefully handles the case where source  is NULL, when it is a valid string, and when source is not null-terminated, but at least the first 32 bytes are valid. However, in cases where source is not NULL, but points to invalid memory, or any of the first 32 bytes are invalid memory, the first call to strnlen() will access this invalid memory, and the resulting behavior is undefined. Unfortunately, standard C provides no way to prevent or even detect this condition without some external knowledge about the memory source points to.

Risk Assessment

Failure to properly null-terminate a character sequence that is passed to a library function that expects a string can result in buffer overflows and the execution of arbitrary code with the permissions of the vulnerable process. Null-termination errors can also result in unintended information disclosure.

Rule

Severity

Likelihood

Remediation Cost

Priority

Level

STR32-C

High

Probable

Medium

P12

L1

Automated Detection

Tool

Version

Checker

Description

Astrée
Include Page
Astrée_V
Astrée_V

Supported

Astrée supports the implementation of library stubs to fully verify this guideline.

Axivion Bauhaus Suite

Include Page
Axivion Bauhaus Suite_V
Axivion Bauhaus Suite_V

CertC-STR32Partially implemented: can detect some violation of the rule
CodeSonar
Include Page
CodeSonar_V
CodeSonar_V
MISC.MEM.NTERM.CSTRINGUnterminated C String
Compass/ROSE

 

 



Can detect some violations of this rule

Coverity
Include Page
Coverity
6.5
_V
Coverity_V
STRING_NULLFully
Implemented
implemented
Helix QAC

Include Page
Helix QAC_V
Helix QAC_V

DF2835, DF2836, DF2839


Klocwork
Include Page
Klocwork_V
Klocwork_V

NNTS

 

.MIGHT
NNTS.MUST
SV.STRBO.BOUND_COPY.UNTERM


LDRA tool suite
Include Page
LDRA_V
LDRA_V

404 S, 600 S

Partially implemented

Parasoft C/C++test
Include Page
Parasoft_V
Parasoft_V
CERT_C-STR32-a

Avoid overflow due to reading a not zero terminated string

Polyspace Bug Finder

Include Page
Polyspace Bug Finder_V
Polyspace Bug Finder_V

CERT C: Rule STR32-C


Checks for:

  • Invalid use of standard library string routine
  • Tainted NULL or non-null-terminated string

Rule partially covered.

PVS-Studio

Include Page
PVS-Studio_V
PVS-Studio_V

V692
TrustInSoft Analyzer

Include Page
TrustInSoft Analyzer_V
TrustInSoft Analyzer_V

match format and argumentsPartially verified.

Related Vulnerabilities

Search for vulnerabilities resulting from the violation of this rule on the CERT website.

Related Guidelines

Key here (explains table format and definitions)

Taxonomy

Taxonomy item

Relationship

ISO/IEC TR 24772:2013String Termination [CMJ]Prior to 2018-01-12: CERT: Unspecified Relationship
ISO/IEC TS 17961:2013Passing a non-null-terminated character sequence to a library function that expects a string [strmod]
MITRE CWE
Prior to 2018-01-12: CERT: Unspecified Relationship
CWE 2.11CWE-119, Improper Restriction of Operations
with
within the Bounds of a Memory Buffer2017-05-18: CERT: Rule subset of CWE
CWE 2.11CWE-123, Write-what-where Condition2017-06-12: CERT: Partial overlap
CWE 2.11CWE-125, Out-of-bounds Read2017-05-18: CERT: Rule subset of CWE
CWE 2.11CWE-170, Improper Null Termination2017-06-13: CERT: Exact

CERT-CWE Mapping Notes

Key here for mapping notes

CWE-119 and STR32-C

Independent( ARR30-C, ARR38-C, ARR32-C, INT30-C, INT31-C, EXP39-C, EXP33-C, FIO37-C) STR31-C = Subset( Union( ARR30-C, ARR38-C)) STR32-C = Subset( ARR38-C)

CWE-119 = Union( STR32-C, list) where list =


  • Out-of-bounds reads or writes that do not involve non-null-terminated byte strings.


CWE-125 and STR32-C

Independent( ARR30-C, ARR38-C, EXP39-C, INT30-C) STR31-C = Subset( Union( ARR30-C, ARR38-C)) STR32-C = Subset( ARR38-C)

CWE-125 = Union( STR32-C, list) where list =


  • Out-of-bounds reads that do not involve non-null-terminated byte strings.


CWE-123 and STR32-C

Independent(ARR30-C, ARR38-C) STR31-C = Subset( Union( ARR30-C, ARR38-C)) STR32-C = Subset( ARR38-C)

Intersection( CWE-123, STR32-C) =


  • Buffer overflow from passing a non-null-terminated byte string to a standard C library copying function that expects null termination, and that overwrites an (unrelated) pointer


STR32-C - CWE-123 =


  • Buffer overflow from passing a non-null-terminated byte string to a standard C library copying function that expects null termination, but it does not overwrite an (unrelated) pointer


CWE-123 – STR31-C =


  • Arbitrary writes that do not involve standard C library copying functions, such as strcpy()


Bibliography

[Seacord 2013] Chapter 2, "Strings" 
[Viega 2005]Section 5.2.14, "Miscalculated NULL Termination"

 


...

Image Modified Image Modified Image Modified