summaryrefslogtreecommitdiff
path: root/tools/testing/selftests/kdbus/test-endpoint.c
blob: 34a7be49c4829d3bce27a4de9348f143c8b52862 (plain)
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stddef.h>
#include <unistd.h>
#include <stdint.h>
#include <errno.h>
#include <assert.h>
#include <libgen.h>
#include <sys/capability.h>
#include <sys/wait.h>
#include <stdbool.h>

#include "kdbus-api.h"
#include "kdbus-util.h"
#include "kdbus-enum.h"
#include "kdbus-test.h"

#define KDBUS_SYSNAME_MAX_LEN			63

static int install_name_add_match(struct kdbus_conn *conn, const char *name)
{
	struct {
		struct kdbus_cmd_match cmd;
		struct {
			uint64_t size;
			uint64_t type;
			struct kdbus_notify_name_change chg;
		} item;
		char name[64];
	} buf;
	int ret;

	/* install the match rule */
	memset(&buf, 0, sizeof(buf));
	buf.item.type = KDBUS_ITEM_NAME_ADD;
	buf.item.chg.old_id.id = KDBUS_MATCH_ID_ANY;
	buf.item.chg.new_id.id = KDBUS_MATCH_ID_ANY;
	strncpy(buf.name, name, sizeof(buf.name) - 1);
	buf.item.size = sizeof(buf.item) + strlen(buf.name) + 1;
	buf.cmd.size = sizeof(buf.cmd) + buf.item.size;

	ret = kdbus_cmd_match_add(conn->fd, &buf.cmd);
	if (ret < 0)
		return ret;

	return 0;
}

static int create_endpoint(const char *buspath, uid_t uid, const char *name,
			   uint64_t flags)
{
	struct {
		struct kdbus_cmd cmd;

		/* name item */
		struct {
			uint64_t size;
			uint64_t type;
			/* max should be KDBUS_SYSNAME_MAX_LEN */
			char str[128];
		} name;
	} ep_make;
	int fd, ret;

	fd = open(buspath, O_RDWR);
	if (fd < 0)
		return fd;

	memset(&ep_make, 0, sizeof(ep_make));

	snprintf(ep_make.name.str,
		 /* Use the KDBUS_SYSNAME_MAX_LEN or sizeof(str) */
		 KDBUS_SYSNAME_MAX_LEN > strlen(name) ?
		 KDBUS_SYSNAME_MAX_LEN : sizeof(ep_make.name.str),
		 "%u-%s", uid, name);

	ep_make.name.type = KDBUS_ITEM_MAKE_NAME;
	ep_make.name.size = KDBUS_ITEM_HEADER_SIZE +
			    strlen(ep_make.name.str) + 1;

	ep_make.cmd.flags = flags;
	ep_make.cmd.size = sizeof(ep_make.cmd) + ep_make.name.size;

	ret = kdbus_cmd_endpoint_make(fd, &ep_make.cmd);
	if (ret < 0) {
		kdbus_printf("error creating endpoint: %d (%m)\n", ret);
		return ret;
	}

	return fd;
}

static int unpriv_test_custom_ep(const char *buspath)
{
	int ret, ep_fd1, ep_fd2;
	char *ep1, *ep2, *tmp1, *tmp2;

	tmp1 = strdup(buspath);
	tmp2 = strdup(buspath);
	ASSERT_RETURN(tmp1 && tmp2);

	ret = asprintf(&ep1, "%s/%u-%s", dirname(tmp1), getuid(), "apps1");
	ASSERT_RETURN(ret >= 0);

	ret = asprintf(&ep2, "%s/%u-%s", dirname(tmp2), getuid(), "apps2");
	ASSERT_RETURN(ret >= 0);

	free(tmp1);
	free(tmp2);

	/* endpoint only accessible to current uid */
	ep_fd1 = create_endpoint(buspath, getuid(), "apps1", 0);
	ASSERT_RETURN(ep_fd1 >= 0);

	/* endpoint world accessible */
	ep_fd2 = create_endpoint(buspath, getuid(), "apps2",
				  KDBUS_MAKE_ACCESS_WORLD);
	ASSERT_RETURN(ep_fd2 >= 0);

	ret = RUN_UNPRIVILEGED(UNPRIV_UID, UNPRIV_UID, ({
		int ep_fd;
		struct kdbus_conn *ep_conn;

		/*
		 * Make sure that we are not able to create custom
		 * endpoints
		 */
		ep_fd = create_endpoint(buspath, getuid(),
					"unpriv_costum_ep", 0);
		ASSERT_EXIT(ep_fd == -EPERM);

		/*
		 * Endpoint "apps1" only accessible to same users,
		 * that own the endpoint. Access denied by VFS
		 */
		ep_conn = kdbus_hello(ep1, 0, NULL, 0);
		ASSERT_EXIT(!ep_conn && errno == EACCES);

		/* Endpoint "apps2" world accessible */
		ep_conn = kdbus_hello(ep2, 0, NULL, 0);
		ASSERT_EXIT(ep_conn);

		kdbus_conn_free(ep_conn);

		_exit(EXIT_SUCCESS);
	}),
	({ 0; }));
	ASSERT_RETURN(ret == 0);

	close(ep_fd1);
	close(ep_fd2);
	free(ep1);
	free(ep2);

	return 0;
}

static int update_endpoint(int fd, const char *name)
{
	int len = strlen(name) + 1;
	struct {
		struct kdbus_cmd cmd;

		/* name item */
		struct {
			uint64_t size;
			uint64_t type;
			char str[KDBUS_ALIGN8(len)];
		} name;

		struct {
			uint64_t size;
			uint64_t type;
			struct kdbus_policy_access access;
		} access;
	} ep_update;
	int ret;

	memset(&ep_update, 0, sizeof(ep_update));

	ep_update.name.size = KDBUS_ITEM_HEADER_SIZE + len;
	ep_update.name.type = KDBUS_ITEM_NAME;
	strncpy(ep_update.name.str, name, sizeof(ep_update.name.str) - 1);

	ep_update.access.size = sizeof(ep_update.access);
	ep_update.access.type = KDBUS_ITEM_POLICY_ACCESS;
	ep_update.access.access.type = KDBUS_POLICY_ACCESS_WORLD;
	ep_update.access.access.access = KDBUS_POLICY_SEE;

	ep_update.cmd.size = sizeof(ep_update);

	ret = kdbus_cmd_endpoint_update(fd, &ep_update.cmd);
	if (ret < 0) {
		kdbus_printf("error updating endpoint: %d (%m)\n", ret);
		return ret;
	}

	return 0;
}

int kdbus_test_custom_endpoint(struct kdbus_test_env *env)
{
	char *ep, *tmp;
	int ret, ep_fd;
	struct kdbus_msg *msg;
	struct kdbus_conn *ep_conn;
	struct kdbus_conn *reader;
	const char *name = "foo.bar.baz";
	const char *epname = "foo";
	char fake_ep[KDBUS_SYSNAME_MAX_LEN + 1] = {'\0'};

	memset(fake_ep, 'X', sizeof(fake_ep) - 1);

	/* Try to create a custom endpoint with a long name */
	ret = create_endpoint(env->buspath, getuid(), fake_ep, 0);
	ASSERT_RETURN(ret == -ENAMETOOLONG);

	/* Try to create a custom endpoint with a different uid */
	ret = create_endpoint(env->buspath, getuid() + 1, "foobar", 0);
	ASSERT_RETURN(ret == -EINVAL);

	/* create a custom endpoint, and open a connection on it */
	ep_fd = create_endpoint(env->buspath, getuid(), "foo", 0);
	ASSERT_RETURN(ep_fd >= 0);

	tmp = strdup(env->buspath);
	ASSERT_RETURN(tmp);

	ret = asprintf(&ep, "%s/%u-%s", dirname(tmp), getuid(), epname);
	free(tmp);
	ASSERT_RETURN(ret >= 0);

	/* Register a connection that listen to broadcasts */
	reader = kdbus_hello(ep, 0, NULL, 0);
	ASSERT_RETURN(reader);

	/* Register to kernel signals */
	ret = kdbus_add_match_id(reader, 0x1, KDBUS_ITEM_ID_ADD,
				 KDBUS_MATCH_ID_ANY);
	ASSERT_RETURN(ret == 0);

	ret = kdbus_add_match_id(reader, 0x2, KDBUS_ITEM_ID_REMOVE,
				 KDBUS_MATCH_ID_ANY);
	ASSERT_RETURN(ret == 0);

	ret = install_name_add_match(reader, name);
	ASSERT_RETURN(ret == 0);

	/* Monitor connections are not supported on custom endpoints */
	ep_conn = kdbus_hello(ep, KDBUS_HELLO_MONITOR, NULL, 0);
	ASSERT_RETURN(!ep_conn && errno == EOPNOTSUPP);

	ep_conn = kdbus_hello(ep, 0, NULL, 0);
	ASSERT_RETURN(ep_conn);

	/* Check that the reader got the IdAdd notification */
	ret = kdbus_msg_recv(reader, &msg, NULL);
	ASSERT_RETURN(ret == 0);
	ASSERT_RETURN(msg->items[0].type == KDBUS_ITEM_ID_ADD);
	ASSERT_RETURN(msg->items[0].id_change.id == ep_conn->id);
	kdbus_msg_free(msg);

	/*
	 * Add a name add match on the endpoint connection, acquire name from
	 * the unfiltered connection, and make sure the filtered connection
	 * did not get the notification on the name owner change. Also, the
	 * endpoint connection may not be able to call conn_info, neither on
	 * the name nor on the ID.
	 */
	ret = install_name_add_match(ep_conn, name);
	ASSERT_RETURN(ret == 0);

	ret = kdbus_name_acquire(env->conn, name, NULL);
	ASSERT_RETURN(ret == 0);

	ret = kdbus_msg_recv(ep_conn, NULL, NULL);
	ASSERT_RETURN(ret == -EAGAIN);

	ret = kdbus_conn_info(ep_conn, 0, name, 0, NULL);
	ASSERT_RETURN(ret == -ESRCH);

	ret = kdbus_conn_info(ep_conn, 0, "random.crappy.name", 0, NULL);
	ASSERT_RETURN(ret == -ESRCH);

	ret = kdbus_conn_info(ep_conn, env->conn->id, NULL, 0, NULL);
	ASSERT_RETURN(ret == -ENXIO);

	ret = kdbus_conn_info(ep_conn, 0x0fffffffffffffffULL, NULL, 0, NULL);
	ASSERT_RETURN(ret == -ENXIO);

	/* Check that the reader did not receive the name notification */
	ret = kdbus_msg_recv(reader, NULL, NULL);
	ASSERT_RETURN(ret == -EAGAIN);

	/*
	 * Release the name again, update the custom endpoint policy,
	 * and try again. This time, the connection on the custom endpoint
	 * should have gotten it.
	 */
	ret = kdbus_name_release(env->conn, name);
	ASSERT_RETURN(ret == 0);

	/* Check that the reader did not receive the name notification */
	ret = kdbus_msg_recv(reader, NULL, NULL);
	ASSERT_RETURN(ret == -EAGAIN);

	ret = update_endpoint(ep_fd, name);
	ASSERT_RETURN(ret == 0);

	ret = kdbus_name_acquire(env->conn, name, NULL);
	ASSERT_RETURN(ret == 0);

	ret = kdbus_msg_recv(ep_conn, &msg, NULL);
	ASSERT_RETURN(ret == 0);
	ASSERT_RETURN(msg->items[0].type == KDBUS_ITEM_NAME_ADD);
	ASSERT_RETURN(msg->items[0].name_change.old_id.id == 0);
	ASSERT_RETURN(msg->items[0].name_change.new_id.id == env->conn->id);
	ASSERT_RETURN(strcmp(msg->items[0].name_change.name, name) == 0);
	kdbus_msg_free(msg);

	ret = kdbus_msg_recv(reader, &msg, NULL);
	ASSERT_RETURN(ret == 0);
	ASSERT_RETURN(strcmp(msg->items[0].name_change.name, name) == 0);

	kdbus_msg_free(msg);

	ret = kdbus_conn_info(ep_conn, 0, name, 0, NULL);
	ASSERT_RETURN(ret == 0);

	ret = kdbus_conn_info(ep_conn, env->conn->id, NULL, 0, NULL);
	ASSERT_RETURN(ret == 0);

	/* If we have privileges test custom endpoints */
	ret = test_is_capable(CAP_SETUID, CAP_SETGID, -1);
	ASSERT_RETURN(ret >= 0);

	/*
	 * All uids/gids are mapped and we have the necessary caps
	 */
	if (ret && all_uids_gids_are_mapped()) {
		ret = unpriv_test_custom_ep(env->buspath);
		ASSERT_RETURN(ret == 0);
	}

	kdbus_conn_free(reader);
	kdbus_conn_free(ep_conn);
	close(ep_fd);

	return TEST_OK;
}