diff options
| author | Christian Brauner <christian@brauner.io> | 2019-01-17 06:48:54 -0500 |
|---|---|---|
| committer | Greg Kroah-Hartman <gregkh@linuxfoundation.org> | 2019-01-30 09:19:56 -0500 |
| commit | 75abec73de65a86a23019ee7a3569220ee344f37 (patch) | |
| tree | 150b4d375c70c2fdba9105ed0ca99c447fd9580d /tools/testing | |
| parent | f17b5f06cb92ef2250513a1e154c47b78df07d40 (diff) | |
selftests: add binderfs selftests
This adds the promised selftest for binderfs. It will verify the following
things:
- binderfs mounting works
- binder device allocation works
- performing a binder ioctl() request through a binderfs device works
- binder device removal works
- binder-control removal fails
- binderfs unmounting works
The tests are performed both privileged and unprivileged. The latter
verifies that binderfs behaves correctly in user namespaces.
Cc: Todd Kjos <tkjos@google.com>
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
Acked-by: Shuah Khan <shuah@kernel.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Diffstat (limited to 'tools/testing')
5 files changed, 286 insertions, 0 deletions
diff --git a/tools/testing/selftests/Makefile b/tools/testing/selftests/Makefile index 1a2bd15c5b6e..400ee81a3043 100644 --- a/tools/testing/selftests/Makefile +++ b/tools/testing/selftests/Makefile | |||
| @@ -10,6 +10,7 @@ TARGETS += drivers/dma-buf | |||
| 10 | TARGETS += efivarfs | 10 | TARGETS += efivarfs |
| 11 | TARGETS += exec | 11 | TARGETS += exec |
| 12 | TARGETS += filesystems | 12 | TARGETS += filesystems |
| 13 | TARGETS += filesystems/binderfs | ||
| 13 | TARGETS += firmware | 14 | TARGETS += firmware |
| 14 | TARGETS += ftrace | 15 | TARGETS += ftrace |
| 15 | TARGETS += futex | 16 | TARGETS += futex |
diff --git a/tools/testing/selftests/filesystems/binderfs/.gitignore b/tools/testing/selftests/filesystems/binderfs/.gitignore new file mode 100644 index 000000000000..8a5d9bf63dd4 --- /dev/null +++ b/tools/testing/selftests/filesystems/binderfs/.gitignore | |||
| @@ -0,0 +1 @@ | |||
| binderfs_test | |||
diff --git a/tools/testing/selftests/filesystems/binderfs/Makefile b/tools/testing/selftests/filesystems/binderfs/Makefile new file mode 100644 index 000000000000..58cb659b56b4 --- /dev/null +++ b/tools/testing/selftests/filesystems/binderfs/Makefile | |||
| @@ -0,0 +1,6 @@ | |||
| 1 | # SPDX-License-Identifier: GPL-2.0 | ||
| 2 | |||
| 3 | CFLAGS += -I../../../../../usr/include/ | ||
| 4 | TEST_GEN_PROGS := binderfs_test | ||
| 5 | |||
| 6 | include ../../lib.mk | ||
diff --git a/tools/testing/selftests/filesystems/binderfs/binderfs_test.c b/tools/testing/selftests/filesystems/binderfs/binderfs_test.c new file mode 100644 index 000000000000..8c2ed962e1c7 --- /dev/null +++ b/tools/testing/selftests/filesystems/binderfs/binderfs_test.c | |||
| @@ -0,0 +1,275 @@ | |||
| 1 | // SPDX-License-Identifier: GPL-2.0 | ||
| 2 | |||
| 3 | #define _GNU_SOURCE | ||
| 4 | #include <errno.h> | ||
| 5 | #include <fcntl.h> | ||
| 6 | #include <sched.h> | ||
| 7 | #include <stdbool.h> | ||
| 8 | #include <stdio.h> | ||
| 9 | #include <stdlib.h> | ||
| 10 | #include <string.h> | ||
| 11 | #include <sys/ioctl.h> | ||
| 12 | #include <sys/mount.h> | ||
| 13 | #include <sys/stat.h> | ||
| 14 | #include <sys/types.h> | ||
| 15 | #include <unistd.h> | ||
| 16 | #include <linux/android/binder.h> | ||
| 17 | #include <linux/android/binderfs.h> | ||
| 18 | #include "../../kselftest.h" | ||
| 19 | |||
| 20 | static ssize_t write_nointr(int fd, const void *buf, size_t count) | ||
| 21 | { | ||
| 22 | ssize_t ret; | ||
| 23 | again: | ||
| 24 | ret = write(fd, buf, count); | ||
| 25 | if (ret < 0 && errno == EINTR) | ||
| 26 | goto again; | ||
| 27 | |||
| 28 | return ret; | ||
| 29 | } | ||
| 30 | |||
| 31 | static void write_to_file(const char *filename, const void *buf, size_t count, | ||
| 32 | int allowed_errno) | ||
| 33 | { | ||
| 34 | int fd, saved_errno; | ||
| 35 | ssize_t ret; | ||
| 36 | |||
| 37 | fd = open(filename, O_WRONLY | O_CLOEXEC); | ||
| 38 | if (fd < 0) | ||
| 39 | ksft_exit_fail_msg("%s - Failed to open file %s\n", | ||
| 40 | strerror(errno), filename); | ||
| 41 | |||
| 42 | ret = write_nointr(fd, buf, count); | ||
| 43 | if (ret < 0) { | ||
| 44 | if (allowed_errno && (errno == allowed_errno)) { | ||
| 45 | close(fd); | ||
| 46 | return; | ||
| 47 | } | ||
| 48 | |||
| 49 | goto on_error; | ||
| 50 | } | ||
| 51 | |||
| 52 | if ((size_t)ret != count) | ||
| 53 | goto on_error; | ||
| 54 | |||
| 55 | close(fd); | ||
| 56 | return; | ||
| 57 | |||
| 58 | on_error: | ||
| 59 | saved_errno = errno; | ||
| 60 | close(fd); | ||
| 61 | errno = saved_errno; | ||
| 62 | |||
| 63 | if (ret < 0) | ||
| 64 | ksft_exit_fail_msg("%s - Failed to write to file %s\n", | ||
| 65 | strerror(errno), filename); | ||
| 66 | |||
| 67 | ksft_exit_fail_msg("Failed to write to file %s\n", filename); | ||
| 68 | } | ||
| 69 | |||
| 70 | static void change_to_userns(void) | ||
| 71 | { | ||
| 72 | int ret; | ||
| 73 | uid_t uid; | ||
| 74 | gid_t gid; | ||
| 75 | /* {g,u}id_map files only allow a max of 4096 bytes written to them */ | ||
| 76 | char idmap[4096]; | ||
| 77 | |||
| 78 | uid = getuid(); | ||
| 79 | gid = getgid(); | ||
| 80 | |||
| 81 | ret = unshare(CLONE_NEWUSER); | ||
| 82 | if (ret < 0) | ||
| 83 | ksft_exit_fail_msg("%s - Failed to unshare user namespace\n", | ||
| 84 | strerror(errno)); | ||
| 85 | |||
| 86 | write_to_file("/proc/self/setgroups", "deny", strlen("deny"), ENOENT); | ||
| 87 | |||
| 88 | ret = snprintf(idmap, sizeof(idmap), "0 %d 1", uid); | ||
| 89 | if (ret < 0 || (size_t)ret >= sizeof(idmap)) | ||
| 90 | ksft_exit_fail_msg("%s - Failed to prepare uid mapping\n", | ||
| 91 | strerror(errno)); | ||
| 92 | |||
| 93 | write_to_file("/proc/self/uid_map", idmap, strlen(idmap), 0); | ||
| 94 | |||
| 95 | ret = snprintf(idmap, sizeof(idmap), "0 %d 1", gid); | ||
| 96 | if (ret < 0 || (size_t)ret >= sizeof(idmap)) | ||
| 97 | ksft_exit_fail_msg("%s - Failed to prepare uid mapping\n", | ||
| 98 | strerror(errno)); | ||
| 99 | |||
| 100 | write_to_file("/proc/self/gid_map", idmap, strlen(idmap), 0); | ||
| 101 | |||
| 102 | ret = setgid(0); | ||
| 103 | if (ret) | ||
| 104 | ksft_exit_fail_msg("%s - Failed to setgid(0)\n", | ||
| 105 | strerror(errno)); | ||
| 106 | |||
| 107 | ret = setuid(0); | ||
| 108 | if (ret) | ||
| 109 | ksft_exit_fail_msg("%s - Failed to setgid(0)\n", | ||
| 110 | strerror(errno)); | ||
| 111 | } | ||
| 112 | |||
| 113 | static void change_to_mountns(void) | ||
| 114 | { | ||
| 115 | int ret; | ||
| 116 | |||
| 117 | ret = unshare(CLONE_NEWNS); | ||
| 118 | if (ret < 0) | ||
| 119 | ksft_exit_fail_msg("%s - Failed to unshare mount namespace\n", | ||
| 120 | strerror(errno)); | ||
| 121 | |||
| 122 | ret = mount(NULL, "/", NULL, MS_REC | MS_PRIVATE, 0); | ||
| 123 | if (ret < 0) | ||
| 124 | ksft_exit_fail_msg("%s - Failed to mount / as private\n", | ||
| 125 | strerror(errno)); | ||
| 126 | } | ||
| 127 | |||
| 128 | static void rmdir_protect_errno(const char *dir) | ||
| 129 | { | ||
| 130 | int saved_errno = errno; | ||
| 131 | (void)rmdir(dir); | ||
| 132 | errno = saved_errno; | ||
| 133 | } | ||
| 134 | |||
| 135 | static void __do_binderfs_test(void) | ||
| 136 | { | ||
| 137 | int fd, ret, saved_errno; | ||
| 138 | size_t len; | ||
| 139 | ssize_t wret; | ||
| 140 | bool keep = false; | ||
| 141 | struct binderfs_device device = { 0 }; | ||
| 142 | struct binder_version version = { 0 }; | ||
| 143 | |||
| 144 | change_to_mountns(); | ||
| 145 | |||
| 146 | ret = mkdir("/dev/binderfs", 0755); | ||
| 147 | if (ret < 0) { | ||
| 148 | if (errno != EEXIST) | ||
| 149 | ksft_exit_fail_msg( | ||
| 150 | "%s - Failed to create binderfs mountpoint\n", | ||
| 151 | strerror(errno)); | ||
| 152 | |||
| 153 | keep = true; | ||
| 154 | } | ||
| 155 | |||
| 156 | ret = mount(NULL, "/dev/binderfs", "binder", 0, 0); | ||
| 157 | if (ret < 0) { | ||
| 158 | if (errno != ENODEV) | ||
| 159 | ksft_exit_fail_msg("%s - Failed to mount binderfs\n", | ||
