From 8fdd26a8e7f959e6b6536a4edf816008b3cc4b9a Mon Sep 17 00:00:00 2001 From: Dhanuka Warusadura Date: Wed, 17 May 2023 16:27:19 +0530 Subject: [PATCH] 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 --- test/zdtm/static/Makefile | 1 + test/zdtm/static/memfd-secret00.c | 79 ++++++++++++++++++++++++++++ test/zdtm/static/memfd-secret00.desc | 1 + 3 files changed, 81 insertions(+) create mode 100644 test/zdtm/static/memfd-secret00.c create mode 100644 test/zdtm/static/memfd-secret00.desc diff --git a/test/zdtm/static/Makefile b/test/zdtm/static/Makefile index 07d3bc6e21..597a320070 100644 --- a/test/zdtm/static/Makefile +++ b/test/zdtm/static/Makefile @@ -260,6 +260,7 @@ TST_NOFILE := \ memfd03 \ memfd04 \ memfd05 \ + memfd-secret00 \ shmemfd \ shmemfd-priv \ time \ diff --git a/test/zdtm/static/memfd-secret00.c b/test/zdtm/static/memfd-secret00.c new file mode 100644 index 0000000000..c1a5e63742 --- /dev/null +++ b/test/zdtm/static/memfd-secret00.c @@ -0,0 +1,79 @@ +#include +#include +#include +#include +#include + +#include "zdtmtst.h" + +#define SECRET "ABCDEFGHIJKLMNOPQRSTUVWXYZ" +#define SIZE 26 + +const char *test_doc = "memfd_secret file descriptor"; +const char *test_author = "Dhanuka Warusadura "; + +#ifndef __NR_memfd_secret +#define __NR_memfd_secret 447 +#endif + +static int _memfd_secret(unsigned int flags) +{ + return syscall(__NR_memfd_secret, flags); +} + +static void *secret_init(size_t size) +{ + int fd; + void *secretmem = NULL; + + fd = _memfd_secret(0); + if (fd < 0) + return secretmem; + + if (ftruncate(fd, size) < 0) { + close(fd); + return secretmem; + } + + secretmem = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); + if (secretmem == MAP_FAILED) { + close(fd); + return secretmem; + } + + return secretmem; +} + +static void secret_fini(void *mem, size_t size) +{ + munmap(mem, size); +} + +int main(int argc, char *argv[]) +{ + char *secretmem; + + test_init(argc, argv); + + secretmem = secret_init(SIZE); + if (!secretmem) { + fail("memfd_secret: not supported operation"); + return 1; + } + + memcpy(secretmem, SECRET, SIZE); + + test_daemon(); + test_waitsig(); + + if (strncmp(secretmem, SECRET, SIZE)) { + fail("secretmem content mismatch"); + return 1; + } + + secret_fini(secretmem, SIZE); + + pass(); + + return 0; +} diff --git a/test/zdtm/static/memfd-secret00.desc b/test/zdtm/static/memfd-secret00.desc new file mode 100644 index 0000000000..2bff09e633 --- /dev/null +++ b/test/zdtm/static/memfd-secret00.desc @@ -0,0 +1 @@ +{'feature': 'memfd_secret', 'flags': 'noauto'}