/* * NOTES about this test: * - requries libcap-dev to be installed on test system * - requires securityfs to me mounted at /sys/kernel/security, e.g.: * mount -n -t securityfs -o nodev,noexec,nosuid securityfs /sys/kernel/security * - needs CONFIG_SECURITYFS and CONFIG_SAFESETID to be enabled
*/
if (getpwuid(uid) == NULL) {
memset(&p,0x00,sizeof(p));
fd=fopen("/etc/passwd","a"); if (fd == NULL)
die("couldn't open file\n"); if (fseek(fd, 0, SEEK_END))
die("couldn't fseek\n");
snprintf(name_str, 10, "user %d", uid);
p.pw_name=name_str;
p.pw_uid=uid;
p.pw_gid=uid;
p.pw_gecos="Test account";
p.pw_dir="/dev/null";
p.pw_shell="/bin/false"; int value = putpwent(&p,fd); if (value != 0)
die("putpwent failed\n"); if (fclose(fd))
die("fclose failed\n");
}
}
staticvoid ensure_group_exists(gid_t gid)
{ struct group g;
FILE *fd; char name_str[10];
if (getgrgid(gid) == NULL) {
memset(&g,0x00,sizeof(g));
fd=fopen("/etc/group","a"); if (fd == NULL)
die("couldn't open group file\n"); if (fseek(fd, 0, SEEK_END))
die("couldn't fseek group file\n");
snprintf(name_str, 10, "group %d", gid);
g.gr_name=name_str;
g.gr_gid=gid;
g.gr_passwd=NULL;
g.gr_mem=NULL; int value = putgrent(&g,fd); if (value != 0)
die("putgrent failed\n"); if (fclose(fd))
die("fclose failed\n");
}
}
staticvoid ensure_securityfs_mounted(void)
{ int fd = open(add_uid_whitelist_policy_file, O_WRONLY); if (fd < 0) { if (errno == ENOENT) { // Need to mount securityfs if (mount("securityfs", "/sys/kernel/security", "securityfs", 0, NULL) < 0)
die("mounting securityfs failed\n");
} else {
die("couldn't find securityfs for unknown reason\n");
}
} else { if (close(fd) != 0) {
die("close of %s failed: %s\n",
add_uid_whitelist_policy_file, strerror(errno));
}
}
}
if (prctl(PR_SET_KEEPCAPS, 1L))
die("Error with set keepcaps\n");
// First test to make sure we can write userns mappings from a non-root // user that doesn't have any restrictions (as long as it has // CAP_SETUID); if (setgid(NO_POLICY_UGID) < 0)
die("Error with set gid(%d)\n", NO_POLICY_UGID); if (setuid(NO_POLICY_UGID) < 0)
die("Error with set uid(%d)\n", NO_POLICY_UGID); // Take away all but setid caps
drop_caps(true); // Need PR_SET_DUMPABLE flag set so we can write /proc/[pid]/uid_map // from non-root parent process. if (prctl(PR_SET_DUMPABLE, 1, 0, 0, 0))
die("Error with set dumpable\n"); if (!test_userns(true)) {
die("test_userns failed when it should work\n");
}
// Now switch to a user/group with restrictions if (setgid(RESTRICTED_PARENT_UGID) < 0)
die("Error with set gid(%d)\n", RESTRICTED_PARENT_UGID); if (setuid(RESTRICTED_PARENT_UGID) < 0)
die("Error with set uid(%d)\n", RESTRICTED_PARENT_UGID);
if (!test_userns(false)) {
die("test_userns worked when it should fail\n");
}
// Now take away all caps
drop_caps(false);
test_setuid(2, false);
test_setuid(3, false);
test_setuid(4, false);
test_setgid(2, false);
test_setgid(3, false);
test_setgid(4, false);
// NOTE: this test doesn't clean up users that were created in // /etc/passwd or flush policies that were added to the LSM.
printf("test successful!\n"); return EXIT_SUCCESS;
}
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.