test: fix usage print endless loop

do not use the some iterator in nested loops

Signed-off-by: Rui Miguel Silva <rmfrfs@gmail.com>
match: init list rule so we can remove it on error

initialise the rules_entry so we don't oops on error paths when the rule is not
in list yet.

Signed-off-by: Rui Miguel Silva <rmfrfs@gmail.com>
connection: return -ETIMEDOUT immediately if msg->timeout_ns <= now

If the timeout of a message has already exceeded at the time of
sending a message, there's no point in going to sleep. Return
-ETIMEDOUT immediately in such cases.

While at it, move the !reply_wait check to kdbus_conn_wait_reply().

Signed-off-by: Daniel Mack <daniel@zonque.org>
connection: kdbus_conn_reply_unref() can take NULL pointers

No need to check the parameter prior to calling that function.

Signed-off-by: Daniel Mack <daniel@zonque.org>
kdbus.h: remove KDBUS_CMD_CANCEL

Kill KDBUS_CMD_CANCEL. The API wasn't thought to the end, as it cannot
be used in a race-free way from a threaded user-space environment.

Instead, we have to implement a signal mask that is installed before
the waitqueue is entered.

Signed-off-by: Daniel Mack <daniel@zonque.org>
test: preparation support for a namespaced test setup

Signed-off-by: Djalal Harouni <tixxdz@opendz.org>
test: allow our tests to run in a namespaced setup

Tests can take run now in their namespaced setup, if the --mntns switch
is given a new mount namespace is created and kdbusfs is remounted in
that namespace.

Currently all tests succeed, only when we combine the userns switch some
test will fail the ones that call setuid(), we have to make sure that
the uid/gid are mapped in the userns, next patch will fix the ones that
fail.

Signed-off-by: Djalal Harouni <tixxdz@opendz.org>
test-attach: fix attach-flags so we can SKIP it in unprivleged userns

Signed-off-by: Djalal Harouni <tixxdz@opendz.org>
test-activator: check if all uids/gids are mapped in

Signed-off-by: Djalal Harouni <tixxdz@opendz.org>
test: tree-wide check if all uids and gids are mapped in current userns

Tests that are running in user namespaces where not all the uids and
gids are mapped will be SKIPPED automatically, this is the case of
unprivileged which turn privileged in its userns.

We skip test since even if we have CAP_SETUID we also need the target
uid to be mapped in that userns. Well, we do not need all the uids but
we are just lazy here...

The ones that do not like CONFIG_USER_NS can also test kdbus as it was
in a container, running under root:
  # ./test/kdbus-test --mntns --pidns

With this change unprivileged running on kernel with CONFIR_USER_NS can run
kdbus tests:
$ ./test/kdbus-test --userns --uidns "0 1000 1" --gidns "0 1000 1" --mntns --pidns

(but some tests are skipped)

Signed-off-by: Djalal Harouni <tixxdz@opendz.org>
test: when starting tests print the attach flags mask

Signed-off-by: Djalal Harouni <tixxdz@opendz.org>
kdbus.h: add KDBUS_ITEM_SIGMASK (ABI break)

Introduce KDBUS_ITEM_SIGMASK to store a 64-bit signal mask.

If attached to KDBUS_CMD_SEND in sync mode, the provided signal mask
will be set before the kernel blocks the execution, and restored
afterwards, unless the syscall is interrupted. This makes the
KDBUS_CMD_SEND ioctl behavior similar to what ppoll() does.

Signed-off-by: Daniel Mack <daniel@zonque.org>
Changelog: comment CANCEL and SIGMASK changes

Signed-off-by: Daniel Mack <daniel@zonque.org>
connection: fix BUG: unable to handle kernel paging request at ffffffffffffffc6

Fix the following oops triggered by the sync send. When fetching the
SIGMASK kdbus_items_get() will return an ERR_PTR not NULL, so just check
it and update the item to be NULL again in case of errors.

Note that the code should validate if the SIGMASK was passed two times
or more in the cmd_send and fail with -EEXIST before reaching this call
site.

[  153.671313] BUG: unable to handle kernel paging request at ffffffffffffffc6
[  153.672278] IP: [<ffffffffa0298520>] kdbus_conn_kmsg_send+0xb40/0xc30 [kdbus]
[  153.672286] PGD 1e17067 PUD 1e19067 PMD 0
[  153.672286] Oops: 0000 [#1] SMP
[  153.672286] Modules linked in: kdbus(OE) nf_conntrack_netbios_ns nf_conntrack_broadcast ip6t_REJECT nf_conntrack_ipv6 nf_defrag_ipv6 nf_conntrack_ipv4 nf_defrag_ipv4 xt_conntrack nf_conntrack cfg80211 rfkill ebtable_nat ebtable_broute bridge stp llc ebtable_filter ebtables ip6table_mangle ip6table_raw ip6table_filter ip6_tables iptable_mangle iptable_raw ppdev parport_pc parport i2c_piix4 microcode 8139too serio_raw bochs_drm drm_kms_helper ttm drm 8139cp ata_generic mii i2c_core pata_acpi
[  153.672286] CPU: 0 PID: 1538 Comm: kdbus-test Tainted: G           OE  3.18.0 #4
[  153.672286] Hardware name: Bochs Bochs, BIOS Bochs 01/01/2011
[  153.672286] task: ffff880068d04ce0 ti: ffff880068a40000 task.ti: ffff880068a40000
[  153.672286] RIP: 0010:[<ffffffffa0298520>]  [<ffffffffa0298520>] kdbus_conn_kmsg_send+0xb40/0xc30 [kdbus]
[  153.672286] RSP: 0018:ffff880068a43d98  EFLAGS: 00010246
[  153.672286] RAX: 0000000000000000 RBX: ffff88006795a800 RCX: 0000000000000000
[  153.672286] RDX: 0000000000000286 RSI: ffffffffa02a5040 RDI: ffffffffa02a5c1f
[  153.672286] RBP: ffff880068a43e58 R08: 0000000000000001 R09: ffff88006800bc60
[  153.672286] R10: 0000000000000001 R11: 0000000000aaaaaa R12: ffff88006795a000
[  153.672286] R13: ffff880069d3c000 R14: ffffffffffffffb6 R15: ffff88006800b360
[  153.672286] FS:  00007fd883ce1740(0000) GS:ffff88006d200000(0000) knlGS:0000000000000000
[  153.672286] CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
[  153.672286] CR2: ffffffffffffffc6 CR3: 00000000689b1000 CR4: 00000000000006f0
[  153.672286] Stack:
[  153.672286]  ffff880068a43e58 0000000000003fff ffffffffffffffb6 ffff88006800bc60
[  153.672286]  0000000129e97e4b 0000000000000000 ffff8800698d8470 0000000000000000
[  153.672286]  ffff8800698d84b0 ffff880069d3c000 0000000000000099 0000000027a201d8
[  153.672286] Call Trace:
[  153.672286]  [<ffffffffa029bad0>] handle_ep_ioctl+0xcc0/0xef0 [kdbus]
[  153.672286]  [<ffffffff811efb19>] ? shmem_add_seals+0x89/0x8a0
[  153.672286]  [<ffffffff811efb78>] ? shmem_add_seals+0xe8/0x8a0
[  153.672286]  [<ffffffff8126e8d0>] do_vfs_ioctl+0x300/0x520
[  153.672286]  [<ffffffff8111d37e>] ? rcu_read_lock_held+0x6e/0x80
[  153.672286]  [<ffffffff8127ad9e>] ? __fget_light+0xbe/0xe0
[  153.672286]  [<ffffffff8126eb71>] SyS_ioctl+0x81/0xa0
[  153.672286]  [<ffffffff817fcfa9>] system_call_fastpath+0x12/0x17
[  153.672286] Code: c3 00 00 e9 4e f6 ff ff 4c 8b b5 50 ff ff ff ba 86 02 00 00 48 c7 c6 40 50 2a a0 48 c7 c7 1f 5c 2a a0 31 c0 4c 89 8d 58 ff ff ff <49> 8b 4e 10 e8 b3 65 55 e1 ba 87 02 00 00 48 c7 c6 40 50 2a a0
[  153.672286] RIP  [<ffffffffa0298520>] kdbus_conn_kmsg_send+0xb40/0xc30 [kdbus]
[  153.672286]  RSP <ffff880068a43d98>
[  153.672286] CR2: ffffffffffffffc6
[  153.672286] ---[ end trace 3d9e048aa5474063 ]---
[  153.672286] BUG: sleeping function called from invalid context at kernel/locking/rwsem.c:41
[  153.672286] in_atomic(): 0, irqs_disabled(): 1, pid: 1538, name: kdbus-test
[  153.672286] INFO: lockdep is turned off.
[  153.672286] irq event stamp: 1350
[  153.672286] hardirqs last  enabled at (1349): [<ffffffff81114205>] vprintk_emit+0x245/0x620
[  153.672286] hardirqs last disabled at (1350): [<ffffffff817ff206>] error_sti+0x5/0x6
[  153.672286] softirqs last  enabled at (824): [<ffffffff810ab358>] __do_softirq+0x388/0x660
[  153.672286] softirqs last disabled at (771): [<ffffffff810ab975>] irq_exit+0xc5/0xd0
[  153.672286] CPU: 0 PID: 1538 Comm: kdbus-test Tainted: G      D    OE  3.18.0 #4
[  153.672286] Hardware name: Bochs Bochs, BIOS Bochs 01/01/2011
[  153.672286]  0000000000000000 00000000076526b1 ffff880068a43968 ffffffff817f28fd
[  153.672286]  0000000000000000 0000000000000000 ffff880068a43998 ffffffff810d3fca
[  153.672286]  0000000000000000 ffff880069049878 ffff8800690498e8 ffff880068d04ce0
[  153.672286] Call Trace:
[  153.672286]  [<ffffffff817f28fd>] dump_stack+0x4e/0x68
[  153.672286]  [<ffffffff810d3fca>] __might_sleep+0x18a/0x240
[  153.672286]  [<ffffffff817fa35a>] down_read+0x2a/0xa0
[  153.672286]  [<ffffffff810b8a33>] exit_signals+0x33/0x150
[  153.672286]  [<ffffffff810a8400>] do_exit+0xd0/0xc50
[  153.672286]  [<ffffffff811156fe>] ? kmsg_dump+0x16e/0x220
[  153.672286]  [<ffffffff811155c4>] ? kmsg_dump+0x34/0x220
[  153.672286]  [<ffffffff81021a70>] oops_end+0xa0/0xe0
[  153.672286]  [<ffffffff817ecd8a>] no_context+0x2f4/0x371
[  153.672286]  [<ffffffff817ece85>] __bad_area_nosemaphore+0x7e/0x1d7
[  153.672286]  [<ffffffff817ecff1>] bad_area_nosemaphore+0x13/0x15
[  153.672286]  [<ffffffff81069852>] __do_page_fault+0xc2/0x5f0
[  153.672286]  [<ffffffff810f9b86>] ? up+0x16/0x50
[  153.672286]  [<ffffffff810fb3bf>] ? __lock_is_held+0x5f/0x90
[  153.672286]  [<ffffffff81069e18>] trace_do_page_fault+0x68/0x400
[  153.672286]  [<ffffffff81064e2a>] do_async_page_fault+0x1a/0x80
[  153.672286]  [<ffffffff817fefe8>] async_page_fault+0x28/0x30
[  153.672286]  [<ffffffffa0298520>] ? kdbus_conn_kmsg_send+0xb40/0xc30 [kdbus]
[  153.672286]  [<ffffffffa0297ce5>] ? kdbus_conn_kmsg_send+0x305/0xc30 [kdbus]
[  153.672286]  [<ffffffffa029bad0>] handle_ep_ioctl+0xcc0/0xef0 [kdbus]
[  153.672286]  [<ffffffff811efb19>] ? shmem_add_seals+0x89/0x8a0
[  153.672286]  [<ffffffff811efb78>] ? shmem_add_seals+0xe8/0x8a0
[  153.672286]  [<ffffffff8126e8d0>] do_vfs_ioctl+0x300/0x520
[  153.672286]  [<ffffffff8111d37e>] ? rcu_read_lock_held+0x6e/0x80
[  153.672286]  [<ffffffff8127ad9e>] ? __fget_light+0xbe/0xe0
[  153.672286]  [<ffffffff8126eb71>] SyS_ioctl+0x81/0xa0
[  153.672286]  [<ffffffff817fcfa9>] system_call_fastpath+0x12/0x17

Signed-off-by: Djalal Harouni <tixxdz@opendz.org>
connection: handle -EINTR correctly

When our sync send call is interrupted, we need to store our saved
sigmask to current->saved_sigmask and call set_restore_sigmask().

Signed-off-by: Daniel Mack <daniel@zonque.org>
connection: kdbus_conn_kmsg_send(): cmd_send cannot be NULL

There's only one caller of kdbus_conn_kmsg_send(), and it ensures
that cmd_send is never NULL. No need to check or allow that.

While at it, shorten the name to 'cmd'.

Signed-off-by: Daniel Mack <daniel@zonque.org>
connection: check for invalid items in kdbus_conn_kmsg_send()

Iterate the items and reject everything but KDBUS_ITEM_SIGMASK.

Signed-off-by: Daniel Mack <daniel@zonque.org>
kdbus.txt: document KDBUS_ITEM_SIGMASK

Signed-off-by: Daniel Mack <daniel@zonque.org>
connection: sync send do not block SIGKILL or SIGSTOP

SIGKILL and SIGSTOP can't be blocked so clear them out before applying
the sigmask.

Signed-off-by: Djalal Harouni <tixxdz@opendz.org>
connection: kdbus_ep_policy_check_own_access() -> kdbus_conn_policy_own_name()

The OWN access check is not a property of an endpoint, but of a
connection. Move the function to connection.c and drop the "endpoint"
argument.

Furthermore, prepare for further renamed and use the common
kdbus_conn_policy() prefix.

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