Had*_*ski 5 c linux namespaces ubuntu-17.04
我使用的是 Ubuntu 17.04。
挂载命名空间的单个非特权取消共享有效。您可以尝试使用 unshare(1) 命令:
$ unshare -m -U /bin/sh
#
Run Code Online (Sandbox Code Playgroud)
但是,不允许在取消共享中取消共享:
$ unshare -m -U /bin/sh
# unshare -m -U /bin/sh
unshare: Operation not permitted
#
Run Code Online (Sandbox Code Playgroud)
这是一个基本上执行相同操作的 C 程序:
#define _GNU_SOURCE
#include <stdio.h>
#include <sched.h>
#include <sys/mount.h>
#include <unistd.h>
int
main(int argc, char *argv[])
{
if(unshare(CLONE_NEWUSER|CLONE_NEWNS) == -1) {
perror("unshare");
return -1;
}
if(unshare(CLONE_NEWUSER|CLONE_NEWNS) == -1) {
perror("unshare2");
return -1;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
为什么不允许?我在哪里可以找到有关此内容的文档?我未能在取消共享或克隆手册页以及内核取消共享文档中找到此信息。
是否有系统设置允许这样做?
我想要实现的目标:
第一次取消共享:我想用我自己的版本屏蔽系统上的一些二进制文件。
第二次取消共享:非特权 chroot。
我在这里有点猜测,但我认为原因是 UID 映射。为了执行它,必须满足某些条件(来自user_namespaces手册页):
\n\n\nRun Code Online (Sandbox Code Playgroud)\nIn order for a process to write to the /proc/[pid]/uid_map (/proc/[pid]/gid_map) file, all of the following require\xe2\x80\x90\n ments must be met:\n\n 1. The writing process must have the CAP_SETUID (CAP_SETGID) capability in the user namespace of the process pid.\n\n 2. The writing process must either be in the user namespace of the process pid or be in the parent user namespace of\n the process pid.\n\n 3. The mapped user IDs (group IDs) must in turn have a mapping in the parent user namespace.\n
我相信第一次运行时,映射会与父 UID 匹配。然而,第二次却没有,系统调用失败。
\n\n从 unshare(2) 手册页:
\n\n\n\nRun Code Online (Sandbox Code Playgroud)\nEPERM CLONE_NEWUSER was specified in flags, but either the effective user ID or the effective group ID of the caller\n does not have a mapping in the parent namespace (see user_namespaces(7)).\n