summaryrefslogtreecommitdiff
path: root/match.c
blob: 04fb82f87f2dc79dad6d8cfb65183c1b623ed694 (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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
/*
 * 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.
 */

#define pr_fmt(fmt)	KBUILD_MODNAME ": " fmt

#include <linux/fs.h>
#include <linux/slab.h>
#include <linux/sched.h>
#include <linux/mutex.h>
#include <linux/init.h>
#include <linux/device.h>
#include <linux/hash.h>
#include <linux/uaccess.h>
#include <linux/sizes.h>

#include "match.h"
#include "connection.h"
#include "endpoint.h"
#include "message.h"
#include "bus.h"

struct kdbus_match_db_entry_item {
	u64 type;
	union {
		char	*name;
		u64	*bloom;
		u64	id;
	};

	struct list_head	list_entry;
};

struct kdbus_match_db_entry {
	u64			id;
	u64			cookie;
	u64			src_id;
	struct list_head	list_entry;
	struct list_head	items_list;
};

static void
kdbus_match_db_entry_item_free(struct kdbus_match_db_entry_item *item)
{
	switch (item->type) {
	case KDBUS_MATCH_BLOOM:
		kfree(item->bloom);
		break;

	case KDBUS_MATCH_SRC_NAME:
	case KDBUS_MATCH_NAME_ADD:
	case KDBUS_MATCH_NAME_REMOVE:
	case KDBUS_MATCH_NAME_CHANGE:
		kfree(item->name);
		break;

	case KDBUS_MATCH_ID_ADD:
	case KDBUS_MATCH_ID_REMOVE:
		break;
	}

	list_del(&item->list_entry);
	kfree(item);
}

static void kdbus_match_db_entry_free(struct kdbus_match_db_entry *e)
{
	struct kdbus_match_db_entry_item *ei, *ei_tmp;

	list_for_each_entry_safe(ei, ei_tmp, &e->items_list, list_entry)
		kdbus_match_db_entry_item_free(ei);

	list_del(&e->list_entry);
	kfree(e);
}

static void __kdbus_match_db_free(struct kref *kref)
{
	struct kdbus_match_db_entry *e, *tmp;
	struct kdbus_match_db *db =
		container_of(kref, struct kdbus_match_db, kref);

	mutex_lock(&db->entries_lock);
	list_for_each_entry_safe(e, tmp, &db->entries, list_entry)
		kdbus_match_db_entry_free(e);
	mutex_unlock(&db->entries_lock);

	kfree(db);
}

void kdbus_match_db_unref(struct kdbus_match_db *db)
{
	kref_put(&db->kref, __kdbus_match_db_free);
}

struct kdbus_match_db *kdbus_match_db_new(void)
{
	struct kdbus_match_db *db;

	db = kzalloc(sizeof(*db), GFP_KERNEL);
	if (!db)
		return NULL;

	kref_init(&db->kref);
	mutex_init(&db->entries_lock);
	INIT_LIST_HEAD(&db->entries);

	return db;
}

static inline
bool kdbus_match_db_test_bloom(const u64 *filter,
			       const u64 *mask,
			       size_t len)
{
	size_t i;

	for (i = 0; i < len; i++)
		if ((filter[i] & mask[i]) != mask[i])
			return false;

	return true;
}

static inline
bool kdbus_match_db_test_src_names(const char *haystack,
				   size_t haystack_size,
				   const char *needle)
{
	size_t i;

	for (i = 0; i < haystack_size; i += strlen(haystack) + 1)
		if (strcmp(haystack + i, needle) == 0)
			return true;

	return false;
}

static
bool kdbus_match_db_match_with_src(struct kdbus_match_db *db,
				   struct kdbus_conn *conn_src,
				   struct kdbus_kmsg *kmsg)
{
	struct kdbus_match_db_entry *e;
	bool matched = false;

	mutex_lock(&db->entries_lock);
	list_for_each_entry(e, &db->entries, list_entry) {
		struct kdbus_match_db_entry_item *ei;

		if (e->src_id != KDBUS_MATCH_SRC_ID_ANY &&
		    e->src_id != conn_src->id)
			continue;

		matched = true;

		list_for_each_entry(ei, &e->items_list, list_entry) {
			if (kmsg->bloom) {
				size_t bloom_size =
					conn_src->ep->bus->bloom_size / sizeof(u64);

				if (!kdbus_match_db_test_bloom(ei->bloom,
							       kmsg->bloom,
							       bloom_size)) {
					matched = false;
					break;
				}
			}

			if (kmsg->src_names) {
				if (!kdbus_match_db_test_src_names(kmsg->src_names,
								   kmsg->src_names_len,
								   ei->name)) {
					matched = false;
					break;
				}
			}
		}

		if (matched)
			break;
	}
	mutex_unlock(&db->entries_lock);

	return matched;
}

static
bool kdbus_match_db_match_from_kernel(struct kdbus_match_db *db,
				      struct kdbus_conn *conn_dst,
				      struct kdbus_kmsg *kmsg)
{
	u64 type = kmsg->notification_type;
	struct kdbus_match_db_entry *e;
	bool matched = false;

	mutex_lock(&db->entries_lock);
	list_for_each_entry(e, &db->entries, list_entry) {
		struct kdbus_match_db_entry_item *ei;

		if (e->src_id != KDBUS_MATCH_SRC_ID_ANY &&
		    e->src_id != 0)
			continue;

		matched = true;

		list_for_each_entry(ei, &e->items_list, list_entry) {
			if (ei->type == KDBUS_MATCH_ID_ADD &&
			    type != KDBUS_MSG_ID_ADD) {
				matched = false;
				break;
			}

			if (ei->type == KDBUS_MATCH_ID_REMOVE &&
			    type != KDBUS_MSG_ID_REMOVE) {
				matched = false;
				break;
			}

			if (ei->type == KDBUS_MATCH_NAME_ADD &&
			    type != KDBUS_MSG_NAME_ADD) {
				matched = false;
				break;
			}

			if (ei->type == KDBUS_MATCH_NAME_CHANGE &&
			    type != KDBUS_MSG_NAME_CHANGE) {
				matched = false;
				break;
			}

			if (ei->type == KDBUS_MATCH_NAME_REMOVE &&
			    type != KDBUS_MSG_NAME_REMOVE) {
				matched = false;
				break;
			}
		}

		if (matched)
			break;
	}
	mutex_unlock(&db->entries_lock);

	return matched;
}

bool kdbus_match_db_match_kmsg(struct kdbus_match_db *db,
			       struct kdbus_conn *conn_src,
			       struct kdbus_conn *conn_dst,
			       struct kdbus_kmsg *kmsg)
{
	if (conn_src)
		return kdbus_match_db_match_with_src(db, conn_src, kmsg);
	else
		return kdbus_match_db_match_from_kernel(db, conn_dst, kmsg);
}

static
struct kdbus_cmd_match *cmd_match_from_user(void __user *buf)
{
	struct kdbus_cmd_match *cmd_match;
	u64 size;

	if (kdbus_size_get_user(size, buf, struct kdbus_cmd_match))
		return ERR_PTR(-EFAULT);

	if (size < sizeof(*cmd_match) || size > KDBUS_MATCH_MAX_SIZE)
		return ERR_PTR(-EMSGSIZE);

	return memdup_user(buf, size);
}

int kdbus_cmd_match_db_add(struct kdbus_conn *conn, void __user *buf)
{
	struct kdbus_match_db *db = conn->match_db;
	struct kdbus_cmd_match *cmd_match;
	struct kdbus_item *item;
	struct kdbus_match_db_entry *e;
	int ret = 0;

	cmd_match = cmd_match_from_user(buf);
	if (IS_ERR(cmd_match))
		return PTR_ERR(cmd_match);

	e = kzalloc(sizeof(*e), GFP_KERNEL);
	if (!e)
		return -ENOMEM;

	mutex_lock(&db->entries_lock);
	INIT_LIST_HEAD(&e->list_entry);
	INIT_LIST_HEAD(&e->items_list);
	e->id = cmd_match->id;
	e->src_id = cmd_match->src_id;
	e->cookie = cmd_match->cookie;

	KDBUS_ITEM_FOREACH_VALIDATE(item, cmd_match) {
		struct kdbus_match_db_entry_item *ei;
		size_t size;

		if (item->size < sizeof(*item)) {
			ret = -EBADMSG;
			break;
		}

		ei = kzalloc(sizeof(*ei), GFP_KERNEL);
		if (!ei) {
			ret = -ENOMEM;
			break;
		}

		ei->type = item->type;
		INIT_LIST_HEAD(&ei->list_entry);

		switch (item->type) {
		case KDBUS_MATCH_BLOOM:
			size = item->size - offsetof(struct kdbus_item, data);
			if (size != conn->ep->bus->bloom_size) {
				ret = -EBADMSG;
				break;
			}

			ei->bloom = kmemdup(item->data, item->size - offsetof(struct kdbus_item, data),
					GFP_KERNEL);
			break;

		case KDBUS_MATCH_SRC_NAME:
		case KDBUS_MATCH_NAME_ADD:
		case KDBUS_MATCH_NAME_REMOVE:
		case KDBUS_MATCH_NAME_CHANGE:
			ei->name = kstrdup(item->str, GFP_KERNEL);
			if (!ei->name)
				ret = -ENOMEM;
			break;

		case KDBUS_MATCH_ID_ADD:
		case KDBUS_MATCH_ID_REMOVE:
			ei->id = item->id;
			break;
		}

		list_add_tail(&ei->list_entry, &e->items_list);
	}

	if (ret >= 0) {
		list_add_tail(&e->list_entry, &db->entries);
	} else {
		kdbus_match_db_entry_free(e);
		kfree(e);
	}

	mutex_unlock(&db->entries_lock);
	kfree(cmd_match);

	return ret;
}

int kdbus_cmd_match_db_remove(struct kdbus_match_db *db, void __user *buf)
{
	struct kdbus_cmd_match *cmd_match;
	struct kdbus_match_db_entry *e, *tmp;

	cmd_match = cmd_match_from_user(buf);
	if (IS_ERR(cmd_match))
		return PTR_ERR(cmd_match);

	mutex_lock(&db->entries_lock);
	list_for_each_entry_safe(e, tmp, &db->entries, list_entry)
		if (e->cookie == cmd_match->cookie &&
		    e->id == cmd_match->id)
			kdbus_match_db_entry_free(e);
	mutex_unlock(&db->entries_lock);

	kfree(cmd_match);

	return 0;
}