Java,利用部分初始化对象的目的是什么
Java, what is the purpose of Partially Initialised Object exploitation
我正在为考试做一些个人研究。过去的考试已经问了Outline how a partially initialised object in Java is vulnerable to exploitation
。还有,What are the possible complications of somebody exploiting said Objects in your application
现在,我在这里找到了这个资源:securecoding.cert
在上面的网站上,我可以看到它是如何完成的例子,但我似乎看不到或理解它的目的,你实际上可以用这些对象恶意做什么。
据我了解,您应该始终在像这样执行操作(例如布尔值或类似操作)时检查对象实例化是否已完成;
class BankAccount {
private int balance; // Defaults to 0
private volatile boolean initialized = false;
public BankAccount() {
if (!performAccountVerification()) {
throw new SecurityException("Invalid Account");
}
balance = 1000;
// ...
initialized = true;
}
public int getBalance() {
if (!initialized) {
throw new IllegalStateException("Class not initialized");
}
return balance;
}
// ...
}
代码取自上述资源。
您还应该使用 volatile,因为您希望确保同步,因为部分问题是 Java 内存允许其他线程访问这些部分初始化的对象。
总而言之:
- 你为什么要这样做?
- 您实际上可以用这些对象做什么
- 您应该始终关注这个问题,还是只关注关键系统?
谢谢,
克里斯
好吧,如果我无法访问银行帐户,并且您的 class 没有检查 initialized
标志,理论上我可以这样做:
class Thief extends Thread {
public BankAccount ba = null;
void run() {
do {
if(ba != null) ba.transferAllMoneyToDima();
} while(ba == null);
}
}
Thief th = new Thief();
th.start();
th.ba = new BankAccount();
这里发生的是 BankAccount
构造函数应该验证我是否有权访问该帐户,如果没有则抛出异常。优化器允许对某些操作重新排序。特别是,它可以在分配对象后,在构造函数完成之前立即将对象分配给 th.ba
。如果发生这种情况,我的 Thief
线程将看到非空值,并在验证完成之前窃取钱并确定我不应该被允许这样做。
我正在为考试做一些个人研究。过去的考试已经问了Outline how a partially initialised object in Java is vulnerable to exploitation
。还有,What are the possible complications of somebody exploiting said Objects in your application
现在,我在这里找到了这个资源:securecoding.cert
在上面的网站上,我可以看到它是如何完成的例子,但我似乎看不到或理解它的目的,你实际上可以用这些对象恶意做什么。
据我了解,您应该始终在像这样执行操作(例如布尔值或类似操作)时检查对象实例化是否已完成;
class BankAccount {
private int balance; // Defaults to 0
private volatile boolean initialized = false;
public BankAccount() {
if (!performAccountVerification()) {
throw new SecurityException("Invalid Account");
}
balance = 1000;
// ...
initialized = true;
}
public int getBalance() {
if (!initialized) {
throw new IllegalStateException("Class not initialized");
}
return balance;
}
// ...
}
代码取自上述资源。
您还应该使用 volatile,因为您希望确保同步,因为部分问题是 Java 内存允许其他线程访问这些部分初始化的对象。
总而言之:
- 你为什么要这样做?
- 您实际上可以用这些对象做什么
- 您应该始终关注这个问题,还是只关注关键系统?
谢谢, 克里斯
好吧,如果我无法访问银行帐户,并且您的 class 没有检查 initialized
标志,理论上我可以这样做:
class Thief extends Thread {
public BankAccount ba = null;
void run() {
do {
if(ba != null) ba.transferAllMoneyToDima();
} while(ba == null);
}
}
Thief th = new Thief();
th.start();
th.ba = new BankAccount();
这里发生的是 BankAccount
构造函数应该验证我是否有权访问该帐户,如果没有则抛出异常。优化器允许对某些操作重新排序。特别是,它可以在分配对象后,在构造函数完成之前立即将对象分配给 th.ba
。如果发生这种情况,我的 Thief
线程将看到非空值,并在验证完成之前窃取钱并确定我不应该被允许这样做。