1627
Example 4:
Under normal conditions the following C# code executes a database query, processes the results
returned by the database, and closes the allocated SqlConnection object. But if an exception
occurs while executing the SQL or processing the results, the SqlConnection object is not closed.
If this happens often enough, the database will run out of available cursors and not be able to
execute any more SQL queries.
Example Language: C# (Bad)
...
SqlConnection conn = new SqlConnection(connString);
SqlCommand cmd = new SqlCommand(queryString);
cmd.Connection = conn;
conn.Open();
SqlDataReader rdr = cmd.ExecuteReader();
HarvestResults(rdr);
conn.Connection.Close();
...
Example 5:
The following C function does not close the file handle it opens if an error occurs. If the process is
long-lived, the process can run out of file handles.
Example Language: C (Bad)
int decodeFile(char* fName) {
char buf[BUF_SZ];
FILE* f = fopen(fName, "r");
if (!f) {
printf("cannot open %s\n", fName);
return DECODE_FAIL;
}
else {
while (fgets(buf, BUF_SZ, f)) {
if (!checkChecksum(buf)) {
return DECODE_FAIL;
}
else {
decodeBlock(buf);
}
}
}
fclose(f);
return DECODE_SUCCESS;
}
Observed Examples
Reference Description
CVE-2007-0897 Chain: anti-virus product encounters a malformed file but returns from a
function without closing a file descriptor (CWE-775) leading to file descriptor
consumption (CWE-400) and failed scans.
https://www.cve.org/CVERecord?id=CVE-2007-0897
CVE-2001-0830 Sockets not properly closed when attacker repeatedly connects and
disconnects from server.
https://www.cve.org/CVERecord?id=CVE-2001-0830
CVE-1999-1127 Does not shut down named pipe connections if malformed data is sent.
https://www.cve.org/CVERecord?id=CVE-1999-1127
CVE-2009-2858 Chain: memory leak (CWE-404) leads to resource exhaustion.
https://www.cve.org/CVERecord?id=CVE-2009-2858
CWE Version 4.14
CWE-772: Missing Release of Resource after Effective Lifetime
CWE-772: Missing Release of Resource after Effective Lifetime