-
Notifications
You must be signed in to change notification settings - Fork 599
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Michał Mirosław <[email protected]>
- Loading branch information
Showing
3 changed files
with
107 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -258,6 +258,7 @@ TST_NOFILE := \ | |
memfd02 \ | ||
memfd02-hugetlb \ | ||
memfd03 \ | ||
memfd04 \ | ||
shmemfd \ | ||
shmemfd-priv \ | ||
time \ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
#include <linux/memfd.h> | ||
#include <sys/syscall.h> | ||
#include <sys/types.h> | ||
#include <sys/wait.h> | ||
#include <stdbool.h> | ||
#include <stdint.h> | ||
#include <unistd.h> | ||
#include <fcntl.h> | ||
|
||
#include "zdtmtst.h" | ||
|
||
const char *test_doc = "exec(memfd)"; | ||
const char *test_author = "Michał Mirosław <[email protected]>"; | ||
|
||
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{'deps': ['/bin/true']} |