Programs must comply with the principle of least privilege not only by providing privileged blocks with the minimum permissions required for correct operation , (see SEC50-J. Avoid granting excess privileges) but also by ensuring that privileged blocks contain code contains only those operations that require the increased privileges. Superfluous code contained within a privileged block necessarily operates must operate with the privileges of that block; this increases , increasing the potential attack surface exposed to an attacker. Consequently, privileged blocks are forbidden to contain superfluous code.
Noncompliant Code Example
This noncompliant code example shows contains a changePassword()
method that attempts to open a password file using within a doPrivileged
block and performs operations using that file. The doPrivileged
block also contains logic that operates on the file and a superfluous System.loadLibrary()
call that loads the authentication library.
Code Block | ||
---|---|---|
| ||
public void changePassword(String currentPassword, String newPassword) { final FileInputStream f[] = { null }; AccessController.doPrivileged(new PrivilegedAction() { public Object run() { try { String passwordFile = System.getProperty("user.dir") + File.separator + "PasswordFileName"; f[0] = new FileInputStream(passwordFile); // Operate on Check whether oldPassword matches the one in the file ... // If not, throw an exception System.loadLibrary("LibNameauthentication"); } catch (FileNotFoundException cnf) { // Forward to handler } return null; } }); // endEnd of doPrivileged() } |
This is a violation of example violates the principle of least privilege because a caller who does not have the required privileges can indirectly load the library provided the security policy allows doing so. This transfers the burden of ensuring security to the administrator who implements the security policy.
Compliant Solution
because an unprivileged caller could also cause the authentication library to be loaded. An unprivileged caller cannot invoke the System.loadLibrary()
method directly because this could expose native methods to the unprivileged code [SCG 2010]. Furthermore, the System.loadLibrary()
method checks only the privileges of its immediate caller, so it should be used only with great care. For more information, see SEC52-J. Do not expose methods that use reduced-security checks to untrusted code.
Compliant Solution
This compliant solution moves the call to System.loadLibrary()
outside the doPrivileged()
block. Doing so allows unprivileged code to perform preliminary password reset checks using the file but prevents it from loading the authentication library. This compliant solution moves the call to {{System.loadLibrary()}} outside the {{doPrivileged()}} block. Any operations on the file descriptor {{f\[0\]}} must also occur outside the privileged block to make it easier to audit privileged code. However, {{f\[0\]}} should not leak out to untrusted code (see [SEC00-J. Do not allow doPrivileged() blocks to leak sensitive information outside a trust boundary]). As a result, the "operations on the file" must not allow {{f\[0\]}} to escape out of {{changePassword()}}. Minimizing the amount of code that requires elevated privileges eases the necessary task of auditing privileged code. Wiki Markup
Code Block | ||
---|---|---|
| ||
public void changePassword(String currentPassword, String newPassword) { final FileInputStream f[] = { null }; AccessController.doPrivileged(new PrivilegedAction() { public Object run() { try { String passwordFile = System.getProperty("user.dir") + File.separator + "PasswordFileName"; f[0] = new FileInputStream(passwordFile); // Check whether oldPassword matches the one in the file // If not, throw an exception } catch (FileNotFoundException cnf) { // Forward to handler } return null; } }); // endEnd of doPrivileged() // Operations on the file using handle f[0] // while ensuring that the f[0] reference // remains contained within changePassword() System.loadLibrary("LibNameauthentication"); } |
Risk Assessment
Failure to follow the principle of least privilege can lead to privilege escalation.
Rule | Severity | Likelihood | Remediation Cost | Priority | Level |
---|---|---|---|---|---|
SEC02-J | high | probable | high | P6 | L2 |
Automated Detection
Automated checking is clearly not possible in the general case. We might be able to do something with escape analysis to check that we are not leaking privileged data provided that privileged data is marked by the user, and even that would be difficult.
Related Vulnerabilities
Search for vulnerabilities resulting from the violation of this rule on the CERT website.
Related Guidelines
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="731b60ba-f2ad-42b1-8e97-9455e40016d1"><ac:plain-text-body><![CDATA[ | [ISO/IEC TR 24772:2010 | http://www.aitcnet.org/isai/] | "Privilege Sandbox Issues [XYO]" | ]]></ac:plain-text-body></ac:structured-macro> |
CWE ID 272, "Least Privilege Violation" |
Bibliography
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="4d9bf406-4d21-4f83-8490-96f11e118418"><ac:plain-text-body><![CDATA[ | [[API 2006 | AA. Bibliography#API 06]] | Class | ]]></ac:plain-text-body></ac:structured-macro> |
The loadLibrary()
invocation could also occur before preliminary password reset checks are performed; in this example, it is deferred for performance reasons.
Applicability
Minimizing privileged code reduces the attack surface of an application and simplifies the task of auditing privileged code.
Automated Detection
Tool | Version | Checker | Description | ||||||
---|---|---|---|---|---|---|---|---|---|
Parasoft Jtest |
| CERT.SEC51.PCL | Limit the number of lines in "privileged" code blocks |
Bibliography
...
SEC01-J. Do not allow tainted variables in doPrivileged blocks 14. Platform Security (SEC) SEC03-J. Protect sensitive operations with security manager checks