kernel/user_namespace.c

    1. 不能reenter 同一个user namespace,避免通过这种方式获得权限
    2. 不能修改一个线程的user namespace,一个进程内所有线程必须使用同一个namespace
    3. 调用者和其他进程共享root directory (current->fs->users != 1)
    4. target user namespace必须拥有CAP_SYS_ADMIN
    1. static int userns_install(struct nsset *nsset, struct ns_common *ns)
    2. {
    3. struct user_namespace *user_ns = to_user_ns(ns);
    4. struct cred *cred;
    5. /* Don't allow gaining capabilities by reentering
    6. * the same user namespace.
    7. */
    8. if (user_ns == current_user_ns())
    9. return -EINVAL;
    10. /* Tasks that share a thread group must share a user namespace */
    11. if (!thread_group_empty(current))
    12. return -EINVAL;
    13. if (current->fs->users != 1)
    14. return -EINVAL;
    15. if (!ns_capable(user_ns, CAP_SYS_ADMIN))
    16. return -EPERM;
    17. cred = nsset_cred(nsset);
    18. if (!cred)
    19. return -EINVAL;
    20. put_user_ns(cred->user_ns);
    21. set_cred_user_ns(cred, get_user_ns(user_ns));
    22. return 0;
    23. }