kdbus.h: Use #defines rather than an enum for ioctl definition

Arnd Bergmann writes:

I think in general, using enum is great, but for ioctl command numbers,
we probably want to have defines so the user space implementation can
use #ifdef to see if the kernel version that it is being built for
knows a particular command.

[...]

which might in fact help some tools that try to do automated parsing
of header files to find ioctl commands.

Signed-off-by: Daniel Mack <daniel@zonque.org>
convert *_new() functions to return the new object directly

Al Viro writes:

BTW, the calling conventions for your foo_new() are annoying - instead of
"return -E... or 0, storing the reference to new object in var parameter
passed as the last argument", could you please just return ERR_PTR(-E...)
on error, a pointer to new object on success and to hell with those
struct foo **foo in the argument lists?

Signed-off-by: Daniel Mack <daniel@zonque.org>
handle.c: rework pointer assignment logic

During the review on LKML, Thomas Gleixner stubled over the usage
of our 'p' variable assignment. Apparantly, the idea of assigning
memdup()'ed memory to a void pointer that is automatically freed
at the end of the function wasn't obvious to readers.

Let's fix this, and

a) make kdbus_memdup_user() return void* instead of int, so we can
   directly assign variables to the return value

b) assign the void* variable after the memdup, and call it 'free_ptr'
   to make clearer what's going on.

Signed-off-by: Daniel Mack <daniel@zonque.org>
handle.c: remove unnecessary pointer assignment

No need to assign `ptr' to the return value of idr_replace()

Signed-off-by: Daniel Mack <daniel@zonque.org>
handle: when caching the creator's metadata cache also KDBUS_ATTACH_AUXGROUPS

Signed-off-by: Djalal Harouni <tixxdz@opendz.org>
kdbus.txt: grammer fixes from Peter Meerwald
kdbus.h: rename KDBUS_{ITEM,ATTACH}_CONN_NAME → KDBUS_{ITEM,ATTACH}_CONN_DESCRIPTION

To not overload the term 'name' too much, rename the attach flag and
the item type to 'DESCRIPTION'.

Signed-off-by: Daniel Mack <daniel@zonque.org>
Add Changelog as template for next cover letter

Signed-off-by: Daniel Mack <daniel@zonque.org>
domain: fix kref race condition around domain_user objects

domain_user currently suffers from a race condition bug:

CPU0				CPU1
-----------------------------   -----------------------------
kdbus_domain_user_unref()
  [u->kref->refcount == 1]
  kref_put()
    [u->kref->refcount == 0]
    __kdbus_domain_user_free()
				mutex_lock(...)
				kdbus_domain_get_user()
				  kdbus_domain_get_user_unlocked()
				    [→ u->kref->refcount == 0]
				    kref_get()
				      [→ u->kref->refcount == 1]
				mutex_unlock(...)
    mutex_lock(...)
    ...
    mutex_unlock(...)
    kfree(u)

Fix this by never acquiring a ref to a user-object with ref-count 0.
Instead, ignore any such objects as they're guaranteed to be removed in
parallel by another task.

As a side-effect, we can now have multiple user-objects in the hashtable
with the same uid. However, all but one must have a ref-count of 0 and
don't carry any useful information. Therefore, we can safely ignore those.

Signed-off-by: David Herrmann <dh.herrmann@gmail.com>
tree-wide: convert more code to direct object return logic

Follow the logic of returning newly allocated or looked up objects
directly, rather than through a function argument.

Signed-off-by: Daniel Mack <daniel@zonque.org>
connection: rename kdbus_conn_is_connected() to kdbus_conn_is_ordinary()

Signed-off-by: Djalal Harouni <tixxdz@opendz.org>
handle: avoid magic numbers for max-minor index

Instead of using magic 0xfffff directly, define a constant as
KDBUS_MINOR_MAX that explains this magic number.

Signed-off-by: David Herrmann <dh.herrmann@gmail.com>
handle: make sure to never allocate minors above KDBUS_MINOR_MAX

It's not the register_chrdev() call that limits our minors, but the
idr_alloc() call. Make sure to limit it to KDBUS_MINOR_MAX so we never
allocate minors higher than that.

Signed-off-by: David Herrmann <dh.herrmann@gmail.com>
pool.h: cosmetics

Signed-off-by: Daniel Mack <daniel@zonque.org>
connection: fix KDBUS_CONN_MAX_MSGS_PER_USER accounting

The user who caused msg_users array to be created has thereby
automatically exceeded its quote. Mark it as such.

Reported-by: John de la Garza <john@jjdev.com>
Signed-off-by: Daniel Mack <daniel@zonque.org>
test-message: add message quota test

Signed-off-by: John de la Garza <john@jjdev.com>
test: add more tests for KDBUS_CMD_CONN_INFO ioctl

We still have to add privileged -> unprivileged KDBUS_CMD_CONN_INFO
ioctl calls.

Signed-off-by: Djalal Harouni <tixxdz@opendz.org>
connection.c: use krealloc() in kdbus_conn_queue_user_quota()

Signed-off-by: Daniel Mack <daniel@zonque.org>
connection: fix kdbus_conn_queue_user_quota() again

John was right - the quota accounting for 'user' needs to be done
unconditionally. That is because if we came to that code path for
reallocation, 'user' was not part of the array before and needs to
get its message count bumped.

Reported-by: John de la Garza <john@jjdev.com>
Signed-off-by: Daniel Mack <daniel@zonque.org>
Changelog additions