【发布时间】:2012-01-20 11:55:37
【问题描述】:
我有以下示例程序:
#include <stdio.h>
int
main(int argc, char ** argv){
char buf[100];
printf("Please enter your name: ");
fflush(stdout);
gets(buf);
printf("Hello \"%s\"\n", buf);
execve("/bin/sh", 0, 0);
}
当我在没有任何管道的情况下运行时,它会正常工作并返回sh promt:
bash$ ./a.out
Please enter your name: warning: this program uses gets() which is unsafe.
testName
Hello "testName"
$ exit
bash$
但这在管道中不起作用,我想我知道为什么会这样,但我无法找到解决方案。示例运行如下。
bash$ echo -e "testName\npwd" | ./a.out
Please enter your name: warning: this program uses gets() which is unsafe.
Hello "testName"
bash$
我认为这与gets 清空stdin 的方式有关,/bin/sh 收到 EOF 并立即退出而没有错误消息。
但是我该如何解决这个问题(如果可能,不修改程序,如果没有,不删除gets)以便即使我通过管道提供输入也能得到提示?
附:我在 FreeBSD (4.8) 机器 D.S. 上运行它
【问题讨论】:
-
从不使用
gets。它总是会打开一个缓冲区溢出安全漏洞。 -
我知道 ;) ...这是我大学安全实验室计算机上缓冲区溢出尝试的一部分。纯学术。 =D
标签: c shell pipe freebsd execve