...
This noncompliant code example simply prints the exception's stack trace.:
Code Block | ||
---|---|---|
| ||
try { //... } catch (IOException ioe) { ioe.printStackTrace(); } |
Printing the exception's stack trace can be useful for debugging purposes, but the resulting program execution is equivalent to suppressing the exception. Printing the stack trace can also leak information about the structure and state of the process to an attacker . (See rule see ERR01-J. Do not allow exceptions to expose sensitive information for more information). ) Note that even though this noncompliant code example reacts to the exception by printing out a stack trace, it then proceeds as though the exception were not thrown. That is, the behavior of the application is unaffected by the exception being thrown , except that any expressions or statements that occur in the try
block after the point from which the exception is thrown are not evaluated.
...
This compliant solution handles a FileNotFoundException
by requesting that the user specify another file name.:
Code Block | ||
---|---|---|
| ||
volatile boolean validFlag = false; do { try { // If requested file does not exist, throws FileNotFoundException // If requested file exists, sets validFlag to true validFlag = true; } catch (FileNotFoundException e) { // Ask the user for a different file name } } while (validFlag != true); // Use the file |
To comply with rule ERR01-J. Do not allow exceptions to expose sensitive information, the user should only be allowed to access files in a user-specific directory. This prevents any other IOException
that escapes the loop from leaking sensitive file system information.
...
Code Block | ||
---|---|---|
| ||
public interface Reporter { public void report(Throwable t); } public class ExceptionReporter { // Exception reporter that prints the exception // to the console (used as default) private static final Reporter PrintException = new Reporter() { public void report(Throwable t) { System.err.println(t.toString()); } }; // Stores the default reporter. // The default reporter can be changed by the user. private static Reporter Default = PrintException; // Helps change the default reporter back to // PrintException in the future public static Reporter getPrintException() { return PrintException; } public static Reporter getExceptionReporter() { return Default; } // May throw a SecurityException (which is unchecked) public static void setExceptionReporter(Reporter reporter) { // Custom permission ExceptionReporterPermission perm = new ExceptionReporterPermission("exc.reporter"); SecurityManager sm = System.getSecurityManager(); if (sm != null) { // Check whether the caller has appropriate permissions sm.checkPermission(perm); } // Change the default exception reporter Default = reporter; } } |
...
Code Block | ||
---|---|---|
| ||
ExceptionReporter.setExceptionReporter(new ExceptionReporter() { public void report(Throwable exception) { JOptionPane.showMessageDialog(frame, exception.toString, exception.getClass().getName(), JOptionPane.ERROR_MESSAGE); }}); |
Compliant Solution (Subclass Exception Reporter and Filter-Sensitive Exceptions)
Sometimes exceptions must be hidden from the user for security reasons (see rule ERR01-J. Do not allow exceptions to expose sensitive information). In such cases, one acceptable approach is to subclass the ExceptionReporter
class and add a filter()
method in addition to overriding the default report()
method.
...
The report()
method accepts a Throwable
instance and consequently handles all errors, checked exceptions, and unchecked exceptions. The filtering mechanism is based on a whitelisting approach wherein only non-sensitive nonsensitive exceptions are propagated to the user. Exceptions that are forbidden to appear in a log file can be filtered in the same fashion (see rule FIO13-J. Do not log sensitive information outside a trust boundary). This approach provides the benefits of exception chaining by reporting exceptions tailored to the abstraction while also logging the low-level cause for future failure analysis [Bloch 2008].
...
If a thread is interrupted while sleeping or waiting, it causes a java.lang.InterruptedException
to be thrown. However, the run()
method of interface Runnable
cannot throw a checked exception and must handle InterruptedException
. This noncompliant code example catches and suppresses InterruptedException
.:
Code Block | ||
---|---|---|
| ||
class Foo implements Runnable { public void run() { try { Thread.sleep(1000); } catch (InterruptedException e) { // Ignore } } } |
This code prevents callers of the run()
method from determining that an interrupted exception occurred. Consequently, the caller methods such as Thread.start()
cannot act on the exception [Goetz 2006]. Likewise, if this code were called in its own thread, it would prevent the calling thread from knowing that the thread was interrupted.
...
This compliant solution catches the InterruptedException
and restores the interrupted status by calling the interrupt()
method on the current thread.:
Code Block | ||
---|---|---|
| ||
class Foo implements Runnable { public void run() { try { Thread.sleep(1000); } catch (InterruptedException e) { Thread.currentThread().interrupt(); // Reset interrupted status } } } |
...
ERR00-EX0: Exceptions that occur during the freeing of a resource may be suppressed in those cases where failure to free the resource cannot affect future program behavior. Examples of freeing resources include closing files, network sockets, shutting down threads, and so forth. Such resources are often freed in catch
or finally
blocks and never reused during subsequent execution. Consequently, the exception cannot influence future program behavior through any avenue other than resource exhaustion. When resource exhaustion is adequately handled, it is sufficient to sanitize and log the exception for future improvement; additional error handling is unnecessary in this case.
ERR00-EX1: When recovery from an exceptional condition is impossible at a particular abstraction level, code at that level must not handle that exceptional condition. In such cases, an appropriate exception must be thrown so that higher level code can catch the exceptional condition and can attempt recovery. The most common implementation for this case is to omit a catch
block and allow the exception to propagate normally:
...
Some APIs may limit the permissible exceptions thrown by particular methods. In such cases, it may be necessary to catch an exception and either wrap it in a permitted exception or translate it to one of the permitted exceptions.:
Code Block | ||
---|---|---|
| ||
public void myMethod() throws MyProgramException { // ... try { // Requested file does not exist // User is unable to supply the file name } catch (FileNotFoundException e) { throw new MyProgramException(e); } // ... } |
...
ERR00-EX2: An InterruptedException
may be caught and suppressed when extending class Thread
[Goetz 2006]. An interruption request may also be suppressed by code that implements a thread's interruption policy [Goetz 2006, p. 143].
Risk Assessment
...
Rule | Severity | Likelihood | Remediation Cost | Priority | Level |
---|---|---|---|---|---|
ERR00-J | lowLow | probableProbable | mediumMedium | P4 | L3 |
Automated Detection
Detection of suppressed exceptions is straightforward. Sound determination of which specific cases represent violations of this rule and which represent permitted exceptions to the rule is infeasible. Heuristic approaches may be effective.
Tool | Version | Checker | Description |
---|---|---|---|
Coverity | 7.5 | MISSING_THROW | Implemented |
Related Vulnerabilities
AMQ-1272 describes a vulnerability in the ActiveMQ service. When ActiveMQ receives an invalid username and password from a Stomp client, a security exception is generated but is subsequently ignored, leaving the client connected with full and unrestricted access to ActiveMQ.
...
Bibliography
Item 62, "Document All Exceptions Thrown by Each Method" | |
Section 5.4, "Blocking and interruptible methodsInterruptible Methods" | |
...