Skip to content

Commit e7f3ddc

Browse files
committed
zdtm: add test for memfd_secret
Adds a zdtm test to test checkpoint/restore a memfd_secret fd containing process. Signed-off-by: Dhanuka Warusadura <[email protected]>
1 parent d8af0b0 commit e7f3ddc

File tree

3 files changed

+81
-0
lines changed

3 files changed

+81
-0
lines changed

test/zdtm/static/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,7 @@ TST_NOFILE := \
260260
memfd03 \
261261
memfd04 \
262262
memfd05 \
263+
memfd-secret00 \
263264
shmemfd \
264265
shmemfd-priv \
265266
time \

test/zdtm/static/memfd-secret00.c

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#include <stdio.h>
2+
#include <string.h>
3+
#include <sys/mman.h>
4+
#include <sys/syscall.h>
5+
#include <unistd.h>
6+
7+
#include "zdtmtst.h"
8+
9+
#define SECRET "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
10+
#define SIZE 26
11+
12+
const char *test_doc = "memfd_secret file descriptor";
13+
const char *test_author = "Dhanuka Warusadura <[email protected]>";
14+
15+
#ifndef __NR_memfd_secret
16+
#define __NR_memfd_secret 447
17+
#endif
18+
19+
static int _memfd_secret(unsigned int flags)
20+
{
21+
return syscall(__NR_memfd_secret, flags);
22+
}
23+
24+
static void *secret_init(size_t size)
25+
{
26+
int fd;
27+
void *secretmem = NULL;
28+
29+
fd = _memfd_secret(0);
30+
if (fd < 0)
31+
return secretmem;
32+
33+
if (ftruncate(fd, size) < 0) {
34+
close(fd);
35+
return secretmem;
36+
}
37+
38+
secretmem = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
39+
if (secretmem == MAP_FAILED) {
40+
close(fd);
41+
return secretmem;
42+
}
43+
44+
return secretmem;
45+
}
46+
47+
static void secret_fini(void *mem, size_t size)
48+
{
49+
munmap(mem, size);
50+
}
51+
52+
int main(int argc, char *argv[])
53+
{
54+
char *secretmem;
55+
56+
test_init(argc, argv);
57+
58+
secretmem = secret_init(SIZE);
59+
if (!secretmem) {
60+
fail("memfd_secret: not supported operation");
61+
return 1;
62+
}
63+
64+
memcpy(secretmem, SECRET, SIZE);
65+
66+
test_daemon();
67+
test_waitsig();
68+
69+
if (strncmp(secretmem, SECRET, SIZE)) {
70+
fail("secretmem content mismatch");
71+
return 1;
72+
}
73+
74+
secret_fini(secretmem, SIZE);
75+
76+
pass();
77+
78+
return 0;
79+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{'feature': 'memfd_secret', 'flags': 'noauto'}

0 commit comments

Comments
 (0)