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
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.
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>
28 #include "connection.h"
40 static void kdbus_bus_free(struct kdbus_node *node)
42 struct kdbus_bus *bus = container_of(node, struct kdbus_bus, node);
44 WARN_ON(!list_empty(&bus->monitors_list));
45 WARN_ON(!hash_empty(bus->conn_hash));
47 kdbus_notify_free(bus);
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);
57 static void kdbus_bus_release(struct kdbus_node *node, bool was_active)
59 struct kdbus_bus *bus = container_of(node, struct kdbus_bus, node);
62 atomic_dec(&bus->creator->buses);
65 static struct kdbus_bus *kdbus_bus_new(struct kdbus_domain *domain,
67 struct kdbus_bloom_parameter *bloom,
68 const u64 *pattach_owner,
69 u64 flags, kuid_t uid, kgid_t gid)
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);
79 ret = kdbus_sanitize_attach_flags(pattach_owner ? *pattach_owner : 0,
84 ret = kdbus_verify_uid_prefix(name, domain->user_namespace, uid);
88 b = kzalloc(sizeof(*b), GFP_KERNEL);
90 return ERR_PTR(-ENOMEM);
92 kdbus_node_init(&b->node, KDBUS_NODE_BUS);
94 b->node.free_cb = kdbus_bus_free;
95 b->node.release_cb = kdbus_bus_release;
98 b->node.mode = S_IRUSR | S_IXUSR;
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;
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);
110 b->domain = kdbus_domain_ref(domain);
112 kdbus_policy_db_init(&b->policy_db);
114 init_rwsem(&b->conn_rwlock);
115 hash_init(b->conn_hash);
116 INIT_LIST_HEAD(&b->monitors_list);
118 INIT_LIST_HEAD(&b->notify_list);
119 spin_lock_init(&b->notify_lock);
120 mutex_init(&b->notify_flush_lock);
122 ret = kdbus_node_link(&b->node, &domain->node, name);
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;
134 ret = kdbus_meta_proc_collect(b->creator_meta,
137 KDBUS_ATTACH_AUXGROUPS |
138 KDBUS_ATTACH_TID_COMM |
139 KDBUS_ATTACH_PID_COMM |
141 KDBUS_ATTACH_CMDLINE |
142 KDBUS_ATTACH_CGROUP |
144 KDBUS_ATTACH_SECLABEL |
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;
157 * Bus-limits of the creator are accounted on its real UID, just like
158 * all other per-user limits.
160 b->creator = kdbus_user_lookup(domain, current_uid());
161 if (IS_ERR(b->creator)) {
162 ret = PTR_ERR(b->creator);
170 kdbus_node_deactivate(&b->node);
171 kdbus_node_unref(&b->node);
176 * kdbus_bus_ref() - increase the reference counter of a kdbus_bus
177 * @bus: The bus to reference
179 * Every user of a bus, except for its creator, must add a reference to the
180 * kdbus_bus using this function.
182 * Return: the bus itself
184 struct kdbus_bus *kdbus_bus_ref(struct kdbus_bus *bus)
187 kdbus_node_ref(&bus->node);
192 * kdbus_bus_unref() - decrease the reference counter of a kdbus_bus
193 * @bus: The bus to unref
195 * Release a reference. If the reference count drops to 0, the bus will be
200 struct kdbus_bus *kdbus_bus_unref(struct kdbus_bus *bus)
203 kdbus_node_unref(&bus->node);
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
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.
216 struct kdbus_conn *kdbus_bus_find_conn_by_id(struct kdbus_bus *bus, u64 id)
218 struct kdbus_conn *conn, *found = NULL;
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);
226 up_read(&bus->conn_rwlock);
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
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
241 * The caller must hold the name-registry lock of @bus.
243 void kdbus_bus_broadcast(struct kdbus_bus *bus,
244 struct kdbus_conn *conn_src,
245 struct kdbus_staging *staging)
247 struct kdbus_conn *conn_dst;
251 lockdep_assert_held(&bus->name_registry->rwlock);
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.
262 kdbus_bus_eavesdrop(bus, conn_src, staging);
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))
270 * Check if there is a match for the kmsg object in
271 * the destination connection match db
273 if (!kdbus_match_db_match_msg(conn_dst->match_db, conn_src,
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.
283 if (!kdbus_conn_policy_talk(conn_dst, NULL, conn_src))
287 * Check if there is a policy db that prevents the
288 * destination connection from receiving this kernel
291 if (!kdbus_conn_policy_see_notification(conn_dst, NULL,
296 ret = kdbus_conn_entry_insert(conn_src, conn_dst, staging,
299 kdbus_conn_lost_message(conn_dst);
301 up_read(&bus->conn_rwlock);
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
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.
313 * The caller must hold the name-registry lock of @bus.
315 void kdbus_bus_eavesdrop(struct kdbus_bus *bus,
316 struct kdbus_conn *conn_src,
317 struct kdbus_staging *staging)
319 struct kdbus_conn *conn_dst;
323 * Monitor connections get all messages; ignore possible errors
324 * when sending messages to monitor connections.
327 lockdep_assert_held(&bus->name_registry->rwlock);
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,
334 kdbus_conn_lost_message(conn_dst);
336 up_read(&bus->conn_rwlock);
340 * kdbus_cmd_bus_make() - handle KDBUS_CMD_BUS_MAKE
341 * @domain: domain to operate on
342 * @argp: command payload
344 * Return: NULL or newly created bus on success, ERR_PTR on failure.
346 struct kdbus_bus *kdbus_cmd_bus_make(struct kdbus_domain *domain,
349 struct kdbus_bus *bus = NULL;
350 struct kdbus_cmd *cmd;
351 struct kdbus_ep *ep = NULL;
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 },
360 struct kdbus_args args = {
361 .allowed_flags = KDBUS_FLAG_NEGOTIATE |
362 KDBUS_MAKE_ACCESS_GROUP |
363 KDBUS_MAKE_ACCESS_WORLD,
365 .argc = ARRAY_SIZE(argv),
368 ret = kdbus_args_parse(&args, argp, &cmd);
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());
384 if (atomic_inc_return(&bus->creator->buses) > KDBUS_USER_MAX_BUSES) {
385 atomic_dec(&bus->creator->buses);
390 if (!kdbus_node_activate(&bus->node)) {
391 atomic_dec(&bus->creator->buses);
396 ep = kdbus_ep_new(bus, "bus", cmd->flags, bus->node.uid, bus->node.gid,
404 if (!kdbus_node_activate(&ep->node)) {
410 * Drop our own reference, effectively causing the endpoint to be
411 * deactivated and released when the parent bus is.
413 ep = kdbus_ep_unref(ep);
416 ret = kdbus_args_clear(&args, ret);
419 kdbus_node_deactivate(&ep->node);
423 kdbus_node_deactivate(&bus->node);
424 kdbus_bus_unref(bus);
432 * kdbus_cmd_bus_creator_info() - handle KDBUS_CMD_BUS_CREATOR_INFO
433 * @conn: connection to operate on
434 * @argp: command payload
436 * Return: >=0 on success, negative error code on failure.
438 int kdbus_cmd_bus_creator_info(struct kdbus_conn *conn, void __user *argp)
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;
448 u64 attach_flags, size = 0;
451 struct kdbus_arg argv[] = {
452 { .type = KDBUS_ITEM_NEGOTIATE },
454 struct kdbus_args args = {
455 .allowed_flags = KDBUS_FLAG_NEGOTIATE,
457 .argc = ARRAY_SIZE(argv),
460 ret = kdbus_args_parse(&args, argp, &cmd);
464 ret = kdbus_sanitize_attach_flags(cmd->attach_flags, &attach_flags);
468 attach_flags &= bus->attach_flags_owner;
470 ret = kdbus_meta_emit(bus->creator_meta, NULL, NULL, conn,
471 attach_flags, &meta_items, &meta_size);
475 name_len = strlen(bus->node.name) + 1;
477 info.flags = bus->bus_flags;
478 item_hdr.type = KDBUS_ITEM_MAKE_NAME;
479 item_hdr.size = KDBUS_ITEM_HEADER_SIZE + name_len;
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);
486 kdbus_kvec_set(&kvec[cnt++], meta_items, meta_size, &size);
487 cnt += !!kdbus_kvec_pad(&kvec[cnt], &size);
492 slice = kdbus_pool_slice_alloc(conn->pool, size, false);
494 ret = PTR_ERR(slice);
499 ret = kdbus_pool_slice_copy_kvec(slice, 0, kvec, cnt, size);
503 kdbus_pool_slice_publish(slice, &cmd->offset, &cmd->info_size);
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))
511 kdbus_pool_slice_release(slice);
513 return kdbus_args_clear(&args, ret);