tools/testing/selftests/safesetid/safesetid-test.c
Source file repositories/reference/linux-study-clean/tools/testing/selftests/safesetid/safesetid-test.c
File Facts
- System
- Linux kernel
- Corpus path
tools/testing/selftests/safesetid/safesetid-test.c- Extension
.c- Size
- 12607 bytes
- Lines
- 543
- Domain
- Support Tooling And Documentation
- Bucket
- tools
- Inferred role
- Support Tooling And Documentation: implementation source
- Status
- source implementation candidate
Why This File Exists
Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
- Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
- Touches IRQ or DMA behavior; this matters for the representative real-device path.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
stdio.herrno.hpwd.hgrp.hstring.hsyscall.hsys/capability.hsys/types.hsys/mount.hsys/prctl.hsys/wait.hstdlib.hunistd.hfcntl.hstdbool.hstdarg.h
Detected Declarations
function diefunction vmaybe_write_filefunction write_filefunction ensure_user_existsfunction ensure_group_existsfunction ensure_securityfs_mountedfunction write_uid_policiesfunction write_gid_policiesfunction test_usernsfunction test_setuidfunction test_setgidfunction test_setgroupsfunction ensure_users_existfunction ensure_groups_existfunction drop_capsfunction main
Annotated Snippet
if (written >= 0) {
printf("short write to %s\n", filename);
return false;
} else {
printf("write to %s failed: %s\n",
filename, strerror(errno));
return false;
}
}
if (close(fd) != 0) {
printf("close of %s failed: %s\n",
filename, strerror(errno));
return false;
}
return true;
}
static bool write_file(char *filename, char *fmt, ...)
{
va_list ap;
bool ret;
va_start(ap, fmt);
ret = vmaybe_write_file(false, filename, fmt, ap);
va_end(ap);
return ret;
}
static void ensure_user_exists(uid_t uid)
{
struct passwd p;
FILE *fd;
char name_str[10];
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");
}
}
static void 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");
}
}
static void ensure_securityfs_mounted(void)
{
int fd = open(add_uid_whitelist_policy_file, O_WRONLY);
if (fd < 0) {
if (errno == ENOENT) {
Annotation
- Immediate include surface: `stdio.h`, `errno.h`, `pwd.h`, `grp.h`, `string.h`, `syscall.h`, `sys/capability.h`, `sys/types.h`.
- Detected declarations: `function die`, `function vmaybe_write_file`, `function write_file`, `function ensure_user_exists`, `function ensure_group_exists`, `function ensure_securityfs_mounted`, `function write_uid_policies`, `function write_gid_policies`, `function test_userns`, `function test_setuid`.
- Atlas domain: Support Tooling And Documentation / tools.
- Implementation status: source implementation candidate.
- IRQ or DMA behavior appears here, which is relevant to the selected PCIe/NVMe device path.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.