Commit Graph

163 Commits

Author SHA1 Message Date
David Herrmann 03ebf5e89b sd-bus: fix encoding/decoding gvariant root container
The gvariant root container contains a 'variant' at the end, which embeds
the whole message body. This variant *must* contain a structure so we are
compatible to dbus1. Otherwise, it could encode at most 1 type, instead
of a full signature.

Our gvariant message parser already parses the variant-content as a
structure, so we're mostly good. However, it does *not* include the
opening and closing parantheses, nor does it parse them.

This patch fixes the decoder to verify a message contains the
parantheses, and also make the encoder add those parantheses into the
marshaled message.
2015-07-29 16:46:24 +02:00
David Herrmann e53d21d007 sd-bus: fix marshaling of unary type
The unary type has a fixed size of 1 in gvariant. Make sure we properly
encode it as such. Right now, we encode/decode it as empty sequence.
2015-07-29 16:38:14 +02:00
David Herrmann 9a8fb5ae17 sd-bus: drop redundant code
If c->item_size is 0, the next item to parse in a structure is empty.
However, this also implies that the signature must be empty. The latter
case is already handled just fine by enter_struct_or_dict_entry() so
there is no reason to handle the same case in the caller.
2015-07-29 13:03:09 +02:00
David Herrmann a388569f60 sd-bus: don't assert() on valid signatures
Right now sd_bus_message_skip() will abort execution if passed a
signature of the unary type "()". Regardless whether this should be
supported or not, we really must not abort. Drop the incorrect assertion
and add a test-case for this.
2015-07-29 13:03:09 +02:00
David Herrmann 443a559813 sd-bus: fix gvariant structure encoding
In gvariant, all fixed-size objects need to be sized a multiple of their
alignment. If a structure has only fixed-size members, it is required to
be fixed size itself. If you imagine a structure like (ty), you have an
8-byte member followed by an 1-byte member. Hence, the overall inner-size
is 9. The alignment of the object is 8, though. Therefore, the specs
mandates final padding after fixed-size structures, to make sure it's
sized a multiple of its alignment (=> 16).

On the gvariant decoder side, we already account for this in
bus_gvariant_get_size(), as we apply overall padding to the size of the
structure. Therefore, our decoder correctly skips such final padding when
parsing fixed-size structure.

On the gvariant encoder side, however, we don't account for this final
padding. This patch fixes the structure and dict-entry encoders to
properly place such padding at the end of non-uniform fixed-size
structures.

The problem can be easily seen by running:
    $ busctl --user monitor
and
    $ busctl call --user org.freedesktop.systemd1 / org.foobar foobar "(ty)" 777 8

The monitor will fail to parse the message and print an error. With this
patch applied, everything works fine again.

This patch also adds a bunch of test-cases to force non-uniform
structures with non-pre-aligned positions.

Thanks to Jan Alexander Steffens <jan.steffens@gmail.com> for spotting
this and narrowing it down to non-uniform gvariant structures. Fixes #597.
2015-07-16 11:23:34 +02:00
Umut Tezduyar Lindskog dcf6952028 sd-bus: use proper cleanup macro 2015-06-16 11:20:10 +02:00
Lennart Poettering aa0d0ed6b8 sd-bus: remove ucred parameter from bus_message_from_header() since we don't use it anymore 2015-06-10 15:52:52 +02:00
David Herrmann 43ef760376 bus: simplify bus_message_read_strv_extend()
Avoid unbound for(;;) loop and use the established coding-style:
  while ((r = sd_bus_message_read*(...)) > 0) {
  }
  if (r < 0)
          return r;

This is much easier to read and used all over the code base.
2015-04-09 22:59:59 +02:00
Zbigniew Jędrzejewski-Szmek d3839740fa bus: explicitly ignore failure during error handling
CID #1237548.
2015-03-13 23:42:17 -04:00
David Herrmann 15411c0cb1 tree-wide: there is no ENOTSUP on linux
Replace ENOTSUP by EOPNOTSUPP as this is what linux actually uses.
2015-03-13 14:10:39 +01:00
David Herrmann 8b9972db83 bus: drop redundant assignment
This assignment is already done in the parent context, no need to do it
again.
2015-03-11 16:36:41 +01:00
Zbigniew Jędrzejewski-Szmek bcf88fc3f1 bus: fix leak in error path
CID #1271349.
2015-03-07 15:05:50 -05:00
Thomas Hindoe Paaboel Andersen 2eec67acbb remove unused includes
This patch removes includes that are not used. The removals were found with
include-what-you-use which checks if any of the symbols from a header is
in use.
2015-02-23 23:53:42 +01:00
Lennart Poettering c0765ddb74 sd-bus: allow setting a per-connection default value for the "allow-interactive-authentication" message flag
Most of our client tools want to set this bit for all their method
calls, even though it defaults to off in sd-bus, and rightfully so.
Hence, to simplify thing, introduce a per sd_bus-object flag that sets
the default value for all messages created on the connection.
2015-02-18 11:43:18 +01:00
Lennart Poettering b267a6d204 sd-bus: make sure we always serialize the reply cookie as 64bit on dbus2, and 32bit on dbus1 2015-02-16 16:31:29 +01:00
Lennart Poettering 2ac7c17f9d Revert "Revert "sd-bus: change serialization of kdbus messages to qualify in their entirety as gvariant objects""
This reverts commit 954871d8ba.
2015-02-16 15:03:44 +01:00
Lennart Poettering 07a0d22f9e sd-bus: export sd_bus_error_set_errnofv() 2015-02-13 15:49:51 +01:00
Kay Sievers 954871d8ba Revert "sd-bus: change serialization of kdbus messages to qualify in their entirety as gvariant objects"
This breaks booting with kdbus.

This reverts commit b381de4197.
2015-01-30 13:53:45 +01:00
Lennart Poettering b381de4197 sd-bus: change serialization of kdbus messages to qualify in their entirety as gvariant objects
Previously, we only minimally altered the dbus1 framing for kdbus, and
while the header and its fields where compliant Gvariant objects, and so
was the body, the entire message together was not.

As result of discussions with Ryan Lortie this is now changed, so that
the messages in there entirely are fully compliant GVariants. This
follows the framing description described here:

https://wiki.gnome.org/Projects/GLib/GDBus/Version2

Note that this change changes the framing of *all* messages sent via
kdbus, this means you have to reboot your kdbus system, after compiling
and installing this new version.
2015-01-26 21:52:07 +01:00
Lennart Poettering 72e6110485 sd-bus: reuse the KDBUS_CMD_FREE wrapper wherever appropriate 2015-01-26 21:52:07 +01:00
Lennart Poettering 6cd37a5e59 sd-bus: fix handling of double parameters in sd_bus_message_append()
We really need to use va_arg() with the right type here as uint64_t and
double might have the same size, but are passed differently as
arguments.
2015-01-23 01:17:55 +01:00
David Herrmann 05bae4a60c bus: use EUID over UID and fix unix-creds
Whenever a process performs an action on an object, the kernel uses the
EUID of the process to do permission checks and to apply on any newly
created objects. The UID of a process is only used if someone *ELSE* acts
on the process. That is, the UID of a process defines who owns the
process, the EUID defines what privileges are used by this process when
performing an action.

Process limits, on the other hand, are always applied to the real UID, not
the effective UID. This is, because a process has a user object linked,
which always corresponds to its UID. A process never has a user object
linked for its EUID. Thus, accounting (and limits) is always done on the
real UID.

This commit fixes all sd-bus users to use the EUID when performing
privilege checks and alike. Furthermore, it fixes unix-creds to be parsed
as EUID, not UID (as the kernel always takes the EUID on UDS). Anyone
using UID (eg., to do user-accounting) has to fall back to the EUID as UDS
does not transmit the UID.
2015-01-18 13:55:55 +01:00
Lennart Poettering d29ae2914e sd-bus: unify how we set the sender of synthetic messages 2015-01-07 20:25:30 +01:00
David Herrmann d31f486b83 bus: sync with kdbus.git
Changes:
 * bloom parameters are returned in an offset via HELLO
 * FREE now takes items just like any other ioctl
2014-12-11 17:26:03 +01:00
Torstein Husebø ad67ef274e sd-bus: correct spacing near eol in code comments 2014-12-11 15:04:56 +01:00
David Herrmann 055b27f294 bus: zero cmd_free before passing to ioctl
Make sure the whole cmd_free object is zeroed before passing it into the
kernel. This makes valgrind happy and makes us future proof.
2014-12-11 10:45:58 +01:00
David Herrmann becb1a6e9d bus: fix memfd-cache regarding memfd offsets
We must restore part->mmap_begin when poping memfds from the memfd-cache.
We rely on the memfds to be unsealed, so we can be sure that we own the
whole FD. Therefore, simply set part->mmap_begin to the same as
part->data.

This fixes test-bus-kernel-benchmark.
2014-12-11 10:44:41 +01:00
Lennart Poettering 7dcd79c295 sd-bus: properly support passing memfds around with offset/size parameters 2014-12-09 18:07:46 +01:00
David Herrmann 77adde6382 bus: sync with kdbus-git (ABI break)
kdbus-git gained two new features:
 * memfd offsets: This allows to specify a 'start' offset in kdbus_memfd
                  so you can send partial memfd hunks instead of always
                  the full memfd
 * KDBUS_HELLO_UNPRIVILEGED: If passed during HELLO, the client will be
                             treated as unprivileged.
2014-12-09 11:14:55 +01:00
Lennart Poettering 3e49a3a063 sd-bus: add extra assert check 2014-12-04 20:30:46 +01:00
Lennart Poettering fed1e721fd treewide: introduce UID_INVALID (and friends) as macro for (uid_t) -1 2014-11-28 20:55:04 +01:00
Lennart Poettering 198b158f49 sd-bus: add support for matches against arrays of strings in messages 2014-11-28 20:29:44 +01:00
Lennart Poettering d9fba53316 sd-bus: if a NULL signatures is passed to sd_bus_message_skip(), make it skip a single element of any type 2014-11-28 20:29:44 +01:00
Lennart Poettering 7fa934b0d3 sd-bus: be stricter with mismatches between dbus1 and kdbus message headers 2014-11-27 22:02:12 +01:00
Lennart Poettering 64e96a194d bus-proxy: check passed parameter signature of all driver method calls 2014-11-27 21:58:07 +01:00
Lennart Poettering dc18cefdc3 sd-bus: don't blindly take incomplete ucred bits from AF_UNIX when constructing message 2014-11-25 14:28:34 +01:00
Lennart Poettering 8022212b3b sd-bus: add sd_bus_message_is_empty() for checking whether a message carries any body 2014-11-14 13:18:51 +01:00
Daniel Mack 022fb8558e sd-bus: add sd_bus_message.verify_destination_id and .destination_ptr
kdbus learned to accept both a numerical destination ID as well as a
well-known-name. In that case, kdbus makes sure that the numerical ID is in
fact the owner of the provided name and fails otherwise.

This allows for race-free assertion of a bus name owner while sending a
message, which is a requirement for bus-proxyd.

Add two new fields to sd_bus_message, and set the numerical ID to
verify_destination_id if bus_message_setup_kmsg() is called for a
message with a well-known name.

Also, set the destination's name in the kdbus item to .destination_ptr
if it is non-NULL.

Normal users should not touch these fields, and they're not publicy
accessible.
2014-11-11 14:14:01 +01:00
Lennart Poettering a09abc4ae0 memfd: rename memfd.h to memfd-util.h to avoid any confusion with any libc provided headers 2014-10-30 18:32:37 +01:00
Lennart Poettering 73843b5258 memfd: always use our internal utility functions where we have them 2014-10-30 18:28:37 +01:00
Lennart Poettering 53a83f4bc0 sd-bus: add support for new allow-interactive-authorization message header flag 2014-10-13 17:19:31 +02:00
Daniel Mack 53e9dbcdfb sd-bus: fix KDBUS_CMD_FREE user
Fix a user of the KDBUS_CMD_FREE ioctl that was missed in the transition.
2014-10-07 12:02:04 +02:00
David Herrmann 7f3d3ba1a6 bus: align kdbus ioctl parameters to 8byte
All kdbus ioctl arguments must be 8byte aligned. Make sure we use
alloca_align() and _alignas_(8) in all situations where gcc doesn't
guarantee 8-byte alignment.

Note that objects on the stack are always 8byte aligned as we put
_alignas_(8) into the structure definition in kdbus.h.
2014-09-22 14:27:02 +02:00
Thomas Hindoe Paaboel Andersen b49ffb29ed sd-bus: sd_bus_message_get_errno should only return positive errno
sd_bus_message_get_errno can currently return either a number of
different poitive errno values (from bus-error-mapping), or a negative
EINVAL if passed null as parameter.

The check for null parameter was introduced in 40ca29a137
at the same as the function was renamed from bus_message_to_errno and
made public API. Before becoming public the function used to return
only negative values.

It is weird to have a function return both positive and negative errno
and it generally looks like a mistake. The function is guarded by the
--enable-kdbus flags so I wonder if we still have time to fix it up?
It does not have any documentation yet. However, except for a few details
it is just a convenient way to call sd_bus_error_get_errno which is documented
to return only positive errno.

This patch makes it return only positive errno and fixes up the two
calls to the function that tried to cope with both positive and negative
values.
2014-09-16 21:31:15 +02:00
Philippe De Swert fd989a0bc9 bus: avoid using m->kdbus after freeing it
m->kdbus could be freed before it is released. Changing the
order fixes the issue.

Found with Coverity. Fixes: CID#1237798
2014-09-11 17:09:50 +02:00
Hristo Venev f2322f0b64 sd-bus: don't include internal header memfd.h in public header sd-bus.h
https://bugs.freedesktop.org/show_bug.cgi?id=83097
2014-08-26 20:40:35 +02:00
David Herrmann 1ac36c67dd bus: map sealed memfds as MAP_PRIVATE
Mapping files as MAP_SHARED is handled by the kernel as 'writable'
mapping. Always! Even with PROT_READ. Reason for that is,
mprotect(PROT_WRITE) could change the mapping underneath and currently
there is no kernel infrastructure to add protection there. This might
change in the future, but until then, map sealed files as MAP_PRIVATE so
we don't get EPERM.
2014-08-18 23:56:36 +02:00
Daniel Mack fac9c0d508 memfd: internalize functions, drop sd_memfd type
Remove the sd_ prefix from internal functions and get rid of the sd_memfd
type. As a memfd is now just a native file descriptor, we can get rid of our
own wrapper type, and also use close() and dup() on them directly.
2014-08-18 13:32:08 +02:00
Daniel Mack a6082d778e kdbus: switch over to generic memfd implementation (ABI+API break) 2014-08-17 21:47:00 +02:00
Lennart Poettering b5eca3a205 bus: drop bus/message GC logic
When a caller drops all references to a bus and its messages while the
messages where still queue, this causes the bus to reference the
messages, and the messages to reference the bus, without anybody else
keeping a reference, which is something we so far considered a leak, and
tried to fix with a GC logic that would recognize cases like this, and
drop the reference.

This GC logic has been broken sofar, and remained unfixed. This commit
removes it altogther, replacing it with nothing. The rationale is that
simply because all refs to the bus have been dropped its queued messages
should *still* be written to the bus, even if the caller doesn't retain
any reference to either bus nor message. This means it was actually
wrong to attempt to clean up the bus in this case.

The proper way how applications should handle this is by explicitly
invoking sd_bus_close(), when they want busses to go away. This is
probably want they want to do anyway to avoid getting spurious
callbacks after they stopped using a bus.
2014-07-02 17:29:09 +02:00
Lennart Poettering 2a0958d2d0 sd-bus: don't allow creating message objects that are not attached to a bus
It seems unnecessary to support this, and we rather should avoid
allowing this at all, so that people don't program against this
sloppily and we end up remarshalling all the time...
2014-06-06 18:30:01 +02:00
Lennart Poettering 03e334a1c7 util: replace close_nointr_nofail() by a more useful safe_close()
safe_close() automatically becomes a NOP when a negative fd is passed,
and returns -1 unconditionally. This makes it easy to write lines like
this:

        fd = safe_close(fd);

Which will close an fd if it is open, and reset the fd variable
correctly.

By making use of this new scheme we can drop a > 200 lines of code that
was required to test for non-negative fds or to reset the closed fd
variable afterwards.
2014-03-18 19:31:34 +01:00
Miklos Vajna 6f285378aa core, libsystemd, systemd, timedate, udev: spelling fixes 2014-03-17 02:35:35 -04:00
Lennart Poettering 5a4d665ad6 sd-bus: don't choke if somebody sends us a message with a unix fd count of 0
It's kinda pointless to include a unix fd header field in messages if it
carries the value 0, but let's do this anyway...
2014-03-14 21:17:14 +01:00
Lennart Poettering 42c4ebcbd4 sd-bus: don't look for a 64bit value when we only have 32bit value on reply cookie hash table access
This broke hashtable lookups for the message cookies on s390x, which is
a 64bit BE machine where accessing 32bit values as 64bit and vice versa
will explode.

Also, while we are at it, be a bit more careful when dealing with the
64bit cookies we expose and the 32bit serial numbers dbus uses in its
payload.

Problem identified by Fridrich Strba.
2014-03-13 20:33:22 +01:00
Lennart Poettering 151b9b9662 api: in constructor function calls, always put the returned object pointer first (or second)
Previously the returned object of constructor functions where sometimes
returned as last, sometimes as first and sometimes as second parameter.
Let's clean this up a bit. Here are the new rules:

1. The object the new object is derived from is put first, if there is any

2. The object we are creating will be returned in the next arguments

3. This is followed by any additional arguments

Rationale:

For functions that operate on an object we always put that object first.
Constructors should probably not be too different in this regard. Also,
if the additional parameters might want to use varargs which suggests to
put them last.

Note that this new scheme only applies to constructor functions, not to
all other functions. We do give a lot of freedom for those.

Note that this commit only changes the order of the new functions we
added, for old ones we accept the wrong order and leave it like that.
2014-02-20 00:03:10 +01:00
Lennart Poettering 39883f622f make gcc shut up
If -flto is used then gcc will generate a lot more warnings than before,
among them a number of use-without-initialization warnings. Most of them
without are false positives, but let's make them go away, because it
doesn't really matter.
2014-02-19 17:53:50 +01:00
Greg KH 29804cc1e0 use memzero(foo, length); for all memset(foo, 0, length); calls
In trying to track down a stupid linker bug, I noticed a bunch of
memset() calls that should be using memzero() to make it more "obvious"
that the options are correct (i.e. 0 is not the length, but the data to
set).  So fix up all current calls to memset(foo, 0, length) to
memzero(foo, length).
2014-01-31 11:55:01 +01:00
Lennart Poettering eee9ec0e28 bus: change API to expose "inverted" no_reply and no_auto_start message flags non-inverted 2014-01-22 20:45:05 +01:00
Lennart Poettering ca7b42c816 bus: expose priority field of messages, in preparation for prioq support 2014-01-22 20:01:23 +01:00
Lennart Poettering 1fedcf59e0 bus: rename sd_bus_get_realtime_timestamp() to sd_bus_get_realtime_usec()
This brings the calls into similar style as the respective functions in
libsystemd-journal, and also is a bi shorter and more descriptive since
it clarifies the time unit used.
2014-01-22 11:25:39 +01:00
Lennart Poettering 6a0e376c1c bus: add API for querying the kdbus message sequence number 2014-01-22 11:19:26 +01:00
Tom Gundersen 607553f930 libsystemd: split up into subdirs
We still only produce on .so, but let's keep the sources separate to make things a bit
less messy.
2014-01-21 14:41:35 +01:00
Renamed from src/libsystemd/bus-message.c (Browse further)