Subclause 7.21.9.3 of the C Standard [ISO/IEC 9899:2011] defines the following behavior for fsetpos():
The
fsetposfunction sets thembstate_tobject (if any) and file position indicator for the stream pointed to bystreamaccording to the value of the object pointed to bypos, which shall be a value obtained from an earlier successful call to thefgetposfunction on a stream associated with the same file.
Consequently, using any other values for pos results in undefined behavior.
Noncompliant Code Example
This noncompliant code example attempts to read three values from a file and then set the cursor position back to the beginning of the file and return to the caller:
#include <stdio.h>
#include <string.h>
enum { NO_FILE_POS_VALUES = 3 };
int opener(FILE *file) {
int rc;
fpos_t offset;
memset(&offset, 0, sizeof(offset));
if (file == NULL) { return -1; }
/* Read in data from file */
rc = fsetpos(file, &offset);
if (rc != 0 ) { return rc; }
return 0;
}
However, because only the return value of an fgetpos() call is a valid argument to fsetpos(), passing an fpos_t value that was created in any other way is undefined behavior.
Compliant Solution
In this compliant solution, the initial file position indicator is stored by first calling fgetpos(), which is used to restore the state to the beginning of the file in the later call to fsetpos():
#include <stdio.h>
#include <string.h>
enum { NO_FILE_POS_VALUES = 3 };
int opener(FILE *file) {
int rc;
fpos_t offset;
if (file == NULL) { return -1; }
rc = fgetpos(file, &offset);
if (rc != 0 ) { return rc; }
/* Read in data from file */
rc = fsetpos(file, &offset);
if (rc != 0 ) { return rc; }
return 0;
}
Risk Assessment
The misuse of fsetpos() can move a file stream read to a unintended location in the file. If this location held input from the user, the user would then gain control of the variables being read from the file.
Rule | Severity | Likelihood | Remediation Cost | Priority | Level |
|---|---|---|---|---|---|
FIO44-C | Medium | Unlikely | Medium | P4 | L3 |
Automated Detection
Tool | Version | Checker | Description |
|---|---|---|---|
|
| Can detect common violations of this rule. However, it cannot handle cases in which the value returned by | |
5.0 |
| Can detect violations of this rule with CERT C Rule Pack | |
| 9.7.1 | 82 D | Fully implemented |
Related Vulnerabilities
Search for vulnerabilities resulting from the violation of this rule on the CERT website.
Related Guidelines
| CERT C++ Secure Coding Standard | FIO44-CPP. Only use values for fsetpos() that are returned from fgetpos() |
| ISO/IEC TS 17961 | Using a value for fsetpos other than a value returned from fgetpos [xfilepos] |
Bibliography
| [ISO/IEC 9899:2011] | Subclause 7.21.9.3, "The fsetpos Function" |