forked from gregkh/kdbus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
endpoint.c
316 lines (269 loc) · 6.35 KB
/
endpoint.c
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
/*
* 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/device.h>
#include <linux/fs.h>
#include <linux/idr.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/sched.h>
#include <linux/sizes.h>
#include <linux/slab.h>
#include <linux/uaccess.h>
#include "bus.h"
#include "endpoint.h"
#include "namespace.h"
#include "policy.h"
/* endpoints are by default owned by the bus owner */
static char *kdbus_devnode_ep(struct device *dev, umode_t *mode,
kuid_t *uid, kgid_t *gid)
{
struct kdbus_ep *ep = dev_get_drvdata(dev);
if (mode)
*mode = ep->mode;
if (uid)
*uid = ep->uid;
if (gid)
*gid = ep->gid;
return NULL;
}
static void kdbus_dev_release(struct device *dev)
{
kfree(dev);
}
static struct device_type kdbus_devtype_ep = {
.name = "ep",
.release = kdbus_dev_release,
.devnode = kdbus_devnode_ep,
};
struct kdbus_ep *kdbus_ep_ref(struct kdbus_ep *ep)
{
kref_get(&ep->kref);
return ep;
}
/**
* kdbus_ep_disconnect() - disconnect an endpoint
* @ep: Endpoint
*/
void kdbus_ep_disconnect(struct kdbus_ep *ep)
{
mutex_lock(&ep->lock);
if (ep->disconnected) {
mutex_unlock(&ep->lock);
return;
}
ep->disconnected = true;
mutex_unlock(&ep->lock);
/* disconnect from bus */
mutex_lock(&ep->bus->lock);
if (ep->bus)
list_del(&ep->bus_entry);
mutex_unlock(&ep->bus->lock);
if (ep->dev) {
device_unregister(ep->dev);
ep->dev = NULL;
}
if (ep->minor > 0) {
idr_remove(&ep->bus->ns->idr, ep->minor);
ep->minor = 0;
}
/*
* wake up the queue so the connections can report
* POLLERR to their users.
*/
wake_up_interruptible(&ep->wait);
}
static void __kdbus_ep_free(struct kref *kref)
{
struct kdbus_ep *ep = container_of(kref, struct kdbus_ep, kref);
kdbus_ep_disconnect(ep);
if (ep->policy_db)
kdbus_policy_db_free(ep->policy_db);
kdbus_bus_unref(ep->bus);
kfree(ep->name);
kfree(ep);
}
struct kdbus_ep *kdbus_ep_unref(struct kdbus_ep *ep)
{
if (!ep)
return NULL;
kref_put(&ep->kref, __kdbus_ep_free);
return NULL;
}
static struct kdbus_ep *kdbus_ep_find(struct kdbus_bus *bus, const char *name)
{
struct kdbus_ep *ep = NULL;
struct kdbus_ep *e;
mutex_lock(&bus->lock);
list_for_each_entry(e, &bus->ep_list, bus_entry) {
if (strcmp(e->name, name) != 0)
continue;
ep = kdbus_ep_ref(e);
}
mutex_unlock(&bus->lock);
return ep;
}
/**
* kdbus_ep_new() - create a new endpoint
* @bus: The bus this endpoint will be created for
* @ns: The namespace of the bus; needed separately when
* creating the default endpoint for a new bus
* @name: The name of the endpoint
* @mode: The access mode for the device node
* @uid: The uid of the device node
* @gid: The gid of the device node
* @policy_open: Default policy of allow or deny
*
* This function will create a new enpoint with the given
* name and properties for a given bus.
*
* Return: 0 on success, negative errno on failure.
*/
int kdbus_ep_new(struct kdbus_bus *bus, struct kdbus_ns *ns, const char *name,
umode_t mode, kuid_t uid, kgid_t gid, bool policy_open)
{
struct kdbus_ep *e;
int ret;
e = kdbus_ep_find(bus, name);
if (e) {
kdbus_ep_unref(e);
return -EEXIST;
}
e = kzalloc(sizeof(struct kdbus_ep), GFP_KERNEL);
if (!e)
return -ENOMEM;
mutex_init(&e->lock);
kref_init(&e->kref);
e->uid = uid;
e->gid = gid;
e->mode = mode;
init_waitqueue_head(&e->wait);
e->name = kstrdup(name, GFP_KERNEL);
if (!e->name) {
ret = -ENOMEM;
goto exit;
}
mutex_lock(&ns->lock);
/* register minor in our endpoint map */
ret = idr_alloc(&ns->idr, e, 1, 0, GFP_KERNEL);
if (ret < 0) {
if (ret == -ENOSPC)
ret = -EEXIST;
mutex_unlock(&ns->lock);
goto exit;
}
e->minor = ret;
mutex_unlock(&ns->lock);
/* register bus endpoint device */
e->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
if (!e->dev) {
ret = -ENOMEM;
goto exit;
}
dev_set_name(e->dev, "%s/%s/%s", ns->devpath, bus->name, name);
e->dev->bus = &kdbus_subsys;
e->dev->type = &kdbus_devtype_ep;
e->dev->devt = MKDEV(ns->major, e->minor);
dev_set_drvdata(e->dev, e);
ret = device_register(e->dev);
if (ret < 0) {
put_device(e->dev);
e->dev = NULL;
goto exit;
}
/* install policy */
e->policy_open = policy_open;
if (!policy_open) {
ret = kdbus_policy_db_new(&e->policy_db);
if (ret < 0)
goto exit;
}
/* link into bus */
mutex_lock(&bus->lock);
e->id = ++bus->ep_seq_last;
e->bus = kdbus_bus_ref(bus);
list_add_tail(&e->bus_entry, &bus->ep_list);
mutex_unlock(&bus->lock);
return 0;
exit:
kdbus_ep_unref(e);
return ret;
}
/**
* kdbus_ep_make_user() - create endpoint data from user data
* @buf: User data
* @make: The returned copy of user data
* @name: The name of the endpoint to create
*
* Return: 0 on success, negative errno on failure.
*/
int kdbus_ep_make_user(void __user *buf,
struct kdbus_cmd_make **make, char **name)
{
u64 size;
struct kdbus_cmd_make *m;
const struct kdbus_item *item;
const char *n = NULL;
int ret;
if (kdbus_size_get_user(&size, buf, struct kdbus_cmd_make))
return -EFAULT;
if (size < sizeof(struct kdbus_cmd_make) || size > KDBUS_MAKE_MAX_SIZE)
return -EMSGSIZE;
m = memdup_user(buf, size);
if (IS_ERR(m))
return PTR_ERR(m);
KDBUS_ITEM_FOREACH(item, m, items) {
if (!KDBUS_ITEM_VALID(item, m)) {
ret = -EINVAL;
goto exit;
}
switch (item->type) {
case KDBUS_ITEM_MAKE_NAME:
if (n) {
ret = -EEXIST;
goto exit;
}
if (item->size < KDBUS_ITEM_HEADER_SIZE + 2) {
ret = -EINVAL;
goto exit;
}
if (item->size > KDBUS_ITEM_HEADER_SIZE +
KDBUS_SYSNAME_MAX_LEN + 1) {
ret = -ENAMETOOLONG;
goto exit;
}
if (!kdbus_validate_nul(item->str,
item->size - KDBUS_ITEM_HEADER_SIZE)) {
ret = -EINVAL;
goto exit;
}
ret = kdbus_sysname_is_valid(item->str);
if (ret < 0)
goto exit;
n = item->str;
continue;
}
}
if (!KDBUS_ITEM_END(item, m)) {
ret = -EINVAL;
goto exit;
}
if (!n) {
ret = -EBADMSG;
goto exit;
}
*make = m;
*name = (char *)n;
return 0;
exit:
kfree(m);
return ret;
}