Events
Mar 17, 9 PM - Mar 21, 10 AM
Join the meetup series to build scalable AI solutions based on real-world use cases with fellow developers and experts.
Register nowThis browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Property | Value |
---|---|
Rule ID | CA2153 |
Title | Avoid handling Corrupted State Exceptions |
Category | Security |
Fix is breaking or non-breaking | Non-breaking |
Enabled by default in .NET 9 | No |
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.
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.
To resolve this warning, do one of the following:
Remove the HandleProcessCorruptedStateExceptionsAttribute attribute. This reverts to the default run-time 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.
Do not suppress a warning from this rule.
The following pseudo-code illustrates the pattern detected by this rule.
[HandleProcessCorruptedStateExceptions]
// Method that handles CSE exceptions.
void TestMethod1()
{
try
{
FileStream fileStream = new FileStream("name", FileMode.Create);
}
catch (Exception e)
{
// Handle exception.
}
}
Removing the HandleProcessCorruptedStateExceptionsAttribute attribute ensures that Corrupted State Exceptions are not handled by your method.
void TestMethod1()
{
try
{
FileStream fileStream = new FileStream("name", FileMode.Create);
}
catch (Exception e)
{
// Handle exception.
}
}
Remove the general catch handler and catch only specific exception types.
void TestMethod1()
{
try
{
FileStream fileStream = new FileStream("name", FileMode.Create);
}
catch (IOException e)
{
// Handle IOException.
}
catch (UnauthorizedAccessException e)
{
// Handle UnauthorizedAccessException.
}
}
Rethrow the exception.
[HandleProcessCorruptedStateExceptions]
void TestMethod1()
{
try
{
FileStream fileStream = new FileStream("name", FileMode.Create);
}
catch (Exception e)
{
// Rethrow the exception.
throw;
}
}
.NET feedback
.NET is an open source project. Select a link to provide feedback:
Events
Mar 17, 9 PM - Mar 21, 10 AM
Join the meetup series to build scalable AI solutions based on real-world use cases with fellow developers and experts.
Register now