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 <tixxdz@opendz.org>
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.
15 #include <linux/ctype.h>
17 #include <linux/hash.h>
18 #include <linux/idr.h>
19 #include <linux/init.h>
20 #include <linux/module.h>
21 #include <linux/mutex.h>
22 #include <linux/rwsem.h>
23 #include <linux/sched.h>
24 #include <linux/slab.h>
25 #include <linux/uaccess.h>
26 #include <linux/uio.h>
29 #include "connection.h"
37 #define KDBUS_NAME_SAVED_MASK (KDBUS_NAME_ALLOW_REPLACEMENT | \
40 static bool kdbus_name_owner_is_used(struct kdbus_name_owner *owner)
42 return !list_empty(&owner->name_entry) ||
43 owner == owner->name->activator;
46 static struct kdbus_name_owner *
47 kdbus_name_owner_new(struct kdbus_conn *conn, struct kdbus_name_entry *name,
50 struct kdbus_name_owner *owner;
52 kdbus_conn_assert_active(conn);
54 if (conn->name_count >= KDBUS_CONN_MAX_NAMES)
55 return ERR_PTR(-E2BIG);
57 owner = kmalloc(sizeof(*owner), GFP_KERNEL);
59 return ERR_PTR(-ENOMEM);
61 owner->flags = flags & KDBUS_NAME_SAVED_MASK;
64 list_add_tail(&owner->conn_entry, &conn->names_list);
65 INIT_LIST_HEAD(&owner->name_entry);
71 static void kdbus_name_owner_free(struct kdbus_name_owner *owner)
76 WARN_ON(kdbus_name_owner_is_used(owner));
77 --owner->conn->name_count;
78 list_del(&owner->conn_entry);
82 static struct kdbus_name_owner *
83 kdbus_name_owner_find(struct kdbus_name_entry *name, struct kdbus_conn *conn)
85 struct kdbus_name_owner *owner;
88 * Use conn->names_list over name->queue to make sure boundaries of
89 * this linear search are controlled by the connection itself.
90 * Furthermore, this will find normal owners as well as activators
91 * without any additional code.
93 list_for_each_entry(owner, &conn->names_list, conn_entry)
94 if (owner->name == name)
100 static bool kdbus_name_entry_is_used(struct kdbus_name_entry *name)
102 return !list_empty(&name->queue) || name->activator;
105 static struct kdbus_name_owner *
106 kdbus_name_entry_first(struct kdbus_name_entry *name)
108 return list_first_entry_or_null(&name->queue, struct kdbus_name_owner,
112 static struct kdbus_name_entry *
113 kdbus_name_entry_new(struct kdbus_name_registry *r, u32 hash,
114 const char *name_str)
116 struct kdbus_name_entry *name;
119 lockdep_assert_held(&r->rwlock);
121 namelen = strlen(name_str);
123 name = kmalloc(sizeof(*name) + namelen + 1, GFP_KERNEL);
125 return ERR_PTR(-ENOMEM);
127 name->name_id = ++r->name_seq_last;
128 name->activator = NULL;
129 INIT_LIST_HEAD(&name->queue);
130 hash_add(r->entries_hash, &name->hentry, hash);
131 memcpy(name->name, name_str, namelen + 1);
136 static void kdbus_name_entry_free(struct kdbus_name_entry *name)
141 WARN_ON(kdbus_name_entry_is_used(name));
142 hash_del(&name->hentry);
146 static struct kdbus_name_entry *
147 kdbus_name_entry_find(struct kdbus_name_registry *r, u32 hash,
148 const char *name_str)
150 struct kdbus_name_entry *name;
152 lockdep_assert_held(&r->rwlock);
154 hash_for_each_possible(r->entries_hash, name, hentry, hash)
155 if (!strcmp(name->name, name_str))
162 * kdbus_name_registry_new() - create a new name registry
164 * Return: a new kdbus_name_registry on success, ERR_PTR on failure.
166 struct kdbus_name_registry *kdbus_name_registry_new(void)
168 struct kdbus_name_registry *r;
170 r = kmalloc(sizeof(*r), GFP_KERNEL);
172 return ERR_PTR(-ENOMEM);
174 hash_init(r->entries_hash);
175 init_rwsem(&r->rwlock);
176 r->name_seq_last = 0;
182 * kdbus_name_registry_free() - free name registry
183 * @r: name registry to free, or NULL
185 * Free a name registry and cleanup all internal objects. This is a no-op if
186 * you pass NULL as registry.
188 void kdbus_name_registry_free(struct kdbus_name_registry *r)
193 WARN_ON(!hash_empty(r->entries_hash));
198 * kdbus_name_lookup_unlocked() - lookup name in registry
199 * @reg: name registry
200 * @name: name to lookup
202 * This looks up @name in the given name-registry and returns the
203 * kdbus_name_entry object. The caller must hold the registry-lock and must not
204 * access the returned object after releasing the lock.
206 * Return: Pointer to name-entry, or NULL if not found.
208 struct kdbus_name_entry *
209 kdbus_name_lookup_unlocked(struct kdbus_name_registry *reg, const char *name)
211 return kdbus_name_entry_find(reg, kdbus_strhash(name), name);
214 static int kdbus_name_become_activator(struct kdbus_name_owner *owner,
217 if (kdbus_name_owner_is_used(owner))
219 if (owner->name->activator)
222 owner->name->activator = owner;
223 owner->flags |= KDBUS_NAME_ACTIVATOR;
225 if (kdbus_name_entry_first(owner->name)) {
226 owner->flags |= KDBUS_NAME_IN_QUEUE;
228 owner->flags |= KDBUS_NAME_PRIMARY;
229 kdbus_notify_name_change(owner->conn->ep->bus,
237 *return_flags = owner->flags | KDBUS_NAME_ACQUIRED;
242 static int kdbus_name_update(struct kdbus_name_owner *owner, u64 flags,
245 struct kdbus_name_owner *primary, *activator;
246 struct kdbus_name_entry *name;
247 struct kdbus_bus *bus;
252 bus = owner->conn->ep->bus;
253 primary = kdbus_name_entry_first(name);
254 activator = name->activator;
256 /* cannot be activator and acquire a name */
257 if (owner == activator)
260 /* update saved flags */
261 owner->flags = flags & KDBUS_NAME_SAVED_MASK;
265 * No primary owner (but maybe an activator). Take over the
269 list_add(&owner->name_entry, &name->queue);
270 owner->flags |= KDBUS_NAME_PRIMARY;
271 nflags |= KDBUS_NAME_ACQUIRED;
273 /* move messages to new owner on activation */
275 kdbus_conn_move_messages(owner->conn, activator->conn,
277 kdbus_notify_name_change(bus, KDBUS_ITEM_NAME_CHANGE,
278 activator->conn->id, owner->conn->id,
279 activator->flags, owner->flags,
281 activator->flags &= ~KDBUS_NAME_PRIMARY;
282 activator->flags |= KDBUS_NAME_IN_QUEUE;
284 kdbus_notify_name_change(bus, KDBUS_ITEM_NAME_ADD,
290 } else if (owner == primary) {
292 * Already the primary owner of the name, flags were already
293 * updated. Nothing to do.
296 owner->flags |= KDBUS_NAME_PRIMARY;
298 } else if ((primary->flags & KDBUS_NAME_ALLOW_REPLACEMENT) &&
299 (flags & KDBUS_NAME_REPLACE_EXISTING)) {
301 * We're not the primary owner but can replace it. Move us
302 * ahead of the primary owner and acquire the name (possibly
303 * skipping queued owners ahead of us).
306 list_del_init(&owner->name_entry);
307 list_add(&owner->name_entry, &name->queue);
308 owner->flags |= KDBUS_NAME_PRIMARY;
309 nflags |= KDBUS_NAME_ACQUIRED;
311 kdbus_notify_name_change(bus, KDBUS_ITEM_NAME_CHANGE,
312 primary->conn->id, owner->conn->id,
313 primary->flags, owner->flags,
316 /* requeue old primary, or drop if queueing not wanted */
317 if (primary->flags & KDBUS_NAME_QUEUE) {
318 primary->flags &= ~KDBUS_NAME_PRIMARY;
319 primary->flags |= KDBUS_NAME_IN_QUEUE;
321 list_del_init(&primary->name_entry);
322 kdbus_name_owner_free(primary);
325 } else if (flags & KDBUS_NAME_QUEUE) {
327 * Name is already occupied and we cannot take it over, but
328 * queuing is allowed. Put us silently on the queue, if not
332 owner->flags |= KDBUS_NAME_IN_QUEUE;
333 if (!kdbus_name_owner_is_used(owner)) {
334 list_add_tail(&owner->name_entry, &name->queue);
335 nflags |= KDBUS_NAME_ACQUIRED;
337 } else if (kdbus_name_owner_is_used(owner)) {
339 * Already queued on name, but re-queueing was not requested.
340 * Make sure to unlink it from the name, the caller is
341 * responsible for releasing it.
344 list_del_init(&owner->name_entry);
347 * Name is already claimed and queueing is not requested.
348 * Return error to the caller.
355 *return_flags = owner->flags | nflags;
360 int kdbus_name_acquire(struct kdbus_name_registry *reg,
361 struct kdbus_conn *conn, const char *name_str,
362 u64 flags, u64 *return_flags)
364 struct kdbus_name_entry *name = NULL;
365 struct kdbus_name_owner *owner = NULL;
369 kdbus_conn_assert_active(conn);
371 down_write(®->rwlock);
374 * Verify the connection has access to the name. Do this before testing
375 * for double-acquisitions and other errors to make sure we do not leak
376 * information about this name through possible custom endpoints.
378 if (!kdbus_conn_policy_own_name(conn, current_cred(), name_str)) {
384 * Lookup the name entry. If it already exists, search for an owner
385 * entry as we might already own that name. If either does not exist,
386 * we will allocate a fresh one.
388 hash = kdbus_strhash(name_str);
389 name = kdbus_name_entry_find(reg, hash, name_str);
391 owner = kdbus_name_owner_find(name, conn);
393 name = kdbus_name_entry_new(reg, hash, name_str);
401 /* create name owner object if not already queued */
403 owner = kdbus_name_owner_new(conn, name, flags);
405 ret = PTR_ERR(owner);
411 if (flags & KDBUS_NAME_ACTIVATOR)
412 ret = kdbus_name_become_activator(owner, return_flags);
414 ret = kdbus_name_update(owner, flags, return_flags);
419 if (owner && !kdbus_name_owner_is_used(owner))
420 kdbus_name_owner_free(owner);
421 if (name && !kdbus_name_entry_is_used(name))
422 kdbus_name_entry_free(name);
423 up_write(®->rwlock);
424 kdbus_notify_flush(conn->ep->bus);
428 static void kdbus_name_release_unlocked(struct kdbus_name_owner *owner)
430 struct kdbus_name_owner *primary, *next;
431 struct kdbus_name_entry *name;
434 primary = kdbus_name_entry_first(name);
436 list_del_init(&owner->name_entry);
437 if (owner == name->activator)
438 name->activator = NULL;
440 if (!primary || owner == primary) {
441 next = kdbus_name_entry_first(name);
443 next = name->activator;
446 /* hand to next in queue */
447 next->flags &= ~KDBUS_NAME_IN_QUEUE;
448 next->flags |= KDBUS_NAME_PRIMARY;
449 if (next == name->activator)
450 kdbus_conn_move_messages(next->conn,
454 kdbus_notify_name_change(owner->conn->ep->bus,
455 KDBUS_ITEM_NAME_CHANGE,
456 owner->conn->id, next->conn->id,
457 owner->flags, next->flags,
460 kdbus_notify_name_change(owner->conn->ep->bus,
461 KDBUS_ITEM_NAME_REMOVE,
468 kdbus_name_owner_free(owner);
469 if (!kdbus_name_entry_is_used(name))
470 kdbus_name_entry_free(name);
473 static int kdbus_name_release(struct kdbus_name_registry *reg,
474 struct kdbus_conn *conn,
475 const char *name_str)
477 struct kdbus_name_owner *owner;
478 struct kdbus_name_entry *name;
481 down_write(®->rwlock);
482 name = kdbus_name_entry_find(reg, kdbus_strhash(name_str), name_str);
484 owner = kdbus_name_owner_find(name, conn);
486 kdbus_name_release_unlocked(owner);
492 up_write(®->rwlock);
494 kdbus_notify_flush(conn->ep->bus);
499 * kdbus_name_release_all() - remove all name entries of a given connection
500 * @reg: name registry
503 void kdbus_name_release_all(struct kdbus_name_registry *reg,
504 struct kdbus_conn *conn)
506 struct kdbus_name_owner *owner;
508 down_write(®->rwlock);
510 while ((owner = list_first_entry_or_null(&conn->names_list,
511 struct kdbus_name_owner,
513 kdbus_name_release_unlocked(owner);
515 up_write(®->rwlock);
517 kdbus_notify_flush(conn->ep->bus);
521 * kdbus_name_is_valid() - check if a name is valid
522 * @p: The name to check
523 * @allow_wildcard: Whether or not to allow a wildcard name
525 * A name is valid if all of the following criterias are met:
527 * - The name has two or more elements separated by a period ('.') character.
528 * - All elements must contain at least one character.
529 * - Each element must only contain the ASCII characters "[A-Z][a-z][0-9]_-"
530 * and must not begin with a digit.
531 * - The name must not exceed KDBUS_NAME_MAX_LEN.
532 * - If @allow_wildcard is true, the name may end on '.*'
534 bool kdbus_name_is_valid(const char *p, bool allow_wildcard)
536 bool dot, found_dot = false;
539 for (dot = true, q = p; *q; q++) {
549 good = isalpha(*q) || (!dot && isdigit(*q)) ||
550 *q == '_' || *q == '-' ||
551 (allow_wildcard && dot &&
552 *q == '*' && *(q + 1) == '\0');
561 if (q - p > KDBUS_NAME_MAX_LEN)
574 * kdbus_cmd_name_acquire() - handle KDBUS_CMD_NAME_ACQUIRE
575 * @conn: connection to operate on
576 * @argp: command payload
578 * Return: >=0 on success, negative error code on failure.
580 int kdbus_cmd_name_acquire(struct kdbus_conn *conn, void __user *argp)
582 const char *item_name;
583 struct kdbus_cmd *cmd;
586 struct kdbus_arg argv[] = {
587 { .type = KDBUS_ITEM_NEGOTIATE },
588 { .type = KDBUS_ITEM_NAME, .mandatory = true },
590 struct kdbus_args args = {
591 .allowed_flags = KDBUS_FLAG_NEGOTIATE |
592 KDBUS_NAME_REPLACE_EXISTING |
593 KDBUS_NAME_ALLOW_REPLACEMENT |
596 .argc = ARRAY_SIZE(argv),
599 if (!kdbus_conn_is_ordinary(conn))
602 ret = kdbus_args_parse(&args, argp, &cmd);
606 item_name = argv[1].item->str;
607 if (!kdbus_name_is_valid(item_name, false)) {
612 ret = kdbus_name_acquire(conn->ep->bus->name_registry, conn, item_name,
613 cmd->flags, &cmd->return_flags);
616 return kdbus_args_clear(&args, ret);
620 * kdbus_cmd_name_release() - handle KDBUS_CMD_NAME_RELEASE
621 * @conn: connection to operate on
622 * @argp: command payload
624 * Return: >=0 on success, negative error code on failure.
626 int kdbus_cmd_name_release(struct kdbus_conn *conn, void __user *argp)
628 struct kdbus_cmd *cmd;
631 struct kdbus_arg argv[] = {
632 { .type = KDBUS_ITEM_NEGOTIATE },
633 { .type = KDBUS_ITEM_NAME, .mandatory = true },
635 struct kdbus_args args = {
636 .allowed_flags = KDBUS_FLAG_NEGOTIATE,
638 .argc = ARRAY_SIZE(argv),
641 if (!kdbus_conn_is_ordinary(conn))
644 ret = kdbus_args_parse(&args, argp, &cmd);
648 ret = kdbus_name_release(conn->ep->bus->name_registry, conn,
650 return kdbus_args_clear(&args, ret);
653 static int kdbus_list_write(struct kdbus_conn *conn,
654 struct kdbus_conn *c,
655 struct kdbus_pool_slice *slice,
657 struct kdbus_name_owner *o,
665 struct kdbus_info info = {
671 /* fake the header of a kdbus_name item */
678 if (o && !kdbus_conn_policy_see_name_unlocked(conn, current_cred(),
682 kdbus_kvec_set(&kvec[cnt++], &info, sizeof(info), &info.size);
686 size_t slen = strlen(o->name->name) + 1;
688 h.size = offsetof(struct kdbus_item, name.name) + slen;
689 h.type = KDBUS_ITEM_OWNED_NAME;
692 kdbus_kvec_set(&kvec[cnt++], &h, sizeof(h), &info.size);
693 kdbus_kvec_set(&kvec[cnt++], o->name->name, slen, &info.size);
694 cnt += !!kdbus_kvec_pad(&kvec[cnt], &info.size);
698 ret = kdbus_pool_slice_copy_kvec(slice, *pos, kvec,
708 static int kdbus_list_all(struct kdbus_conn *conn, u64 flags,
709 struct kdbus_pool_slice *slice,
710 size_t *pos, bool write)
712 struct kdbus_conn *c;
716 hash_for_each(conn->ep->bus->conn_hash, i, c, hentry) {
720 if (kdbus_conn_is_monitor(c))
723 /* all names the connection owns */
724 if (flags & (KDBUS_LIST_NAMES |
725 KDBUS_LIST_ACTIVATORS |
726 KDBUS_LIST_QUEUED)) {
727 struct kdbus_name_owner *o;
729 list_for_each_entry(o, &c->names_list, conn_entry) {
730 if (o->flags & KDBUS_NAME_ACTIVATOR) {
731 if (!(flags & KDBUS_LIST_ACTIVATORS))
734 ret = kdbus_list_write(conn, c, slice,
737 mutex_unlock(&c->lock);
742 } else if (o->flags & KDBUS_NAME_IN_QUEUE) {
743 if (!(flags & KDBUS_LIST_QUEUED))
746 ret = kdbus_list_write(conn, c, slice,
749 mutex_unlock(&c->lock);
754 } else if (flags & KDBUS_LIST_NAMES) {
755 ret = kdbus_list_write(conn, c, slice,
758 mutex_unlock(&c->lock);
767 /* nothing added so far, just add the unique ID */
768 if (!added && (flags & KDBUS_LIST_UNIQUE)) {
769 ret = kdbus_list_write(conn, c, slice, &p, NULL, write);
780 * kdbus_cmd_list() - handle KDBUS_CMD_LIST
781 * @conn: connection to operate on
782 * @argp: command payload
784 * Return: >=0 on success, negative error code on failure.
786 int kdbus_cmd_list(struct kdbus_conn *conn, void __user *argp)
788 struct kdbus_name_registry *reg = conn->ep->bus->name_registry;
789 struct kdbus_pool_slice *slice = NULL;
790 struct kdbus_cmd_list *cmd;
794 struct kdbus_arg argv[] = {
795 { .type = KDBUS_ITEM_NEGOTIATE },
797 struct kdbus_args args = {
798 .allowed_flags = KDBUS_FLAG_NEGOTIATE |
801 KDBUS_LIST_ACTIVATORS |
804 .argc = ARRAY_SIZE(argv),
807 ret = kdbus_args_parse(&args, argp, &cmd);
811 /* lock order: domain -> bus -> ep -> names -> conn */
812 down_read(®->rwlock);
813 down_read(&conn->ep->bus->conn_rwlock);
814 down_read(&conn->ep->policy_db.entries_rwlock);
816 /* size of records */
818 ret = kdbus_list_all(conn, cmd->flags, NULL, &size, false);
823 kdbus_pool_publish_empty(conn->pool, &cmd->offset,
826 slice = kdbus_pool_slice_alloc(conn->pool, size, false);
828 ret = PTR_ERR(slice);
833 /* copy the records */
835 ret = kdbus_list_all(conn, cmd->flags, slice, &pos, true);
839 WARN_ON(pos != size);
840 kdbus_pool_slice_publish(slice, &cmd->offset, &cmd->list_size);
843 if (kdbus_member_set_user(&cmd->offset, argp, typeof(*cmd), offset) ||
844 kdbus_member_set_user(&cmd->list_size, argp,
845 typeof(*cmd), list_size))
849 up_read(&conn->ep->policy_db.entries_rwlock);
850 up_read(&conn->ep->bus->conn_rwlock);
851 up_read(®->rwlock);
852 kdbus_pool_slice_release(slice);
853 return kdbus_args_clear(&args, ret);