From d1ea32ca133ce53b230ce7472db24e7bef60ccab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=C2=A0Miros=C5=82aw?= Date: Thu, 27 Jul 2023 17:33:42 +0200 Subject: [PATCH] zdtm: test execveat(memfd) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Michał Mirosław --- test/zdtm/static/Makefile | 1 + test/zdtm/static/memfd05.c | 108 ++++++++++++++++++++++++++++++++++ test/zdtm/static/memfd05.desc | 1 + 3 files changed, 110 insertions(+) create mode 100644 test/zdtm/static/memfd05.c create mode 100644 test/zdtm/static/memfd05.desc diff --git a/test/zdtm/static/Makefile b/test/zdtm/static/Makefile index 026867631d..a11492912e 100644 --- a/test/zdtm/static/Makefile +++ b/test/zdtm/static/Makefile @@ -258,6 +258,7 @@ TST_NOFILE := \ memfd02-hugetlb \ memfd03 \ memfd04 \ + memfd05 \ shmemfd \ shmemfd-priv \ time \ diff --git a/test/zdtm/static/memfd05.c b/test/zdtm/static/memfd05.c new file mode 100644 index 0000000000..868b5a4ae7 --- /dev/null +++ b/test/zdtm/static/memfd05.c @@ -0,0 +1,108 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +#include "zdtmtst.h" + +const char *test_doc = "exec(memfd)"; +const char *test_author = "Michał Mirosław "; + +static int _memfd_create(const char *name, unsigned int flags) +{ + return syscall(SYS_memfd_create, name, flags); +} + +static int _execveat(int dirfd, const char *pathname, + const char *const argv[], + const char *const envp[], + int flags) +{ + return syscall(SYS_execveat, dirfd, pathname, argv, envp, flags); +} + +static const char *const script_argv[] = { "true", NULL }; +static const char *const script_env[] = { NULL }; + +static bool test_exec_fd(int fd) +{ + int err, pid, status; + + err = fcntl(fd, F_GETFD); + if (err < 0) { + fail("fcntl(F_GETFD)"); + return false; + } + if (err) { + errno = 0; + fail("F_GETFD for the memfd returned %d but expected 0", err); + return false; + } + + pid = fork(); + if (!pid) { + _execveat(fd, "", script_argv, script_env, AT_EMPTY_PATH); + err = errno; + pr_perror("execveat()"); + _exit(err); + } + + if (pid < 0) { + fail("fork()"); + return false; + } + + while (waitpid(pid, &status, 0) != pid) { + if (errno == EINTR) + continue; + fail("waitpid(child=%d)", pid); + return false; + } + + if (status != 0) { + pr_err("child exited with status=%d\n", status); + return false; + } + + return true; +} + +static const char script[] = "#!/bin/true"; +static const size_t script_len = sizeof(script) - 1; + +int main(int argc, char *argv[]) +{ + int fd; + + test_init(argc, argv); + + fd = _memfd_create("somename", 0); + if (fd < 0) { + fail("memfd_create()"); + return 1; + } + + if (write(fd, script, script_len) != script_len) { + fail("write(memfd)"); + return 1; + } + + if (!test_exec_fd(fd)) + return 1; + + test_msg("execveat(memfd) succeeded before C/R.\n"); + + test_daemon(); + test_waitsig(); + + if (!test_exec_fd(fd)) + return 1; + + pass(); + + return 0; +} diff --git a/test/zdtm/static/memfd05.desc b/test/zdtm/static/memfd05.desc new file mode 100644 index 0000000000..bbf136d145 --- /dev/null +++ b/test/zdtm/static/memfd05.desc @@ -0,0 +1 @@ +{'deps': ['/bin/true']}