diff --git a/test/zdtm/static/Makefile b/test/zdtm/static/Makefile index b7fb79643c..59182a446a 100644 --- a/test/zdtm/static/Makefile +++ b/test/zdtm/static/Makefile @@ -259,6 +259,7 @@ TST_NOFILE := \ memfd02-hugetlb \ memfd03 \ memfd04 \ + 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..db09fa4ab9 --- /dev/null +++ b/test/zdtm/static/memfd-secret00.c @@ -0,0 +1,80 @@ +#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 "; + +static int _memfd_secret(unsigned int flags) +{ +#ifdef __NR_memfd_secret + return syscall(__NR_memfd_secret, flags); +#else + return -1; +#endif +} + +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; + } + + for (int i = 0; i < SIZE; i++) + secretmem[i] = 'A' + i; + + test_daemon(); + test_waitsig(); + + if (strncmp(secretmem, SECRET, SIZE)) { + fail("secretmem content mismatch"); + return 1; + } + + secret_fini(secretmem, SIZE); + + pass(); + + return 0; +}