All rules
CA2153Security Enabled by default: No

Avoid handling Corrupted State Exceptions

Avoid handling Corrupted State Exceptions

Microsoft docs

Description

CSE indicates that the state of a process has been corrupted and not caught by the system. In the corrupted state scenario, a general handler only catches the exception if you mark your method with the System.Runtime.ExceptionServices.HandleProcessCorruptedStateExceptionsAttribute attribute. By default, the Common Language Runtime (CLR) does not invoke catch handlers for CSEs.

The safest option is to allow the process to crash without catching these kinds of exceptions. Even logging code can allow attackers to exploit memory corruption bugs.

This warning triggers when catching CSEs with a general handler that catches all exceptions, for example, catch (System.Exception e) or catch with no exception parameter.

Cause

Corrupted State Exceptions (CSEs) indicate that memory corruption exists in your process. Catching these rather than allowing the process to crash can lead to security vulnerabilities if an attacker can place an exploit into the corrupted memory region.

How to fix violations

To resolve this warning, do one of the following:

  • Remove the System.Runtime.ExceptionServices.HandleProcessCorruptedStateExceptionsAttribute attribute. This reverts to the default runtime behavior where CSEs are not passed to catch handlers.
  • Remove the general catch handler in preference of handlers that catch specific exception types. This may include CSEs, assuming the handler code can safely handle them (rare).
  • Rethrow the CSE in the catch handler, which passes the exception to the caller and should result in ending the running process.

Example

[HandleProcessCorruptedStateExceptions]
// Method that handles CSE exceptions.
void TestMethod1()
{
    try
    {
        FileStream fileStream = new FileStream("name", FileMode.Create);
    }
    catch (Exception e)
    {
        // Handle exception.
    }
}

When to suppress

Do not suppress a warning from this rule.

Group results
0 yes 0 no
ConsensusNone (disabled)
Severity preference (yes voters)
Suggestion0
Warning0
Error0