我正在 FreeBSD 上试验 Capsicum 框架。我正在尝试学习如何使用其沙箱功能。在实验过程中,我发现如果我在调用 getpass() 之前使用 cap_enter() 进入功能模式,则在您键入时密码短语会回显到终端。我显然不希望这样,但程序的其余部分仍然按预期运行,没有错误。如果我们在 getpass() 之后调用 cap_enter(),它会正常运行,程序的其余部分将按预期执行。
我首先想到的两个问题是:
可能会在第二页的“文件描述符作为功能”部分中给出提示。 正如我所说,我正在研究 FreeBSD; 14.0-发布-p4。这是一个简单的程序,演示了 getpass() 的问题。我最初的期望是 getpass() 会正常运行。我想了解为什么它不在功能模式内,以及如何修复它,以便我可以在功能模式内使用 getpass() 而没有回声。
//cap_test.c
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/capsicum.h>
#include "test.h"
int main(int argc, char* argv[]) {
// Entering capability mode here causes getpass()
// to echo the passphrase.
//cap_enter();
char* pwd = getpass("Passphrase: ");
// By making the call here, the passphrase is
// not echoed.
cap_enter();
// The SHA256() function takes a char* as input and returns
// an unsigned char pointer.
uchar* key = SHA256(pwd);
memset(pwd, 0, sizeof(pwd)*sizeof(pwd[0])); // added in edit
// Echo 32 hash bytes
for (int i=0; i<32; i++) {
printf("%x ", key[i]);
}
printf("\n");
// Zero-out key memory location to check that it works.
memset(key, 0, 32*sizeof(key[0]));
return 0;
}
简单的头文件为我们提供了 typedef 和函数原型:
typedef unsigned char uchar;
extern unsigned char* SHA256(char*);
函数被明确标记:“此函数已过时。请勿使用它。”现在可能是听取该建议的好时机。另请参阅注释部分以及有关使用 libbsd 中的
readpassphrase()
的评论。读完您在问题中引用的文章后,在我看来,您似乎需要使用 Capsicum 设施编写自己的
readpassphrase()
类似物,以允许您打开
/dev/tty
。如果程序没有控制终端,您必须决定该怎么做。不过,这篇文章是2012年的;在过去的十年中,Capsicum 的世界可能发生了很多变化/扩展,因此请阅读手册以了解有哪些选项。值得寻找有关辣椒的最新材料。 Capsicum 上的维基百科是一个显而易见的地方,还有 capsicum(4)
的手册页和Capsicum 上的 FreeBSD Wiki — 毫无疑问,您已经知道这些了。谷歌搜索还显示了剑桥大学计算机实验室的链接,该实验室发起了Capsicum的工作。