Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

The static method java.security.AccessController class is part of Java's security mechanism; it is responsible for enforcing the applicable security policy. doPrivileged is used to affirm that the invoking method is taking This class's static doPrivileged() method executes a code block with a relaxed security policy. The doPrivileged() method stops permissions from being checked further down the call chain.

Consequently, any method that invokes doPrivileged() must assume responsibility for enforcing its own privileges and that the access permissions of its callers should be ignored. For example, an application may have permissions to operate on a sensitive file, however, a caller of the application may be allowed to operate with only the very basic permissions. Invoking doPrivileged() in this context allows the application operating with only basic permissions to use the sensitive file, for instance, when a user password change request requires an unprivileged application to use a more privileged application to set the new passwordsecurity on the code block supplied to doPrivileged(). Likewise, code in the doPrivileged() method must not leak sensitive information or capabilities.

For example, suppose that a web application must maintain a sensitive password file for a web service and also must run untrusted code. The application could then enforce a security policy preventing the majority of its own code—as well as all untrusted code—from accessing the sensitive file. Because it must also provide mechanisms for adding and changing passwords, it can call the doPrivileged() method to temporarily allow untrusted code to access the sensitive file. In this case, any privileged block must prevent all information about passwords from being accessible to untrusted code.

Noncompliant Code Example

In this noncompliant code example, the doPrivileged() method is called from the openPasswordFile() method. The openPasswordFile() method is privileged and returns a FileInputStream reference to its caller. This allows an untrusted caller to call openPasswordFile() directly and obtain a reference to for the sensitive password file because of the inherent privileges possessed by the corresponding code. Because the method is public, it could be invoked by an untrusted caller.

Code Block
bgColor#FFcccc

public class PasswordPasswordManager { 

  public static void changePassword(final String password_file) throws FileNotFoundException {
    FileInputStream fin;
    fin = openPasswordFile(password_file);

   }
	
 // publicTest staticold FileInputStreampassword openPasswordFile(Stringwith password_ in file) throwscontents; FileNotFoundExceptionchange {password,
    // Declarethen asclose finalthe andpassword assignfile
 before the}

 body ofpublic thestatic anonymous inner classFileInputStream openPasswordFile()
    // Array f[]throws isFileNotFoundException used{
 to maintain language semanticsfinal whileString usingpassword_file final= "password";
    final FileInputStream f[]fin = {null};
    //try Use{
 own privilege to open the sensitivefin password file
= AccessController.doPrivileged(
     AccessController.doPrivileged(   new PrivilegedActionPrivilegedExceptionAction<FileInputStream>() {
          public ObjectFileInputStream run() throws FileNotFoundException {
         
   // Sensitive action; can't be done outside tryprivileged {block
           f[0] FileInputStream in = new FileInputStream(password_file);	//Perform privileged action

           } catch(FileNotFoundException cnf) { 
return in;
          }
  // cannot recover if password});
 file is not found;} logcatch to(PrivilegedActionException filex) {
      Exception cause  }= x.getException();
      if (cause instanceof return null;FileNotFoundException) {
    //Still mandatory to return fromthrow run(FileNotFoundException) cause;
      } else {
    }    throw new Error("Unexpected exception type", cause); 
      return f[0];}
   //Returns a}
 reference to privileged objects (inappropriate)return fin;
  }	
}

Compliant Solution

In general, when any method containing the doPrivileged a privileged block exposes a field (such as a an object reference) beyond its own boundary, it becomes trivial for untrusted callers to exploit the program.

Compliant Solution

The This compliant solution mitigates the vulnerability by declaring openPasswordFile() method controls access to the sensitive password file and returns its reference. For this reason, it should not be directly invokable. Instead, the changePassword() method must be used to forward any requests to openPasswordFile(). This is because changePassword() does not return a reference to the sensitive file to any caller and processes the file internally. Observe that caller supplied (tainted) inputs are not used because the name of the password file is hard-codedbe private. Consequently, an untrusted caller can call changePassword() but cannot directly invoke the openPasswordFile() method.

Code Block
bgColor#ccccff

public class PasswordPasswordManager {
  privatepublic static void changePassword() throws FileNotFoundException {
    // Use...
 own privilege}

 to openprivate thestatic sensitiveFileInputStream passwordopenPasswordFile() file
    final Stringthrows password_file = "password"; FileNotFoundException {
    // ...
  }
}

Compliant Solution (Hiding Exceptions)

The previous noncompliant code example and the previous compliant solution throw a FileNotFoundException when the password file is missing. If the existence of the password file is itself considered sensitive information, this exception also must be prevented from leaking outside the trusted code.

This compliant solution suppresses the exception, leaving the array to contain a single null value to indicate that the file does not exist. It uses the simpler PrivilegedAction class rather than PrivilegedExceptionAction to prevent exceptions from propagating out of the doPrivileged() block. The Void return type is recommended for privileged actions that do not return any value.

Code Block
bgColor#ccccff
class PasswordManager {

  public static void changePasswordfinal FileInputStream f[] = {null};
    AccessController.doPrivileged(new PrivilegedAction() {
      public Object run() {
        try {
          f[0]FileInputStream fin = openPasswordFile(password_file);  // call the privileged method here
    if (fin   } catch(FileNotFoundException cnf== null) { 
          // cannotNo recover if password file is not found; log to file 
 handle error
       }

    // Test old password returnwith null;
password in     }
    });
    //Perform other operations such as old password verificationfile contents; change password
  }	

  private static FileInputStream openPasswordFile(String password_file) throws FileNotFoundException {
    final FileInputStreamString fpassword_file = new FileInputStream(password_file)"password";
    //final Perform read/write operations on password fileFileInputStream fin[] = { null };
    return f;
  }
}

Compliant Solution

The previous compliant solution logs the exception instead of revealing sensitive information (See EXC06-J. Do not allow exceptions to transmit sensitive information) and as a result. Sometimes no sensitive information can be revealed by any of the possible exceptions. In such cases, an equivalent mechanism that allows exceptions to be wrapped can be used. This allows the caller to obtain better diagnostic information. For example, if an applet doesn't have access to read system files that contain fonts, it can accomplish the task from a privileged block without revealing any sensitive information. In fact, if non-sensitive exceptions provide more information, the client can deduce the symptoms of a read failure more easily.

Code Block
bgColor#ccccff

public static void readFont() throws FileNotFoundException {
  // Use own privilege to open the font file
  final String font_file = "fontfile";
AccessController.doPrivileged(new PrivilegedAction<Void>() {
        public Void run() {
          try {
    final InputStream in =
    AccessController.doPrivileged(new PrivilegedExceptionAction<InputStream>() {
				  
     public InputStream run() throws FileNotFoundException {
 // Sensitive action; can't be done outside
            // doPrivileged() return openFontFile(font_file); //call the privileged method here
block
           }
 fin[0] = new }FileInputStream(password_file);
    // Perform other operations 
  } catch (PrivilegedActionExceptionFileNotFoundException excx) {				

           Exception // causeReport = exc.getException();to handler
      if (cause instanceof FileNotFoundException) {}
          throw (FileNotFoundException)causereturn null;
        }
 else  { throw});
 new Error("Unexpected exception type", cause); }return fin[0];
    }			
}

In summary, if the code can throw a checked exception without leaking sensitive information, prefer the form of doPrivileged method that takes a PrivilegedExceptionAction instead of a PrivilegedAction.

Risk Assessment

Returning references to sensitive resources from within a doPrivileged() block can break encapsulation and confinement and can leak capabilities. Any caller who can invoke the privileged code directly and obtain a reference to a sensitive resource or field can maliciously modify its elements.

Rule

Severity

Likelihood

Remediation Cost

Priority

Level

SEC02SEC00-J

medium Medium

likely Likely

high High

P6

L2

Automated Detection

TODO

Related Vulnerabilities

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

References

Wiki Markup
\[[API 06|AA. Java References#API 06]\] [method doPrivileged()|http://java.sun.com/javase/6/docs/api/java/security/AccessController.html#doPrivileged(java.security.PrivilegedAction)]
\[[Gong 03|AA. Java References#Gong 03]\] Sections 6.4, AccessController and 9.5 Privileged Code
\[[SCG 07|AA. Java References#SCG 07]\] Guideline 6-1 Safely invoke java.security.AccessController.doPrivileged
\[[MITRE 09|AA. Java References#MITRE 09]\] [CWE ID 266|http://cwe.mitre.org/data/definitions/266.html] "Incorrect Privilege Assignment", [CWE ID 272|http://cwe.mitre.org/data/definitions/272.html] "Least Privilege Violation"

Identifying sensitive information requires assistance from the programmer; fully automated identification of sensitive information is beyond the current state of the art.

Assuming user-provided tagging of sensitive information, escape analysis could be performed on the doPrivileged() blocks to prove that nothing sensitive leaks out from them. Methods similar to those used in thread-role analysis could be used to identify the methods that must, or must not, be called from doPrivileged() blocks.

Related Guidelines

MITRE CWE

CWE-266, Incorrect Privilege Assignment
CWE-272, Least Privilege Violation

Secure Coding Guidelines for Java SE, Version 5.0

Guideline 9-3 / ACCESS-3: Safely invoke java.security.AccessController.doPrivileged

Android Implementation Details

The java.security package exists on Android for compatibility purposes only, and it should not be used.

Bibliography

[API 2014]

Method doPrivileged()

[Gong 2003]

Section 6.4, "AccessController"
Section 9.5, "Privileged Code"

 

...

Image Added Image Added Image AddedSEC18-J. Define wrappers around native methods      02. Platform Security (SEC)      SEC17-J. Create and sign a SignedObject before creating a SealedObject