kdbus: re-order payload declarations

The diff looks scarier than it is: This moves kdbus_xyz items _before_
their kdbus_cmd_xyz user. We don't directly embed those in the cmd
structures, but we would, if we could. So lets keep the logical order and
move them before their user.

Signed-off-by: David Herrmann <dh.herrmann@gmail.com>
kdbus: add comment about payload types

Clarify that any payload type is allowed. We will just add further
payloads once they are established in user-space and everyone agrees on
them.

Signed-off-by: David Herrmann <dh.herrmann@gmail.com>
kdbus: KDBUS_MSG_FLAGS_* -> KDBUS_MSG_*

Drop the _FLAGS_ part of the message flags constants. We don't use it
anywhere else, so lets be consistent and drop it here, too.

Signed-off-by: David Herrmann <dh.herrmann@gmail.com>
kdbus: be paranoid and avoid kernel stack information leaks

Lets initialize all our temporary variables to 0 so any memcpy() does not
leak kernel information. Furthermore, initialize allocated meta-buffers to
zero to not leak padding bytes.

Note that we still rely on all our external objects to _not_ have padding
bytes, otherwise, a simple initialization would leave them untouched and
we'd have to use memset().

Signed-off-by: David Herrmann <dh.herrmann@gmail.com>
kdbus: rename cmd_name_list.size to cmd_name_list.list_size

Avoid generic 'size' names in ioctl structs. Rename it to list_size so we
can add a 'size'+'items' combination in a followup.

Signed-off-by: David Herrmann <dh.herrmann@gmail.com>
kdbus: add items to kdbus_cmd_name_list

Make kdbus_cmd_name_list future proof and add items. This way we can
extend the ioctl easily in the future.

Signed-off-by: David Herrmann <dh.herrmann@gmail.com>
item: cosmetic alignment fix

Signed-off-by: Daniel Mack <daniel@zonque.org>
handle: fix memleak on cmd_name_list

Properly free any allocated memory after the command is done. Fixes the
previous commit that introduced items to cmd_name_list.

Signed-off-by: David Herrmann <dh.herrmann@gmail.com>
kdbus: bring kdbus_cmd_cancel up-to-date

Lets make sure kdbus_cmd_cancel supports all the fancy new features
including return_flags, kernel_flags and items.

Signed-off-by: David Herrmann <dh.herrmann@gmail.com>
util: add iovec alignment helper

iovecs can't have holes, so we have to add extra members in order to
fill in zero-bytes in cases where an item doesn't have a length of
multiple of 8 bytes.

This was implemented in bus.c and names.c, and is now moved to util.c.

Signed-off-by: Daniel Mack <daniel@zonque.org>
util: fix race when copying 'sized' objects from userspace

We copy the 'size' field of userspace objects twice, first to get the
overall size and then again as part of the object. We must properly verify
the size didn't change in between, otherwise we must fail or user-space
might trigger page-faults in the kernel.

Note that we could just avoid copying it twice, but that would mean
hard-coding a lot of helpers like memdup_user() as we have additional
constraints then.

Signed-off-by: David Herrmann <dh.herrmann@gmail.com>
names: fix name item size

Fix and cleanup the code in kdbus_name_list_write().
item->size does *not* need to be 8-byte aligned.

Signed-off-by: Daniel Mack <daniel@zonque.org>
kdbus: turn cmd_send.msg into cmd_send.msg_address

Lets avoid embedding messages into command ioctls. Otherwise, it will
become really hard to treat them as external entities or re-use them.

This partly reverts the previous commit that introduced cmd_send.

Signed-off-by: David Herrmann <dh.herrmann@gmail.com>
bus: calculate info.size when assembling the items

Be consistent, and increase info.size with each new iov, rather
than pre-calcualting the sum again. With the iovec implementation,
there's no need to know the slice size before assembling the copy
vectors.

Signed-off-by: Daniel Mack <daniel@zonque.org>
util: add kdbus_iovec_set()

Add helper utility to make the call sizes that assemble arrays of iovecs
a bit smaller.

Signed-off-by: Daniel Mack <daniel@zonque.org>
util: simplify call-side of kdbus_iovec_pad()

Lets make the callers of kdbus_iovec_pad() one-lines, too. This simplifies
the iovec setup and makes it more inline with iovec_set().

Signed-off-by: David Herrmann <dh.herrmann@gmail.com>
metadata: remove kdbus_meta_collect_dst(), add kdbus_meta_calc_attach_flags()

Introduce a new function kdbus_meta_calc_attach_flags() that takes into
account both a sender and a receiver connection, and intersects the
attach masks.

Use that function, and remove kdbus_meta_collect_dst().

Signed-off-by: Daniel Mack <daniel@zonque.org>
pool: allow passing an iovec to kdbus_pool_slice_alloc()

kdbus_pool_slice_alloc() and kdbus_pool_slice_copy() are mostly used
in pairs. By allowing kdbus_pool_slice_alloc() to take an iovec array,
we can simplify the call sites a bit. If the iovec parameter is NULL,
no content it copied.

Signed-off-by: Daniel Mack <daniel@zonque.org>
kdbus: make kdbus_cmd_free more consistent

Lets add items to kdbus_cmd_free to make it consistent with the other
commands (this is the last command that needs this update).

Signed-off-by: David Herrmann <dh.herrmann@gmail.com>
metadata: rename functions for clarity

Rename functions so their name reflects what they're actually doing:

  kdbus_meta_collect		→ kdbus_meta_add_current
  kdbus_meta_collect_src	→ kdbus_meta_add_conn_info
  kdbus_meta_fake		→ kdbus_meta_add_fake

Signed-off-by: Daniel Mack <daniel@zonque.org>