帮忙找下这个程序的安全漏洞

kangkings98 2009-10-06 03:53:10
abstract class SortDouble {
private double[] values;
private final SortMetrics curMetrics = new SortMetrics();

/** Invoked to do the full sort */
public final SortMetrics sort(double[] data) {
values = data;
curMetrics.init();
doSort();
return getMetrics();
}

public final SortMetrics getMetrics() {
return curMetrics.clone();
}

/** For extended classes to know the number of elements*/
protected final int getDataLength() {
return values.length;
}

/** For extended classes to probe elements */
protected final double probe(int i) {
curMetrics.probeCnt++;
return values[i];
}

/** For extended classes to compare elements */
protected final int compare(int i, int j) {
curMetrics.compareCnt++;
double d1 = values[i];
double d2 = values[j];
if (d1 == d2)
return 0;
else
return (d1 < d2 ? -1 : 1);
}

/** For extended classes to swap elements */
protected final void swap(int i, int j) {
curMetrics.swapCnt++;
double tmp = values[i];
values[i] = values[j];
values[j] = tmp;
}

/** Extended classes implement this -- used by sort */
protected abstract void doSort();
}

final class SortMetrics implements Cloneable {
public long probeCnt, // simple data probes
compareCnt, // comparing two elements
swapCnt; // swapping two elements

public void init() {
probeCnt = swapCnt = compareCnt = 0;
}

public String toString() {
return probeCnt + " probes " +
compareCnt + " compares " +
swapCnt + " swaps";
}

/** This class supports clone */
public SortMetrics clone() {
try {
// default mechanism works


return (SortMetrics) super.clone();
} catch (CloneNotSupportedException e) {
// can't happen: this and Object both clone
throw new InternalError(e.toString());
}
}
}

class SimpleSortDouble extends SortDouble {
protected void doSort() {
for (int i = 0; i < getDataLength(); i++) {
for (int j = i + 1; j < getDataLength(); j++) {
if (compare(i, j) > 0)
swap(i, j);
}
}
}
}

public class TestSort {
static double[] testData = {
0.3, 1.3e-2, 7.9, 3.17
};

public static void main(String[] args) {
SortDouble bsort = new SimpleSortDouble();
SortMetrics metrics = bsort.sort(testData);
System.out.println("Metrics: " + metrics);
for (int i = 0; i < testData.length; i++)
System.out.println("\t" + testData[i]);
}
}

Exercise:Find at least one security hole in SortDouble that would let a sorting algorithm cheat on its metrics without getting caught. Fix the security hole. Assume that the sorting algorithm author doesn't get to write main.
...全文
116 3 打赏 收藏 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
kangkings98 2009-10-08
  • 打赏
  • 举报
回复
2楼说的不错,但不单单改为private,因为SortMetrics要修改他们的值,所以要加setPro,getPro,但setPro又不能被客户端所用,所以最好是这样setPro(values,password),password对SortMetrics可见,但客户端不知道,也就不能修改了。以上纯属个人看法,不是标准答案
frans11 2009-10-06
  • 打赏
  • 举报
回复
乍一看,应该是类的属性问题
SortMetrics 类的属性为public 很容易在客户端被改掉的。
应该为private 或者protected吧
lz12366 2009-10-06
  • 打赏
  • 举报
回复
顶下 晚上回来看下

62,620

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧