1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 2f08c3bdfSopenharmony_ci/* 3f08c3bdfSopenharmony_ci * Copyright (c) 2016 Richard Palethorpe <richiejp@f-m.fm> 4f08c3bdfSopenharmony_ci * Copyright (c) 2017 SUSE LLC 5f08c3bdfSopenharmony_ci */ 6f08c3bdfSopenharmony_ci/* 7f08c3bdfSopenharmony_ci * Check that memory marked with MADV_DONTDUMP is not included in a core dump 8f08c3bdfSopenharmony_ci * and check that the same memory then marked with MADV_DODUMP is included in 9f08c3bdfSopenharmony_ci * a core dump. 10f08c3bdfSopenharmony_ci * 11f08c3bdfSopenharmony_ci * In order to reliably find the core dump this test temporarily changes the 12f08c3bdfSopenharmony_ci * system wide core_pattern setting. Meaning all core dumps will be sent to the 13f08c3bdfSopenharmony_ci * test's temporary dir until the setting is restored during cleanup. 14f08c3bdfSopenharmony_ci * 15f08c3bdfSopenharmony_ci * Test flow: map memory, 16f08c3bdfSopenharmony_ci * write generated character sequence to memory, 17f08c3bdfSopenharmony_ci * start child process, 18f08c3bdfSopenharmony_ci * mark memory with MADV_DONTDUMP in child, 19f08c3bdfSopenharmony_ci * abort child, 20f08c3bdfSopenharmony_ci * scan child's core dump for character sequence, 21f08c3bdfSopenharmony_ci * if the sequence is not found it is a pass otherwise a fail, 22f08c3bdfSopenharmony_ci */ 23f08c3bdfSopenharmony_ci 24f08c3bdfSopenharmony_ci#include <sys/types.h> 25f08c3bdfSopenharmony_ci#include <sys/wait.h> 26f08c3bdfSopenharmony_ci#include <sys/prctl.h> 27f08c3bdfSopenharmony_ci#include <fcntl.h> 28f08c3bdfSopenharmony_ci#include <unistd.h> 29f08c3bdfSopenharmony_ci#include <signal.h> 30f08c3bdfSopenharmony_ci#include <stdlib.h> 31f08c3bdfSopenharmony_ci#include <stdio.h> 32f08c3bdfSopenharmony_ci 33f08c3bdfSopenharmony_ci#include "tst_test.h" 34f08c3bdfSopenharmony_ci#include "lapi/mmap.h" 35f08c3bdfSopenharmony_ci 36f08c3bdfSopenharmony_ci#define CORE_PATTERN "/proc/sys/kernel/core_pattern" 37f08c3bdfSopenharmony_ci#define CORE_FILTER "/proc/self/coredump_filter" 38f08c3bdfSopenharmony_ci#define YCOUNT 0x500L 39f08c3bdfSopenharmony_ci#define FMEMSIZE (YCOUNT + 0x2L) 40f08c3bdfSopenharmony_ci#define CORENAME_MAX_SIZE 512 41f08c3bdfSopenharmony_ci 42f08c3bdfSopenharmony_cistatic int dfd; 43f08c3bdfSopenharmony_cistatic void *fmem; 44f08c3bdfSopenharmony_ci 45f08c3bdfSopenharmony_cistatic void setup(void) 46f08c3bdfSopenharmony_ci{ 47f08c3bdfSopenharmony_ci char cwd[1024]; 48f08c3bdfSopenharmony_ci char tmpcpattern[1048]; 49f08c3bdfSopenharmony_ci char *fmemc; 50f08c3bdfSopenharmony_ci int i; 51f08c3bdfSopenharmony_ci unsigned int filter; 52f08c3bdfSopenharmony_ci struct rlimit limit; 53f08c3bdfSopenharmony_ci 54f08c3bdfSopenharmony_ci limit.rlim_max = RLIM_INFINITY; 55f08c3bdfSopenharmony_ci limit.rlim_cur = limit.rlim_max; 56f08c3bdfSopenharmony_ci SAFE_SETRLIMIT(RLIMIT_CORE, &limit); 57f08c3bdfSopenharmony_ci 58f08c3bdfSopenharmony_ci switch (prctl(PR_GET_DUMPABLE)) { 59f08c3bdfSopenharmony_ci case 0: 60f08c3bdfSopenharmony_ci tst_brk(TCONF, "Process is not dumpable."); 61f08c3bdfSopenharmony_ci case 1: 62f08c3bdfSopenharmony_ci break; 63f08c3bdfSopenharmony_ci default: 64f08c3bdfSopenharmony_ci tst_brk(TBROK | TERRNO, "prctl(PR_GET_DUMPABLE)"); 65f08c3bdfSopenharmony_ci } 66f08c3bdfSopenharmony_ci 67f08c3bdfSopenharmony_ci SAFE_FILE_SCANF(CORE_FILTER, "%x", &filter); 68f08c3bdfSopenharmony_ci if (!(0x1 & filter)) 69f08c3bdfSopenharmony_ci tst_brk(TCONF, "Anonymous private memory is not dumpable."); 70f08c3bdfSopenharmony_ci 71f08c3bdfSopenharmony_ci SAFE_GETCWD(cwd, sizeof(cwd)); 72f08c3bdfSopenharmony_ci snprintf(tmpcpattern, sizeof(tmpcpattern), "%s/dump-%%p", cwd); 73f08c3bdfSopenharmony_ci tst_res(TINFO, "Temporary core pattern is '%s'", tmpcpattern); 74f08c3bdfSopenharmony_ci SAFE_FILE_PRINTF(CORE_PATTERN, "%s", tmpcpattern); 75f08c3bdfSopenharmony_ci 76f08c3bdfSopenharmony_ci fmem = SAFE_MMAP(NULL, 77f08c3bdfSopenharmony_ci FMEMSIZE, 78f08c3bdfSopenharmony_ci PROT_READ | PROT_WRITE, 79f08c3bdfSopenharmony_ci MAP_ANONYMOUS | MAP_PRIVATE, 80f08c3bdfSopenharmony_ci -1, 81f08c3bdfSopenharmony_ci 0); 82f08c3bdfSopenharmony_ci 83f08c3bdfSopenharmony_ci /* 84f08c3bdfSopenharmony_ci * Write a generated character sequence to the mapped memory, 85f08c3bdfSopenharmony_ci * which we later look for in the core dump. 86f08c3bdfSopenharmony_ci */ 87f08c3bdfSopenharmony_ci fmemc = (char *)fmem; 88f08c3bdfSopenharmony_ci *fmemc = 'x'; 89f08c3bdfSopenharmony_ci for (i = 0; i < YCOUNT; i++) 90f08c3bdfSopenharmony_ci fmemc[i + 1] = 'y'; 91f08c3bdfSopenharmony_ci fmemc[++i] = 'z'; 92f08c3bdfSopenharmony_ci} 93f08c3bdfSopenharmony_ci 94f08c3bdfSopenharmony_cistatic void cleanup(void) 95f08c3bdfSopenharmony_ci{ 96f08c3bdfSopenharmony_ci if (fmem) 97f08c3bdfSopenharmony_ci SAFE_MUNMAP(fmem, FMEMSIZE); 98f08c3bdfSopenharmony_ci 99f08c3bdfSopenharmony_ci if (dfd > 0) 100f08c3bdfSopenharmony_ci SAFE_CLOSE(dfd); 101f08c3bdfSopenharmony_ci} 102f08c3bdfSopenharmony_ci 103f08c3bdfSopenharmony_cistatic int find_sequence(int pid) 104f08c3bdfSopenharmony_ci{ 105f08c3bdfSopenharmony_ci char expectc = 'x'; 106f08c3bdfSopenharmony_ci ssize_t read, pos = 0; 107f08c3bdfSopenharmony_ci char rbuf[1024]; 108f08c3bdfSopenharmony_ci int ycount = 0; 109f08c3bdfSopenharmony_ci char dumpname[256]; 110f08c3bdfSopenharmony_ci 111f08c3bdfSopenharmony_ci snprintf(dumpname, 256, "dump-%d", pid); 112f08c3bdfSopenharmony_ci tst_res(TINFO, "Dump file should be %s", dumpname); 113f08c3bdfSopenharmony_ci SAFE_ACCESS(dumpname, F_OK); 114f08c3bdfSopenharmony_ci 115f08c3bdfSopenharmony_ci dfd = SAFE_OPEN(dumpname, O_RDONLY); 116f08c3bdfSopenharmony_ci 117f08c3bdfSopenharmony_ci read = SAFE_READ(0, dfd, &rbuf, sizeof(rbuf)); 118f08c3bdfSopenharmony_ci while (read) { 119f08c3bdfSopenharmony_ci switch (rbuf[pos]) { 120f08c3bdfSopenharmony_ci case 'x': 121f08c3bdfSopenharmony_ci ycount = 0; 122f08c3bdfSopenharmony_ci expectc = 'y'; 123f08c3bdfSopenharmony_ci break; 124f08c3bdfSopenharmony_ci case 'y': 125f08c3bdfSopenharmony_ci if (expectc == 'y') { 126f08c3bdfSopenharmony_ci ycount++; 127f08c3bdfSopenharmony_ci } else { 128f08c3bdfSopenharmony_ci expectc = 'x'; 129f08c3bdfSopenharmony_ci break; 130f08c3bdfSopenharmony_ci } 131f08c3bdfSopenharmony_ci 132f08c3bdfSopenharmony_ci if (ycount == YCOUNT) 133f08c3bdfSopenharmony_ci expectc = 'z'; 134f08c3bdfSopenharmony_ci break; 135f08c3bdfSopenharmony_ci case 'z': 136f08c3bdfSopenharmony_ci if (expectc == 'z') { 137f08c3bdfSopenharmony_ci SAFE_CLOSE(dfd); 138f08c3bdfSopenharmony_ci return 1; 139f08c3bdfSopenharmony_ci } 140f08c3bdfSopenharmony_ci default: 141f08c3bdfSopenharmony_ci expectc = 'x'; 142f08c3bdfSopenharmony_ci } 143f08c3bdfSopenharmony_ci if (++pos >= read) { 144f08c3bdfSopenharmony_ci read = SAFE_READ(0, dfd, &rbuf, sizeof(rbuf)); 145f08c3bdfSopenharmony_ci pos = 0; 146f08c3bdfSopenharmony_ci } 147f08c3bdfSopenharmony_ci } 148f08c3bdfSopenharmony_ci 149f08c3bdfSopenharmony_ci SAFE_CLOSE(dfd); 150f08c3bdfSopenharmony_ci return 0; 151f08c3bdfSopenharmony_ci} 152f08c3bdfSopenharmony_ci 153f08c3bdfSopenharmony_cistatic pid_t run_child(int advice) 154f08c3bdfSopenharmony_ci{ 155f08c3bdfSopenharmony_ci int status; 156f08c3bdfSopenharmony_ci pid_t pid; 157f08c3bdfSopenharmony_ci char *advstr = 158f08c3bdfSopenharmony_ci advice == MADV_DONTDUMP ? "MADV_DONTDUMP" : "MADV_DODUMP"; 159f08c3bdfSopenharmony_ci 160f08c3bdfSopenharmony_ci pid = SAFE_FORK(); 161f08c3bdfSopenharmony_ci if (pid == 0) { 162f08c3bdfSopenharmony_ci if (madvise(fmem, FMEMSIZE, advice) == -1) { 163f08c3bdfSopenharmony_ci tst_res(TFAIL | TERRNO, 164f08c3bdfSopenharmony_ci "madvise(%p, %lu, %s) = -1", 165f08c3bdfSopenharmony_ci fmem, 166f08c3bdfSopenharmony_ci FMEMSIZE, 167f08c3bdfSopenharmony_ci advstr); 168f08c3bdfSopenharmony_ci exit(1); 169f08c3bdfSopenharmony_ci } 170f08c3bdfSopenharmony_ci abort(); 171f08c3bdfSopenharmony_ci } 172f08c3bdfSopenharmony_ci 173f08c3bdfSopenharmony_ci SAFE_WAITPID(pid, &status, 0); 174f08c3bdfSopenharmony_ci if (WIFSIGNALED(status) && WCOREDUMP(status)) 175f08c3bdfSopenharmony_ci return pid; 176f08c3bdfSopenharmony_ci if (WIFEXITED(status)) 177f08c3bdfSopenharmony_ci return 0; 178f08c3bdfSopenharmony_ci 179f08c3bdfSopenharmony_ci tst_res(TCONF, "No coredump produced after signal (%d)", 180f08c3bdfSopenharmony_ci WTERMSIG(status)); 181f08c3bdfSopenharmony_ci 182f08c3bdfSopenharmony_ci return 0; 183f08c3bdfSopenharmony_ci} 184f08c3bdfSopenharmony_ci 185f08c3bdfSopenharmony_cistatic void run(unsigned int test_nr) 186f08c3bdfSopenharmony_ci{ 187f08c3bdfSopenharmony_ci pid_t pid; 188f08c3bdfSopenharmony_ci 189f08c3bdfSopenharmony_ci if (!test_nr) { 190f08c3bdfSopenharmony_ci pid = run_child(MADV_DONTDUMP); 191f08c3bdfSopenharmony_ci if (pid && find_sequence(pid)) 192f08c3bdfSopenharmony_ci tst_res(TFAIL, 193f08c3bdfSopenharmony_ci "Found sequence in dump when MADV_DONTDUMP set"); 194f08c3bdfSopenharmony_ci else if (pid) 195f08c3bdfSopenharmony_ci tst_res(TPASS, "madvise(..., MADV_DONTDUMP)"); 196f08c3bdfSopenharmony_ci } else { 197f08c3bdfSopenharmony_ci pid = run_child(MADV_DODUMP); 198f08c3bdfSopenharmony_ci if (pid && find_sequence(pid)) 199f08c3bdfSopenharmony_ci tst_res(TPASS, "madvise(..., MADV_DODUMP)"); 200f08c3bdfSopenharmony_ci else if (pid) 201f08c3bdfSopenharmony_ci tst_res(TFAIL, 202f08c3bdfSopenharmony_ci "No sequence in dump after MADV_DODUMP."); 203f08c3bdfSopenharmony_ci } 204f08c3bdfSopenharmony_ci} 205f08c3bdfSopenharmony_ci 206f08c3bdfSopenharmony_cistatic struct tst_test test = { 207f08c3bdfSopenharmony_ci .test = run, 208f08c3bdfSopenharmony_ci .tcnt = 2, 209f08c3bdfSopenharmony_ci .setup = setup, 210f08c3bdfSopenharmony_ci .cleanup = cleanup, 211f08c3bdfSopenharmony_ci .needs_tmpdir = 1, 212f08c3bdfSopenharmony_ci .needs_root = 1, 213f08c3bdfSopenharmony_ci .forks_child = 1, 214f08c3bdfSopenharmony_ci .save_restore = (const struct tst_path_val[]) { 215f08c3bdfSopenharmony_ci {CORE_PATTERN, NULL, TST_SR_TCONF}, 216f08c3bdfSopenharmony_ci {} 217f08c3bdfSopenharmony_ci }, 218f08c3bdfSopenharmony_ci}; 219