1 // SPDX-License-Identifier: GPL-2.0
11 #include <sys/ioctl.h>
12 #include <sys/mount.h>
14 #include <sys/types.h>
16 #include <linux/android/binder.h>
17 #include <linux/android/binderfs.h>
18 #include "../../kselftest.h"
20 static ssize_t
write_nointr(int fd
, const void *buf
, size_t count
)
24 ret
= write(fd
, buf
, count
);
25 if (ret
< 0 && errno
== EINTR
)
31 static void write_to_file(const char *filename
, const void *buf
, size_t count
,
37 fd
= open(filename
, O_WRONLY
| O_CLOEXEC
);
39 ksft_exit_fail_msg("%s - Failed to open file %s\n",
40 strerror(errno
), filename
);
42 ret
= write_nointr(fd
, buf
, count
);
44 if (allowed_errno
&& (errno
== allowed_errno
)) {
52 if ((size_t)ret
!= count
)
64 ksft_exit_fail_msg("%s - Failed to write to file %s\n",
65 strerror(errno
), filename
);
67 ksft_exit_fail_msg("Failed to write to file %s\n", filename
);
70 static void change_to_userns(void)
75 /* {g,u}id_map files only allow a max of 4096 bytes written to them */
81 ret
= unshare(CLONE_NEWUSER
);
83 ksft_exit_fail_msg("%s - Failed to unshare user namespace\n",
86 write_to_file("/proc/self/setgroups", "deny", strlen("deny"), ENOENT
);
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",
93 write_to_file("/proc/self/uid_map", idmap
, strlen(idmap
), 0);
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",
100 write_to_file("/proc/self/gid_map", idmap
, strlen(idmap
), 0);
104 ksft_exit_fail_msg("%s - Failed to setgid(0)\n",
109 ksft_exit_fail_msg("%s - Failed to setgid(0)\n",
113 static void change_to_mountns(void)
117 ret
= unshare(CLONE_NEWNS
);
119 ksft_exit_fail_msg("%s - Failed to unshare mount namespace\n",
122 ret
= mount(NULL
, "/", NULL
, MS_REC
| MS_PRIVATE
, 0);
124 ksft_exit_fail_msg("%s - Failed to mount / as private\n",
128 static void rmdir_protect_errno(const char *dir
)
130 int saved_errno
= errno
;
135 static void __do_binderfs_test(void)
137 int fd
, ret
, saved_errno
;
141 struct binderfs_device device
= { 0 };
142 struct binder_version version
= { 0 };
146 ret
= mkdir("/dev/binderfs", 0755);
150 "%s - Failed to create binderfs mountpoint\n",
156 ret
= mount(NULL
, "/dev/binderfs", "binder", 0, 0);
159 ksft_exit_fail_msg("%s - Failed to mount binderfs\n",
162 keep
? : rmdir_protect_errno("/dev/binderfs");
164 "The Android binderfs filesystem is not available\n");
167 /* binderfs mount test passed */
170 memcpy(device
.name
, "my-binder", strlen("my-binder"));
172 fd
= open("/dev/binderfs/binder-control", O_RDONLY
| O_CLOEXEC
);
175 "%s - Failed to open binder-control device\n",
178 ret
= ioctl(fd
, BINDER_CTL_ADD
, &device
);
183 keep
? : rmdir_protect_errno("/dev/binderfs");
185 "%s - Failed to allocate new binder device\n",
190 "Allocated new binder device with major %d, minor %d, and name %s\n",
191 device
.major
, device
.minor
, device
.name
);
193 /* binder device allocation test passed */
196 fd
= open("/dev/binderfs/my-binder", O_CLOEXEC
| O_RDONLY
);
198 keep
? : rmdir_protect_errno("/dev/binderfs");
199 ksft_exit_fail_msg("%s - Failed to open my-binder device\n",
203 ret
= ioctl(fd
, BINDER_VERSION
, &version
);
208 keep
? : rmdir_protect_errno("/dev/binderfs");
210 "%s - Failed to open perform BINDER_VERSION request\n",
214 ksft_print_msg("Detected binder version: %d\n",
215 version
.protocol_version
);
217 /* binder transaction with binderfs binder device passed */
220 ret
= unlink("/dev/binderfs/my-binder");
222 keep
? : rmdir_protect_errno("/dev/binderfs");
223 ksft_exit_fail_msg("%s - Failed to delete binder device\n",
227 /* binder device removal passed */
230 ret
= unlink("/dev/binderfs/binder-control");
232 keep
? : rmdir_protect_errno("/dev/binderfs");
233 ksft_exit_fail_msg("Managed to delete binder-control device\n");
234 } else if (errno
!= EPERM
) {
235 keep
? : rmdir_protect_errno("/dev/binderfs");
237 "%s - Failed to delete binder-control device but exited with unexpected error code\n",
241 /* binder-control device removal failed as expected */
242 ksft_inc_xfail_cnt();
245 ret
= umount2("/dev/binderfs", MNT_DETACH
);
246 keep
?: rmdir_protect_errno("/dev/binderfs");
248 ksft_exit_fail_msg("%s - Failed to unmount binderfs\n",
251 /* binderfs unmount test passed */
255 static void binderfs_test_privileged()
259 "Tests are not run as root. Skipping privileged tests\n");
261 __do_binderfs_test();
264 static void binderfs_test_unprivileged()
267 __do_binderfs_test();
270 int main(int argc
, char *argv
[])
272 binderfs_test_privileged();
273 binderfs_test_unprivileged();