【发布时间】:2018-07-01 06:20:45
【问题描述】:
在java.lang.SecurityManager中,有一个名为initialized的布尔字段。
public class SecurityManager {
/*
* Have we been initialized. Effective against finalizer attacks.
*/
private boolean initialized = false;
//some code
/**
* Constructs a new <code>SecurityManager</code>.
*
* <p> If there is a security manager already installed, this method first
* calls the security manager's <code>checkPermission</code> method
* with the <code>RuntimePermission("createSecurityManager")</code>
* permission to ensure the calling thread has permission to create a new
* security manager.
* This may result in throwing a <code>SecurityException</code>.
*
* @exception java.lang.SecurityException if a security manager already
* exists and its <code>checkPermission</code> method
* doesn't allow creation of a new security manager.
* @see java.lang.System#getSecurityManager()
* @see #checkPermission(java.security.Permission) checkPermission
* @see java.lang.RuntimePermission
*/
public SecurityManager() {
synchronized(SecurityManager.class) {
SecurityManager sm = System.getSecurityManager();
if (sm != null) {
// ask the currently installed security manager if we
// can create a new one.
sm.checkPermission(new RuntimePermission
("createSecurityManager"));
}
initialized = true;
}
}
//some code
}
显然,初始化字段默认为false,但如果实例化通过安全检查并成功,则初始化字段将分配为true。初始化的字段上面只有注释,说对finalizer攻击有效,没有对该字段的描述。
我在互联网上搜索了finalizer attacks。我的理解是,我们不应该依赖可以被不受信任的代码覆盖的方法。但是它与初始化的字段有什么关系呢?我仍然可以继承java.lang.SecurityManager,并且如果安装了SecurityManager但允许通过反射访问私有字段,则应该可以编辑初始化字段。那么它如何有效对抗终结器攻击呢?
【问题讨论】:
标签: java security initialization finalizer securitymanager