1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
|
// SPDX-License-Identifier: GPL-2.0
#define _GNU_SOURCE
#include <errno.h>
#include <fcntl.h>
#include <sched.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/mount.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <linux/android/binder.h>
#include <linux/android/binderfs.h>
#include "../../kselftest.h"
#include "../../kselftest_harness.h"
static ssize_t write_nointr(int fd, const void *buf, size_t count)
{
ssize_t ret;
again:
ret = write(fd, buf, count);
if (ret < 0 && errno == EINTR)
goto again;
return ret;
}
static void write_to_file(const char *filename, const void *buf, size_t count,
int allowed_errno)
{
int fd, saved_errno;
ssize_t ret;
fd = open(filename, O_WRONLY | O_CLOEXEC);
if (fd < 0)
ksft_exit_fail_msg("%s - Failed to open file %s\n",
strerror(errno), filename);
ret = write_nointr(fd, buf, count);
if (ret < 0) {
if (allowed_errno && (errno == allowed_errno)) {
close(fd);
return;
}
goto on_error;
}
if ((size_t)ret != count)
goto on_error;
close(fd);
return;
on_error:
saved_errno = errno;
close(fd);
errno = saved_errno;
if (ret < 0)
ksft_exit_fail_msg("%s - Failed to write to file %s\n",
strerror(errno), filename);
ksft_exit_fail_msg("Failed to write to file %s\n", filename);
}
static void change_to_userns(void)
{
int ret;
uid_t uid;
gid_t gid;
/* {g,u}id_map files only allow a max of 4096 bytes written to them */
char idmap[4096];
uid = getuid();
gid = getgid();
ret = unshare(CLONE_NEWUSER);
if (ret < 0)
ksft_exit_fail_msg("%s - Failed to unshare user namespace\n",
strerror(errno));
write_to_file("/proc/self/setgroups", "deny", strlen("deny"), ENOENT);
ret = snprintf(idmap, sizeof(idmap), "0 %d 1", uid);
if (ret < 0 || (size_t)ret >= sizeof(idmap))
ksft_exit_fail_msg("%s - Failed to prepare uid mapping\n",
strerror(errno));
write_to_file("/proc/self/uid_map", idmap, strlen(idmap), 0);
ret = snprintf(idmap, sizeof(idmap), "0 %d 1", gid);
if (ret < 0 || (size_t)ret >= sizeof(idmap))
ksft_exit_fail_msg("%s - Failed to prepare uid mapping\n",
strerror(errno));
write_to_file("/proc/self/gid_map", idmap, strlen(idmap), 0);
ret = setgid(0);
if (ret)
ksft_exit_fail_msg("%s - Failed to setgid(0)\n",
strerror(errno));
ret = setuid(0);
if (ret)
ksft_exit_fail_msg("%s - Failed to setgid(0)\n",
strerror(errno));
}
static void change_to_mountns(void)
{
int ret;
ret = unshare(CLONE_NEWNS);
if (ret < 0)
ksft_exit_fail_msg("%s - Failed to unshare mount namespace\n",
strerror(errno));
ret = mount(NULL, "/", NULL, MS_REC | MS_PRIVATE, 0);
if (ret < 0)
ksft_exit_fail_msg("%s - Failed to mount / as private\n",
strerror(errno));
}
static void rmdir_protect_errno(const char *dir)
{
int saved_errno = errno;
(void)rmdir(dir);
errno = saved_errno;
}
static int __do_binderfs_test(void)
{
int fd, ret, saved_errno;
size_t len;
ssize_t wret;
bool keep = false;
struct binderfs_device device = { 0 };
struct binder_version version = { 0 };
change_to_mountns();
ret = mkdir("/dev/binderfs", 0755);
if (ret < 0) {
if (errno != EEXIST)
ksft_exit_fail_msg(
"%s - Failed to create binderfs mountpoint\n",
strerror(errno));
keep = true;
}
ret = mount(NULL, "/dev/binderfs", "binder", 0, 0);
if (ret < 0) {
if (errno != ENODEV)
ksft_exit_fail_msg("%s - Failed to mount binderfs\n",
strerror(errno));
keep ? : rmdir_protect_errno("/dev/binderfs");
return 1;
}
/* binderfs mount test passed */
ksft_inc_pass_cnt();
memcpy(device.name, "my-binder", strlen("my-binder"));
fd = open("/dev/binderfs/binder-control", O_RDONLY | O_CLOEXEC);
if (fd < 0)
ksft_exit_fail_msg(
"%s - Failed to open binder-control device\n",
strerror(errno));
ret = ioctl(fd, BINDER_CTL_ADD, &device);
saved_errno = errno;
close(fd);
errno = saved_errno;
if (ret < 0) {
keep ? : rmdir_protect_errno("/dev/binderfs");
ksft_exit_fail_msg(
"%s - Failed to allocate new binder device\n",
strerror(errno));
}
ksft_print_msg(
"Allocated new binder device with major %d, minor %d, and name %s\n",
device.major, device.minor, device.name);
/* binder device allocation test passed */
ksft_inc_pass_cnt();
fd = open("/dev/binderfs/my-binder", O_CLOEXEC | O_RDONLY);
if (fd < 0) {
keep ? : rmdir_protect_errno("/dev/binderfs");
ksft_exit_fail_msg("%s - Failed to open my-binder device\n",
strerror(errno));
}
ret = ioctl(fd, BINDER_VERSION, &version);
saved_errno = errno;
close(fd);
errno = saved_errno;
if (ret < 0) {
keep ? : rmdir_protect_errno("/dev/binderfs");
ksft_exit_fail_msg(
"%s - Failed to open perform BINDER_VERSION request\n",
strerror(errno));
}
ksft_print_msg("Detected binder version: %d\n",
version.protocol_version);
/* binder transaction with binderfs binder device passed */
ksft_inc_pass_cnt();
ret = unlink("/dev/binderfs/my-binder");
if (ret < 0) {
keep ? : rmdir_protect_errno("/dev/binderfs");
ksft_exit_fail_msg("%s - Failed to delete binder device\n",
strerror(errno));
}
/* binder device removal passed */
ksft_inc_pass_cnt();
ret = unlink("/dev/binderfs/binder-control");
if (!ret) {
keep ? : rmdir_protect_errno("/dev/binderfs");
ksft_exit_fail_msg("Managed to delete binder-control device\n");
} else if (errno != EPERM) {
keep ? : rmdir_protect_errno("/dev/binderfs");
ksft_exit_fail_msg(
"%s - Failed to delete binder-control device but exited with unexpected error code\n",
strerror(errno));
}
/* binder-control device removal failed as expected */
ksft_inc_xfail_cnt();
on_error:
ret = umount2("/dev/binderfs", MNT_DETACH);
keep ?: rmdir_protect_errno("/dev/binderfs");
if (ret < 0)
ksft_exit_fail_msg("%s - Failed to unmount binderfs\n",
strerror(errno));
/* binderfs unmount test passed */
ksft_inc_pass_cnt();
return 0;
}
TEST(binderfs_test_privileged)
{
if (geteuid() != 0)
XFAIL(return, "Tests are not run as root. Skipping privileged tests");
if (__do_binderfs_test() == 1)
XFAIL(return, "The Android binderfs filesystem is not available");
}
TEST(binderfs_test_unprivileged)
{
change_to_userns();
if (__do_binderfs_test() == 1)
XFAIL(return, "The Android binderfs filesystem is not available");
}
TEST_HARNESS_MAIN
|