summaryrefslogtreecommitdiff
path: root/bus.c
blob: 88836b715a82d5392fee89a478c8178611cd2427 (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
/*
 * Copyright (C) 2013 Kay Sievers
 * Copyright (C) 2013 Greg Kroah-Hartman <gregkh@linuxfoundation.org>
 * Copyright (C) 2013 Daniel Mack
 * 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.
 */

#define pr_fmt(fmt)	KBUILD_MODNAME ": " fmt

#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 "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);
	pr_debug("clean up bus %s/%s\n", bus->ns->devpath, bus->name);

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

/**
 * kdbus_bus_unref() - decrease the reference counter of a kdbus_bus
 * @bus:	The bus to unref
 *
 * Give up a retain. 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);
}

struct kdbus_conn *kdbus_bus_find_conn_by_id(struct kdbus_bus *bus, u64 id)
{
	struct kdbus_conn *conn;

	hash_for_each_possible(bus->conn_hash, conn, hentry, id)
		if (conn->id == id)
			return conn;

	return NULL;
}

/**
 * kdbus_bus_disconnect() - disconnect a kdbus_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->bus_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);
	}

	pr_debug("closing bus %s/%s\n", bus->ns->devpath, bus->name);
}

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, bus_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 struct kdbus_bus
 * @ns:		The namespace to work on
 * @bus_kmake:	Pointer to a struct kdbus_cmd_bus_kmake 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.
 *
 * Return: 0 on success, < 0 on failure
 */
int kdbus_bus_new(struct kdbus_ns *ns, struct kdbus_cmd_bus_kmake *bus_kmake,
		  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(bus_kmake->name, prefix, strlen(prefix) != 0))
		return -EPERM;

	b = kdbus_bus_find(ns, bus_kmake->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_kmake->make.flags;
	b->bloom_size = bus_kmake->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);

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

	b->name_registry = kdbus_name_registry_new();
	if (!b->name_registry) {
		ret = -ENOMEM;
		goto ret;
	}

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

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

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

	*bus = b;
	pr_debug("created bus %llu '%s/%s'\n",
		 (unsigned long long)b->id, ns->devpath, b->name);
	return 0;
ret:
	kdbus_bus_unref(b);
	return ret;
}

/**
 * kdbus_bus_make_user() - create a kdbus_cmd_bus_kmake from user-supplied data
 * @buf:	The user supplied data from the ioctl() call
 * @kmage:	Reference to the location where to store the result.
 *
 * This function is part of the connection ioctl() interface and will parse
 * the user-supplied data.
 *
 * Return: 0 on success, < 0 on failure
 */
int kdbus_bus_make_user(void __user *buf, struct kdbus_cmd_bus_kmake **kmake)
{
	u64 size;
	struct kdbus_cmd_bus_kmake *km;
	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;

	km = kmalloc(sizeof(struct kdbus_cmd_bus_kmake) + size, GFP_KERNEL);
	if (!km)
		return -ENOMEM;

	memset(km, 0, offsetof(struct kdbus_cmd_bus_kmake, make));
	if (copy_from_user(&km->make, buf, size)) {
		ret = -EFAULT;
		goto exit;
	}

	KDBUS_PART_FOREACH(item, &km->make, items) {
		if (!KDBUS_PART_VALID(item, &km->make)) {
			ret = -EINVAL;
			goto exit;
		}

		switch (item->type) {
		case KDBUS_MAKE_NAME:
			if (km->name) {
				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;
			}

			km->name = item->str;
			continue;

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

	if (!KDBUS_PART_END(item, &km->make))
		return -EINVAL;

	if (!km->name) {
		ret = -EBADMSG;
		goto exit;
	}

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

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

	*kmake = km;
	return 0;

exit:
	kfree(km);
	return ret;
}