基本原理
用户命名空间(CLONE_NEWUSER),虽然有用的安全功能,但确实增加了内核的攻击面。例如,可以访问 iptables:
$ iptables -S
iptables v1.4.21: can't initialize iptables table `filter': Permission denied (you must be root)
Perhaps iptables or your kernel needs to be upgraded.
$ unshare -rn
# iptables -S
-P INPUT ACCEPT
-P FORWARD ACCEPT
-P OUTPUT ACCEPT
虽然这个网络命名空间不会影响通常的真实网络,但它可以帮助本地根漏洞利用。
此外,用户命名空间似乎会覆盖PR_SET_NO_NEW_PRIVS,降低其缩小攻击面的有效性:
$ ping 127.0.0.1
64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.067 ms
^C
$ setpriv --no-new-privs bash
$ ping 127.0.0.1
ping: icmp open socket: Operation not permitted
$ unshare -rn
# ifconfig lo up
# ping 127.0.0.1
64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.053 ms
^C
解决方法
用户命名空间在 chroot 环境中失败从内核 3.9 开始。它们也可以在内核配置中禁用。
答案1
我自己实现的。这是一个使用的小工具libseccomp:
https://github.com/vi/syscall_limiter/blob/master/ban_CLONE_NEWUSER.c
#include <linux/sched.h> // for CLONE_NEWUSER
#include <seccomp.h> // for seccomp_rule_add, etc
#include <stdint.h> // for uint32_t
#include <stdio.h> // for fprintf, perror, stderr
#include <unistd.h> // for execve
// gcc ban_CLONE_NEWUSER.c -lseccomp -o ban_CLONE_NEWUSER
int main(int argc, char* argv[], char* envp[]) {
if (argc<2) {
fprintf(stderr, "Usage: ban_CLONE_NEWUSER program [arguments]\n");
fprintf(stderr, "Bans unshare(2) with any flags and clone(2) with CLONE_NEWUSER flag\n");
return 126;
}
uint32_t default_action = SCMP_ACT_ALLOW;
scmp_filter_ctx ctx = seccomp_init(default_action);
if (!ctx) {
perror("seccomp_init");
return 126;
}
int ret = 0;
ret |= seccomp_rule_add(ctx, SCMP_ACT_ERRNO(1), seccomp_syscall_resolve_name("unshare"), 0);
ret |= seccomp_rule_add(ctx, SCMP_ACT_ERRNO(1), seccomp_syscall_resolve_name("clone"), 1, SCMP_CMP(0, SCMP_CMP_MASKED_EQ, CLONE_NEWUSER, CLONE_NEWUSER));
if (ret!=0) {
fprintf(stderr, "seccomp_rule_add returned %d\n", ret);
return 124;
}
ret = seccomp_load(ctx);
if (ret!=0) {
fprintf(stderr, "seccomp_load returned %d\n", ret);
return 124;
}
execve(argv[1], argv+1, envp);
perror("execve");
return 127;
}
$ ban_CLONE_NEWUSER /bin/bash
$ unshare -r
unshare: unshare failed: Operation not permitted