kdbus: the driver, original and non-working
[platform/kernel/linux-exynos.git] / ipc / kdbus / bus.c
1 /*
2  * Copyright (C) 2013-2015 Kay Sievers
3  * Copyright (C) 2013-2015 Greg Kroah-Hartman <gregkh@linuxfoundation.org>
4  * Copyright (C) 2013-2015 Daniel Mack <daniel@zonque.org>
5  * Copyright (C) 2013-2015 David Herrmann <dh.herrmann@gmail.com>
6  * Copyright (C) 2013-2015 Linux Foundation
7  * Copyright (C) 2014-2015 Djalal Harouni
8  *
9  * kdbus is free software; you can redistribute it and/or modify it under
10  * the terms of the GNU Lesser General Public License as published by the
11  * Free Software Foundation; either version 2.1 of the License, or (at
12  * your option) any later version.
13  */
14
15 #include <linux/fs.h>
16 #include <linux/hashtable.h>
17 #include <linux/init.h>
18 #include <linux/module.h>
19 #include <linux/random.h>
20 #include <linux/sched.h>
21 #include <linux/sizes.h>
22 #include <linux/slab.h>
23 #include <linux/uaccess.h>
24 #include <linux/uio.h>
25
26 #include "bus.h"
27 #include "notify.h"
28 #include "connection.h"
29 #include "domain.h"
30 #include "endpoint.h"
31 #include "handle.h"
32 #include "item.h"
33 #include "match.h"
34 #include "message.h"
35 #include "metadata.h"
36 #include "names.h"
37 #include "policy.h"
38 #include "util.h"
39
40 static void kdbus_bus_free(struct kdbus_node *node)
41 {
42         struct kdbus_bus *bus = container_of(node, struct kdbus_bus, node);
43
44         WARN_ON(!list_empty(&bus->monitors_list));
45         WARN_ON(!hash_empty(bus->conn_hash));
46
47         kdbus_notify_free(bus);
48
49         kdbus_user_unref(bus->creator);
50         kdbus_name_registry_free(bus->name_registry);
51         kdbus_domain_unref(bus->domain);
52         kdbus_policy_db_clear(&bus->policy_db);
53         kdbus_meta_proc_unref(bus->creator_meta);
54         kfree(bus);
55 }
56
57 static void kdbus_bus_release(struct kdbus_node *node, bool was_active)
58 {
59         struct kdbus_bus *bus = container_of(node, struct kdbus_bus, node);
60
61         if (was_active)
62                 atomic_dec(&bus->creator->buses);
63 }
64
65 static struct kdbus_bus *kdbus_bus_new(struct kdbus_domain *domain,
66                                        const char *name,
67                                        struct kdbus_bloom_parameter *bloom,
68                                        const u64 *pattach_owner,
69                                        u64 flags, kuid_t uid, kgid_t gid)
70 {
71         struct kdbus_bus *b;
72         u64 attach_owner;
73         int ret;
74
75         if (bloom->size < 8 || bloom->size > KDBUS_BUS_BLOOM_MAX_SIZE ||
76             !KDBUS_IS_ALIGNED8(bloom->size) || bloom->n_hash < 1)
77                 return ERR_PTR(-EINVAL);
78
79         ret = kdbus_sanitize_attach_flags(pattach_owner ? *pattach_owner : 0,
80                                           &attach_owner);
81         if (ret < 0)
82                 return ERR_PTR(ret);
83
84         ret = kdbus_verify_uid_prefix(name, domain->user_namespace, uid);
85         if (ret < 0)
86                 return ERR_PTR(ret);
87
88         b = kzalloc(sizeof(*b), GFP_KERNEL);
89         if (!b)
90                 return ERR_PTR(-ENOMEM);
91
92         kdbus_node_init(&b->node, KDBUS_NODE_BUS);
93
94         b->node.free_cb = kdbus_bus_free;
95         b->node.release_cb = kdbus_bus_release;
96         b->node.uid = uid;
97         b->node.gid = gid;
98         b->node.mode = S_IRUSR | S_IXUSR;
99
100         if (flags & (KDBUS_MAKE_ACCESS_GROUP | KDBUS_MAKE_ACCESS_WORLD))
101                 b->node.mode |= S_IRGRP | S_IXGRP;
102         if (flags & KDBUS_MAKE_ACCESS_WORLD)
103                 b->node.mode |= S_IROTH | S_IXOTH;
104
105         b->id = atomic64_inc_return(&domain->last_id);
106         b->bus_flags = flags;
107         b->attach_flags_owner = attach_owner;
108         generate_random_uuid(b->id128);
109         b->bloom = *bloom;
110         b->domain = kdbus_domain_ref(domain);
111
112         kdbus_policy_db_init(&b->policy_db);
113
114         init_rwsem(&b->conn_rwlock);
115         hash_init(b->conn_hash);
116         INIT_LIST_HEAD(&b->monitors_list);
117
118         INIT_LIST_HEAD(&b->notify_list);
119         spin_lock_init(&b->notify_lock);
120         mutex_init(&b->notify_flush_lock);
121
122         ret = kdbus_node_link(&b->node, &domain->node, name);
123         if (ret < 0)
124                 goto exit_unref;
125
126         /* cache the metadata/credentials of the creator */
127         b->creator_meta = kdbus_meta_proc_new();
128         if (IS_ERR(b->creator_meta)) {
129                 ret = PTR_ERR(b->creator_meta);
130                 b->creator_meta = NULL;
131                 goto exit_unref;
132         }
133
134         ret = kdbus_meta_proc_collect(b->creator_meta,
135                                       KDBUS_ATTACH_CREDS |
136                                       KDBUS_ATTACH_PIDS |
137                                       KDBUS_ATTACH_AUXGROUPS |
138                                       KDBUS_ATTACH_TID_COMM |
139                                       KDBUS_ATTACH_PID_COMM |
140                                       KDBUS_ATTACH_EXE |
141                                       KDBUS_ATTACH_CMDLINE |
142                                       KDBUS_ATTACH_CGROUP |
143                                       KDBUS_ATTACH_CAPS |
144                                       KDBUS_ATTACH_SECLABEL |
145                                       KDBUS_ATTACH_AUDIT);
146         if (ret < 0)
147                 goto exit_unref;
148
149         b->name_registry = kdbus_name_registry_new();
150         if (IS_ERR(b->name_registry)) {
151                 ret = PTR_ERR(b->name_registry);
152                 b->name_registry = NULL;
153                 goto exit_unref;
154         }
155
156         /*
157          * Bus-limits of the creator are accounted on its real UID, just like
158          * all other per-user limits.
159          */
160         b->creator = kdbus_user_lookup(domain, current_uid());
161         if (IS_ERR(b->creator)) {
162                 ret = PTR_ERR(b->creator);
163                 b->creator = NULL;
164                 goto exit_unref;
165         }
166
167         return b;
168
169 exit_unref:
170         kdbus_node_deactivate(&b->node);
171         kdbus_node_unref(&b->node);
172         return ERR_PTR(ret);
173 }
174
175 /**
176  * kdbus_bus_ref() - increase the reference counter of a kdbus_bus
177  * @bus:                The bus to reference
178  *
179  * Every user of a bus, except for its creator, must add a reference to the
180  * kdbus_bus using this function.
181  *
182  * Return: the bus itself
183  */
184 struct kdbus_bus *kdbus_bus_ref(struct kdbus_bus *bus)
185 {
186         if (bus)
187                 kdbus_node_ref(&bus->node);
188         return bus;
189 }
190
191 /**
192  * kdbus_bus_unref() - decrease the reference counter of a kdbus_bus
193  * @bus:                The bus to unref
194  *
195  * Release a reference. If the reference count drops to 0, the bus will be
196  * freed.
197  *
198  * Return: NULL
199  */
200 struct kdbus_bus *kdbus_bus_unref(struct kdbus_bus *bus)
201 {
202         if (bus)
203                 kdbus_node_unref(&bus->node);
204         return NULL;
205 }
206
207 /**
208  * kdbus_bus_find_conn_by_id() - find a connection with a given id
209  * @bus:                The bus to look for the connection
210  * @id:                 The 64-bit connection id
211  *
212  * Looks up a connection with a given id. The returned connection
213  * is ref'ed, and needs to be unref'ed by the user. Returns NULL if
214  * the connection can't be found.
215  */
216 struct kdbus_conn *kdbus_bus_find_conn_by_id(struct kdbus_bus *bus, u64 id)
217 {
218         struct kdbus_conn *conn, *found = NULL;
219
220         down_read(&bus->conn_rwlock);
221         hash_for_each_possible(bus->conn_hash, conn, hentry, id)
222                 if (conn->id == id) {
223                         found = kdbus_conn_ref(conn);
224                         break;
225                 }
226         up_read(&bus->conn_rwlock);
227
228         return found;
229 }
230
231 /**
232  * kdbus_bus_broadcast() - send a message to all subscribed connections
233  * @bus:        The bus the connections are connected to
234  * @conn_src:   The source connection, may be %NULL for kernel notifications
235  * @staging:    Staging object containing the message to send
236  *
237  * Send message to all connections that are currently active on the bus.
238  * Connections must still have matches installed in order to let the message
239  * pass.
240  *
241  * The caller must hold the name-registry lock of @bus.
242  */
243 void kdbus_bus_broadcast(struct kdbus_bus *bus,
244                          struct kdbus_conn *conn_src,
245                          struct kdbus_staging *staging)
246 {
247         struct kdbus_conn *conn_dst;
248         unsigned int i;
249         int ret;
250
251         lockdep_assert_held(&bus->name_registry->rwlock);
252
253         /*
254          * Make sure broadcast are queued on monitors before we send it out to
255          * anyone else. Otherwise, connections might react to broadcasts before
256          * the monitor gets the broadcast queued. In the worst case, the
257          * monitor sees a reaction to the broadcast before the broadcast itself.
258          * We don't give ordering guarantees across connections (and monitors
259          * can re-construct order via sequence numbers), but we should at least
260          * try to avoid re-ordering for monitors.
261          */
262         kdbus_bus_eavesdrop(bus, conn_src, staging);
263
264         down_read(&bus->conn_rwlock);
265         hash_for_each(bus->conn_hash, i, conn_dst, hentry) {
266                 if (!kdbus_conn_is_ordinary(conn_dst))
267                         continue;
268
269                 /*
270                  * Check if there is a match for the kmsg object in
271                  * the destination connection match db
272                  */
273                 if (!kdbus_match_db_match_msg(conn_dst->match_db, conn_src,
274                                               staging))
275                         continue;
276
277                 if (conn_src) {
278                         /*
279                          * Anyone can send broadcasts, as they have no
280                          * destination. But a receiver needs TALK access to
281                          * the sender in order to receive broadcasts.
282                          */
283                         if (!kdbus_conn_policy_talk(conn_dst, NULL, conn_src))
284                                 continue;
285                 } else {
286                         /*
287                          * Check if there is a policy db that prevents the
288                          * destination connection from receiving this kernel
289                          * notification
290                          */
291                         if (!kdbus_conn_policy_see_notification(conn_dst, NULL,
292                                                                 staging->msg))
293                                 continue;
294                 }
295
296                 ret = kdbus_conn_entry_insert(conn_src, conn_dst, staging,
297                                               NULL, NULL);
298                 if (ret < 0)
299                         kdbus_conn_lost_message(conn_dst);
300         }
301         up_read(&bus->conn_rwlock);
302 }
303
304 /**
305  * kdbus_bus_eavesdrop() - send a message to all subscribed monitors
306  * @bus:        The bus the monitors are connected to
307  * @conn_src:   The source connection, may be %NULL for kernel notifications
308  * @staging:    Staging object containing the message to send
309  *
310  * Send message to all monitors that are currently active on the bus. Monitors
311  * must still have matches installed in order to let the message pass.
312  *
313  * The caller must hold the name-registry lock of @bus.
314  */
315 void kdbus_bus_eavesdrop(struct kdbus_bus *bus,
316                          struct kdbus_conn *conn_src,
317                          struct kdbus_staging *staging)
318 {
319         struct kdbus_conn *conn_dst;
320         int ret;
321
322         /*
323          * Monitor connections get all messages; ignore possible errors
324          * when sending messages to monitor connections.
325          */
326
327         lockdep_assert_held(&bus->name_registry->rwlock);
328
329         down_read(&bus->conn_rwlock);
330         list_for_each_entry(conn_dst, &bus->monitors_list, monitor_entry) {
331                 ret = kdbus_conn_entry_insert(conn_src, conn_dst, staging,
332                                               NULL, NULL);
333                 if (ret < 0)
334                         kdbus_conn_lost_message(conn_dst);
335         }
336         up_read(&bus->conn_rwlock);
337 }
338
339 /**
340  * kdbus_cmd_bus_make() - handle KDBUS_CMD_BUS_MAKE
341  * @domain:             domain to operate on
342  * @argp:               command payload
343  *
344  * Return: NULL or newly created bus on success, ERR_PTR on failure.
345  */
346 struct kdbus_bus *kdbus_cmd_bus_make(struct kdbus_domain *domain,
347                                      void __user *argp)
348 {
349         struct kdbus_bus *bus = NULL;
350         struct kdbus_cmd *cmd;
351         struct kdbus_ep *ep = NULL;
352         int ret;
353
354         struct kdbus_arg argv[] = {
355                 { .type = KDBUS_ITEM_NEGOTIATE },
356                 { .type = KDBUS_ITEM_MAKE_NAME, .mandatory = true },
357                 { .type = KDBUS_ITEM_BLOOM_PARAMETER, .mandatory = true },
358                 { .type = KDBUS_ITEM_ATTACH_FLAGS_SEND },
359         };
360         struct kdbus_args args = {
361                 .allowed_flags = KDBUS_FLAG_NEGOTIATE |
362                                  KDBUS_MAKE_ACCESS_GROUP |
363                                  KDBUS_MAKE_ACCESS_WORLD,
364                 .argv = argv,
365                 .argc = ARRAY_SIZE(argv),
366         };
367
368         ret = kdbus_args_parse(&args, argp, &cmd);
369         if (ret < 0)
370                 return ERR_PTR(ret);
371         if (ret > 0)
372                 return NULL;
373
374         bus = kdbus_bus_new(domain,
375                             argv[1].item->str, &argv[2].item->bloom_parameter,
376                             argv[3].item ? argv[3].item->data64 : NULL,
377                             cmd->flags, current_euid(), current_egid());
378         if (IS_ERR(bus)) {
379                 ret = PTR_ERR(bus);
380                 bus = NULL;
381                 goto exit;
382         }
383
384         if (atomic_inc_return(&bus->creator->buses) > KDBUS_USER_MAX_BUSES) {
385                 atomic_dec(&bus->creator->buses);
386                 ret = -EMFILE;
387                 goto exit;
388         }
389
390         if (!kdbus_node_activate(&bus->node)) {
391                 atomic_dec(&bus->creator->buses);
392                 ret = -ESHUTDOWN;
393                 goto exit;
394         }
395
396         ep = kdbus_ep_new(bus, "bus", cmd->flags, bus->node.uid, bus->node.gid,
397                           false);
398         if (IS_ERR(ep)) {
399                 ret = PTR_ERR(ep);
400                 ep = NULL;
401                 goto exit;
402         }
403
404         if (!kdbus_node_activate(&ep->node)) {
405                 ret = -ESHUTDOWN;
406                 goto exit;
407         }
408
409         /*
410          * Drop our own reference, effectively causing the endpoint to be
411          * deactivated and released when the parent bus is.
412          */
413         ep = kdbus_ep_unref(ep);
414
415 exit:
416         ret = kdbus_args_clear(&args, ret);
417         if (ret < 0) {
418                 if (ep) {
419                         kdbus_node_deactivate(&ep->node);
420                         kdbus_ep_unref(ep);
421                 }
422                 if (bus) {
423                         kdbus_node_deactivate(&bus->node);
424                         kdbus_bus_unref(bus);
425                 }
426                 return ERR_PTR(ret);
427         }
428         return bus;
429 }
430
431 /**
432  * kdbus_cmd_bus_creator_info() - handle KDBUS_CMD_BUS_CREATOR_INFO
433  * @conn:               connection to operate on
434  * @argp:               command payload
435  *
436  * Return: >=0 on success, negative error code on failure.
437  */
438 int kdbus_cmd_bus_creator_info(struct kdbus_conn *conn, void __user *argp)
439 {
440         struct kdbus_cmd_info *cmd;
441         struct kdbus_bus *bus = conn->ep->bus;
442         struct kdbus_pool_slice *slice = NULL;
443         struct kdbus_item *meta_items = NULL;
444         struct kdbus_item_header item_hdr;
445         struct kdbus_info info = {};
446         size_t meta_size, name_len, cnt = 0;
447         struct kvec kvec[6];
448         u64 attach_flags, size = 0;
449         int ret;
450
451         struct kdbus_arg argv[] = {
452                 { .type = KDBUS_ITEM_NEGOTIATE },
453         };
454         struct kdbus_args args = {
455                 .allowed_flags = KDBUS_FLAG_NEGOTIATE,
456                 .argv = argv,
457                 .argc = ARRAY_SIZE(argv),
458         };
459
460         ret = kdbus_args_parse(&args, argp, &cmd);
461         if (ret != 0)
462                 return ret;
463
464         ret = kdbus_sanitize_attach_flags(cmd->attach_flags, &attach_flags);
465         if (ret < 0)
466                 goto exit;
467
468         attach_flags &= bus->attach_flags_owner;
469
470         ret = kdbus_meta_emit(bus->creator_meta, NULL, NULL, conn,
471                               attach_flags, &meta_items, &meta_size);
472         if (ret < 0)
473                 goto exit;
474
475         name_len = strlen(bus->node.name) + 1;
476         info.id = bus->id;
477         info.flags = bus->bus_flags;
478         item_hdr.type = KDBUS_ITEM_MAKE_NAME;
479         item_hdr.size = KDBUS_ITEM_HEADER_SIZE + name_len;
480
481         kdbus_kvec_set(&kvec[cnt++], &info, sizeof(info), &size);
482         kdbus_kvec_set(&kvec[cnt++], &item_hdr, sizeof(item_hdr), &size);
483         kdbus_kvec_set(&kvec[cnt++], bus->node.name, name_len, &size);
484         cnt += !!kdbus_kvec_pad(&kvec[cnt], &size);
485         if (meta_size > 0) {
486                 kdbus_kvec_set(&kvec[cnt++], meta_items, meta_size, &size);
487                 cnt += !!kdbus_kvec_pad(&kvec[cnt], &size);
488         }
489
490         info.size = size;
491
492         slice = kdbus_pool_slice_alloc(conn->pool, size, false);
493         if (IS_ERR(slice)) {
494                 ret = PTR_ERR(slice);
495                 slice = NULL;
496                 goto exit;
497         }
498
499         ret = kdbus_pool_slice_copy_kvec(slice, 0, kvec, cnt, size);
500         if (ret < 0)
501                 goto exit;
502
503         kdbus_pool_slice_publish(slice, &cmd->offset, &cmd->info_size);
504
505         if (kdbus_member_set_user(&cmd->offset, argp, typeof(*cmd), offset) ||
506             kdbus_member_set_user(&cmd->info_size, argp,
507                                   typeof(*cmd), info_size))
508                 ret = -EFAULT;
509
510 exit:
511         kdbus_pool_slice_release(slice);
512         kfree(meta_items);
513         return kdbus_args_clear(&args, ret);
514 }