bf44ca3f12b6f3532297f949b03626dc62b0a6cc
[platform/kernel/linux-rpi.git] / ipc / kdbus / names.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 <tixxdz@opendz.org>
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/ctype.h>
16 #include <linux/fs.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>
27
28 #include "bus.h"
29 #include "connection.h"
30 #include "endpoint.h"
31 #include "handle.h"
32 #include "item.h"
33 #include "names.h"
34 #include "notify.h"
35 #include "policy.h"
36
37 #define KDBUS_NAME_SAVED_MASK (KDBUS_NAME_ALLOW_REPLACEMENT |   \
38                                KDBUS_NAME_QUEUE)
39
40 static bool kdbus_name_owner_is_used(struct kdbus_name_owner *owner)
41 {
42         return !list_empty(&owner->name_entry) ||
43                owner == owner->name->activator;
44 }
45
46 static struct kdbus_name_owner *
47 kdbus_name_owner_new(struct kdbus_conn *conn, struct kdbus_name_entry *name,
48                      u64 flags)
49 {
50         struct kdbus_name_owner *owner;
51
52         kdbus_conn_assert_active(conn);
53
54         if (conn->name_count >= KDBUS_CONN_MAX_NAMES)
55                 return ERR_PTR(-E2BIG);
56
57         owner = kmalloc(sizeof(*owner), GFP_KERNEL);
58         if (!owner)
59                 return ERR_PTR(-ENOMEM);
60
61         owner->flags = flags & KDBUS_NAME_SAVED_MASK;
62         owner->conn = conn;
63         owner->name = name;
64         list_add_tail(&owner->conn_entry, &conn->names_list);
65         INIT_LIST_HEAD(&owner->name_entry);
66
67         ++conn->name_count;
68         return owner;
69 }
70
71 static void kdbus_name_owner_free(struct kdbus_name_owner *owner)
72 {
73         if (!owner)
74                 return;
75
76         WARN_ON(kdbus_name_owner_is_used(owner));
77         --owner->conn->name_count;
78         list_del(&owner->conn_entry);
79         kfree(owner);
80 }
81
82 static struct kdbus_name_owner *
83 kdbus_name_owner_find(struct kdbus_name_entry *name, struct kdbus_conn *conn)
84 {
85         struct kdbus_name_owner *owner;
86
87         /*
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.
92          */
93         list_for_each_entry(owner, &conn->names_list, conn_entry)
94                 if (owner->name == name)
95                         return owner;
96
97         return NULL;
98 }
99
100 static bool kdbus_name_entry_is_used(struct kdbus_name_entry *name)
101 {
102         return !list_empty(&name->queue) || name->activator;
103 }
104
105 static struct kdbus_name_owner *
106 kdbus_name_entry_first(struct kdbus_name_entry *name)
107 {
108         return list_first_entry_or_null(&name->queue, struct kdbus_name_owner,
109                                         name_entry);
110 }
111
112 static struct kdbus_name_entry *
113 kdbus_name_entry_new(struct kdbus_name_registry *r, u32 hash,
114                      const char *name_str)
115 {
116         struct kdbus_name_entry *name;
117         size_t namelen;
118
119         lockdep_assert_held(&r->rwlock);
120
121         namelen = strlen(name_str);
122
123         name = kmalloc(sizeof(*name) + namelen + 1, GFP_KERNEL);
124         if (!name)
125                 return ERR_PTR(-ENOMEM);
126
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);
132
133         return name;
134 }
135
136 static void kdbus_name_entry_free(struct kdbus_name_entry *name)
137 {
138         if (!name)
139                 return;
140
141         WARN_ON(kdbus_name_entry_is_used(name));
142         hash_del(&name->hentry);
143         kfree(name);
144 }
145
146 static struct kdbus_name_entry *
147 kdbus_name_entry_find(struct kdbus_name_registry *r, u32 hash,
148                       const char *name_str)
149 {
150         struct kdbus_name_entry *name;
151
152         lockdep_assert_held(&r->rwlock);
153
154         hash_for_each_possible(r->entries_hash, name, hentry, hash)
155                 if (!strcmp(name->name, name_str))
156                         return name;
157
158         return NULL;
159 }
160
161 /**
162  * kdbus_name_registry_new() - create a new name registry
163  *
164  * Return: a new kdbus_name_registry on success, ERR_PTR on failure.
165  */
166 struct kdbus_name_registry *kdbus_name_registry_new(void)
167 {
168         struct kdbus_name_registry *r;
169
170         r = kmalloc(sizeof(*r), GFP_KERNEL);
171         if (!r)
172                 return ERR_PTR(-ENOMEM);
173
174         hash_init(r->entries_hash);
175         init_rwsem(&r->rwlock);
176         r->name_seq_last = 0;
177
178         return r;
179 }
180
181 /**
182  * kdbus_name_registry_free() - free name registry
183  * @r:          name registry to free, or NULL
184  *
185  * Free a name registry and cleanup all internal objects. This is a no-op if
186  * you pass NULL as registry.
187  */
188 void kdbus_name_registry_free(struct kdbus_name_registry *r)
189 {
190         if (!r)
191                 return;
192
193         WARN_ON(!hash_empty(r->entries_hash));
194         kfree(r);
195 }
196
197 /**
198  * kdbus_name_lookup_unlocked() - lookup name in registry
199  * @reg:                name registry
200  * @name:               name to lookup
201  *
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.
205  *
206  * Return: Pointer to name-entry, or NULL if not found.
207  */
208 struct kdbus_name_entry *
209 kdbus_name_lookup_unlocked(struct kdbus_name_registry *reg, const char *name)
210 {
211         return kdbus_name_entry_find(reg, kdbus_strhash(name), name);
212 }
213
214 static int kdbus_name_become_activator(struct kdbus_name_owner *owner,
215                                        u64 *return_flags)
216 {
217         if (kdbus_name_owner_is_used(owner))
218                 return -EALREADY;
219         if (owner->name->activator)
220                 return -EEXIST;
221
222         owner->name->activator = owner;
223         owner->flags |= KDBUS_NAME_ACTIVATOR;
224
225         if (kdbus_name_entry_first(owner->name)) {
226                 owner->flags |= KDBUS_NAME_IN_QUEUE;
227         } else {
228                 owner->flags |= KDBUS_NAME_PRIMARY;
229                 kdbus_notify_name_change(owner->conn->ep->bus,
230                                          KDBUS_ITEM_NAME_ADD,
231                                          0, owner->conn->id,
232                                          0, owner->flags,
233                                          owner->name->name);
234         }
235
236         if (return_flags)
237                 *return_flags = owner->flags | KDBUS_NAME_ACQUIRED;
238
239         return 0;
240 }
241
242 static int kdbus_name_update(struct kdbus_name_owner *owner, u64 flags,
243                              u64 *return_flags)
244 {
245         struct kdbus_name_owner *primary, *activator;
246         struct kdbus_name_entry *name;
247         struct kdbus_bus *bus;
248         u64 nflags = 0;
249         int ret = 0;
250
251         name = owner->name;
252         bus = owner->conn->ep->bus;
253         primary = kdbus_name_entry_first(name);
254         activator = name->activator;
255
256         /* cannot be activator and acquire a name */
257         if (owner == activator)
258                 return -EUCLEAN;
259
260         /* update saved flags */
261         owner->flags = flags & KDBUS_NAME_SAVED_MASK;
262
263         if (!primary) {
264                 /*
265                  * No primary owner (but maybe an activator). Take over the
266                  * name.
267                  */
268
269                 list_add(&owner->name_entry, &name->queue);
270                 owner->flags |= KDBUS_NAME_PRIMARY;
271                 nflags |= KDBUS_NAME_ACQUIRED;
272
273                 /* move messages to new owner on activation */
274                 if (activator) {
275                         kdbus_conn_move_messages(owner->conn, activator->conn,
276                                                  name->name_id);
277                         kdbus_notify_name_change(bus, KDBUS_ITEM_NAME_CHANGE,
278                                         activator->conn->id, owner->conn->id,
279                                         activator->flags, owner->flags,
280                                         name->name);
281                         activator->flags &= ~KDBUS_NAME_PRIMARY;
282                         activator->flags |= KDBUS_NAME_IN_QUEUE;
283                 } else {
284                         kdbus_notify_name_change(bus, KDBUS_ITEM_NAME_ADD,
285                                                  0, owner->conn->id,
286                                                  0, owner->flags,
287                                                  name->name);
288                 }
289
290         } else if (owner == primary) {
291                 /*
292                  * Already the primary owner of the name, flags were already
293                  * updated. Nothing to do.
294                  */
295
296                 owner->flags |= KDBUS_NAME_PRIMARY;
297
298         } else if ((primary->flags & KDBUS_NAME_ALLOW_REPLACEMENT) &&
299                    (flags & KDBUS_NAME_REPLACE_EXISTING)) {
300                 /*
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).
304                  */
305
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;
310
311                 kdbus_notify_name_change(bus, KDBUS_ITEM_NAME_CHANGE,
312                                          primary->conn->id, owner->conn->id,
313                                          primary->flags, owner->flags,
314                                          name->name);
315
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;
320                 } else {
321                         list_del_init(&primary->name_entry);
322                         kdbus_name_owner_free(primary);
323                 }
324
325         } else if (flags & KDBUS_NAME_QUEUE) {
326                 /*
327                  * Name is already occupied and we cannot take it over, but
328                  * queuing is allowed. Put us silently on the queue, if not
329                  * already there.
330                  */
331
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;
336                 }
337         } else if (kdbus_name_owner_is_used(owner)) {
338                 /*
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.
342                  */
343
344                 list_del_init(&owner->name_entry);
345         } else {
346                 /*
347                  * Name is already claimed and queueing is not requested.
348                  * Return error to the caller.
349                  */
350
351                 ret = -EEXIST;
352         }
353
354         if (return_flags)
355                 *return_flags = owner->flags | nflags;
356
357         return ret;
358 }
359
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)
363 {
364         struct kdbus_name_entry *name = NULL;
365         struct kdbus_name_owner *owner = NULL;
366         u32 hash;
367         int ret;
368
369         kdbus_conn_assert_active(conn);
370
371         down_write(&reg->rwlock);
372
373         /*
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.
377          */
378         if (!kdbus_conn_policy_own_name(conn, current_cred(), name_str)) {
379                 ret = -EPERM;
380                 goto exit;
381         }
382
383         /*
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.
387          */
388         hash = kdbus_strhash(name_str);
389         name = kdbus_name_entry_find(reg, hash, name_str);
390         if (name) {
391                 owner = kdbus_name_owner_find(name, conn);
392         } else {
393                 name = kdbus_name_entry_new(reg, hash, name_str);
394                 if (IS_ERR(name)) {
395                         ret = PTR_ERR(name);
396                         name = NULL;
397                         goto exit;
398                 }
399         }
400
401         /* create name owner object if not already queued */
402         if (!owner) {
403                 owner = kdbus_name_owner_new(conn, name, flags);
404                 if (IS_ERR(owner)) {
405                         ret = PTR_ERR(owner);
406                         owner = NULL;
407                         goto exit;
408                 }
409         }
410
411         if (flags & KDBUS_NAME_ACTIVATOR)
412                 ret = kdbus_name_become_activator(owner, return_flags);
413         else
414                 ret = kdbus_name_update(owner, flags, return_flags);
415         if (ret < 0)
416                 goto exit;
417
418 exit:
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(&reg->rwlock);
424         kdbus_notify_flush(conn->ep->bus);
425         return ret;
426 }
427
428 static void kdbus_name_release_unlocked(struct kdbus_name_owner *owner)
429 {
430         struct kdbus_name_owner *primary, *next;
431         struct kdbus_name_entry *name;
432
433         name = owner->name;
434         primary = kdbus_name_entry_first(name);
435
436         list_del_init(&owner->name_entry);
437         if (owner == name->activator)
438                 name->activator = NULL;
439
440         if (!primary || owner == primary) {
441                 next = kdbus_name_entry_first(name);
442                 if (!next)
443                         next = name->activator;
444
445                 if (next) {
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,
451                                                          owner->conn,
452                                                          name->name_id);
453
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,
458                                         name->name);
459                 } else {
460                         kdbus_notify_name_change(owner->conn->ep->bus,
461                                                  KDBUS_ITEM_NAME_REMOVE,
462                                                  owner->conn->id, 0,
463                                                  owner->flags, 0,
464                                                  name->name);
465                 }
466         }
467
468         kdbus_name_owner_free(owner);
469         if (!kdbus_name_entry_is_used(name))
470                 kdbus_name_entry_free(name);
471 }
472
473 static int kdbus_name_release(struct kdbus_name_registry *reg,
474                               struct kdbus_conn *conn,
475                               const char *name_str)
476 {
477         struct kdbus_name_owner *owner;
478         struct kdbus_name_entry *name;
479         int ret = 0;
480
481         down_write(&reg->rwlock);
482         name = kdbus_name_entry_find(reg, kdbus_strhash(name_str), name_str);
483         if (name) {
484                 owner = kdbus_name_owner_find(name, conn);
485                 if (owner)
486                         kdbus_name_release_unlocked(owner);
487                 else
488                         ret = -EADDRINUSE;
489         } else {
490                 ret = -ESRCH;
491         }
492         up_write(&reg->rwlock);
493
494         kdbus_notify_flush(conn->ep->bus);
495         return ret;
496 }
497
498 /**
499  * kdbus_name_release_all() - remove all name entries of a given connection
500  * @reg:                name registry
501  * @conn:               connection
502  */
503 void kdbus_name_release_all(struct kdbus_name_registry *reg,
504                             struct kdbus_conn *conn)
505 {
506         struct kdbus_name_owner *owner;
507
508         down_write(&reg->rwlock);
509
510         while ((owner = list_first_entry_or_null(&conn->names_list,
511                                                  struct kdbus_name_owner,
512                                                  conn_entry)))
513                 kdbus_name_release_unlocked(owner);
514
515         up_write(&reg->rwlock);
516
517         kdbus_notify_flush(conn->ep->bus);
518 }
519
520 /**
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
524  *
525  * A name is valid if all of the following criterias are met:
526  *
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 '.*'
533  */
534 bool kdbus_name_is_valid(const char *p, bool allow_wildcard)
535 {
536         bool dot, found_dot = false;
537         const char *q;
538
539         for (dot = true, q = p; *q; q++) {
540                 if (*q == '.') {
541                         if (dot)
542                                 return false;
543
544                         found_dot = true;
545                         dot = true;
546                 } else {
547                         bool good;
548
549                         good = isalpha(*q) || (!dot && isdigit(*q)) ||
550                                 *q == '_' || *q == '-' ||
551                                 (allow_wildcard && dot &&
552                                         *q == '*' && *(q + 1) == '\0');
553
554                         if (!good)
555                                 return false;
556
557                         dot = false;
558                 }
559         }
560
561         if (q - p > KDBUS_NAME_MAX_LEN)
562                 return false;
563
564         if (dot)
565                 return false;
566
567         if (!found_dot)
568                 return false;
569
570         return true;
571 }
572
573 /**
574  * kdbus_cmd_name_acquire() - handle KDBUS_CMD_NAME_ACQUIRE
575  * @conn:               connection to operate on
576  * @argp:               command payload
577  *
578  * Return: >=0 on success, negative error code on failure.
579  */
580 int kdbus_cmd_name_acquire(struct kdbus_conn *conn, void __user *argp)
581 {
582         const char *item_name;
583         struct kdbus_cmd *cmd;
584         int ret;
585
586         struct kdbus_arg argv[] = {
587                 { .type = KDBUS_ITEM_NEGOTIATE },
588                 { .type = KDBUS_ITEM_NAME, .mandatory = true },
589         };
590         struct kdbus_args args = {
591                 .allowed_flags = KDBUS_FLAG_NEGOTIATE |
592                                  KDBUS_NAME_REPLACE_EXISTING |
593                                  KDBUS_NAME_ALLOW_REPLACEMENT |
594                                  KDBUS_NAME_QUEUE,
595                 .argv = argv,
596                 .argc = ARRAY_SIZE(argv),
597         };
598
599         if (!kdbus_conn_is_ordinary(conn))
600                 return -EOPNOTSUPP;
601
602         ret = kdbus_args_parse(&args, argp, &cmd);
603         if (ret != 0)
604                 return ret;
605
606         item_name = argv[1].item->str;
607         if (!kdbus_name_is_valid(item_name, false)) {
608                 ret = -EINVAL;
609                 goto exit;
610         }
611
612         ret = kdbus_name_acquire(conn->ep->bus->name_registry, conn, item_name,
613                                  cmd->flags, &cmd->return_flags);
614
615 exit:
616         return kdbus_args_clear(&args, ret);
617 }
618
619 /**
620  * kdbus_cmd_name_release() - handle KDBUS_CMD_NAME_RELEASE
621  * @conn:               connection to operate on
622  * @argp:               command payload
623  *
624  * Return: >=0 on success, negative error code on failure.
625  */
626 int kdbus_cmd_name_release(struct kdbus_conn *conn, void __user *argp)
627 {
628         struct kdbus_cmd *cmd;
629         int ret;
630
631         struct kdbus_arg argv[] = {
632                 { .type = KDBUS_ITEM_NEGOTIATE },
633                 { .type = KDBUS_ITEM_NAME, .mandatory = true },
634         };
635         struct kdbus_args args = {
636                 .allowed_flags = KDBUS_FLAG_NEGOTIATE,
637                 .argv = argv,
638                 .argc = ARRAY_SIZE(argv),
639         };
640
641         if (!kdbus_conn_is_ordinary(conn))
642                 return -EOPNOTSUPP;
643
644         ret = kdbus_args_parse(&args, argp, &cmd);
645         if (ret != 0)
646                 return ret;
647
648         ret = kdbus_name_release(conn->ep->bus->name_registry, conn,
649                                  argv[1].item->str);
650         return kdbus_args_clear(&args, ret);
651 }
652
653 static int kdbus_list_write(struct kdbus_conn *conn,
654                             struct kdbus_conn *c,
655                             struct kdbus_pool_slice *slice,
656                             size_t *pos,
657                             struct kdbus_name_owner *o,
658                             bool write)
659 {
660         struct kvec kvec[4];
661         size_t cnt = 0;
662         int ret;
663
664         /* info header */
665         struct kdbus_info info = {
666                 .size = 0,
667                 .id = c->id,
668                 .flags = c->flags,
669         };
670
671         /* fake the header of a kdbus_name item */
672         struct {
673                 u64 size;
674                 u64 type;
675                 u64 flags;
676         } h = {};
677
678         if (o && !kdbus_conn_policy_see_name_unlocked(conn, current_cred(),
679                                                       o->name->name))
680                 return 0;
681
682         kdbus_kvec_set(&kvec[cnt++], &info, sizeof(info), &info.size);
683
684         /* append name */
685         if (o) {
686                 size_t slen = strlen(o->name->name) + 1;
687
688                 h.size = offsetof(struct kdbus_item, name.name) + slen;
689                 h.type = KDBUS_ITEM_OWNED_NAME;
690                 h.flags = o->flags;
691
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);
695         }
696
697         if (write) {
698                 ret = kdbus_pool_slice_copy_kvec(slice, *pos, kvec,
699                                                  cnt, info.size);
700                 if (ret < 0)
701                         return ret;
702         }
703
704         *pos += info.size;
705         return 0;
706 }
707
708 static int kdbus_list_all(struct kdbus_conn *conn, u64 flags,
709                           struct kdbus_pool_slice *slice,
710                           size_t *pos, bool write)
711 {
712         struct kdbus_conn *c;
713         size_t p = *pos;
714         int ret, i;
715
716         hash_for_each(conn->ep->bus->conn_hash, i, c, hentry) {
717                 bool added = false;
718
719                 /* skip monitors */
720                 if (kdbus_conn_is_monitor(c))
721                         continue;
722
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;
728
729                         list_for_each_entry(o, &c->names_list, conn_entry) {
730                                 if (o->flags & KDBUS_NAME_ACTIVATOR) {
731                                         if (!(flags & KDBUS_LIST_ACTIVATORS))
732                                                 continue;
733
734                                         ret = kdbus_list_write(conn, c, slice,
735                                                                &p, o, write);
736                                         if (ret < 0) {
737                                                 mutex_unlock(&c->lock);
738                                                 return ret;
739                                         }
740
741                                         added = true;
742                                 } else if (o->flags & KDBUS_NAME_IN_QUEUE) {
743                                         if (!(flags & KDBUS_LIST_QUEUED))
744                                                 continue;
745
746                                         ret = kdbus_list_write(conn, c, slice,
747                                                                &p, o, write);
748                                         if (ret < 0) {
749                                                 mutex_unlock(&c->lock);
750                                                 return ret;
751                                         }
752
753                                         added = true;
754                                 } else if (flags & KDBUS_LIST_NAMES) {
755                                         ret = kdbus_list_write(conn, c, slice,
756                                                                &p, o, write);
757                                         if (ret < 0) {
758                                                 mutex_unlock(&c->lock);
759                                                 return ret;
760                                         }
761
762                                         added = true;
763                                 }
764                         }
765                 }
766
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);
770                         if (ret < 0)
771                                 return ret;
772                 }
773         }
774
775         *pos = p;
776         return 0;
777 }
778
779 /**
780  * kdbus_cmd_list() - handle KDBUS_CMD_LIST
781  * @conn:               connection to operate on
782  * @argp:               command payload
783  *
784  * Return: >=0 on success, negative error code on failure.
785  */
786 int kdbus_cmd_list(struct kdbus_conn *conn, void __user *argp)
787 {
788         struct kdbus_name_registry *reg = conn->ep->bus->name_registry;
789         struct kdbus_pool_slice *slice = NULL;
790         struct kdbus_cmd_list *cmd;
791         size_t pos, size;
792         int ret;
793
794         struct kdbus_arg argv[] = {
795                 { .type = KDBUS_ITEM_NEGOTIATE },
796         };
797         struct kdbus_args args = {
798                 .allowed_flags = KDBUS_FLAG_NEGOTIATE |
799                                  KDBUS_LIST_UNIQUE |
800                                  KDBUS_LIST_NAMES |
801                                  KDBUS_LIST_ACTIVATORS |
802                                  KDBUS_LIST_QUEUED,
803                 .argv = argv,
804                 .argc = ARRAY_SIZE(argv),
805         };
806
807         ret = kdbus_args_parse(&args, argp, &cmd);
808         if (ret != 0)
809                 return ret;
810
811         /* lock order: domain -> bus -> ep -> names -> conn */
812         down_read(&reg->rwlock);
813         down_read(&conn->ep->bus->conn_rwlock);
814         down_read(&conn->ep->policy_db.entries_rwlock);
815
816         /* size of records */
817         size = 0;
818         ret = kdbus_list_all(conn, cmd->flags, NULL, &size, false);
819         if (ret < 0)
820                 goto exit_unlock;
821
822         if (size == 0) {
823                 kdbus_pool_publish_empty(conn->pool, &cmd->offset,
824                                          &cmd->list_size);
825         } else {
826                 slice = kdbus_pool_slice_alloc(conn->pool, size, false);
827                 if (IS_ERR(slice)) {
828                         ret = PTR_ERR(slice);
829                         slice = NULL;
830                         goto exit_unlock;
831                 }
832
833                 /* copy the records */
834                 pos = 0;
835                 ret = kdbus_list_all(conn, cmd->flags, slice, &pos, true);
836                 if (ret < 0)
837                         goto exit_unlock;
838
839                 WARN_ON(pos != size);
840                 kdbus_pool_slice_publish(slice, &cmd->offset, &cmd->list_size);
841         }
842
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))
846                 ret = -EFAULT;
847
848 exit_unlock:
849         up_read(&conn->ep->policy_db.entries_rwlock);
850         up_read(&conn->ep->bus->conn_rwlock);
851         up_read(&reg->rwlock);
852         kdbus_pool_slice_release(slice);
853         return kdbus_args_clear(&args, ret);
854 }