chroot 内的 openssl

3 投票
3 回答
4109 浏览
提问于 2025-04-17 08:47

我在尝试从一个叫做 chroot 监狱的环境里建立 ssl 连接时遇到了以下错误:

twisted.internet.error.ConnectionLost: Connection to the other side was lost in a non-clean fashion.

我使用的是 openssl 0.9.6 和 pyopenssl 来建立 ssl 连接,同时在 Linux(centos 5.5)上使用的是 python 2.4 的 twisted 库。

经过一些排查,我发现 openssl 失败的原因是它试图读取 /dev/random 文件,但在 chroot 环境里没有这个文件,所以读取失败。我确认如果我在 chroot 里创建一个 /dev/random 文件,连接就能成功。

  • 我考虑过挂载一个包含 /dev/random 文件的 devfs 文件系统到我的 chroot 里,但我的应用程序和系统管理员有个坏习惯,就是在没有先卸载所有东西的情况下就删除 chroot 的根目录。
  • 我也想过在进行 chroot 之前先读取 /dev/random 文件,但我现在的设置是在我的程序启动之前就调用 chroot,改变 chroot 的位置会对我的应用程序造成很大的变化,我不确定何时或如何能做到这一点。
  • 我还考虑过在 chroot 监狱外面运行一个程序,专门读取 /dev/random 并写入一个名为 /jail/dev/random 的命名管道,这样在 chroot 里就能访问到,但我不喜欢为了获取随机数源而必须运行一个单独的进程。而且这对于初始化 openssl 来说似乎也太复杂了。

如果我的程序无法访问 /dev/random,初始化 openssl 的正确方法是什么呢?

3 个回答

0

创建 urandom 和 random 后别忘了 SELinux

可以用这个命令查看日志:cat /var/log/messages | grep "SELinux is preventing"

SELinux is preventing /usr/sbin/php-fpm from read access on the chr_file urandom.

If you believe that php-fpm should be allowed read access on the urandom chr_file by default.
Then you should report this as a bug.
You can generate a local policy module to allow this access.
Do allow this access for now by executing:

用这个命令查找 php-fpm 的相关信息:ausearch -c 'php-fpm' --raw

然后生成一个允许的规则包:audit2allow -M my-phpfpm

最后安装这个规则包:semodule -i my-phpfpm.pp

5

你可以在openssl中模拟随机数,比如使用命令行的openssl:

[root@quilt /]# openssl s_client -h
usage: s_client args
...
 -rand file:file:...
...

不过,openssl需要一个随机数的来源,没有随机数就不能保证安全性。比如在维基百科上说:

为了生成用于安全连接的会话密钥,客户端会用服务器的公钥加密一个随机数,然后把结果发送给服务器。只有服务器能用它的私钥解密这个信息。

如果没有随机数的来源,SSL/TLS就很容易被黑客攻击。

如果你担心chroot/dev/会被删除,为什么不只创建chroot/dev/randomchroot/dev/urandom,而不是挂载整个dev目录呢?

[root@quilt /]# mknod /dev/random c 1 8
[root@quilt /]# mknod /dev/urandom c 1 9

哦,对了,你还需要复制系统的/etc/resolv.conf文件,可能还需要其他的hosts、services、ethers等文件……

5

也许更好的方法是像下面这样绑定挂载设备文件:

# touch chroot/dev/random
# mount --bind /dev/random chroot/dev/random

对于urandom也是一样的做法。

撰写回答