summaryrefslogtreecommitdiff
path: root/bus.c
blob: 066d74c9fab19bfe8bbcc312fe9896acc7c89638 (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
/*
 * Copyright (C) 2013 Kay Sievers
 * Copyright (C) 2013 Greg Kroah-Hartman <gregkh@linuxfoundation.org>
 * Copyright (C) 2013 Daniel Mack <daniel@zonque.org>
 * Copyright (C) 2013 Linux Foundation
 *
 * kdbus is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the
 * Free Software Foundation; either version 2.1 of the License, or (at
 * your option) any later version.
 */

#include <linux/module.h>
#include <linux/device.h>
#include <linux/fs.h>
#include <linux/idr.h>
#include <linux/slab.h>
#include <linux/sched.h>
#include <linux/init.h>
#include <linux/hashtable.h>
#include <linux/uaccess.h>
#include <linux/sizes.h>
#include <linux/random.h>

#include "bus.h"
#include "connection.h"
#include "names.h"
#include "endpoint.h"
#include "namespace.h"

bool kdbus_bus_uid_is_privileged(const struct kdbus_bus *bus)
{
	if (capable(CAP_IPC_OWNER))
		return true;

	if (uid_eq(bus->uid_owner, current_fsuid()))
		return true;

	return false;
}

/**
 * kdbus_bus_ref() - increase the reference counter of a kdbus_bus
 * @bus:		The bus to unref
 *
 * Every user of a bus, except for its creator, must add a reference to the
 * kdbus_bus using this function.
 */
struct kdbus_bus *kdbus_bus_ref(struct kdbus_bus *bus)
{
	kref_get(&bus->kref);
	return bus;
}

static void __kdbus_bus_free(struct kref *kref)
{
	struct kdbus_bus *bus = container_of(kref, struct kdbus_bus, kref);

	kdbus_name_registry_unref(bus->name_registry);
	kdbus_bus_disconnect(bus);

	kfree(bus->name);
	kfree(bus);
}

/**
 * kdbus_bus_unref() - decrease the reference counter of a kdbus_bus
 * @bus:		The bus to unref
 *
 * Release a reference. If the reference count drops to 0, the bus will be
 * freed.
 */
void kdbus_bus_unref(struct kdbus_bus *bus)
{
	kref_put(&bus->kref, __kdbus_bus_free);
}

/**
 * kdbus_bus_find_conn_by_id - find a connection with a given id
 * @bus:		The bus to look for the connection
 * @id:			The 64-bit connection id
 *
 * Looks up a connection with a given id. The returned connection
 * is ref'ed, and needs to be unref'ed by the user. Returns NULL if
 * the connection can't be found.
 *
 * This function must be called with bus->lock held.
 */
struct kdbus_conn *kdbus_bus_find_conn_by_id(struct kdbus_bus *bus, u64 id)
{
	struct kdbus_conn *conn, *found = NULL;

	hash_for_each_possible(bus->conn_hash, conn, hentry, id)
		if (conn->id == id) {
			found = kdbus_conn_ref(conn);
			break;
		}

	return found;
}

/**
 * kdbus_bus_disconnect() - disconnect a bus
 * @bus:		The kdbus reference
 *
 * The passed bus will be disconnected and the associated endpoint will be
 * unref'ed.
 */
void kdbus_bus_disconnect(struct kdbus_bus *bus)
{
	struct kdbus_ep *ep, *tmp;

	if (bus->disconnected)
		return;
	bus->disconnected = true;
	list_del(&bus->ns_entry);

	/* remove any endpoints attached to this bus */
	list_for_each_entry_safe(ep, tmp, &bus->eps_list, bus_entry) {
		kdbus_ep_disconnect(ep);
		kdbus_ep_unref(ep);
	}
}

static struct kdbus_bus *kdbus_bus_find(struct kdbus_ns *ns, const char *name)
{
	struct kdbus_bus *bus = NULL;
	struct kdbus_bus *b;

	mutex_lock(&ns->lock);
	list_for_each_entry(b, &ns->bus_list, ns_entry) {
		if (strcmp(b->name, name))
			continue;

		bus = kdbus_bus_ref(b);
		break;
	}

	mutex_unlock(&ns->lock);
	return bus;
}

/**
 * kdbus_bus_new() - create a new bus
 * @ns:			The namespace to work on
 * @bus_make:		Pointer to a struct kdbus_cmd_bus_make containing the
 *			details for the bus creation
 * @mode:		The access mode for the device node
 * @uid:		The uid of the device node
 * @gid:		The gid of the device node
 * @bus:		Pointer to a reference where the new bus is stored
 *
 * This function will allocate a new kdbus_bus and link it to the given
 * namespace.
 *
 * Returns: 0 on success, negative errno on failure.
 */
int kdbus_bus_new(struct kdbus_ns *ns,
		  struct kdbus_cmd_bus_make *bus_make, const char *name,
		  umode_t mode, kuid_t uid, kgid_t gid, struct kdbus_bus **bus)
{
	char prefix[16];
	struct kdbus_bus *b;
	int ret;

	/* enforce "$UID-" prefix */
	snprintf(prefix, sizeof(prefix), "%u-", from_kuid(current_user_ns(), uid));
	if (strncmp(name, prefix, strlen(prefix) != 0))
		return -EPERM;

	b = kdbus_bus_find(ns, name);
	if (b) {
		kdbus_bus_unref(b);
		return -EEXIST;
	}

	b = kzalloc(sizeof(struct kdbus_bus), GFP_KERNEL);
	if (!b)
		return -ENOMEM;

	kref_init(&b->kref);
	b->uid_owner = uid;
	b->ns = ns;
	b->bus_flags = bus_make->flags;
	b->bloom_size = bus_make->bloom_size;
	b->conn_id_next = 1; /* connection 0 == kernel */
	mutex_init(&b->lock);
	hash_init(b->conn_hash);
	INIT_LIST_HEAD(&b->eps_list);
	INIT_LIST_HEAD(&b->monitors_list);

	/* generate unique ID for this bus */
	get_random_bytes(b->id128, sizeof(b->id128));

	/* Set UUID version to 4 --- truly random generation */
	b->id128[6] &= 0x0f;
	b->id128[6] |= 0x40;

	/* Set the UUID variant to DCE */
	b->id128[8] &= 0x3f;
	b->id128[8] |= 0x80;

	b->name = kstrdup(name, GFP_KERNEL);
	if (!b->name) {
		ret = -ENOMEM;
		goto exit;
	}

	ret = kdbus_name_registry_new(&b->name_registry);
	if (ret < 0)
		goto exit;

	ret = kdbus_ep_new(b, "bus", mode, uid, gid,
			   b->bus_flags & KDBUS_MAKE_POLICY_OPEN);
	if (ret < 0)
		goto exit;

	mutex_lock(&ns->lock);
	b->id = ns->bus_id_next++;

	list_add_tail(&b->ns_entry, &ns->bus_list);
	mutex_unlock(&ns->lock);

	*bus = b;
	return 0;

exit:
	kdbus_bus_unref(b);
	return ret;
}

/**
 * kdbus_bus_make_user() - create a kdbus_cmd_bus_make from user-supplied data
 * @buf:		The user supplied data from the ioctl() call
 * @make:		Reference to the location where to store the result
 * @name:		Shortcut to the requested name
 *
 * This function is part of the connection ioctl() interface and will parse
 * the user-supplied data.
 *
 * Returns: 0 on success, negative errno on failure.
 */
int kdbus_bus_make_user(void __user *buf,
			struct kdbus_cmd_bus_make **make, char **name)
{
	u64 size;
	struct kdbus_cmd_bus_make *m;
	const char *n = NULL;
	const struct kdbus_item *item;
	int ret;

	if (kdbus_size_get_user(&size, buf, struct kdbus_cmd_bus_make))
		return -EFAULT;

	if (size < sizeof(struct kdbus_cmd_bus_make) || size > KDBUS_MAKE_MAX_SIZE)
		return -EMSGSIZE;

	m = memdup_user(buf, size);
	if (IS_ERR(m)) {
		ret = PTR_ERR(m);
		goto exit;
	}

	KDBUS_PART_FOREACH(item, m, items) {
		if (!KDBUS_PART_VALID(item, m)) {
			ret = -EINVAL;
			goto exit;
		}

		switch (item->type) {
		case KDBUS_MAKE_NAME:
			if (n) {
				ret = -EEXIST;
				goto exit;
			}

			if (item->size < KDBUS_PART_HEADER_SIZE + 2) {
				ret = -EINVAL;
				goto exit;
			}

			if (item->size > KDBUS_PART_HEADER_SIZE + KDBUS_MAKE_MAX_LEN + 1) {
				ret = -ENAMETOOLONG;
				goto exit;
			}

			if (!kdbus_validate_nul(item->str,
					item->size - KDBUS_PART_HEADER_SIZE)) {
				ret = -EINVAL;
				goto exit;
			}

			n = item->str;
			continue;

		default:
			ret = -ENOTSUPP;
			goto exit;
		}
	}

	if (!KDBUS_PART_END(item, m))
		return -EINVAL;

	if (!n) {
		ret = -EBADMSG;
		goto exit;
	}

	if (!KDBUS_IS_ALIGNED8(m->bloom_size)) {
		ret = -EINVAL;
		goto exit;
	}

	if (m->bloom_size < 8 || m->bloom_size > 16 * 1024) {
		ret = -EINVAL;
		goto exit;
	}

	*make = m;
	*name = (char *)n;
	return 0;

exit:
	kfree(m);
	return ret;
}