diff options
| author | David Herrmann <dh.herrmann@gmail.com> | 2014-08-08 17:25:32 -0400 |
|---|---|---|
| committer | Linus Torvalds <torvalds@linux-foundation.org> | 2014-08-08 18:57:31 -0400 |
| commit | 4f5ce5e8d7e2da3c714df8a7fa42edb9f992fc52 (patch) | |
| tree | 6bdac69397a13f372a4466e7c629924433e619bb /tools/testing | |
| parent | 9183df25fe7b194563db3fec6dc3202a5855839c (diff) | |
selftests: add memfd_create() + sealing tests
Some basic tests to verify sealing on memfds works as expected and
guarantees the advertised semantics.
Signed-off-by: David Herrmann <dh.herrmann@gmail.com>
Acked-by: Hugh Dickins <hughd@google.com>
Cc: Michael Kerrisk <mtk.manpages@gmail.com>
Cc: Ryan Lortie <desrt@desrt.ca>
Cc: Lennart Poettering <lennart@poettering.net>
Cc: Daniel Mack <zonque@gmail.com>
Cc: Andy Lutomirski <luto@amacapital.net>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'tools/testing')
| -rw-r--r-- | tools/testing/selftests/Makefile | 1 | ||||
| -rw-r--r-- | tools/testing/selftests/memfd/.gitignore | 2 | ||||
| -rw-r--r-- | tools/testing/selftests/memfd/Makefile | 29 | ||||
| -rw-r--r-- | tools/testing/selftests/memfd/memfd_test.c | 913 |
4 files changed, 945 insertions, 0 deletions
diff --git a/tools/testing/selftests/Makefile b/tools/testing/selftests/Makefile index d10f95ce2ea4..6fd2a4402069 100644 --- a/tools/testing/selftests/Makefile +++ b/tools/testing/selftests/Makefile | |||
| @@ -2,6 +2,7 @@ TARGETS = breakpoints | |||
| 2 | TARGETS += cpu-hotplug | 2 | TARGETS += cpu-hotplug |
| 3 | TARGETS += efivarfs | 3 | TARGETS += efivarfs |
| 4 | TARGETS += kcmp | 4 | TARGETS += kcmp |
| 5 | TARGETS += memfd | ||
| 5 | TARGETS += memory-hotplug | 6 | TARGETS += memory-hotplug |
| 6 | TARGETS += mqueue | 7 | TARGETS += mqueue |
| 7 | TARGETS += net | 8 | TARGETS += net |
diff --git a/tools/testing/selftests/memfd/.gitignore b/tools/testing/selftests/memfd/.gitignore new file mode 100644 index 000000000000..bcc8ee20c710 --- /dev/null +++ b/tools/testing/selftests/memfd/.gitignore | |||
| @@ -0,0 +1,2 @@ | |||
| 1 | memfd_test | ||
| 2 | memfd-test-file | ||
diff --git a/tools/testing/selftests/memfd/Makefile b/tools/testing/selftests/memfd/Makefile new file mode 100644 index 000000000000..36653b903140 --- /dev/null +++ b/tools/testing/selftests/memfd/Makefile | |||
| @@ -0,0 +1,29 @@ | |||
| 1 | uname_M := $(shell uname -m 2>/dev/null || echo not) | ||
| 2 | ARCH ?= $(shell echo $(uname_M) | sed -e s/i.86/i386/) | ||
| 3 | ifeq ($(ARCH),i386) | ||
| 4 | ARCH := X86 | ||
| 5 | endif | ||
| 6 | ifeq ($(ARCH),x86_64) | ||
| 7 | ARCH := X86 | ||
| 8 | endif | ||
| 9 | |||
| 10 | CFLAGS += -I../../../../arch/x86/include/generated/uapi/ | ||
| 11 | CFLAGS += -I../../../../arch/x86/include/uapi/ | ||
| 12 | CFLAGS += -I../../../../include/uapi/ | ||
| 13 | CFLAGS += -I../../../../include/ | ||
| 14 | |||
| 15 | all: | ||
| 16 | ifeq ($(ARCH),X86) | ||
| 17 | gcc $(CFLAGS) memfd_test.c -o memfd_test | ||
| 18 | else | ||
| 19 | echo "Not an x86 target, can't build memfd selftest" | ||
| 20 | endif | ||
| 21 | |||
| 22 | run_tests: all | ||
| 23 | ifeq ($(ARCH),X86) | ||
| 24 | gcc $(CFLAGS) memfd_test.c -o memfd_test | ||
| 25 | endif | ||
| 26 | @./memfd_test || echo "memfd_test: [FAIL]" | ||
| 27 | |||
| 28 | clean: | ||
| 29 | $(RM) memfd_test | ||
diff --git a/tools/testing/selftests/memfd/memfd_test.c b/tools/testing/selftests/memfd/memfd_test.c new file mode 100644 index 000000000000..3634c909b1b0 --- /dev/null +++ b/tools/testing/selftests/memfd/memfd_test.c | |||
| @@ -0,0 +1,913 @@ | |||
| 1 | #define _GNU_SOURCE | ||
| 2 | #define __EXPORTED_HEADERS__ | ||
| 3 | |||
| 4 | #include <errno.h> | ||
| 5 | #include <inttypes.h> | ||
| 6 | #include <limits.h> | ||
| 7 | #include <linux/falloc.h> | ||
| 8 | #include <linux/fcntl.h> | ||
| 9 | #include <linux/memfd.h> | ||
| 10 | #include <sched.h> | ||
| 11 | #include <stdio.h> | ||
| 12 | #include <stdlib.h> | ||
| 13 | #include <signal.h> | ||
| 14 | #include <string.h> | ||
| 15 | #include <sys/mman.h> | ||
| 16 | #include <sys/stat.h> | ||
| 17 | #include <sys/syscall.h> | ||
| 18 | #include <unistd.h> | ||
| 19 | |||
| 20 | #define MFD_DEF_SIZE 8192 | ||
| 21 | #define STACK_SIZE 65535 | ||
| 22 | |||
| 23 | static int sys_memfd_create(const char *name, | ||
| 24 | unsigned int flags) | ||
| 25 | { | ||
| 26 | return syscall(__NR_memfd_create, name, flags); | ||
| 27 | } | ||
| 28 | |||
| 29 | static int mfd_assert_new(const char *name, loff_t sz, unsigned int flags) | ||
| 30 | { | ||
| 31 | int r, fd; | ||
| 32 | |||
| 33 | fd = sys_memfd_create(name, flags); | ||
| 34 | if (fd < 0) { | ||
| 35 | printf("memfd_create(\"%s\", %u) failed: %m\n", | ||
| 36 | name, flags); | ||
| 37 | abort(); | ||
| 38 | } | ||
| 39 | |||
| 40 | r = ftruncate(fd, sz); | ||
| 41 | if (r < 0) { | ||
| 42 | printf("ftruncate(%llu) failed: %m\n", (unsigned long long)sz); | ||
| 43 | abort(); | ||
| 44 | } | ||
| 45 | |||
| 46 | return fd; | ||
| 47 | } | ||
| 48 | |||
| 49 | static void mfd_fail_new(const char *name, unsigned int flags) | ||
| 50 | { | ||
| 51 | int r; | ||
| 52 | |||
| 53 | r = sys_memfd_create(name, flags); | ||
| 54 | if (r >= 0) { | ||
| 55 | printf("memfd_create(\"%s\", %u) succeeded, but failure expected\n", | ||
| 56 | name, flags); | ||
| 57 | close(r); | ||
| 58 | abort(); | ||
| 59 | } | ||
| 60 | } | ||
| 61 | |||
| 62 | static __u64 mfd_assert_get_seals(int fd) | ||
| 63 | { | ||
| 64 | long r; | ||
| 65 | |||
| 66 | r = fcntl(fd, F_GET_SEALS); | ||
| 67 | if (r < 0) { | ||
| 68 | printf("GET_SEALS(%d) failed: %m\n", fd); | ||
| 69 | abort(); | ||
| 70 | } | ||
| 71 | |||
| 72 | return r; | ||
| 73 | } | ||
| 74 | |||
| 75 | static void mfd_assert_has_seals(int fd, __u64 seals) | ||
| 76 | { | ||
| 77 | __u64 s; | ||
| 78 | |||
| 79 | s = mfd_assert_get_seals(fd); | ||
| 80 | if (s != seals) { | ||
| 81 | printf("%llu != %llu = GET_SEALS(%d)\n", | ||
| 82 | (unsigned long long)seals, (unsigned long long)s, fd); | ||
| 83 | abort(); | ||
| 84 | } | ||
| 85 | } | ||
| 86 | |||
| 87 | static void mfd_assert_add_seals(int fd, __u64 seals) | ||
| 88 | { | ||
| 89 | long r; | ||
| 90 | __u64 s; | ||
| 91 | |||
| 92 | s = mfd_assert_get_seals(fd); | ||
| 93 | r = fcntl(fd, F_ADD_SEALS, seals); | ||
| 94 | if (r < 0) { | ||
| 95 | printf("ADD_SEALS(%d, %llu -> %llu) failed: %m\n", | ||
| 96 | fd, (unsigned long long)s, (unsigned long long)seals); | ||
| 97 | abort(); | ||
| 98 | } | ||
| 99 | } | ||
| 100 | |||
| 101 | static void mfd_fail_add_seals(int fd, __u64 seals) | ||
| 102 | { | ||
| 103 | long r; | ||
| 104 | __u64 s; | ||
| 105 | |||
| 106 | r = fcntl(fd, F_GET_SEALS); | ||
| 107 | if (r < 0) | ||
| 108 | s = 0; | ||
| 109 | else | ||
| 110 | s = r; | ||
| 111 | |||
| 112 | r = fcntl(fd, F_ADD_SEALS, seals); | ||
| 113 | if (r >= 0) { | ||
| 114 | printf("ADD_SEALS(%d, %llu -> %llu) didn't fail as expected\n", | ||
| 115 | fd, (unsigned long long)s, (unsigned long long)seals); | ||
| 116 | abort(); | ||
| 117 | } | ||
| 118 | } | ||
| 119 | |||
| 120 | static void mfd_assert_size(int fd, size_t size) | ||
| 121 | { | ||
| 122 | struct stat st; | ||
| 123 | int r; | ||
| 124 | |||
| 125 | r = fstat(fd, &st); | ||
| 126 | if (r < 0) { | ||
| 127 | printf("fstat(%d) failed: %m\n", fd); | ||
| 128 | abort(); | ||
| 129 | } else if (st.st_size != size) { | ||
| 130 | printf("wrong file size %lld, but expected %lld\n", | ||
| 131 | (long long)st.st_size, (long long)size); | ||
| 132 | abort(); | ||
| 133 | } | ||
| 134 | } | ||
| 135 | |||
| 136 | static int mfd_assert_dup(int fd) | ||
| 137 | { | ||
| 138 | int r; | ||
