4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 * Fork and exec tiny 1 page executable which precisely controls its VM.
18 * Test /proc/$PID/maps
19 * Test /proc/$PID/smaps
20 * Test /proc/$PID/smaps_rollup
21 * Test /proc/$PID/statm
23 * FIXME require CONFIG_TMPFS which can be disabled
24 * FIXME test other values from "smaps"
25 * FIXME support other archs
37 #include <sys/mount.h>
38 #include <sys/types.h>
43 #include <sys/syscall.h>
45 #include <linux/kdev_t.h>
47 #include <sys/resource.h>
49 #include "../kselftest.h"
51 static inline long sys_execveat(int dirfd, const char *pathname, char **argv, char **envp, int flags)
53 return syscall(SYS_execveat, dirfd, pathname, argv, envp, flags);
56 static void make_private_tmp(void)
58 if (unshare(CLONE_NEWNS) == -1) {
59 if (errno == ENOSYS || errno == EPERM) {
64 if (mount(NULL, "/", NULL, MS_PRIVATE|MS_REC, NULL) == -1) {
67 if (mount(NULL, "/tmp", "tmpfs", 0, NULL) == -1) {
72 static pid_t pid = -1;
109 #define PAGE_SIZE 4096
110 #define VADDR (1UL << 32)
111 #define MAPS_OFFSET 73
113 #define syscall 0x0f, 0x05
116 (x)&0xff, ((x)>>8)&0xff, ((x)>>16)&0xff, ((x)>>24)&0xff, \
117 ((x)>>32)&0xff, ((x)>>40)&0xff, ((x)>>48)&0xff, ((x)>>56)&0xff
121 (x)&0xff, ((x)>>8)&0xff, ((x)>>16)&0xff, ((x)>>24)&0xff, \
122 ((x)>>32)&0xff, ((x)>>40)&0xff, ((x)>>48)&0xff, ((x)>>56)&0xff
125 0xb8, (x)&0xff, ((x)>>8)&0xff, ((x)>>16)&0xff, ((x)>>24)&0xff
127 static const uint8_t payload[] = {
128 /* Casually unmap stack, vDSO and everything else. */
130 mov_rdi(VADDR + 4096),
131 mov_rsi((1ULL << 47) - 4096 - VADDR - 4096),
136 /* write(0, &c, 1); */
137 0x31, 0xff, /* xor edi, edi */
138 0x48, 0x8d, 0x35, 0x00, 0x00, 0x00, 0x00, /* lea rsi, [rip] */
139 0xba, 0x01, 0x00, 0x00, 0x00, /* mov edx, 1 */
147 0xeb, 0xf7, /* jmp 1b */
150 static int make_exe(const uint8_t *payload, size_t len)
153 struct elf64_phdr ph;
155 struct iovec iov[3] = {
156 {&h, sizeof(struct elf64_hdr)},
157 {&ph, sizeof(struct elf64_phdr)},
158 {(void *)payload, len},
163 memset(&h, 0, sizeof(h));
175 h.e_entry = VADDR + sizeof(struct elf64_hdr) + sizeof(struct elf64_phdr);
176 h.e_phoff = sizeof(struct elf64_hdr);
179 h.e_ehsize = sizeof(struct elf64_hdr);
180 h.e_phentsize = sizeof(struct elf64_phdr);
186 memset(&ph, 0, sizeof(ph));
188 ph.p_flags = (1<<2)|1;
192 ph.p_filesz = sizeof(struct elf64_hdr) + sizeof(struct elf64_phdr) + len;
193 ph.p_memsz = sizeof(struct elf64_hdr) + sizeof(struct elf64_phdr) + len;
196 fd = openat(AT_FDCWD, "/tmp", O_WRONLY|O_EXCL|O_TMPFILE, 0700);
201 if (writev(fd, iov, 3) != sizeof(struct elf64_hdr) + sizeof(struct elf64_phdr) + len) {
205 /* Avoid ETXTBSY on exec. */
206 snprintf(buf, sizeof(buf), "/proc/self/fd/%u", fd);
207 fd1 = open(buf, O_RDONLY|O_CLOEXEC);
215 * 0: vsyscall VMA doesn't exist vsyscall=none
216 * 1: vsyscall VMA is --xp vsyscall=xonly
217 * 2: vsyscall VMA is r-xp vsyscall=emulate
219 static volatile int g_vsyscall;
220 static const char *str_vsyscall;
222 static const char str_vsyscall_0[] = "";
223 static const char str_vsyscall_1[] =
224 "ffffffffff600000-ffffffffff601000 --xp 00000000 00:00 0 [vsyscall]\n";
225 static const char str_vsyscall_2[] =
226 "ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall]\n";
229 static void sigaction_SIGSEGV(int _, siginfo_t *__, void *___)
235 * vsyscall page can't be unmapped, probe it directly.
237 static void vsyscall(void)
244 fprintf(stderr, "fork, errno %d\n", errno);
248 struct rlimit rlim = {0, 0};
249 (void)setrlimit(RLIMIT_CORE, &rlim);
251 /* Hide "segfault at ffffffffff600000" messages. */
252 struct sigaction act;
253 memset(&act, 0, sizeof(struct sigaction));
254 act.sa_flags = SA_SIGINFO;
255 act.sa_sigaction = sigaction_SIGSEGV;
256 (void)sigaction(SIGSEGV, &act, NULL);
259 /* gettimeofday(NULL, NULL); */
260 uint64_t rax = 0xffffffffff600000;
264 : "D" (NULL), "S" (NULL)
269 *(volatile int *)0xffffffffff600000UL;
274 waitpid(pid, &wstatus, 0);
275 if (WIFEXITED(wstatus)) {
276 g_vsyscall = WEXITSTATUS(wstatus);
278 fprintf(stderr, "error: wstatus %08x\n", wstatus);
289 switch (g_vsyscall) {
291 str_vsyscall = str_vsyscall_0;
294 str_vsyscall = str_vsyscall_1;
297 str_vsyscall = str_vsyscall_2;
307 /* Reserve fd 0 for 1-byte pipe ping from child. */
309 if (open("/", O_RDONLY|O_DIRECTORY|O_PATH) != 0) {
313 exec_fd = make_exe(payload, sizeof(payload));
315 if (pipe(pipefd) == -1) {
318 if (dup2(pipefd[1], 0) != 0) {
327 sys_execveat(exec_fd, "", NULL, NULL, AT_EMPTY_PATH);
332 if (read(pipefd[0], &_, 1) != 1) {
337 if (fstat(exec_fd, &st) == -1) {
341 /* Generate "head -n1 /proc/$PID/maps" */
343 memset(buf0, ' ', sizeof(buf0));
344 int len = snprintf(buf0, sizeof(buf0),
345 "%08lx-%08lx r-xp 00000000 %02lx:%02lx %llu",
346 VADDR, VADDR + PAGE_SIZE,
347 MAJOR(st.st_dev), MINOR(st.st_dev),
348 (unsigned long long)st.st_ino);
350 snprintf(buf0 + MAPS_OFFSET, sizeof(buf0) - MAPS_OFFSET,
351 "/tmp/#%llu (deleted)\n", (unsigned long long)st.st_ino);
353 /* Test /proc/$PID/maps */
355 const size_t len = strlen(buf0) + strlen(str_vsyscall);
360 snprintf(buf, sizeof(buf), "/proc/%u/maps", pid);
361 fd = open(buf, O_RDONLY);
365 rv = read(fd, buf, sizeof(buf));
367 assert(memcmp(buf, buf0, strlen(buf0)) == 0);
368 if (g_vsyscall > 0) {
369 assert(memcmp(buf + strlen(buf0), str_vsyscall, strlen(str_vsyscall)) == 0);
373 /* Test /proc/$PID/smaps */
379 snprintf(buf, sizeof(buf), "/proc/%u/smaps", pid);
380 fd = open(buf, O_RDONLY);
384 rv = read(fd, buf, sizeof(buf));
385 assert(0 <= rv && rv <= sizeof(buf));
387 assert(rv >= strlen(buf0));
388 assert(memcmp(buf, buf0, strlen(buf0)) == 0);
390 #define RSS1 "Rss: 4 kB\n"
391 #define RSS2 "Rss: 0 kB\n"
392 #define PSS1 "Pss: 4 kB\n"
393 #define PSS2 "Pss: 0 kB\n"
394 assert(memmem(buf, rv, RSS1, strlen(RSS1)) ||
395 memmem(buf, rv, RSS2, strlen(RSS2)));
396 assert(memmem(buf, rv, PSS1, strlen(PSS1)) ||
397 memmem(buf, rv, PSS2, strlen(PSS2)));
399 static const char *S[] = {
401 "KernelPageSize: 4 kB\n",
402 "MMUPageSize: 4 kB\n",
404 "AnonHugePages: 0 kB\n",
405 "Shared_Hugetlb: 0 kB\n",
406 "Private_Hugetlb: 0 kB\n",
411 for (i = 0; i < ARRAY_SIZE(S); i++) {
412 assert(memmem(buf, rv, S[i], strlen(S[i])));
415 if (g_vsyscall > 0) {
416 assert(memmem(buf, rv, str_vsyscall, strlen(str_vsyscall)));
420 /* Test /proc/$PID/smaps_rollup */
423 memset(bufr, ' ', sizeof(bufr));
424 len = snprintf(bufr, sizeof(bufr),
425 "%08lx-%08lx ---p 00000000 00:00 0",
426 VADDR, VADDR + PAGE_SIZE);
428 snprintf(bufr + MAPS_OFFSET, sizeof(bufr) - MAPS_OFFSET,
435 snprintf(buf, sizeof(buf), "/proc/%u/smaps_rollup", pid);
436 fd = open(buf, O_RDONLY);
440 rv = read(fd, buf, sizeof(buf));
441 assert(0 <= rv && rv <= sizeof(buf));
443 assert(rv >= strlen(bufr));
444 assert(memcmp(buf, bufr, strlen(bufr)) == 0);
446 assert(memmem(buf, rv, RSS1, strlen(RSS1)) ||
447 memmem(buf, rv, RSS2, strlen(RSS2)));
448 assert(memmem(buf, rv, PSS1, strlen(PSS1)) ||
449 memmem(buf, rv, PSS2, strlen(PSS2)));
451 static const char *S[] = {
453 "AnonHugePages: 0 kB\n",
454 "Shared_Hugetlb: 0 kB\n",
455 "Private_Hugetlb: 0 kB\n",
460 for (i = 0; i < ARRAY_SIZE(S); i++) {
461 assert(memmem(buf, rv, S[i], strlen(S[i])));
465 /* Test /proc/$PID/statm */
471 snprintf(buf, sizeof(buf), "/proc/%u/statm", pid);
472 fd = open(buf, O_RDONLY);
476 rv = read(fd, buf, sizeof(buf));
479 assert(buf[0] == '1'); /* ->total_vm */
480 assert(buf[1] == ' ');
481 assert(buf[2] == '0' || buf[2] == '1'); /* rss */
482 assert(buf[3] == ' ');
483 assert(buf[4] == '0' || buf[2] == '1'); /* file rss */
484 assert(buf[5] == ' ');
485 assert(buf[6] == '1'); /* ELF executable segments */
486 assert(buf[7] == ' ');
487 assert(buf[8] == '0');
488 assert(buf[9] == ' ');
489 assert(buf[10] == '0'); /* ->data_vm + ->stack_vm */
490 assert(buf[11] == ' ');
491 assert(buf[12] == '0');
492 assert(buf[13] == '\n');