Commit graph

219 commits

Author SHA1 Message Date
Demi Marie Obenour
1e848fc299 SPA POD parser: fix several integer overflows
This fixes several integer overflow problems in the POD parser, as well
as fixing a returns-twice warning from GCC and integer truncation
problems in SPA_FLAG_CLEAR and SPA_ROUND_DOWN_N.  The integer overflows
can result in a tiny POD being treated as a huge one, causing
out-of-bounds reads.
2022-09-27 10:21:17 +00:00
Demi Marie Obenour
c1920163d5 Fix calls to sendmsg()
Align cmsg buffers properly and use MSG_NOSIGNAL.
2022-09-23 16:33:36 +00:00
Demi Marie Obenour
671a7102ff Fix some badly-behaved macros
Some macros evaluated their arguments more than once when it was not
needed, or were missing parentheses.
2022-09-23 11:39:33 -04:00
Wim Taymans
ba7d410c3c handle realloc error better
Make sure we free the old pointer, clear it and set the array size to 0.
Use reallocarray where possible.
2022-04-27 10:09:06 +02:00
Wim Taymans
cc7aadde8a connection: debug when we close fd
Also handle DUP failure.
2022-04-20 12:13:41 +02:00
Quentin Wenger
22fc9eec35 Cleanup argument names in methods and events interfaces
First method argument is object, first event argument is data.

Closes !963
2022-04-15 10:11:49 +02:00
Wim Taymans
c6a8f8d3ef global: use SPA_ID_INVALID to send serial id
We use the done event to send the object serial to the client. Use an
invalid object id for this and filter it out on the receiver or else
the client might get an unexpected done event.

Fixes #2253
2022-03-31 09:39:49 +02:00
Wim Taymans
c89595d86f protocol: increase some limits 2022-03-02 10:05:27 +01:00
Pauli Virtanen
d71cf24183 global: update generation number also in clients without registry
Not all clients have an existing registry, and the registry generation
number will not be updated for them.  However, we would like to check
for stale globals also elsewhere, eg.  metadata, and it must work also
in this case.

To avoid failing to update client registry generation, on global
addition which the client would see if it had a registry, send done
message for the new global id instead.
2022-02-27 15:04:23 +02:00
Pauli Virtanen
a9accd1668 protocol-native: footer pre_demarshal must come first
Message footer should be handled before attempting to find the object
the main message is sent to / checking permissions, because it is not
aimed at a specific object.  E.g. the registry generation updates should
be handled regardless of whether the main message is valid or not,
because the updates will not be re-sent.

Fixes registry generation updates sometimes going missing.
2022-02-26 17:48:51 +00:00
Pauli Virtanen
d44bf0ffc0 impl-core/protocol-native: use generation counter for global registry
Some client messages have bare ids (as opposed to proxies/resources),
eg. as in pw_registry_bind/destroy.  If the client is processing
messages late, these may refer to an object that was already removed,
and the id may now refers to a differnt objects.  I.e. the following
race condition needs to be resolved:

server               client
Global 1 (gen. 1)
                     Global 1
Global 1 remove
Global 1 (gen. 2)
                     Bind/destroy 1

Where the client would bind/destroy the wrong global, since it did not
yet see the messages for the second one.

To keep track of which object the client means, the server keeps track of
the "generation number" of its global registry, and what generation
the client is at.

Each global remembers at what generation of registry they were
registered. When processing the messages that use bare ids, check the
registry generation of the client, to know whether the message refers to
a stale global that was already removed.

Messages where client sends bare ids to server are:

    pw_registry_bind, pw_registry_destroy, metadata_set_property

In pw_registry_* do the staleness check directly. Also add staleness
check in pw_impl_client_check_permissions, so that also the metadata
case is handled.

The generation numbers are passed around in message footers, but only if
they have changed.  When the generation number changes on server, we
send the updated value to the client in a message footer.  When client
has received an update value, it will send the value back in the footer
of the next message it sends to the server.

Based on: Wim Taymans <wtaymans@redhat.com> "impl-core: check serial number"
2022-02-20 16:59:25 +02:00
Pauli Virtanen
680c33d3eb protocol-native: extend v3 protocol with message footers
Extend version 3 protocol with message footers, which are for passing
around global state data that is not addressed to a specific object.

The extension is backward compatible with previous v3 clients, and won't
e.g. result to error spam in logs.

The footer is a single SPA POD, appended after the main message POD.
Because both the protocol message and the message POD record their
length, it's possible to append trailing data. Earlier clients will
ignore any data trailing the message POD.

The footer POD contains a sequence [Id opcode, Struct {...}]*,
so there is room to extend with new opcodes later as necessary.

There are separate marshal/demarshal routines for messages aimed at
resources and proxies.
2022-02-20 16:59:25 +02:00
Barnabás Pőcze
15e7a61aa7 treewide: only define feature macros when the feature is available
Most feature checks already use #ifdef, and do not care about
the value of the macro. Convert all feature checks to do that,
and simplify the meson build scripts by replacing

  if cond
    cdata.set('X', 1)
  endif

with

  cdata.set('X', cond)
2022-02-04 00:15:59 +01:00
Wim Taymans
ead827d6cb modules: limit the max amount of items in the protocol
For now, put a limit on the amount of items we can send and receive
over the native protocol. A more complex way of allocating and freeing
can be implemented later when we really need to raise the limits.

Fixes #2070
2022-01-28 15:55:44 +01:00
Wim Taymans
36d78c41a0 modules: refactor permissions parsing 2022-01-28 10:51:09 +01:00
Wim Taymans
ef8fa3dc6f modules: refactory param parsing
So that we can add some more checks later.

See #2070
2022-01-28 10:25:30 +01:00
Wim Taymans
71a86877b7 modules: refactor parse_dict
Make a macro from parse_dict and move the n_items parsing and alloca
in it. This should make it easier to check the data.

See #2070
2022-01-28 10:01:12 +01:00
Barnabás Pőcze
1d4551a98d pipewire: module-protocol-native: avoid file descriptor leaks
At the moment, file descriptors may be leaked
due to a malicious/buggy client:

1. If the control messages have been truncated, some file descriptors
   may still have been successfully transferred. Currently, seeing
   the MSG_CTRUNC bit causes `refill_buffer()` to immediately return
   -EPROTO without doing anything with the control messages, which
   may contain file descriptors.

2. When there is no truncation, it is still possible that the current
   batch of file descriptors causes the total file descriptor count
   to go over the maximum number of fds for the given buffer (currently 1024).
   In this case, too, `refill_buffer()` immediately returns -EPROTO
   without closing the file descriptors that can not be saved.

Fix both of these cases by closing all file descriptors in all
remaining cmsgs when one of the mentioned conditions occur.
2022-01-17 18:00:48 +01:00
Jonas Holmberg
15ce86aff1 module-protocol-native: Fix errno check
handle_connection_error() takes errno as argument so check for positive
error codes.
2021-12-16 17:21:24 +01:00
Torkel Niklasson
10755ff115 protocol-native: change some warnings to infos
Errors caused by EPIPE/Connection reset by peer should not cause an
error in the pipewire log, but rather an info.
2021-12-14 14:54:13 +00:00
Quantum
8bab9a18ec Add all 32-bit packed little endian 10-bit RGB colour formats
Given that 10-bit colour is now becoming supported on Wayland, PipeWire
should be able to represent all the possible colour formats in order
for screen capture to work.

This commit adds all possible orderings of 10-bit RGB channels and 2
extra bits used for nothing or alpha in little endian to enum
spa_video_format. Note that Wayland only uses little endian for its
10-bit colour formats, and these are not the same as the big endian
formats in reverse order.
2021-12-07 11:22:04 +00:00
Peter Hutterer
96f8a7597e modules: switch protocol-native to the new logging system
This is more complicated than a normal module because we have two
logging topics: mod.protocol-native and conn.protocol-native for wire
messages. Because the latter use spa_debug (through spa_debug_pod) we
need to #define our way around so those too use the right topics.

Note that this removes the previous "connection" category, it is now
"conn.protocol-native" instead.
2021-09-28 09:35:39 +02:00
Peter Hutterer
99a10c1606 protocol: assert if the loop initialization fails on the connection test
This test needs an environment set up to load everything correctly,
running it directly results in a segfault. Let's make sure we assert on
a NULL loop to make this look slightly more planned.
2021-08-04 15:43:10 +10:00
Peter Hutterer
51a177eb6d tree-wide: replace strstr(a, b) == a with spa_strstartswith()
spa_strstartswith() is more immediately understandable.

Coccinelle spatch file:
	@@
	expression E1, E2;
	@@
	- strstr(E1, E2) != E1
	+ !spa_strstartswith(E1, E2)

	@@
	expression E1, E2;
	@@
	- strstr(E1, E2) == E1
	+ spa_strstartswith(E1, E2)

Applied to the tree except for alsa/acp/compat.h because it looks like
that header is still mostly as-is from PA.
2021-08-02 08:24:21 +00:00
Wim Taymans
8e8cb34dc2 protocol: don't place sockets in $HOME
No server will put them there and no client will look for it there.
2021-07-27 10:57:31 +02:00
Peter Hutterer
49f3d5842e test: replace all spa_assert() macros with spa_assert_se()
These are tests, we never want those to be optimized away.
2021-07-27 10:24:44 +10:00
Barnabás Pőcze
f5d51162c4 treewide: mark things static and const
Mark some structures, arrays static/const at various places.
In some cases this prevents unnecessary initialization
when a function is entered.

All in all, the text segments across all shared
libraries are reduced by about 2 KiB. However,
the total size increases by about 2 KiB as well.
2021-06-30 14:44:08 +02:00
George Kiagiadakis
59407d2f08 includes: update all references to extensions to point to pipewire/extensions
This also brings the advantage that all tools, examples, modules, components
can also be compiled standalone out-of-tree using libpipewire from the system
2021-06-18 17:54:18 +03:00
Wim Taymans
22b5b6b120 connection: make sure we don't overrun the fd array
Do some checks on the fd array and error out when we would overrun.
2021-06-18 15:26:20 +02:00
Wim Taymans
adee3d79b9 connection: handle truncated control data
We can't recover from truncated control data so return a fatal error
that should stop the client. Truncated control data can happen when
there are no more fds available, for example.

See #1305
2021-06-18 15:26:20 +02:00
Peter Hutterer
95a84e797a treewide: replace !strcmp() with spa_streq()
This change is only done in source files for now, header files will be done
separately.
2021-05-18 22:10:27 +10:00
Peter Hutterer
7697ed0757 treewide: replace strcmp() == 0 with spa_streq()
This change is only done in source files for now, header files will be done
separately.
2021-05-18 22:10:27 +10:00
Peter Hutterer
2405f0942b spa/buffer: rename SPA_MEMBER to SPA_PTROFF
SPA_MEMBER is misleading, all we're doing here is pointer+offset and a
type-casting the result. Rename to SPA_PTROFF which is more expressive (and
has the same number of characters so we don't need to re-indent).
2021-05-06 09:39:39 +00:00
Jonas Holmberg
1e6b7b8a83 module-protocol-native: Handle pending connect
Do not return an error immediately if connect() fails with EAGAIN. Check
if it completed successfully with getsockopt() when the socket becomes
writable instead.

This is the way to handle non-blocking connect() by the book but after
testing it seems that the case when connect() fails with EAGAIN is when
the listen backlog is full on the server side and in that case the
server socket is closed. So even though connect() completes successfully
according to getsockopt() the client socket is no longer usable
(on_remote_data() will get both SPA_IO_OUT and SPA_IO_HUP in mask on the
first call after connect() returned EAGAIN).
2021-04-22 17:46:00 +00:00
Barnabás Pőcze
3c01cfe8f8 protocol-native: connection: fix errno check condition
The branch should be taken if errno is neither EAGAIN,
nor EWOULDBLOCK.

Previously,

  if (errno != EAGAIN || errno != EWOULDBLOCK)

would be taken for all values of errno if EAGAIN != EWOULDBLOCK.
(Except for the ones that are filtered out before.)

Fix that by changing `||` to `&&`.
2021-03-04 19:18:19 +00:00
Wim Taymans
7053936ffd don't attempt to connect to empty remote name
Fixes jconvolver startup.

See #744
2021-02-18 10:22:39 +01:00
George Kiagiadakis
0ce6de8fdf src: change license (LGPL -> MIT) on left-over LGPL files
Discussed with and approved by Wim
2021-02-10 11:52:09 +00:00
Wim Taymans
219248d43e connection: guard against NULL 2021-01-12 10:13:02 +01:00
Pauli Virtanen
84ebce147a module-protocol-native: add test for reentering 2021-01-12 10:13:02 +01:00
Pauli Virtanen
23f010541f module-protocol-native: make demarshaling safe vs. reentering
The message structures returned by pw_protocol_native_connection_get_next
point to data that is contained in the buffer of the connection.

The data was invalidated when pw_protocol_native_connection_get_next was
called the next time, which made the connection loop non-reentrant, in
cases where it was re-entered from demarshal callbacks.

Fix this by allocating new buffers when reentering and stashing the old
buffers onto a stack. The returned message structure is also stored on
the stack to make lifetimes to match.
2021-01-12 10:13:02 +01:00
Wim Taymans
461e3db741 tests: fix some leaks
See #497
2020-12-21 20:07:02 +01:00
Wim Taymans
387009e0d6 return ENOENT for unknown resources
Fix some other errors as well, ENOSYS for invalid interface, ENOTSUP
for unimplemented methods.
2020-11-20 12:11:37 +01:00
Wim Taymans
a2549fc638 clean the hook lists
Make sure the hook lists are emptied so that the removed callbacks
are called. The callers should really remove the hook they installed
themselves but this is a last chance to fix things up.
2020-11-06 15:55:33 +01:00
Wim Taymans
b8c7b36d3b protocol-native: do version check on HELLO
Always do the version check on HELLO because the connection could
have been moved from a v3 portal to a v0 client (like chrome)

Fixes #270
2020-11-04 16:13:53 +01:00
Wim Taymans
21bda3102b connection: do version check only once 2020-10-30 12:56:37 +01:00
Wim Taymans
723ebcf438 protocol-native: only send properties when in change_mask
Only send the properties when the change mask includes them.
This reduces the message size.
2020-10-22 10:57:43 +02:00
Wim Taymans
39513fb681 protocol-native: fix error reporting
Don't say that there is no runtime directory set when there is but
the server is not running.
Simplify things by first trying to connect to the user runtime directory
when set and then try the system directory. There is no need to manually
check the existance of the socket, connect wil that that and set the
appropriate error.
2020-08-21 11:15:24 +02:00
Jonas Holmberg
3e7609dcaf protocol: Add system runtime directory for clients
Add /run/pipewire to the list of runtime directories so that clients can
connect to the system service without specifying runtime directory.

If the otherwise chosen directory (e.g. the user home directory) doesn't
contain any socket but /run/pipewire does, the latter will be used.
2020-08-19 17:46:50 +00:00
Emmanuel Gil Peyrot
14fdf07e8f Run codespell on the entire codebase
This tool detects and fixes common English spelling mistakes, with
generally very few mistakes.

Here is the command I used to generate this commit.  There were a few
changes that had to be done manually, and of course adding the ignore
file:
```shell
codespell -I .codespell-ignore -x .codespell-ignore -w
```

I didn’t add it to the CI, but this would be a good place for it.
2020-08-17 17:16:31 +00:00
Wim Taymans
6f2e274b15 protocol: add a few more options for XDG_RUNTIME_DIR
PIPEWIRE_CORE can be used to specify a server name.
PIPEWIRE_REMOTE can be used to specify what server name to
connect to.

Either use the absolute path of the name to create and connect
to a server, or use a relative path. For a relative path, the
server name will be completed by prefixing the following paths
in order:

PIPEWIRE_RUNTIME_DIR environment variable,
XDG_RUNTIME_DIR environment variable,
HOME environment variable,
USERPROFILE environment variable,
home directory as stored in the password database.

Fixes #259
2020-07-31 12:46:58 +02:00