The C99 \[[ISO/IEC 9899:1999|AA. C References#ISO/IEC 9899-1999]\] function {{ Wiki Markup strtok()
}} is a string tokenization function that takes three two arguments: an initial string to be parsed , and a const
-qualified character delimiter, and a pointer to a pointer to modify to return the resultdelimiter. It returns a pointer to the first character of a token or to a null pointer if there is no token.
The first time strtok()
is called, the string is parsed into tokens , and a character delimiter, and address of the variable in which to return the result are passed as arguments. The strtok()
function parses the string up to the first instance of the delimiter character, replaces the character in place with a null byte ('\0'
), and puts returns the address of the first character in the token to the passed-in variable. Subsequent calls to strtok()
begin parsing immediately after the most recently - placed null character.
Because strtok()
modifies its argumentthe initial string to be parsed, the string is subsequently unsafe and cannot be used in its original form. If you need to preserve the original string, copy it into a buffer and pass the address of the buffer to strtok()
instead of the original string.
...
In this example, the strtok()
function is used to parse the first argument into colon-delimited tokens; it outputs each word from the string on a new line. Assume that PATH
is "/usr/bin:/usr/sbin:/sbin"
.
Code Block | ||||
---|---|---|---|---|
| ||||
char *token;
char *path = getenv("PATH");
token = strtok(path, ":");
puts(token);
while (token = strtok(0, ":")) {
puts(token);
}
printf("PATH: %s\n", path);
/* PATH is now just "/usr/bin" */
|
After the loop ends, path
is modified as follows: "/usr/bin\0/bin\0/usr/sbin\0/sbin\0"
. This is an issue because the local path
variable becomes /usr/bin
and because the environment variable PATH
has been unintentionally changed, which can have unintended consequences. (see See ENV30-C. Do not modify the string returned by getenv()).object referenced by the return value of certain functions.)
Compliant Solution
In this compliant solution, the string being tokenized is copied into a temporary buffer which buffer that is not referenced after the calls call to strtok()
:
Code Block | ||||
---|---|---|---|---|
| ||||
char *token; const char *path = getenv("PATH"); /* PATH is something like "/usr/bin:/bin:/usr/sbin:/sbin" */ char *copy = (char *)malloc(strlen(path) + 1); if (copy == NULL) { /* handleHandle error */ } strcpy(copy, path); token = strtok(copy, ":"); puts(token); while (token = strtok(0, ":")) { puts(token); } free(copy); copy = NULL; printf("PATH: %s\n", path); /* PATH is still "/usr/bin:/bin:/usr/sbin:/sbin" */ |
Another possibility is to provide your own implementation of strtok()
that does not modify the initial arguments.
Risk Assessment
To quote the Linux The Linux Programmer's Manual (man) page on {{ Wiki Markup strtok(3)
}} \[ [Linux 07|AA. C References#Linux 07]\]2008] states:
Never use this function. This function modifies its first argument. The identity of the delimiting character is lost. This function cannot be used on constant strings.
Improper The improper use of strtok()
use is likely to result in truncated data, producing unexpected results later in program execution.
Recommendation | Severity | Likelihood | Remediation Cost | Priority | Level |
---|---|---|---|---|---|
STR06-C |
Medium |
Likely |
Medium | P12 | L1 |
Automated Detection
Tool |
---|
Fortify SCA Version 5.0 can detect violations of this recommendation.
...
Version | Checker | Description | |||||||
---|---|---|---|---|---|---|---|---|---|
CodeSonar |
| (customization) | Users who wish to avoid using strtok() entirely can add a custom check for all uses of strtok() . | ||||||
Compass/ROSE | |||||||||
Helix QAC |
| C5007 | |||||||
LDRA tool suite |
| 602 S | Enhanced Enforcement |
Related Vulnerabilities
Search for vulnerabilities resulting from the violation of this rule on the CERT website.
Other Languages
...
Related Guidelines
...
...
...
...
Wiki Markup |
---|
\[[ISO/IEC 9899:1999|AA. C References#ISO/IEC 9899-1999]\] Section 7.21.5.8, "The {{strtok}} function"
\[[Linux 07|AA. C References#Linux 07]\] [strtok(3)|http://www.kernel.org/doc/man-pages/online/pages/man3/strtok.3.html]
\[[MITRE 07|AA. C References#MITRE 07]\] [CWE ID 464|http://cwe.mitre.org/data/definitions/464.html], "Addition of Data Structure Sentinel" |
CWE-464, Addition of data structure sentinel |
Bibliography
...
07. Characters and Strings (STR) STR07-C. Use TR 24731 for remediation of existing string manipulation code