CWE-772: Missing Release of Resource after Effective Lifetime

Export to Word

Description

The product does not release a resource after its effective lifetime has ended, i.e., after the resource is no longer needed.

Extended Description

When a resource is not released after use, it can allow attackers to cause a denial of service by causing the allocation of resources without triggering their release. Frequently-affected resources include memory, CPU, disk space, power or battery, etc.


ThreatScore

Threat Mapped score: 1.9

Industry: Finiancial

Threat priority: P3 - Important (Medium)


Observed Examples (CVEs)

Related Attack Patterns (CAPEC)


Attack TTPs

Modes of Introduction

Phase Note
Implementation N/A

Common Consequences

Potential Mitigations

Applicable Platforms


Demonstrative Examples

Intro: The following method never closes the new file handle. Given enough time, the Finalize() method for BufferReader should eventually call Close(), but there is no guarantee as to how long this action will take. In fact, there is no guarantee that Finalize() will ever be invoked. In a busy environment, the Operating System could use up all of the available file handles before the Close() function is called.

Body: The good code example simply adds an explicit call to the Close() function when the system is done using the file. Within a simple example such as this the problem is easy to see and fix. In a real system, the problem may be considerably more obscure.

private void processFile(string fName) { BufferReader fil = new BufferReader(new FileReader(fName)); String line; while ((line = fil.ReadLine()) != null) { processLine(line); } }

Intro: The following code attempts to open a new connection to a database, process the results returned by the database, and close the allocated SqlConnection object.

Body: The problem with the above code is that 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.

SqlConnection conn = new SqlConnection(connString); SqlCommand cmd = new SqlCommand(queryString); cmd.Connection = conn; conn.Open(); SqlDataReader rdr = cmd.ExecuteReader(); HarvestResults(rdr); conn.Connection.Close();

Intro: This code attempts to open a connection to a database and catches any exceptions that may occur.

Body: If an exception occurs after establishing the database connection and before the same connection closes, the pool of database connections may become exhausted. If the number of available connections is exceeded, other users cannot access this resource, effectively denying access to the application.

try { Connection con = DriverManager.getConnection(some_connection_string); } catch ( Exception e ) { log( e ); }

Intro: 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.

... SqlConnection conn = new SqlConnection(connString); SqlCommand cmd = new SqlCommand(queryString); cmd.Connection = conn; conn.Open(); SqlDataReader rdr = cmd.ExecuteReader(); HarvestResults(rdr); conn.Connection.Close(); ...

Intro: 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.

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; }

Notes

← Back to CWE list