You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 27 Next »

If an exception is thrown while logging is in progress, data may not be logged unless special care is taken. This can lead to a multitude of vulnerabilities, such as denial of service or vulnerabilities that allow the attacker to conceal critical security exceptions by preventing them from being logged.

Noncompliant Code Example

This noncompliant code example uses statements that can throw exceptions when logging is in process. It attempts to log a security exception generated within main, however, it fails to do so if the log file is renamed or a crafty attacker tampers with its name or maliciously deletes it.

public class ExceptionLog {
  private String logMessage;

  public static void main(String[] args) {
    ExceptionLog log = new ExceptionLog();
    try {
      //some security exception occurs here
      throw new SecurityException();	
    }catch(SecurityException se) {
      log.logMessage("Security Exception has occurred!");
      log.writeLog();
    } 
  }
    
  public void logMessage(String message) {
    logMessage = message;
  }
    
  public void writeLog() {
    FileWriter fw=null;
    BufferedWriter bw=null;
    try {
      // This can throw an exception and prevent logging.
      fw = new FileWriter("log_file.txt", true);
      bw = new BufferedWriter(fw);
      bw.write(logMessage + "\n");
    } catch (FileNotFoundException fnf){ 
        logMessage("File Not Found Exception");
    } catch(IOException ie) { 
        logMessage("IO Exception");             
        // No log message is written to the file in the event of an exception.
    } finally {
     	try {
      	  fw.close();
          bw.close();
        } catch(IOException ioe) { /* ignores */}	
    }
  }
}

Compliant Solution

This compliant solution executes several statements that can possibly throw exceptions prior to performing any security critical operations and allows writeLog() to throw an exception back to the caller in the event of a problem writing to the log file. As a result, exceptions do not result in the silent failure to log a message or a different message than intended being logged. While this is a stringent requirement, it is necessary in cases where an exception can be deliberately thrown to conceal an attacker's tracks. The logging mechanism must be robust and should be able to detect and handle such phenomena.

public class ExceptionLog {
    
    private static String logMessage;
    
    public static void main(String[] args) {
        ExceptionLog log = new ExceptionLog();
        FileWriter fw=null;
        BufferedWriter bw=null;
        try {
            fw = new FileWriter("log_file.txt", true);  
            // This can throw an exception, but the logging of messages is
            // not silently prevented.
            bw = new BufferedWriter(fw);
        }
        catch (IOException e) { 
            // The logging example cannot recover from failure to open
            // the log file.
            throw new RuntimeException(e);
        }
        
        //some security exception occurs here
        try {
            log.logMessage("Security Exception has occurred!");
            log.writeLog(bw);
        }
        catch (IOException e) {
            // Logging of the security exception does not silently fail.
            System.err.println("Logging error failed.");
        }
        
        try {
            bw.close();
        }
        catch (IOException e) {
            System.err.println("Closing log file failed.");
        }
    }
    
    public static void logMessage(String message) {
        logMessage = message;
    }
    
    public void writeLog(BufferedWriter bw) throws IOException {
        bw.write(logMessage + "\n");
        System.err.println(logMessage);    
    }
}

A slightly more expensive alternative is to support recursive logging.

Note that this recommendation does not prevent a program from reopening a closed log file after it realizes that important data must be captured. While an IOException is possible, there is little that can be done when writing the data to the log file is itself under question.

Risk Assessment

If an exception is thrown while data is being logged then data may be lost or problems may be concealed.

Rule

Severity

Likelihood

Remediation Cost

Priority

Level

EXC02- J

medium

likely

high

P6

L2

Automated Detection

TODO

Related Vulnerabilities

Search for vulnerabilities resulting from the violation of this rule on the CERT website.

References

[[API 06]] Class Logger
[[JLS 05]] Chapter 11, Exceptions


EXC01-J. Do not allow exceptions to transmit sensitive information      12. Exceptional Behavior (EXC)      EXC03-J. Try to recover gracefully from system errors

  • No labels