summaryrefslogtreecommitdiff
path: root/tools/testing/selftests/kdbus/test-activator.c
blob: 3d1b76370ce8207ab7705fbd4dfd0938e8b73e73 (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
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stddef.h>
#include <unistd.h>
#include <stdint.h>
#include <errno.h>
#include <assert.h>
#include <poll.h>
#include <sys/capability.h>
#include <sys/types.h>
#include <sys/wait.h>

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

static int kdbus_starter_poll(struct kdbus_conn *conn)
{
	int ret;
	struct pollfd fd;

	fd.fd = conn->fd;
	fd.events = POLLIN | POLLPRI | POLLHUP;
	fd.revents = 0;

	ret = poll(&fd, 1, 100);
	if (ret == 0)
		return -ETIMEDOUT;
	else if (ret > 0) {
		if (fd.revents & POLLIN)
			return 0;

		if (fd.revents & (POLLHUP | POLLERR))
			ret = -ECONNRESET;
	}

	return ret;
}

/* Ensure that kdbus activator logic is safe */
static int kdbus_priv_activator(struct kdbus_test_env *env)
{
	int ret;
	struct kdbus_msg *msg = NULL;
	uint64_t cookie = 0xdeadbeef;
	uint64_t flags = KDBUS_NAME_REPLACE_EXISTING;
	struct kdbus_conn *activator;
	struct kdbus_conn *service;
	struct kdbus_conn *client;
	struct kdbus_conn *holder;
	struct kdbus_policy_access *access;

	access = (struct kdbus_policy_access[]){
		{
			.type = KDBUS_POLICY_ACCESS_USER,
			.id = getuid(),
			.access = KDBUS_POLICY_OWN,
		},
		{
			.type = KDBUS_POLICY_ACCESS_USER,
			.id = getuid(),
			.access = KDBUS_POLICY_TALK,
		},
	};

	activator = kdbus_hello_activator(env->buspath, "foo.priv.activator",
					  access, 2);
	ASSERT_RETURN(activator);

	service = kdbus_hello(env->buspath, 0, NULL, 0);
	ASSERT_RETURN(service);

	client = kdbus_hello(env->buspath, 0, NULL, 0);
	ASSERT_RETURN(client);

	/*
	 * Make sure that other users can't TALK to the activator
	 */

	ret = RUN_UNPRIVILEGED_CONN(unpriv, env->buspath, ({
		/* Try to talk using the ID */
		ret = kdbus_msg_send(unpriv, NULL, 0xdeadbeef, 0, 0,
				     0, activator->id);
		ASSERT_EXIT(ret == -ENXIO);

		/* Try to talk to the name */
		ret = kdbus_msg_send(unpriv, "foo.priv.activator",
				     0xdeadbeef, 0, 0, 0,
				     KDBUS_DST_ID_NAME);
		ASSERT_EXIT(ret == -EPERM);
	}));
	ASSERT_RETURN(ret >= 0);

	/*
	 * Make sure that we did not receive anything, so the
	 * service will not be started automatically
	 */

	ret = kdbus_starter_poll(activator);
	ASSERT_RETURN(ret == -ETIMEDOUT);

	/*
	 * Now try to emulate the starter/service logic and
	 * acquire the name.
	 */

	cookie++;
	ret = kdbus_msg_send(service, "foo.priv.activator", cookie,
			     0, 0, 0, KDBUS_DST_ID_NAME);
	ASSERT_RETURN(ret == 0);

	ret = kdbus_starter_poll(activator);
	ASSERT_RETURN(ret == 0);

	/* Policies are still checked, access denied */

	ret = RUN_UNPRIVILEGED_CONN(unpriv, env->buspath, ({
		ret = kdbus_name_acquire(unpriv, "foo.priv.activator",
					 &flags);
		ASSERT_RETURN(ret == -EPERM);
	}));
	ASSERT_RETURN(ret >= 0);

	ret = kdbus_name_acquire(service, "foo.priv.activator",
				 &flags);
	ASSERT_RETURN(ret == 0);

	/* We read our previous starter message */

	ret = kdbus_msg_recv_poll(service, 100, NULL, NULL);
	ASSERT_RETURN(ret == 0);

	/* Try to talk, we still fail */

	cookie++;
	ret = RUN_UNPRIVILEGED_CONN(unpriv, env->buspath, ({
		/* Try to talk to the name */
		ret = kdbus_msg_send(unpriv, "foo.priv.activator",
				     cookie, 0, 0, 0,
				     KDBUS_DST_ID_NAME);
		ASSERT_EXIT(ret == -EPERM);
	}));
	ASSERT_RETURN(ret >= 0);

	/* Still nothing to read */

	ret = kdbus_msg_recv_poll(service, 100, NULL, NULL);
	ASSERT_RETURN(ret == -ETIMEDOUT);

	/* We receive every thing now */

	cookie++;
	ret = kdbus_msg_send(client, "foo.priv.activator", cookie,
			     0, 0, 0, KDBUS_DST_ID_NAME);
	ASSERT_RETURN(ret == 0);
	ret = kdbus_msg_recv_poll(service, 100, &msg, NULL);
	ASSERT_RETURN(ret == 0 && msg->cookie == cookie);

	kdbus_msg_free(msg);

	/* Policies default to deny TALK now */
	kdbus_conn_free(activator);

	cookie++;
	ret = RUN_UNPRIVILEGED_CONN(unpriv, env->buspath, ({
		/* Try to talk to the name */
		ret = kdbus_msg_send(unpriv, "foo.priv.activator",
				     cookie, 0, 0, 0,
				     KDBUS_DST_ID_NAME);
		ASSERT_EXIT(ret == -EPERM);
	}));
	ASSERT_RETURN(ret >= 0);

	ret = kdbus_msg_recv_poll(service, 100, NULL, NULL);
	ASSERT_RETURN(ret == -ETIMEDOUT);

	/* Same user is able to TALK */
	cookie++;
	ret = kdbus_msg_send(client, "foo.priv.activator", cookie,
			     0, 0, 0, KDBUS_DST_ID_NAME);
	ASSERT_RETURN(ret == 0);
	ret = kdbus_msg_recv_poll(service, 100, &msg, NULL);
	ASSERT_RETURN(ret == 0 && msg->cookie == cookie);

	kdbus_msg_free(msg);

	access = (struct kdbus_policy_access []){
		{
			.type = KDBUS_POLICY_ACCESS_WORLD,
			.id = getuid(),
			.access = KDBUS_POLICY_TALK,
		},
	};

	holder = kdbus_hello_registrar(env->buspath, "foo.priv.activator",
				       access, 1, KDBUS_HELLO_POLICY_HOLDER);
	ASSERT_RETURN(holder);

	/* Now we are able to TALK to the name */

	cookie++;
	ret = RUN_UNPRIVILEGED_CONN(unpriv, env->buspath, ({
		/* Try to talk to the name */
		ret = kdbus_msg_send(unpriv, "foo.priv.activator",
				     cookie, 0, 0, 0,
				     KDBUS_DST_ID_NAME);
		ASSERT_EXIT(ret == 0);
	}));
	ASSERT_RETURN(ret >= 0);

	ret = kdbus_msg_recv_poll(service, 100, NULL, NULL);
	ASSERT_RETURN(ret == 0);

	ret = RUN_UNPRIVILEGED_CONN(unpriv, env->buspath, ({
		ret = kdbus_name_acquire(unpriv, "foo.priv.activator",
					 &flags);
		ASSERT_RETURN(ret == -EPERM);
	}));
	ASSERT_RETURN(ret >= 0);

	kdbus_conn_free(service);
	kdbus_conn_free(client);
	kdbus_conn_free(holder);

	return 0;
}

int kdbus_test_activator(struct kdbus_test_env *env)
{
	int ret;
	struct kdbus_conn *activator;
	struct pollfd fds[2];
	bool activator_done = false;
	struct kdbus_policy_access access[2];

	access[0].type = KDBUS_POLICY_ACCESS_USER;
	access[0].id = getuid();
	access[0].access = KDBUS_POLICY_OWN;

	access[1].type = KDBUS_POLICY_ACCESS_WORLD;
	access[1].access = KDBUS_POLICY_TALK;

	activator = kdbus_hello_activator(env->buspath, "foo.test.activator",
					  access, 2);
	ASSERT_RETURN(activator);

	ret = kdbus_add_match_empty(env->conn);
	ASSERT_RETURN(ret == 0);

	ret = kdbus_list(env->conn, KDBUS_LIST_NAMES |
				    KDBUS_LIST_UNIQUE |
				    KDBUS_LIST_ACTIVATORS |
				    KDBUS_LIST_QUEUED);
	ASSERT_RETURN(ret == 0);

	ret = kdbus_msg_send(env->conn, "foo.test.activator", 0xdeafbeef,
			     0, 0, 0, KDBUS_DST_ID_NAME);
	ASSERT_RETURN(ret == 0);

	fds[0].fd = activator->fd;
	fds[1].fd = env->conn->fd;

	kdbus_printf("-- entering poll loop ...\n");

	for (;;) {
		int i, nfds = sizeof(fds) / sizeof(fds[0]);

		for (i = 0; i < nfds; i++) {
			fds[i].events = POLLIN | POLLPRI;
			fds[i].revents = 0;
		}

		ret = poll(fds, nfds, 3000);
		ASSERT_RETURN(ret >= 0);

		ret = kdbus_list(env->conn, KDBUS_LIST_NAMES);
		ASSERT_RETURN(ret == 0);

		if ((fds[0].revents & POLLIN) && !activator_done) {
			uint64_t flags = KDBUS_NAME_REPLACE_EXISTING;

			kdbus_printf("Starter was called back!\n");

			ret = kdbus_name_acquire(env->conn,
						 "foo.test.activator", &flags);
			ASSERT_RETURN(ret == 0);

			activator_done = true;
		}

		if (fds[1].revents & POLLIN) {
			kdbus_msg_recv(env->conn, NULL, NULL);
			break;
		}
	}

	/* Check if all uids/gids are mapped */
	if (!all_uids_gids_are_mapped())
		return TEST_SKIP;

	/* Check now capabilities, so we run the previous tests */
	ret = test_is_capable(CAP_SETUID, CAP_SETGID, -1);
	ASSERT_RETURN(ret >= 0);

	if (!ret)
		return TEST_SKIP;

	ret = kdbus_priv_activator(env);
	ASSERT_RETURN(ret == 0);

	kdbus_conn_free(activator);

	return TEST_OK;
}