staticbool create_and_enter_ns(uid_t inner_uid)
{
uid_t outer_uid;
gid_t outer_gid; int i, ret; bool have_outer_privilege;
outer_uid = getuid();
outer_gid = getgid();
if (outer_uid == 0 && unshare(CLONE_NEWNS) == 0) {
ksft_print_msg("[NOTE]\tUsing global UIDs for tests\n"); if (prctl(PR_SET_KEEPCAPS, 1, 0, 0, 0) != 0)
ksft_exit_fail_msg("PR_SET_KEEPCAPS - %s\n",
strerror(errno)); if (setresuid(inner_uid, inner_uid, -1) != 0)
ksft_exit_fail_msg("setresuid - %s\n", strerror(errno));
// Re-enable effective caps
ret = capng_get_caps_process(); if (ret == -1)
ksft_exit_fail_msg("capng_get_caps_process failed\n");
for (i = 0; i < CAP_LAST_CAP; i++) if (capng_have_capability(CAPNG_PERMITTED, i))
capng_update(CAPNG_ADD, CAPNG_EFFECTIVE, i); if (capng_apply(CAPNG_SELECT_CAPS) != 0)
ksft_exit_fail_msg( "capng_apply - %s\n", strerror(errno));
have_outer_privilege = true;
} elseif (unshare(CLONE_NEWUSER | CLONE_NEWNS) == 0) {
ksft_print_msg("[NOTE]\tUsing a user namespace for tests\n");
maybe_write_file("/proc/self/setgroups", "deny");
write_file("/proc/self/uid_map", "%d %d 1", inner_uid, outer_uid);
write_file("/proc/self/gid_map", "0 %d 1", outer_gid);
have_outer_privilege = false;
} else {
ksft_exit_skip("must be root or be able to create a userns\n");
}
ret = capng_get_caps_process(); if (ret == -1)
ksft_exit_fail_msg("capng_get_caps_process failed\n");
/* Make sure that i starts out clear */
capng_update(CAPNG_DROP, CAPNG_INHERITABLE, CAP_NET_BIND_SERVICE); if (capng_apply(CAPNG_SELECT_CAPS) != 0)
ksft_exit_fail_msg("capng_apply - %s\n", strerror(errno));
if (uid == 0) {
ksft_print_msg("[RUN]\tRoot => ep\n"); if (fork_wait())
exec_validate_cap(true, true, false, false);
} else {
ksft_print_msg("[RUN]\tNon-root => no caps\n"); if (fork_wait())
exec_validate_cap(false, false, false, false);
}
/* We should not be able to add ambient caps yet. */ if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_RAISE, CAP_NET_BIND_SERVICE, 0, 0, 0) != -1 || errno != EPERM) { if (errno == EINVAL)
ksft_test_result_fail( "PR_CAP_AMBIENT_RAISE isn't supported\n"); else
ksft_test_result_fail( "PR_CAP_AMBIENT_RAISE should have failed eith EPERM on a non-inheritable cap\n"); return 1;
}
ksft_test_result_pass( "PR_CAP_AMBIENT_RAISE failed on non-inheritable cap\n");
capng_update(CAPNG_ADD, CAPNG_INHERITABLE, CAP_NET_RAW);
capng_update(CAPNG_DROP, CAPNG_PERMITTED, CAP_NET_RAW);
capng_update(CAPNG_DROP, CAPNG_EFFECTIVE, CAP_NET_RAW); if (capng_apply(CAPNG_SELECT_CAPS) != 0)
ksft_exit_fail_msg("capng_apply - %s\n", strerror(errno)); if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_RAISE, CAP_NET_RAW, 0, 0, 0) != -1 || errno != EPERM) {
ksft_test_result_fail( "PR_CAP_AMBIENT_RAISE should have failed on a non-permitted cap\n"); return 1;
}
ksft_test_result_pass( "PR_CAP_AMBIENT_RAISE failed on non-permitted cap\n");
capng_update(CAPNG_ADD, CAPNG_INHERITABLE, CAP_NET_BIND_SERVICE); if (capng_apply(CAPNG_SELECT_CAPS) != 0)
ksft_exit_fail_msg("capng_apply - %s\n", strerror(errno)); if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_RAISE, CAP_NET_BIND_SERVICE, 0, 0, 0) != 0) {
ksft_test_result_fail( "PR_CAP_AMBIENT_RAISE should have succeeded\n"); return 1;
}
ksft_test_result_pass("PR_CAP_AMBIENT_RAISE worked\n");
if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_IS_SET, CAP_NET_BIND_SERVICE, 0, 0, 0) != 1) {
ksft_test_result_fail("PR_CAP_AMBIENT_IS_SET is broken\n"); return 1;
}
if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_IS_SET, CAP_NET_BIND_SERVICE, 0, 0, 0) != 0) {
ksft_test_result_fail("Dropping I should have dropped A\n"); return 1;
}
ksft_test_result_pass("Basic manipulation appears to work\n");
capng_update(CAPNG_ADD, CAPNG_INHERITABLE, CAP_NET_BIND_SERVICE); if (capng_apply(CAPNG_SELECT_CAPS) != 0)
ksft_exit_fail_msg("capng_apply - %s\n", strerror(errno)); if (uid == 0) {
ksft_print_msg("[RUN]\tRoot +i => eip\n"); if (fork_wait())
exec_validate_cap(true, true, true, false);
} else {
ksft_print_msg("[RUN]\tNon-root +i => i\n"); if (fork_wait())
exec_validate_cap(false, false, true, false);
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.