tools/testing/selftests/mm/memfd_secret.c
Source file repositories/reference/linux-study-clean/tools/testing/selftests/mm/memfd_secret.c
File Facts
- System
- Linux kernel
- Corpus path
tools/testing/selftests/mm/memfd_secret.c- Extension
.c- Size
- 7064 bytes
- Lines
- 347
- 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.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
sys/uio.hsys/mman.hsys/wait.hsys/types.hsys/ptrace.hsys/syscall.hsys/resource.hsys/capability.hstdlib.hstring.hunistd.herrno.hstdio.hfcntl.hkselftest.h
Detected Declarations
function memfd_secretfunction test_file_apisfunction test_mlock_limitfunction test_vmsplicefunction try_process_vm_readfunction try_ptracefunction check_child_statusfunction test_remote_accessfunction test_process_vm_readfunction test_ptracefunction set_cap_limitsfunction preparefunction mainfunction main
Annotated Snippet
WIFSIGNALED(status)) {
pass("%s is blocked as expected\n", name);
return;
}
fail("%s: unexpected memory access\n", name);
}
static void test_remote_access(int fd, const char *name,
void (*func)(int fd, int pipefd[2]))
{
int pipefd[2];
pid_t pid;
char *mem;
if (pipe(pipefd)) {
fail("pipe failed: %s\n", strerror(errno));
return;
}
pid = fork();
if (pid < 0) {
fail("fork failed: %s\n", strerror(errno));
return;
}
if (pid == 0) {
func(fd, pipefd);
return;
}
mem = mmap(NULL, page_size, prot, mode, fd, 0);
if (mem == MAP_FAILED) {
fail("Unable to mmap secret memory\n");
return;
}
memset(mem, PATTERN, page_size);
if (write(pipefd[1], &mem, sizeof(mem)) < 0) {
fail("pipe write: %s\n", strerror(errno));
return;
}
check_child_status(pid, name);
}
static void test_process_vm_read(int fd)
{
test_remote_access(fd, "process_vm_read", try_process_vm_read);
}
static void test_ptrace(int fd)
{
test_remote_access(fd, "ptrace", try_ptrace);
}
static int set_cap_limits(rlim_t max)
{
struct rlimit new;
cap_t cap = cap_init();
new.rlim_cur = max;
new.rlim_max = max;
if (setrlimit(RLIMIT_MEMLOCK, &new)) {
perror("setrlimit() returns error");
return -1;
}
/* drop capabilities including CAP_IPC_LOCK */
if (cap_set_proc(cap)) {
perror("cap_set_proc() returns error");
return -2;
}
return 0;
}
static void prepare(void)
{
struct rlimit rlim;
page_size = sysconf(_SC_PAGE_SIZE);
if (!page_size)
ksft_exit_fail_msg("Failed to get page size %s\n",
strerror(errno));
if (getrlimit(RLIMIT_MEMLOCK, &rlim))
ksft_exit_fail_msg("Unable to detect mlock limit: %s\n",
strerror(errno));
Annotation
- Immediate include surface: `sys/uio.h`, `sys/mman.h`, `sys/wait.h`, `sys/types.h`, `sys/ptrace.h`, `sys/syscall.h`, `sys/resource.h`, `sys/capability.h`.
- Detected declarations: `function memfd_secret`, `function test_file_apis`, `function test_mlock_limit`, `function test_vmsplice`, `function try_process_vm_read`, `function try_ptrace`, `function check_child_status`, `function test_remote_access`, `function test_process_vm_read`, `function test_ptrace`.
- Atlas domain: Support Tooling And Documentation / tools.
- Implementation status: source implementation candidate.
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.