说明

说明

我正在从源代码编译 nginx,并希望使用 checkinstall,以便以后可以更轻松地卸载它。问题是 checkinstall 执行 /var/tmp 中的脚本,我使用 noexec 挂载该脚本以防止特权升级。

我发现本网站建议将 /var/tmp 临时绑定到其他位置,以允许在 /var/tmp 中执行脚本。但这是否会导致当时正在使用 /var/tmp 的进程出现问题并导致我受到攻击?

这让我想到一个问题:有没有办法使用 checkinstall 而不将 /var/tmp 挂载为 exec?也许可以使用 chroot 或 unshare?

答案1

说明

可以使用 unshare 命令创建专用于脚本的命名空间:

unshare --mount /path/to/script # Execute command in dedicated mount namespace

然后,该脚本使用自己的挂载命名空间。它可能看起来像这样:

mount --make-rslave /            # Prevent this mount namespace
                                 # from changing the real namespace
mount --bind /foo/tmptmp foo/tmp # Do the bind
touch /foo/tmp/tmpFile           # Create tmp files
echo $( ls /foo/tmptmp )
echo $( ls /foo/tmp )
#output:
#tmpFile
#tmpFile

使用 unshare 执行脚本后,让我们看看主系统发生了什么。

ls /foo/tmptmp
#output: tmpFile
ls /foo/tmp
#output: 
#(Note that the file is only present in /foo/tmptmp)
umount /foo/tmptmp
#output: umount: /foo/tmptmp: not mounted
#(Note that the bind did only affect the mount namespace of the script)

解决方案

将其应用于问题的问题会产生以下脚本,需要通过 unshare --mount 来调用:

mount --make-rslave /
mount --bind /your/tmp/file /var/tmp
checkinstall

相关内容