platform/core/system/kdbus-bus.git
10 years agoconnection: split kdbus_conn_wait_reply() off kdbus_conn_kmsg_send()
David Herrmann [Mon, 20 Oct 2014 12:51:51 +0000 (14:51 +0200)]
connection: split kdbus_conn_wait_reply() off kdbus_conn_kmsg_send()

Move the helper to wait synchronously for a reply into
kdbus_conn_wait_reply(). This reduces the size of kdbus_conn_kmsg_send()
further and makes it much easier to review.

Signed-off-by: David Herrmann <dh.herrmann@gmail.com>
10 years agoqueue: consolidate kdbus_queue_entry_*fds_install()
Daniel Mack [Mon, 20 Oct 2014 12:33:13 +0000 (14:33 +0200)]
queue: consolidate kdbus_queue_entry_*fds_install()

Combine code from kdbus_queue_entry_fds_install() and
kdbus_queue_entry_memfds_install() and make simplify the caller site.

Signed-off-by: Daniel Mack <daniel@zonque.org>
10 years agomessage, queue: pin files over their entire lifetime
Daniel Mack [Mon, 20 Oct 2014 11:58:55 +0000 (13:58 +0200)]
message, queue: pin files over their entire lifetime

Make sure the passed fds and memfds are pinned throughout their usage
in kdbus, that is, until they are installed. That closes a race gap in
which a user could possibly replace an fd after submitting a message to
the kernel and the message's delivery and the fd's installation.

While at it, also move the seal check for memfds from queue.c to
message.c and introduce a method to free an array of struct file*.

Now, the incoming QA check in message.c will make sure the files are of
the correct type, memfds are sealed etc. After that, when queue entry
items are created, we call get_file() on each of the passed files to
add increase the reference count once more, and decrement them when the
entry is installed in the receiver's task.

Also, the reference taken my the kmsg are dropped from
kdbus_kmsg_free().

Signed-off-by: Daniel Mack <daniel@zonque.org>
10 years agotree-wide: rework flags negotiation (ABI break)
Daniel Mack [Fri, 17 Oct 2014 11:43:14 +0000 (13:43 +0200)]
tree-wide: rework flags negotiation (ABI break)

We are obliged to reject all bits in flags fields that are not known
to the kernel. In order to let userspace know which flags the kernel
knowns about, we agreed to always write back to the flags field in the
ioctl buffer, even if the call succeeded. The kernel will, however,
will always set the KDBUS_FLAG_KERNEL bit, which consequently is always
invalid when submitted by userspace.

Move some checks from other place to handle.c, and update the testsuite
and documentation accordingly.

Signed-off-by: Daniel Mack <daniel@zonque.org>
10 years agoqueue: set O_CLOEXEC on installed file descriptors
Daniel Mack [Fri, 17 Oct 2014 07:55:51 +0000 (09:55 +0200)]
queue: set O_CLOEXEC on installed file descriptors

The receiver can still opt-out for this with fcntl(), but by default,
we should really set O_CLOEXEC.

Signed-off-by: Daniel Mack <daniel@zonque.org>
10 years agokdbus.h: remove features bitfield from make calls (ABI break)
Daniel Mack [Thu, 16 Oct 2014 16:12:54 +0000 (18:12 +0200)]
kdbus.h: remove features bitfield from make calls (ABI break)

After discussion in the systemd hackfest, we agreed on flags
negotiation via the flags fields, so there's no need for a bitmask
called 'features' anymore. Drop it.

Signed-off-by: Daniel Mack <daniel@zonque.org>
10 years agoqueue.c: move a stack variable before the variable array
Greg Kroah-Hartman [Fri, 17 Oct 2014 08:23:27 +0000 (10:23 +0200)]
queue.c: move a stack variable before the variable array

This way the compiler doesn't have to calculate the location of the
pointer "on the fly".

Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
10 years agoRevert KDBUS_HELLO_ACCEPT_MEMFD support (ABI break)
Kay Sievers [Fri, 17 Oct 2014 08:00:35 +0000 (10:00 +0200)]
Revert KDBUS_HELLO_ACCEPT_MEMFD support (ABI break)

Memfds are a basic exchange mechanism not supposed to be
optional per connection. A per-bus flag, instead of a
per-connection one, would probably be acceptable,
but its usefulness is questionable at this point.

Broadcasts can contain memfds and we would silently messages
for such connections, which is not the expected behavior.

Receivers just need to make sure to be able to receive messages
with memfd payload, otherwise they are just not fully supporting
the common kdbus interface.

Contracts of not supporting memfds on private buses are fine,
but the general purpose communication will always require
memfds to be supported by all clients.

10 years agolimits: lower KDBUS_MSG_MAX_FDS to 253
Daniel Mack [Thu, 16 Oct 2014 10:16:56 +0000 (12:16 +0200)]
limits: lower KDBUS_MSG_MAX_FDS to 253

Lower the number of maximum file descriptors accepted as a message item
to 253. See commit bba14de98 (Linux) for the rationale behind that
number.

Signed-off-by: Daniel Mack <daniel@zonque.org>
10 years agometadata: update meta->attached once the attach succeeded
Daniel Mack [Thu, 16 Oct 2014 09:57:57 +0000 (11:57 +0200)]
metadata: update meta->attached once the attach succeeded

If we fail to append metadata items, but ignore the errors on purpose
for broadcast messages, we might end up with the same metadata multiple
times if we only update meta->attached wt the end of
kdbus_meta_append().

Hence, set each bit individually once the attachment succeeded.

Signed-off-by: Daniel Mack <daniel@zonque.org>
10 years agodomain: detroy IDRs
Daniel Mack [Thu, 16 Oct 2014 09:38:37 +0000 (11:38 +0200)]
domain: detroy IDRs

Signed-off-by: Daniel Mack <daniel@zonque.org>
10 years agobus.c: remove unneeded include
Daniel Mack [Thu, 16 Oct 2014 09:18:44 +0000 (11:18 +0200)]
bus.c: remove unneeded include

Signed-off-by: Daniel Mack <daniel@zonque.org>
10 years agonotify: set item->size
Daniel Mack [Thu, 16 Oct 2014 09:16:20 +0000 (11:16 +0200)]
notify: set item->size

Fully initialize the API. We should also have strict checking for this
on userspace.

Signed-off-by: Daniel Mack <daniel@zonque.org>
10 years agodoc: document the new KDBUS_HELLO_ACCEPT_MEMFD flag
Djalal Harouni [Tue, 14 Oct 2014 21:44:37 +0000 (22:44 +0100)]
doc: document the new KDBUS_HELLO_ACCEPT_MEMFD flag

Signed-off-by: Djalal Harouni <tixxdz@opendz.org>
10 years agoconnection: use KDBUS_HELLO_ACCEPT_MEMFD to check for passed memfds
Djalal Harouni [Tue, 14 Oct 2014 21:34:50 +0000 (22:34 +0100)]
connection: use KDBUS_HELLO_ACCEPT_MEMFD to check for passed memfds

Signed-off-by: Djalal Harouni <tixxdz@opendz.org>
10 years agotest-activator: add more tests for the activation logic
Djalal Harouni [Tue, 14 Oct 2014 19:47:50 +0000 (20:47 +0100)]
test-activator: add more tests for the activation logic

Signed-off-by: Djalal Harouni <tixxdz@opendz.org>
10 years agotest-util: add test_is_capable() to check for capabilities and use it
Djalal Harouni [Tue, 14 Oct 2014 13:22:57 +0000 (14:22 +0100)]
test-util: add test_is_capable() to check for capabilities and use it

Signed-off-by: Djalal Harouni <tixxdz@opendz.org>
10 years agotest-util: move RUN_UNPRIVILEGED definitions to kdbus-util.h
Djalal Harouni [Mon, 13 Oct 2014 22:27:07 +0000 (23:27 +0100)]
test-util: move RUN_UNPRIVILEGED definitions to kdbus-util.h

Will be used by other tests

Signed-off-by: Djalal Harouni <tixxdz@opendz.org>
10 years agokdbus.h: add KDBUS_HELLO_ACCEPT_MEMFD (ABI break)
Daniel Mack [Tue, 14 Oct 2014 17:53:23 +0000 (19:53 +0200)]
kdbus.h: add KDBUS_HELLO_ACCEPT_MEMFD (ABI break)

Add another flags to the connection's flags to denote whether it
want to receive memfds. Reject messages with -ECOMM if it contains
a memfd if the receiver can't cope with it.

Signed-off-by: Daniel Mack <daniel@zonque.org>
10 years agotests: strncpy() corrections
Daniel Mack [Tue, 14 Oct 2014 12:54:27 +0000 (14:54 +0200)]
tests: strncpy() corrections

Use the correct maximum size with strncpy(), even though we're using
small static strings as sources.

Signed-off-by: Daniel Mack <daniel@zonque.org>
10 years agodoc: KDBUS_MSG_FLAGS_SYNC_REPLY can be interrupted by a signal delivery
Djalal Harouni [Mon, 13 Oct 2014 15:59:17 +0000 (16:59 +0100)]
doc: KDBUS_MSG_FLAGS_SYNC_REPLY can be interrupted by a signal delivery

Signed-off-by: Djalal Harouni <tixxdz@opendz.org>
10 years agotest-sync: ensure sync is interruptible and it ignors SA_RESTART flag
Djalal Harouni [Mon, 13 Oct 2014 15:41:35 +0000 (16:41 +0100)]
test-sync: ensure sync is interruptible and it ignors SA_RESTART flag

Signed-off-by: Djalal Harouni <tixxdz@opendz.org>
10 years agotest: test monitor connections for broadcast messages
Djalal Harouni [Sun, 12 Oct 2014 18:42:16 +0000 (19:42 +0100)]
test: test monitor connections for broadcast messages

Signed-off-by: Djalal Harouni <tixxdz@opendz.org>
10 years agotest-fd: ensure that broadcasting fds and memfds will fail with -ENOTUNIQ
Djalal Harouni [Sun, 12 Oct 2014 18:01:52 +0000 (19:01 +0100)]
test-fd: ensure that broadcasting fds and memfds will fail with -ENOTUNIQ

Signed-off-by: Djalal Harouni <tixxdz@opendz.org>
10 years agokdbus.txt: fix things spotted by Alban Crequy
Daniel Mack [Sun, 12 Oct 2014 15:42:44 +0000 (17:42 +0200)]
kdbus.txt: fix things spotted by Alban Crequy

Signed-off-by: Daniel Mack <daniel@zonque.org>
10 years agokdbus.txt: fixes
Daniel Mack [Sun, 12 Oct 2014 14:50:03 +0000 (16:50 +0200)]
kdbus.txt: fixes

Fix issues spotted by Greg.

Signed-off-by: Daniel Mack <daniel@zonque.org>
10 years agohandle: do not look at 'features' for now
Daniel Mack [Fri, 10 Oct 2014 17:08:38 +0000 (19:08 +0200)]
handle: do not look at 'features' for now

We still need to decide how we wanna handle this.

Signed-off-by: Daniel Mack <daniel@zonque.org>
10 years agoMerge branch 'master' of https://code.google.com/p/d-bus
Greg Kroah-Hartman [Fri, 10 Oct 2014 03:04:41 +0000 (20:04 -0700)]
Merge branch 'master' of https://code.google.com/p/d-bus

10 years agoMerge pull request #26 from michelecurti/master
Greg Kroah-Hartman [Fri, 10 Oct 2014 03:04:58 +0000 (20:04 -0700)]
Merge pull request #26 from michelecurti/master

test: fix typo

10 years agonames: on KDBUS_CMD_NAME_RELEASE check that connection can see the name
Djalal Harouni [Thu, 9 Oct 2014 21:56:03 +0000 (22:56 +0100)]
names: on KDBUS_CMD_NAME_RELEASE check that connection can see the name

Before trying to release a name verify that the connection is able to
see the name on the endpoint, we do this since custom endpoint may
install policies to restrict SEE access if we do not perform this check,
then a connection may try KDBUS_CMD_NAME_RELEASE ioctl() and brute force
names owned by other connections, in this case it will get the
-EADDRINUSE error which indicates that the name is in use, later it can
monitor the name by re-trying the call, this way it can bypass the
notification checks that are done for
{KDBUS_ITEM_NAME_ADD|KDBUS_ITEM_NAME_REMOVE}.

The kdbus_ep_policy_check_notification() checks first if the connection
is able to see the notifications. So follow and don't leak hints from
KDBUS_CMD_NAME_RELEASE.

No need to check for OWN access, since for other connections as stated
above a name will fail with -EADDRINUSE.

Signed-off-by: Djalal Harouni <tixxdz@opendz.org>
10 years agoendpoint: add function kdbus_ep_policy_check_see_access() and use it
Djalal Harouni [Thu, 9 Oct 2014 21:51:26 +0000 (22:51 +0100)]
endpoint: add function kdbus_ep_policy_check_see_access() and use it

Add the locked version of kdbus_ep_policy_check_see_access_unlocked()
and use it where appropriate.

Signed-off-by: Djalal Harouni <tixxdz@opendz.org>
10 years agobroadcast: add TALK access checks for broadcast messages
Djalal Harouni [Thu, 9 Oct 2014 21:38:43 +0000 (22:38 +0100)]
broadcast: add TALK access checks for broadcast messages

Add code to perform broadcast access checks, we split the shared code
into two functions:
kdbus_custom_ep_check_talk_access()
kdbus_ep_has_default_talk_access()

And add kdbus_ep_policy_check_broadcast() to do the broadcast access
checks.

To perform broadcast, these rules must be satisfied:

1) Check custom endpoint policies, if it allows the TALK continue,
   otherwise block.

2) If the sender connection is a privileged connection, allow
broadcast.

3) If the sender and receiver run under the same user, allow broadcast.

4) If the sender connection owns names on the bus and if
the destination connection do not own names, allow broadcast. Otherwise
fail check the bus policy rules for these two reasons:

   * anonymous connections should not signal to other connections.
   * receivers that own names may have policies that block the TALK
     access, so do not bypass this.

  This openes the case where connections that own names may gain TALK
  access to other connections on the bus through broadcast! Yes but
  since this is the intended behaviour of signals we can't do
  otherwise. Of course as stated above if the destination owns names
  then broadcasts are subject to policy rules (we do not bypass policy
  rules).

5) If the policy rules of the default endpoint block the TALK access,
then block broadcasts, otherwise allow it.

These are the same rules that apply to TALK access and unicast checks,
the only exception is rule 4) that was introduced to allow services to
signal on the bus.

Signed-off-by: Djalal Harouni <tixxdz@opendz.org>
10 years agoconnection: make conn->name_count atomic
Djalal Harouni [Thu, 9 Oct 2014 21:26:51 +0000 (22:26 +0100)]
connection: make conn->name_count atomic

Make conn->name_count an atomic type, so it can be checked safely later
when checking that the connection does really own names.

While we are it fix another count bug in kdbus_cmd_name_acquire() now we
register a slot by increment the counter before all operations, and we
decrement it before returning, this way we do not race for names and no
need to use complex locks.

Signed-off-by: Djalal Harouni <tixxdz@opendz.org>
10 years agotest: test-policy-priv do broadcast tests after a policy holder is uploaded
Djalal Harouni [Wed, 8 Oct 2014 17:43:34 +0000 (18:43 +0100)]
test: test-policy-priv do broadcast tests after a policy holder is uploaded

Add more broadcast tests that will run after a policy holder is uploaded
on the bus.

Each test is documented. Currently we fail at these tests, next patches
will fix this.

Signed-off-by: Djalal Harouni <tixxdz@opendz.org>
10 years agotest: test-policy-priv do broadcast tests before a policy holder is uploaded
Djalal Harouni [Wed, 8 Oct 2014 09:59:17 +0000 (10:59 +0100)]
test: test-policy-priv do broadcast tests before a policy holder is uploaded

Add broadcast tests, and modify RUN_UNPRIVILEGED() so we can specify
the uid/gid of the user to drop in and run tests.

Move all the tests that check the default behaviour of the bus and
before a policy holder is uploaded to their function:
test_priv_before_policy_upload().

Each test is documented.

Signed-off-by: Djalal Harouni <tixxdz@opendz.org>
10 years agodoc: document the new broadcast behaviour
Djalal Harouni [Thu, 9 Oct 2014 19:32:00 +0000 (20:32 +0100)]
doc: document the new broadcast behaviour

Currently we do not check senders when doing broadcasts.

In order to block the following scenarios, we need to improve the
broadcast logic and check the policy rules before allowing broadcast
messages.

* Since unprivileged users can't use unicast to communicate unless a
  policy that permits this was uploaded, follow and block broadcast
  communications between unprivileged users unless a policy rule that
  allows this is satisfied.

* We do not want unprivileged connections that do not own names to
  send signals to privileged connections.

* We do not want unprivileged connections that do not own names to
  send signals to other connections that might own names.

So to achieve this and to allow broadcast messages for legitimate
scenarios, we follow the same unicast checks, and we introduce one
implicit rule to allow broadcast from connections that own names.

1) Check custom endpoint policies, if it allows the TALK continue,
otherwise block.

2) If the sender connection is a privileged connection, allow broadcast.

3) If the sender and receiver run under the same user, allow broadcast.

4) If the sender connection owns names on the bus and if the
   destination connection do not own names, allow broadcast.
   Otherwise check bus policy rules for these two reasons:

   * anonymous connections should not signal to other connections.
   * receivers that own names may have policies that block the TALK
     access, so do not bypass this.

   This openes the case where connections that own names may gain TALK
   access to other connections on the bus through broadcast! Yes but
   since this is the intended behaviour of signals we can't do
   otherwise. Of course as stated above if the destination owns names
   then broadcasts are subject to policy rules (we do not bypass policy
   rules).

5) If the policy rules of the default endpoint block the TALK access,
then block broadcasts, otherwise allow it.

These are the same rules that apply to TALK access and unicast checks,
the only exception is rule 4) that was instroduced to allow services to
signal on the bus.

Signed-off-by: Djalal Harouni <tixxdz@opendz.org>
10 years agoconnection: attach KDBUS_ATTACH_NAMES | KDBUS_ATTACH_CONN_NAME to faked creds
Daniel Mack [Thu, 9 Oct 2014 14:31:59 +0000 (16:31 +0200)]
connection: attach KDBUS_ATTACH_NAMES | KDBUS_ATTACH_CONN_NAME to faked creds

If the source connection has faked credentials, the metadata object
associated with the kmsg may still be augmented by KDBUS_ATTACH_NAMES
and KDBUS_ATTACH_CONN_NAME.

Signed-off-by: Daniel Mack <daniel@zonque.org>
10 years agotest: fix typo
Michele Curti [Thu, 9 Oct 2014 12:05:42 +0000 (14:05 +0200)]
test: fix typo

fix typo in bybye test description

10 years agoconnection: style nit
Daniel Mack [Wed, 8 Oct 2014 16:04:29 +0000 (18:04 +0200)]
connection: style nit

Signed-off-by: Daniel Mack <daniel@zonque.org>
10 years agoconnection: check for msg->dst_id == name_entry->conn->id
Daniel Mack [Wed, 8 Oct 2014 15:55:53 +0000 (17:55 +0200)]
connection: check for msg->dst_id == name_entry->conn->id

When sending a message, we now allow both the ID and the name to be
specified. In such cases, make sure to reject the message if the
connection that currently owns the name does not match the given ID.

This allows us to tie the action of sending a message to the fact that
the connection still owns a certain name, which is essential for
enforcing a policy from userspace (bus-proxyd) in a race-free way.

Signed-off-by: Daniel Mack <daniel@zonque.org>
10 years agomessage: allow both names and unique IDs at the same time
Daniel Mack [Wed, 8 Oct 2014 15:36:34 +0000 (17:36 +0200)]
message: allow both names and unique IDs at the same time

Do not bail if a name item is passed while the message is addressed to
a unique ID. We'll use that message configuration in the next patch.

Signed-off-by: Daniel Mack <daniel@zonque.org>
10 years agokdbus.txt: more information about faked credentials and metadata attachment
Daniel Mack [Wed, 8 Oct 2014 13:29:35 +0000 (15:29 +0200)]
kdbus.txt: more information about faked credentials and metadata attachment

Signed-off-by: Daniel Mack <daniel@zonque.org>
10 years agoTODO: update
Daniel Mack [Wed, 8 Oct 2014 13:06:17 +0000 (15:06 +0200)]
TODO: update

10 years agoconnection: suppress message metadata attachment for faked creds
Daniel Mack [Wed, 8 Oct 2014 12:59:31 +0000 (14:59 +0200)]
connection: suppress message metadata attachment for faked creds

If a connection has installed faked credentials upon its creation, alter
the message treatment so that

 a) the kmsg's metadata is not a freshly allocated one but a full copy
    of the source connection's owner_meta

 b) no new items are attached to that metadata object

The problem here is that when a privileged bus user provided fake
credentials, it did that because it wants to be a proxy for another
task. In this case, 'current' would point us to the information of
the proxy's task, not the proxied one. As we don't want to provide
receivers with wrong information, make sure the only metadata items
that are added to messages are those which we have in faked form
already.

Signed-off-by: Daniel Mack <daniel@zonque.org>
10 years agometadata: add kdbus_meta_dup()
Daniel Mack [Wed, 8 Oct 2014 12:47:44 +0000 (14:47 +0200)]
metadata: add kdbus_meta_dup()

Add a way to duplicate a metadata object. This will be needed to dup
conn->owner_meta and attach that to messages, instead of new items
retrieved from 'current'.

Signed-off-by: Daniel Mack <daniel@zonque.org>
10 years agotree-wide: reject unknown flags
Daniel Mack [Wed, 8 Oct 2014 10:42:07 +0000 (12:42 +0200)]
tree-wide: reject unknown flags

After further discussion, we concluded that we need to be strict on
the checking of flags and reject everything that we don't know.

If we eventually need more flags, we have to deal with feature
negotiation.

Signed-off-by: Daniel Mack <daniel@zonque.org>
10 years agotest: print filename in assertion
Daniel Mack [Wed, 8 Oct 2014 09:28:42 +0000 (11:28 +0200)]
test: print filename in assertion

Signed-off-by: Daniel Mack <daniel@zonque.org>
10 years agohandle: use sizeof(*type)
Daniel Mack [Wed, 8 Oct 2014 09:09:40 +0000 (11:09 +0200)]
handle: use sizeof(*type)

Just a cosmetic change for the sake of consistency.

Signed-off-by: Daniel Mack <daniel@zonque.org>
10 years agometadata: clean up code, and remove invalid comment
Daniel Mack [Tue, 7 Oct 2014 15:39:47 +0000 (17:39 +0200)]
metadata: clean up code, and remove invalid comment

Signed-off-by: Daniel Mack <daniel@zonque.org>
10 years agotree-wide: s/_EP_/_ENDPOINT_/g (API break)
Daniel Mack [Wed, 8 Oct 2014 11:52:41 +0000 (13:52 +0200)]
tree-wide: s/_EP_/_ENDPOINT_/g (API break)

Avoid abbr. and rename EP â†’ ENDPOINT.

Signed-off-by: Daniel Mack <daniel@zonque.org>
10 years agokdbus.h: add features bitmask to cmd_make and cmd_hello (ABI BREAK)
Daniel Mack [Wed, 8 Oct 2014 09:29:49 +0000 (11:29 +0200)]
kdbus.h: add features bitmask to cmd_make and cmd_hello (ABI BREAK)

Add a new field called 'features' to struct kdbus_cmd_make and
kdbus_cmd_hello. In these bitmasks, userspace can request features from
the kernel, and the kernel can itself request features from userspace.

For now, we check that this field is set to 0, and bail with
 -EOPNOTSUPP otherwise.

Signed-off-by: Daniel Mack <daniel@zonque.org>
10 years agotest: fix some 'dead assignment' errors
Daniel Mack [Tue, 7 Oct 2014 12:43:18 +0000 (14:43 +0200)]
test: fix some 'dead assignment' errors

10 years agotest-endpoint: fix nul-termination of string
Daniel Mack [Tue, 7 Oct 2014 08:29:26 +0000 (10:29 +0200)]
test-endpoint: fix nul-termination of string

Found by coverity.

Signed-off-by: Daniel Mack <daniel@zonque.org>
10 years agohandle.c: add kdbus_copy_from_user() helper
Daniel Mack [Mon, 6 Oct 2014 16:27:05 +0000 (18:27 +0200)]
handle.c: add kdbus_copy_from_user() helper

Add a little helper to check for 8-byte aligment as well as doing
the actual copy_from_user(). Use it from location where we used to
open-code the same functionality.

Signed-off-by: Daniel Mack <daniel@zonque.org>
10 years agokdbus.h: implement KDBUS_CMD_{MSG_CANCEL,FREE} with structs (ABI break)
Daniel Mack [Mon, 6 Oct 2014 16:09:35 +0000 (18:09 +0200)]
kdbus.h: implement KDBUS_CMD_{MSG_CANCEL,FREE} with structs (ABI break)

Rather than passing a pointer to a u64 with these ioctls, use a struct.
That also allows us to pass (currently unused) flags around.

Signed-off-by: Daniel Mack <daniel@zonque.org>
10 years agopool: kdbus_pool_slice_alloc() release the pool lock on errors
Djalal Harouni [Mon, 6 Oct 2014 15:44:43 +0000 (16:44 +0100)]
pool: kdbus_pool_slice_alloc() release the pool lock on errors

Signed-off-by: Djalal Harouni <tixxdz@opendz.org>
10 years agodoc: KDBUS_CMD_NAME_LIST ioctl might fail with -ENOBUFS
Djalal Harouni [Mon, 6 Oct 2014 15:44:42 +0000 (16:44 +0100)]
doc: KDBUS_CMD_NAME_LIST ioctl might fail with -ENOBUFS

KDBUS_CMD_NAME_LIST might fail with -ENOBUFS if there is no available
memory in the connection's pool.

Signed-off-by: Djalal Harouni <tixxdz@opendz.org>
10 years agonames: simplify code in kdbus_cmd_name_acquire()
Daniel Mack [Mon, 6 Oct 2014 12:41:35 +0000 (14:41 +0200)]
names: simplify code in kdbus_cmd_name_acquire()

There's no need to flush notifications after
kdbus_ep_policy_check_own_access(). Hence, we can get rid of the
jump label.

Signed-off-by: Daniel Mack <daniel@zonque.org>
10 years agonames: don't refuse unknown flags
Daniel Mack [Mon, 6 Oct 2014 12:28:19 +0000 (14:28 +0200)]
names: don't refuse unknown flags

We need to allow for forward-compat, so just ignore invalid flags in
kdbus_cmd_name_acquire().

Signed-off-by: Daniel Mack <daniel@zonque.org>
10 years agoconnection: make kdbus_conn_broadcast() void
Daniel Mack [Mon, 6 Oct 2014 10:25:52 +0000 (12:25 +0200)]
connection: make kdbus_conn_broadcast() void

As we never return any error from this function anyway, we can as well
make it void.

Signed-off-by: Daniel Mack <daniel@zonque.org>
10 years agodoc: policy: specify on which endpoint implicit rules are enforced
Djalal Harouni [Sat, 4 Oct 2014 21:37:17 +0000 (22:37 +0100)]
doc: policy: specify on which endpoint implicit rules are enforced

Separate implicit rules doc into two parts one for default endpoints and
the other for custom ones.

Signed-off-by: Djalal Harouni <tixxdz@opendz.org>
10 years agoendpoint: check if the endpoint has a policy before locking DB and connection
Djalal Harouni [Fri, 3 Oct 2014 23:22:00 +0000 (00:22 +0100)]
endpoint: check if the endpoint has a policy before locking DB and connection

Littel optimization on kdbus_ep_policy_check_notification().

Default bus endpoints do not enforce the endpoint policy, so instead
of checking if the endpoint is using the policy in
kdbus_ep_policy_check_see_access_unlocked(), just do the check at the
beginning and avoid all the useless locking.

This is already done for kdbus_ep_policy_check_src_names().

Signed-off-by: Djalal Harouni <tixxdz@opendz.org>
10 years agoconnection: broadcast and signaling hide non-fatal errors
Djalal Harouni [Fri, 3 Oct 2014 23:21:59 +0000 (00:21 +0100)]
connection: broadcast and signaling hide non-fatal errors

Broadcast is one way, we should not care for receivers or their errors.

Currently we may return an error on custom endpoints if the last
receiver is not authorized to see name's of the sender. Even if we
have signaled all previous receivers, if we fail for the last one we may
return an error.

Improve this by just ignoring this type of error. We hide non-fatal
errors, and we return an error only if kdbus_meta_append() fails since
it can not recover. kdbus_meta_append() fails with -ENOMEM, its logic
can perhaps be improved later to have at least a nofail version where we
can recover.

And from D-Bus spec:

"Errors

Messages of type ERROR are most commonly replies to a METHOD_CALL, but
may be returned in reply to any kind of message. The message bus for
example will return an ERROR in reply to a signal emission if the bus
does not have enough memory to send the signal. "

So take this shortcut here, and hide non-fatal errors.

Signed-off-by: Djalal Harouni <tixxdz@opendz.org>
10 years agoendpoint: fix kernel doc for kdbus_ep_policy_check_src_names()
Djalal Harouni [Fri, 3 Oct 2014 23:21:58 +0000 (00:21 +0100)]
endpoint: fix kernel doc for kdbus_ep_policy_check_src_names()

Signed-off-by: Djalal Harouni <tixxdz@opendz.org>
10 years agokdbus-doc: fix policy doc on default behaviour on buses and custom endpoints
Djalal Harouni [Fri, 3 Oct 2014 23:21:57 +0000 (00:21 +0100)]
kdbus-doc: fix policy doc on default behaviour on buses and custom endpoints

Operations are denied by default on default endpoints unless they are
covered by one of the implicit policy rules.

Operations are denied by default on custom endpoints unless the policy
is instructed to allow some exceptions...

The custom endpoint policy if any goes on top of the bus policy.

Signed-off-by: Djalal Harouni <tixxdz@opendz.org>
10 years agoconnection: allow unlimited bus connections for privileged users
Daniel Mack [Fri, 3 Oct 2014 12:01:55 +0000 (14:01 +0200)]
connection: allow unlimited bus connections for privileged users

As described in https://code.google.com/p/d-bus/issues/detail?id=9 by
Simon McVittie:

  kdbus allows 256 connections per uid per bus, which the CAP_IPC_OWNER
  may exceed.

  This could get quite tight for the session bus: I currently have 46
  session bus connections, so I'm only an order of magnitude away from
  the limit. The session bus is currently restricted to its owning uid
  only, and even if other uids are allowed in (perhaps via restricted
  endpoints) for Android-style sandboxing, I expect it will be mostly
  populated by its owning uid.

I agree with David Herrmann's solution:

  Connections are already limited by the per-user FD limit. So maybe we
  should just allow unlimited connections by the bus UID, too? All other
  connections must be limited, obviously. Otherwise, they can trigger
  slow-paths and reduce performance of the bus considerably.

Signed-off-by: Daniel Mack <daniel@zonque.org>
10 years agokdbus.txt: more details on matches and rules
Daniel Mack [Thu, 2 Oct 2014 18:26:15 +0000 (20:26 +0200)]
kdbus.txt: more details on matches and rules

Signed-off-by: Daniel Mack <daniel@zonque.org>
10 years agomatch: add flags to kdbus_cmd_match (ABI break!)
Daniel Mack [Thu, 2 Oct 2014 15:09:09 +0000 (17:09 +0200)]
match: add flags to kdbus_cmd_match (ABI break!)

Add a 64-bit flags field to struct kdbus_cmd_match, and define a flag
that allows for a race-free way of replacing existing matches.

Signed-off-by: Daniel Mack <daniel@zonque.org>
10 years agohandle: clean up 'cmd' variable namespace
Daniel Mack [Thu, 2 Oct 2014 14:38:03 +0000 (16:38 +0200)]
handle: clean up 'cmd' variable namespace

'cmd' is an integer variable in the ioctl callbacks that denotes the
actual command. A few places in those handlers, however, used to
define a struct by the same name.

Clean up the namespace be renaming the latter.

Signed-off-by: Daniel Mack <daniel@zonque.org>
10 years agomatch: make KDBUS_CMD_MATCH_REMOVE return -ENOENT when cookie was not found
Daniel Mack [Thu, 2 Oct 2014 14:01:28 +0000 (16:01 +0200)]
match: make KDBUS_CMD_MATCH_REMOVE return -ENOENT when cookie was not found

When the cookie specified in the command struct was not found in the
match database, return -ENOENT.

Signed-off-by: Daniel Mack <daniel@zonque.org>
10 years agokdbus.h: attach items[] to kdbus_cmd_conn_info, not name[] (ABI break!)
Daniel Mack [Thu, 2 Oct 2014 10:57:53 +0000 (12:57 +0200)]
kdbus.h: attach items[] to kdbus_cmd_conn_info, not name[] (ABI break!)

Instead of directly attaching the string to kdbus_cmd_conn_info, use
items as we do in all other commands. That allows for more flexibility
later.

Signed-off-by: Daniel Mack <daniel@zonque.org>
10 years agotest: fix benchmark stats accounting
David Herrmann [Thu, 2 Oct 2014 10:55:29 +0000 (12:55 +0200)]
test: fix benchmark stats accounting

We should not reset stats before retying a test, otherwise, the test will
file in batch mode as stats.count is 0.

Signed-off-by: David Herrmann <dh.herrmann@gmail.com>
10 years agotest: add benchmark-mode to clear attach-flags
David Herrmann [Thu, 2 Oct 2014 10:35:33 +0000 (12:35 +0200)]
test: add benchmark-mode to clear attach-flags

Processing message attachments can take quite some time. Add a mode to
skip those.

Signed-off-by: David Herrmann <dh.herrmann@gmail.com>
10 years agotest: beef up benchmark
David Herrmann [Thu, 2 Oct 2014 08:58:43 +0000 (10:58 +0200)]
test: beef up benchmark

Make some adjustments to the kdbus benchmark:
 * use CLOCK_THREAD_CPUTIME_ID
 * use nano-second precision
 * make memfd-usage optional
 * add UDS comparison

Signed-off-by: David Herrmann <dh.herrmann@gmail.com>
10 years agoendpoint: name the DEVTYPE 'endpoint' rather than 'ep'
Tom Gundersen [Wed, 1 Oct 2014 18:54:57 +0000 (20:54 +0200)]
endpoint: name the DEVTYPE 'endpoint' rather than 'ep'

Signed-off-by: Tom Gundersen <teg@jklm.no>
10 years agokdbus.txt: document fixed implicit policy rules on custom endpoints
Daniel Mack [Wed, 1 Oct 2014 19:00:21 +0000 (21:00 +0200)]
kdbus.txt: document fixed implicit policy rules on custom endpoints

Signed-off-by: Daniel Mack <daniel@zonque.org>
10 years agotest/test-endpoint: add checks on conn_info and update_endpoint
Daniel Mack [Wed, 1 Oct 2014 18:54:23 +0000 (20:54 +0200)]
test/test-endpoint: add checks on conn_info and update_endpoint

Implement a new helper update_endpoint() to update a custom endpoint,
and duplicate the checks to verify that they succeed after the policy
has been updated on the endpoint.

Also, check that conn_info denies handing out information with an empty
policy set, and allows it after the update.

Signed-off-by: Daniel Mack <daniel@zonque.org>
10 years agoconnection: limit kdbus_cmd_conn_info()
Daniel Mack [Wed, 1 Oct 2014 18:53:12 +0000 (20:53 +0200)]
connection: limit kdbus_cmd_conn_info()

Don't leak information on connection through KDBUS_CMD_CONN_INFO if
the asking connection is not allowed to see the other connection.

Signed-off-by: Daniel Mack <daniel@zonque.org>
10 years agotest/kdbus-util: add kdbus_conn_info()
Daniel Mack [Wed, 1 Oct 2014 18:52:39 +0000 (20:52 +0200)]
test/kdbus-util: add kdbus_conn_info()

Add a user of KDBUS_CMD_CONN_INFO as utility function to the test suite.

Signed-off-by: Daniel Mack <daniel@zonque.org>
10 years agopolicy: don't allow privileged bus users to own names
Daniel Mack [Wed, 1 Oct 2014 17:47:05 +0000 (19:47 +0200)]
policy: don't allow privileged bus users to own names

That check was moved to endpoint.c, but I forgot to stage that hunk.

Signed-off-by: Daniel Mack <daniel@zonque.org>
10 years agotest-endpoint: enable test of changed policy behavior
Daniel Mack [Wed, 1 Oct 2014 17:42:54 +0000 (19:42 +0200)]
test-endpoint: enable test of changed policy behavior

Now that the policy does the right thing, actually check that the
custom endpoint connection has not gotten a name change notification.

This would have failed previously.

Signed-off-by: Daniel Mack <daniel@zonque.org>
10 years agopolicy: move credential checks from policy.c to endpooint.c
Daniel Mack [Wed, 1 Oct 2014 17:34:49 +0000 (19:34 +0200)]
policy: move credential checks from policy.c to endpooint.c

When checking the policy on custom endpoints, we cannot white-list
privileged users. Even when the bus owner and the bus client are
using the same uid, we still have to look at the policy entries,

For connections on the default endpoint, however, things are different,
so move the checks from policy.c into endpoint.c and conduct them
after checking the custom endpoint's policy db, but before the default
endpoint's is looked at.

Signed-off-by: Daniel Mack <daniel@zonque.org>
10 years agokdbus: document -ENOBUFS error code
Daniel Mack [Wed, 1 Oct 2014 17:17:33 +0000 (19:17 +0200)]
kdbus: document -ENOBUFS error code

Signed-off-by: Daniel Mack <daniel@zonque.org>
10 years agoRevert "item: make kdbus_items_get_str() work with KDBUS_ITEM_NAME_{ADD,REMOVE,CHANGE}"
Daniel Mack [Wed, 1 Oct 2014 17:11:26 +0000 (19:11 +0200)]
Revert "item: make kdbus_items_get_str() work with KDBUS_ITEM_NAME_{ADD,REMOVE,CHANGE}"

Turns out the only use case for this tweak can be done differently.
Remove it for now. We might eventually switch to a more generic
implementation that hands out the entire item.

This reverts commit c9f76f807f366625f446053a4d529995aca9b37a.

10 years agoendpoint: use kmsg->notify_name in kdbus_ep_policy_check_notification()
Daniel Mack [Wed, 1 Oct 2014 17:16:14 +0000 (19:16 +0200)]
endpoint: use kmsg->notify_name in kdbus_ep_policy_check_notification()

Use kmsg->notify_name rather than walking the items of the kmsg in
kdbus_ep_policy_check_notification(). We have that short-cut already
anyway, and it's faster.

Signed-off-by: Daniel Mack <daniel@zonque.org>
10 years agoadd temporary defines for memfd_create() syscall
Kay Sievers [Wed, 1 Oct 2014 17:09:19 +0000 (19:09 +0200)]
add temporary defines for memfd_create() syscall

10 years agoconnection: minor documentation fixes
Tom Gundersen [Wed, 1 Oct 2014 14:35:30 +0000 (16:35 +0200)]
connection: minor documentation fixes

10 years agoendpoint: introduce kdbus_ep_policy_check_src_names()
Daniel Mack [Wed, 1 Oct 2014 13:54:44 +0000 (15:54 +0200)]
endpoint: introduce kdbus_ep_policy_check_src_names()

In order to consequently block connections on custom endpoints from
seeing particular names, we must filter broadcast messages unless the
receiver is allowed to see any of the sender's currently owned names.

For this, introduce kdbus_ep_policy_check_src_names(). We might
eventually need a cache here for speeding up things.

Signed-off-by: Daniel Mack <daniel@zonque.org>
10 years agoendpoint: fix kernel-doc
Daniel Mack [Wed, 1 Oct 2014 13:49:16 +0000 (15:49 +0200)]
endpoint: fix kernel-doc

Fix kernel-doc spelling for kdbus_ep_policy_check_notification()

Signed-off-by: Daniel Mack <daniel@zonque.org>
10 years agoconnection: switch to timespec64
Daniel Mack [Wed, 1 Oct 2014 10:03:48 +0000 (12:03 +0200)]
connection: switch to timespec64

Signed-off-by: Daniel Mack <daniel@zonque.org>
10 years agoep: add kdbus_ep_policy_check_notification()
Daniel Mack [Wed, 1 Oct 2014 09:44:24 +0000 (11:44 +0200)]
ep: add kdbus_ep_policy_check_notification()

When delivering notification to connections, make sure to check whether
their carried name is allowed by the destination endpoint's policy, and
supress the notification otherwise.

This is important to we don't leak notifications on added names if the
endpoint enforces a SEE policy.

Signed-off-by: Daniel Mack <daniel@zonque.org>
10 years agoitem: make kdbus_items_get_str() work with KDBUS_ITEM_NAME_{ADD,REMOVE,CHANGE}
Daniel Mack [Wed, 1 Oct 2014 09:43:11 +0000 (11:43 +0200)]
item: make kdbus_items_get_str() work with KDBUS_ITEM_NAME_{ADD,REMOVE,CHANGE}

KDBUS_ITEM_NAME_{ADD,REMOVE,CHANGE} embed their string in
item->name_change.name, not item->str. Handle that case.

Signed-off-by: Daniel Mack <daniel@zonque.org>
10 years agohandle.c: drop unused include
Daniel Mack [Wed, 1 Oct 2014 00:34:29 +0000 (02:34 +0200)]
handle.c: drop unused include

Signed-off-by: Daniel Mack <daniel@zonque.org>
10 years agohandle: document kdbus_handle.ptr
Daniel Mack [Wed, 1 Oct 2014 00:10:11 +0000 (02:10 +0200)]
handle: document kdbus_handle.ptr

Signed-off-by: Daniel Mack <daniel@zonque.org>
10 years agotest-util: fix kdbus_msg_recv_poll()
Daniel Mack [Wed, 1 Oct 2014 00:04:02 +0000 (02:04 +0200)]
test-util: fix kdbus_msg_recv_poll()

Implement kdbus_msg_recv_poll() so that it really takes the time before
and after poll() is called, so that we know how ofter we have to repeat
the loop before we give up.

Signed-off-by: Daniel Mack <daniel@zonque.org>
10 years agotest: add custom endpoint test
Daniel Mack [Tue, 30 Sep 2014 23:36:50 +0000 (01:36 +0200)]
test: add custom endpoint test

Add a test to check the behavior of custom endpoints. Not finished yet.

Signed-off-by: Daniel Mack <daniel@zonque.org>
10 years agoitem: add kdbus_items_get_str()
Daniel Mack [Tue, 30 Sep 2014 23:02:20 +0000 (01:02 +0200)]
item: add kdbus_items_get_str()

Introduce a new helper function called kdbus_items_get_str(), which
walks a list of items in order to find one which matches a given type
and return the ->str pointer inside of it.

This allows us to ditch various implementations of similar functions.

Signed-off-by: Daniel Mack <daniel@zonque.org>
10 years agoendpoint: don't leak hints on whether a name exists on a custom endpoint
Daniel Mack [Tue, 30 Sep 2014 22:07:58 +0000 (00:07 +0200)]
endpoint: don't leak hints on whether a name exists on a custom endpoint

The whole point of installing policy on custom endpoint is not not let
users of that endpoint know that a name exists. Returning -EPERM in
response to such attempts leaks the information that the name in fact
exists, but is firewalled.

To fix this, return -ENOENT when policy returns -EPERM on custom
endpoints.

Signed-off-by: Daniel Mack <daniel@zonque.org>
10 years agoendpoint: clean up kdbus_ep_policy_check_see_access_unlocked()
Daniel Mack [Tue, 30 Sep 2014 21:44:05 +0000 (23:44 +0200)]
endpoint: clean up kdbus_ep_policy_check_see_access_unlocked()

Just a cosmetic cleanup.

Signed-off-by: Daniel Mack <daniel@zonque.org>
10 years agohandle: don't leak endpoint when KDBUS_HANDLE_EP_OWNER handle is released
Tom Gundersen [Tue, 30 Sep 2014 15:41:06 +0000 (17:41 +0200)]
handle: don't leak endpoint when KDBUS_HANDLE_EP_OWNER handle is released

Signed-off-by: Tom Gundersen <teg@jklm.no>