23332 Commits

Author SHA1 Message Date
Philip Withnall
b53b26c559 gvariant: Track checked and ordered offsets independently
The past few commits introduced the concept of known-good offsets in the
offset table (which is used for variable-width arrays and tuples).
Good offsets are ones which are non-overlapping with all the previous
offsets in the table.

If a bad offset is encountered when indexing into the array or tuple,
the cached known-good offset index will not be increased. In this way,
all child variants at and beyond the first bad offset can be returned as
default values rather than dereferencing potentially invalid data.

In this case, there was no information about the fact that the indexes
between the highest known-good index and the requested one had been
checked already. That could lead to a pathological case where an offset
table with an invalid first offset is repeatedly checked in full when
trying to access higher-indexed children.

Avoid that by storing the index of the highest checked offset in the
table, as well as the index of the highest good/ordered offset.

Signed-off-by: Philip Withnall <pwithnall@endlessos.org>

Helps: #2121
2023-03-24 08:55:17 -05:00
Philip Withnall
98d5b84c2c gvariant: Don’t allow child elements of a tuple to overlap each other
This is similar to the earlier commit which prevents child elements of a
variable-sized array from overlapping each other, but this time for
tuples. It is based heavily on ideas by William Manley.

Tuples are slightly different from variable-sized arrays in that they
contain a mixture of fixed and variable sized elements. All but one of
the variable sized elements have an entry in the frame offsets table.
This means that if we were to just check the ordering of the frame
offsets table, the variable sized elements could still overlap
interleaving fixed sized elements, which would be bad.

Therefore we have to check the elements rather than the frame offsets.

The logic of checking the elements up to the index currently being
requested, and caching the result in `ordered_offsets_up_to`, means that
the algorithmic cost implications are the same for this commit as for
variable-sized arrays: an O(N) cost for these checks is amortised out
over N accesses to O(1) per access.

Signed-off-by: Philip Withnall <pwithnall@endlessos.org>

Fixes: #2121
2023-03-24 08:54:44 -05:00
Philip Withnall
a4ddf8ea87 gvariant: Don’t allow child elements of a tuple to overlap each other
This is similar to the earlier commit which prevents child elements of a
variable-sized array from overlapping each other, but this time for
tuples. It is based heavily on ideas by William Manley.

Tuples are slightly different from variable-sized arrays in that they
contain a mixture of fixed and variable sized elements. All but one of
the variable sized elements have an entry in the frame offsets table.
This means that if we were to just check the ordering of the frame
offsets table, the variable sized elements could still overlap
interleaving fixed sized elements, which would be bad.

Therefore we have to check the elements rather than the frame offsets.

The logic of checking the elements up to the index currently being
requested, and caching the result in `ordered_offsets_up_to`, means that
the algorithmic cost implications are the same for this commit as for
variable-sized arrays: an O(N) cost for these checks is amortised out
over N accesses to O(1) per access.

Signed-off-by: Philip Withnall <pwithnall@endlessos.org>

Fixes: #2121
2023-03-24 08:53:54 -05:00
Philip Withnall
44ae51d046 gvariant-serialiser: Rework child size calculation
This reduces a few duplicate calls to `g_variant_type_info_query()` and
explains why they’re needed.

Signed-off-by: Philip Withnall <pwithnall@endlessos.org>

Helps: #2121
2023-03-24 08:53:48 -05:00
Philip Withnall
21fba6a534 gvariant-serialiser: Factor out code to get bounds of a tuple member
This introduces no functional changes.

Signed-off-by: Philip Withnall <pwithnall@endlessos.org>

Helps: #2121
2023-03-24 08:53:39 -05:00
William Manley
96e27afc7a gvariant: Don’t allow child elements to overlap with each other
If different elements of a variable sized array can overlap with each
other then we can cause a `GVariant` to normalise to a much larger type.

This commit changes the behaviour of `GVariant` with non-normal form data. If
an invalid frame offset is found all subsequent elements are given their
default value.

When retrieving an element at index `n` we scan the frame offsets up to index
`n` and if they are not in order we return an element with the default value
for that type.  This guarantees that elements don't overlap with each
other.  We remember the offset we've scanned up to so we don't need to
repeat this work on subsequent accesses.  We skip these checks for trusted
data.

Unfortunately this makes random access of untrusted data O(n) — at least
on first access.  It doesn't affect the algorithmic complexity of accessing
elements in order, such as when using the `GVariantIter` interface.  Also:
the cost of validation will be amortised as the `GVariant` instance is
continued to be used.

I've implemented this with 4 different functions, 1 for each element size,
rather than looping calling `gvs_read_unaligned_le` in the hope that the
compiler will find it easy to optimise and should produce fairly tight
code.

Fixes: #2121
2023-03-24 08:53:32 -05:00
Philip Withnall
fd215233ae gvariant: Zero-initialise various GVariantSerialised objects
The following few commits will add a couple of new fields to
`GVariantSerialised`, and they should be zero-filled by default.

Try and pre-empt that a bit by zero-filling `GVariantSerialised` by
default in a few places.

Signed-off-by: Philip Withnall <pwithnall@endlessos.org>

Helps: #2121
2023-03-24 08:53:26 -05:00
William Manley
d51c16a7c8 gvariant-serialiser: Factor out functions for dealing with framing offsets
This introduces no functional changes.

Helps: #2121
2023-03-24 08:53:21 -05:00
William Manley
b4ae1179cb gvariant-core: Consolidate construction of GVariantSerialised
So I only need to change it in one place.

This introduces no functional changes.

Helps: #2121
2023-03-24 08:53:15 -05:00
Philip Withnall
a879d08e91 gspawn: Report errors with closing file descriptors between fork/exec
If a seccomp policy is set up incorrectly so that it returns `EPERM` for
`close_range()` rather than `ENOSYS` due to it not being recognised, no
error would previously be reported from GLib, but some file descriptors
wouldn’t be closed, and that would cause a hung zombie process. The
zombie process would be waiting for one half of a socket to be closed.

Fix that by correctly propagating errors from `close_range()` back to the
parent process so they can be reported correctly.

Distributions which aren’t yet carrying the Docker fix to correctly
return `ENOSYS` from unrecognised syscalls may want to temporarily carry
an additional patch to fall back to `safe_fdwalk()` if `close_range()`
fails with `EPERM`. This change will not be accepted upstream as `EPERM`
is not the right error for `close_range()` to be returning.

Signed-off-by: Philip Withnall <pwithnall@endlessos.org>

Fixes: #2580
2022-01-27 10:24:38 -06:00
Julian Andres Klode
56e6e247f6 gnetworkmonitornm: Do not re-update cached property
GDBusProxy already takes care of updating the cached property
before emitting the signal, so there is no need to do this
a second time ourselves.
2022-01-27 10:14:19 -06:00
Julian Andres Klode
8e0a5d9879 gnetworkmonitornm: Stop using removed PropertiesChanged signal
Use the org.freedesktop.DBus.Properties interface to listen
to PropertiesChanged signals on /org/freedesktop/NetworkManager.

NetworkManager used to provide its own legacy PropertiesChanged
signal, but that was dropped in
https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/merge_requests/853

This requires NetworkManager >= 1.2 (2016)

Fixes: #2505
Bug-Ubuntu: https://bugs.launchpad.net/bugs/1946196
2022-01-27 10:14:19 -06:00
Jamie Bainbridge
ffc00caa73 gutils: Avoid segfault in g_get_user_database_entry
g_get_user_database_entry() uses variable pwd to store the contents of
the call to getpwnam_r(), then capitalises the first letter of pw_name
with g_ascii_toupper (pw->pw_name[0]).

However, as per the getpwnam manpage, the result of that call "may point
to a static area". When this happens, GLib is trying to edit static
memory which belongs to a shared library, so segfaults.

Instead, copy pw_name off to a temporary variable, set uppercase on
that variable, and use the variable to join into the desired string.
Free the new variable after it is no longer needed.

Signed-off-by: Jamie Bainbridge <jamie.bainbridge@gmail.com>
2022-01-27 10:14:14 -06:00
Bastien Nocera
63d0e9750e gio: Simplify memory monitor tests by using assertEventually() helper
assertEventually is a helper used in a number of projects that use
dbusmock.

See https://github.com/martinpitt/python-dbusmock/issues/82
2022-01-27 10:14:12 -06:00
Bastien Nocera
01514cf724 gio: Remove left-over debug statement from memory monitor portal test 2022-01-27 10:14:12 -06:00
Bastien Nocera
e787f30ccc gio: Add GPowerProfileMonitor tests
Tests both the portal and direct D-Bus variants.
2022-01-27 10:14:12 -06:00
Bastien Nocera
cb7e31d5ca gio: Add portal version of GPowerProfileMonitor 2022-01-27 10:14:12 -06:00
Patrick Griffis
4e12236ea4 Add GPowerProfileMonitor 2022-01-27 10:14:07 -06:00
Bastien Nocera
9064efaf37 gio: Do not block when low-memory-monitor daemon appears 2022-01-27 10:14:07 -06:00
Bastien Nocera
99520dd6e0 gio: g_clear_signal_handler() can handle NULL args 2022-01-27 10:14:07 -06:00
Bastien Nocera
d94ef19f2f tests: Remove unused constant in GMemoryMonitor test 2022-01-27 10:14:07 -06:00
Michael Catanzaro
b4f5632ceb Add test for child_err_report_fd conflation with target fds
This tests for glib#2506.
2022-01-27 10:14:02 -06:00
Michael Catanzaro
585406421f Add tests for GSubprocess fd conflation issues
This tests for #2503. It's fragile, but there is no non-fragile way to
test this. If the test breaks in the future, it will pass without
successfully testing the bug, not fail spuriously, so I think this is
OK.
2022-01-27 10:14:02 -06:00
Michael Catanzaro
3e1d6e9e58 gspawn: add new error message for open() failures
Reporting these as dup2() failures is bogus.
2022-01-27 10:14:02 -06:00
Michael Catanzaro
cbb72948f6 gspawn: Check from errors from safe_dup2() and dupfd_cloexec()
Although unlikely, these functions can fail, e.g. if we run out of file
descriptors. Check for errors to improve robustness. This is especially
important now that I changed our use of dupfd_cloexec() to avoid
returning fds smaller than the largest fd in target_fds. An application
that attempts to remap to the highest-allowed fd value deserves at least
some sort of attempt at error reporting, not silent failure.
2022-01-27 10:14:02 -06:00
Michael Catanzaro
80c105531d gsubprocess: ensure we test fd remapping on the posix_spawn() codepath
We should run test_pass_fd twice, once using gspawn's fork/exec codepath
and once attempting to use its posix_spawn() codepath. There's no
guarantee we'll actually get the posix_spawn() codepath, but it works
for now on Linux.

For good measure, run it a third time with no flags at all.

This causes the test to fail if I separately break the fd remapping
implementation. Without this, we fail to test fd remapping on the
posix_spawn() codepath.
2022-01-27 10:14:02 -06:00
Michael Catanzaro
59da3753d1 gspawn: Implement fd remapping for posix_spawn codepath
This means that GSubprocess will (sometimes) be able to use the
optimized posix_spawn codepath instead of having to fall back to
fork/exec.
2022-01-27 10:14:02 -06:00
Michael Catanzaro
87257f1f46 gspawn: fix fd remapping conflation issue
We currently dup all source fds to avoid possible conflation with the
target fds, but fail to consider that the result of a dup might itself
conflict with one of the target fds. Solve this the easy way by duping
all source_fds to values that are greater than the largest fd in
target_fds.

Fixes #2503
2022-01-27 10:14:02 -06:00
Michael Catanzaro
dc4e25832c gspawn: fix hangs when duping child_err_report_fd
In case child_err_report_fd conflicts with one of the target_fds, the
code here is careful to dup child_err_report_fd in order to avoid
conflating the two. It was a good idea, but evidently was not tested,
because the newly-created fd is not created with CLOEXEC set. This means
it stays open in the child process, causing the parent to hang forever
waiting to read from the other end of the pipe. Oops!

The fix is simple: just set CLOEXEC. This removes our only usage of the
safe_dup() function, so it can be dropped.

Fixes #2506
2022-01-27 10:14:02 -06:00
Michael Catanzaro
1e1002469c gspawn: Improve error message when dup fails
This error message is no longer accurate now that we allow arbitrary fd
remapping.
2022-01-27 10:14:02 -06:00
Michael Catanzaro
ecde1b75d0 gspawn: use close_and_invalidate more 2022-01-27 10:14:02 -06:00
Simon McVittie
2f489d0b0f gversionmacros: Add version macros for GLib 2.70
Signed-off-by: Simon McVittie <smcv@collabora.com>
2022-01-27 10:13:58 -06:00
Benjamin Berg
eea325b4ef gdesktopappinfo: Add SourcePath= to transient systemd units
systemd allows setting a SourcePath= which shows the file that the unit
has been generated from. KDE is starting to set this and it seems like a
good idea, so do the same here.

See https://invent.kde.org/frameworks/kio/-/merge_requests/124
2022-01-27 10:13:52 -06:00
Benjamin Berg
35d24b2fa6 gdesktopappinfo: Handle task completion from spawn function
This allows delaying the return of the task until all dbus calls (in
particular the ones to setup the scope) have finished.

This fixes the behaviour of the previous commit which would not
correctly move the process into the scope if the application exited
right after the task returned.
2022-01-27 10:13:52 -06:00
Benjamin Berg
ee61859c22 gdesktopappinfo: Move launched applications into transient scope
Try to move the spawned executable into its own systemd scope. To avoid
possible race conditions and ensure proper accounting, we delay the
execution of the real command until after the DBus call to systemd has
finished.

From the two approaches we can take here, this is better in the sense
that we have a child that the API consumer can watch. API consumers
should not be doing this, however, gnome-session needs to watch children
during session startup. Until gnome-session is fixed, we will not be
able to change this.

The alternative approach is to delegate launching itself to systemd by
creating a transient .service unit instead. This is cleaner and has e.g.
the advantage that systemd will take care of log redirection and similar
issues.

Note that this patch is incomplete. The DBus call is done in a "fire and
forget" manner, which is fine in most cases, but means that "gio open"
will fail to move the child into the new scope as gio quits before the
DBus call finishes.
2022-01-27 10:13:52 -06:00
Benjamin Berg
6d0c0be031 tests: Iterate mainloop during launch test
When launching an application, we wait for the DBus response from
systemd before executing the binary. Because of this the main loop needs
to be iterated for spawning to completed and the file to be created.

Without this the test will time out if GLib was able to connect to the
session bus.
2022-01-27 10:13:52 -06:00
Goran Vidović
b3023ecc28 Update Croatian translation 2021-10-27 17:15:22 +00:00
Fabio Tomat
a413d98d62 Update Friulian translation 2021-09-25 18:13:16 +00:00
Philip Withnall
d99d7967a2 2.68.4
Signed-off-by: Philip Withnall <pwithnall@endlessos.org>
2.68.4
2021-08-19 16:25:53 +01:00
Emmanuele Bassi
4e13a16029 Merge branch 'backport-2174-newline-glib-2-68' into 'glib-2-68'
Backport !2174 “data-to-c.py: generate new-line at the end of the file” to glib-2-68

See merge request GNOME/glib!2175
2021-08-19 15:21:54 +00:00
Philip Withnall
94ef9408f7 Merge branch 'glib-2-68-backport-g-win32-app-info-fixes' into 'glib-2-68'
Backport GWin32AppInfo fixes to glib-2-68

See merge request GNOME/glib!2220
2021-08-09 13:01:30 +00:00
Jonathan Boeing
ee94434632
gwin32packageparser: Fix read past end of buffer
g_win32_package_parser_enum_packages() reads beyond the end of a buffer
when doing a memcpy.  With app verifier enabled on Windows, it causes
the application to crash on startup.

This change limits the memcpy to the size of the source string.

Fixes: #2454
2021-08-09 14:13:15 +02:00
Руслан Ижбулатов
771a6e557a
GWin32AppInfo: Fix missing initialization
The value should be initialized to NULL before calling
g_win32_registry_key_get_value_w(), to ensure that cleanup
can be done unconditionally afterward.
2021-08-09 14:12:35 +02:00
Руслан Ижбулатов
d0ef6399d4
GWin32AppInfo: re-trigger registry watcher from the callback
To ensure that the watch is properly re-set every time, call
watch_keys() from the watch callback. Previously the watch was only
renewed after a data update was done in a worker thread, which made
no sense, since the update function was implemented in such a way
that it can (and should) be re-triggered on each key change, until
the changes stop coming, and that can only happen if we renew
the registry watcher right away.
2021-08-09 14:12:16 +02:00
Руслан Ижбулатов
22e5e428de
GWin32RegistryKey: ensure reqeueing works correctly
If a key watch is renewed from the key watch callback, it results
in the callback being NULL, since we clear it after we call it.

Rearrange the function to make sure that the changes done by the
callback function are preserved properly.
2021-08-09 14:12:07 +02:00
Руслан Ижбулатов
d6a734a44c
GWin32RegistryKey: Change STATUS_SUCCESS handling
This function can, in fact, return STATUS_SUCCESS. We shouldn't
assert that it doesn't.

For now interpret it just like STATUS_PENDING (i.e. APC will be called),
see how it goes (it isn't documented how the function behaves in this
case, we have to play it by ear).

Note that while we *can* use a better-documented RegNotifyChangeKeyValue() here,
it communicates back to us via event objects, which means that the registry
watcher would have to interact with the main loop directly and insert its
events (plural; one event per key) there. That would make the API more complicated.
Whereas the internal NT function communicates by calling an APC - we're good
as long as something somewhere puts the thread in alertable state.
2021-08-09 14:11:39 +02:00
Simon McVittie
6d9e493264 Merge branch 'backport-2208-string-replace-loop-glib-2-68' into 'glib-2-68'
Backport !2208 “g_string_replace: Don't replace empty string more than once per location” to glib-2-68

See merge request GNOME/glib!2209
2021-08-02 14:38:02 +00:00
Simon McVittie
4ce606878e g_string_replace: Document behaviour of zero-length match pattern
Signed-off-by: Simon McVittie <smcv@collabora.com>
2021-08-02 13:49:19 +01:00
Simon McVittie
79fa8db828 test_string_replace: Exercise zero-length replacements
Previously, these would have done 2**32 replacements, and the first one
would have consumed 6GB of memory in the process. They now match what
Python `str.replace()` does.

Reproduces: https://gitlab.gnome.org/GNOME/glib/-/issues/2452
Signed-off-by: Simon McVittie <smcv@collabora.com>
2021-08-02 13:49:19 +01:00
Simon McVittie
c70eb3f0aa g_string_replace: Don't replace empty string more than once per location
This matches the behaviour of Python `str.replace()`, and avoids carrying
out 2**32 replacements before n wraps around, which is almost certainly
not what we want.

Resolves: https://gitlab.gnome.org/GNOME/glib/-/issues/2452
Signed-off-by: Simon McVittie <smcv@collabora.com>
2021-08-02 13:49:19 +01:00