8386 Commits

Author SHA1 Message Date
Philip Withnall
4c4cf568f0 gvariant: Fix g_variant_byteswap() returning non-normal data sometimes
If `g_variant_byteswap()` was called on a non-normal variant of a type
which doesn’t need byteswapping, it would return a non-normal output.

That contradicts the documentation, which says that the return value is
always in normal form.

Fix the code so it matches the documentation.

Includes a unit test.

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

Helps: #2797
2022-12-13 19:04:15 +00:00
Philip Withnall
5f4485c4ff gvariant-serialiser: Check offset table entry size is minimal
The entries in an offset table (which is used for variable sized arrays
and tuples containing variable sized members) are sized so that they can
address every byte in the overall variant.

The specification requires that for a variant to be in normal form, its
offset table entries must be the minimum width such that they can
address every byte in the variant.

That minimality requirement was not checked in
`g_variant_is_normal_form()`, leading to two different byte arrays being
interpreted as the normal form of a given variant tree. That kind of
confusion could potentially be exploited, and is certainly a bug.

Fix it by adding the necessary checks on offset table entry width, and
unit tests.

Spotted by William Manley.

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

Fixes: #2794
2022-12-13 18:20:16 +00:00
Philip Withnall
f98c60e4ee gvariant: Fix a leak of a GVariantTypeInfo on an error handling path
Signed-off-by: Philip Withnall <pwithnall@endlessos.org>
2022-12-13 18:15:20 +00:00
Philip Withnall
c2dc74e2ec gvariant: Cut allocs of default values for children of non-normal arrays
This improves a slow case in `g_variant_get_normal_form()` where
allocating many identical default values for the children of a
variable-sized array which has a malformed offset table would take a lot
of time.

The fix is to make all child values after the first invalid one be
references to the default value emitted for the first invalid one,
rather than identical new `GVariant`s.

In particular, this fixes a case where an attacker could create an array
of length L of very large tuples of size T each, corrupt the offset table
so they don’t have to specify the array content, and then induce
`g_variant_get_normal_form()` into allocating L×T default values from an
input which is significantly smaller than L×T in length.

A pre-existing workaround for this issue is for code to call
`g_variant_is_normal_form()` before calling
`g_variant_get_normal_form()`, and to skip the latter call if the former
returns false. This commit improves the behaviour in the case that
`g_variant_get_normal_form()` is called anyway.

This fix changes the time to run the `fuzz_variant_binary` test on the
testcase from oss-fuzz#19777 from >60s (before being terminated) with
2.3GB of memory usage and 580k page faults; to 32s, 8.3MB of memory
usage and 1500 page faults (as measured by `time -v`).

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

Fixes: #2540
oss-fuzz#19777
2022-12-13 18:15:20 +00:00
Philip Withnall
168f9b42e5 gvariant: Add internal g_variant_maybe_get_child_value()
This will be used in a following commit.

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

Helps: #2540
2022-12-13 18:15:19 +00:00
Philip Withnall
e6490c84e8 gvariant: Port g_variant_deep_copy() to count its iterations directly
This is equivalent to what `GVariantIter` does, but it means that
`g_variant_deep_copy()` is making its own `g_variant_get_child_value()`
calls.

This will be useful in an upcoming commit, where those child values will
be inspected a little more deeply.

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

Helps: #2121
2022-12-13 18:15:19 +00:00
Philip Withnall
35dee77ed8 gvariant: Clarify the docs for g_variant_get_normal_form()
Document how non-normal parts of the `GVariant` are handled.

Signed-off-by: Philip Withnall <pwithnall@endlessos.org>
2022-12-13 18:15:19 +00:00
Philip Withnall
4c0ddb26bc tests: Disable some random instance tests of GVariants
Building a `GVariant` using entirely random data may result in a
non-normally-formed `GVariant`. It’s always possible to read these
`GVariant`s, but the API might return default values for some or all of
their components.

In particular, this can easily happen when randomly generating the
offset tables for non-fixed-width container types.

If it does happen, bytewise comparison of the parsed `GVariant` with the
original bytes will not always match. So skip those checks.

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

Helps: #2121
2022-12-13 18:15:19 +00:00
Philip Withnall
6fa41d5bf6 tests: Add another test for overlapping offsets in GVariant
Signed-off-by: Philip Withnall <withnall@endlessm.com>

Helps: #2121
2022-12-13 18:15:19 +00:00
Philip Withnall
d1a293c4e2 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
2022-12-13 18:15:19 +00:00
Philip Withnall
7cf6f5b691 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
2022-12-13 18:15:17 +00:00
Philip Withnall
73d0aa81c2 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
2022-12-13 18:14:30 +00:00
Philip Withnall
345cae9c1a 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
2022-12-13 18:14:30 +00:00
William Manley
ade71fb544 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
2022-12-13 18:14:26 +00:00
Philip Withnall
298a537d5f 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
2022-12-13 17:36:33 +00:00
William Manley
446e69f5ed gvariant-serialiser: Factor out functions for dealing with framing offsets
This introduces no functional changes.

Helps: #2121
2022-12-13 17:36:33 +00:00
William Manley
1deacdd4e8 gvariant-core: Consolidate construction of GVariantSerialised
So I only need to change it in one place.

This introduces no functional changes.

Helps: #2121
2022-12-13 17:36:33 +00:00
Marco Trevisan
9d51f98ecb Merge branch '2836-uninit-fds' into 'main'
tests: Fix use of three uninitialised array elements in spawn-singlethread

Closes #2836

See merge request GNOME/glib!3123
2022-12-13 16:24:13 +00:00
Philip Withnall
d5011f91a8 tests: Fix use of three uninitialised array elements in spawn-singlethread
Signed-off-by: Philip Withnall <pwithnall@endlessos.org>

Fixes: #2836
2022-12-13 14:49:23 +00:00
Sebastian Dröge
a79c6af23e glib/gthread-posix: Conditionally use futex and/or futex_time64 syscalls as necessary and use the correct struct timespec definition
On some systems only `futex_time64` exists (e.g. riscv32) while on
others only `futex` exists (old Linux, 64 bit platforms), so it is
necessary to check for both and try calling both at runtime.

Additionally use the correct `struct timespec` definition. There is not
necessarily any relation between the libc's definition and the kernel's.

Specifically, the libc headers might use 64-bit `time_t` while the kernel
headers use 32-bit `__kernel_old_time_t` on certain systems.

To get around this problem we
  a) check if `futex_time64` is available, which only exists on 32-bit
     platforms and always uses 64-bit `time_t`.
  b) otherwise (or if that returns `ENOSYS`), we call the normal `futex`
     syscall with the `struct timespec` used by the kernel, which uses
     `__kernel_long_t` for both its fields. We use that instead of
     `__kernel_old_time_t` because it is equivalent and available in the
     kernel headers for a longer time.
2022-12-13 16:45:57 +02:00
Philip Withnall
3c15df01c8 Merge branch 'wip/3v1n0/desktop-app-info-fail-on-not-existent' into 'main'
gdesktopappinfo: Fail early if trying to launch an invalid executable and always use desktop Path and context $PATH

See merge request GNOME/glib!3042
2022-12-13 13:01:09 +00:00
Michael Catanzaro
d900d0efce Revert "GThread: Don't g_error() if setting the thread scheduler settings fails"
This reverts commit 965061797d74847d2e2d1237bb7a63328608e28c.

We are having trouble tracking down the cause of #2769. When the bug
occurs, we fail to set scheduler settings for the new thread pool
thread. This can have serious consequences and should not be ignored. In
retrospect, making this a critical instead of a fatal error has made it
more difficult to notice, debug, and fix. This operation needs to always
work, so let's crash when it fails.

This does not fix #2769, but will hopefully help.
2022-12-12 12:39:46 -06:00
Michael Catanzaro
4d172a2015 Revert "gthread: Only print scheduler setting warnings once"
This reverts commit c8840ff9a8f8445e81ded935bb6637857089a99f.
2022-12-12 12:39:25 -06:00
Marco Trevisan (Treviño)
7bac92a2bb gutils: Split g_find_program_path() to make it more flexible and testable
Split g_find_program_path() in g_find_program_for_path() that supports
passing path arguments and providing a custom working directory.

Adding tests to cover the cases we were not doing before.
2022-12-12 15:58:13 +01:00
Philip Withnall
34618aea70 Merge branch 'gdbinit' into 'main'
tests: fix assert-msg-test with custom gdbinit

See merge request GNOME/glib!3117
2022-12-12 14:01:47 +00:00
Marc-André Lureau
5f21c8da1c glib/tests: add /gthread/spawn-async-with-invalid-fds
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
2022-12-12 17:41:12 +04:00
Marc-André Lureau
d8448636b4 glib/spawn: check user source_fds doesn't contain private fds
If the user provided source_fds set contains internal fds, this is a
programmer mistake. We can avoid further damage by preventing this
situation.

Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
2022-12-12 11:28:58 +04:00
Marc-André Lureau
13acc3176b tests: fix assert-msg-test with custom gdbinit
As recommended by GDB on Fedora, I have "set debuginfod enabled on" in
my .gdbinit. However, this make assert-msg-test time out.

Let's ignore user gdbinit for the test, as this shouldn't be required
and can easily break the test.

Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
2022-12-12 10:53:47 +04:00
Philip Withnall
fc6db764cb gtestutils: Use backslashes for isolated test dirs on Windows
Using `test_run_name` in the path for the isolated dir tree for a test
is fine on Unix, because the `/` separator from GTest paths is suitable
as a file system separator.

On Windows, however, it doesn‘t work when mixed and concatenated with
paths which use backslashes. In particular, byte-by-byte path
comparisons don’t work. There are likely also issues if running on a
system with non-UTF-8 file system encoding.

Fix that by storing a file system path version of `test_run_name`
separately, and using the correct `G_DIR_SEPARATOR` for the host OS.

Signed-off-by: Philip Withnall <pwithnall@endlessos.org>
2022-12-08 18:03:01 +00:00
Philip Withnall
2ca08bde4f Merge branch 'gwakeup-cleanups' into 'main'
gwakeuptest: Do not rely on alarm() to stop tests on timeout

See merge request GNOME/glib!3108
2022-12-06 13:16:35 +00:00
Marco Trevisan
49a7762ec0 Merge branch 'iochannel-buf-size' into 'main'
giochannel: Clarify assertions in g_io_channel_write_chars()

See merge request GNOME/glib!3079
2022-12-06 11:38:29 +00:00
Marco Trevisan (Treviño)
94b658ab4c gwakeup: Be consistent in reading the same data we wrote
During acknowledge read the same quantity we wrote (and expected by eventfd)
instead of always reading just 16 bytes.
2022-12-06 12:28:11 +01:00
Marco Trevisan (Treviño)
345e5bcf2c gwakeuptest: Do not rely on alarm() to stop tests on timeout
We have meson nowadays, so tests are timing out by default and test timeout
may vary depending on the meson test parameters or test setups.

So don't hardcode it using alarm().
2022-12-06 12:25:22 +01:00
Philip Withnall
a03160adf3 giochannel: Fix incorrect use of a signed gsize when unsigned will do
The value of `wrote_bytes` will never be negative, so there’s no need to
store it in a signed type.

Add a couple of assertions to validate that it never decreases and hence
can never go negative.

Signed-off-by: Philip Withnall <pwithnall@endlessos.org>
2022-12-02 21:35:21 +00:00
Philip Withnall
8b863cfd78 garray: Add assertions to help static analysis
In both these cases, the static analyser (Coverity) was worrying that
the array `data`/`pdata` wasn’t allocated before an element was written
to. That was a false positive: all the necessary conditions are met in
both cases for `g_{ptr_,}array_maybe_expand()` to always allocate the
array.

But it makes things a bit easier for the analyser if we add an assertion
to double-check that.

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

Coverity CID: #1474426, #1489512
2022-11-29 12:16:11 +00:00
Philip Withnall
6c39f08f37 gstrfuncs: Fix regression in C++ types accepted by g_str_equal()
Further to commit bcd364afef984da894045, fix the types accepted by the
`g_str_equal()` macro for C++ too. C++ is more restrictive about
const-correctness.

Add unit tests.

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

Fixes: #2820
2022-11-28 12:58:05 +00:00
Philip Withnall
2ee44ba1d5 Merge branch 'msvc-cxx-use-native-attribute-specifiers' into 'main'
gmacros: Use C++ namespaces attribute specifier sequences for msvc

See merge request GNOME/glib!3086
2022-11-24 18:12:29 +00:00
Marco Trevisan (Treviño)
5ff02aa5f9 gmacros: Use C++ namespaces attribute specifier sequences for msvc
As we do already for GNU compilers, when using C++ we should use
attribute sequences with msvc namespace.

This is not supported by msvc versions earlier than 2019 16.7 according
to [1], and thus we need to check if `_MSC_VER` is at least 1927 [2].

See: https://gitlab.gnome.org/GNOME/glib/-/merge_requests/2895#note_1572952

[1] https://www.codetd.com/en/article/11761480
[2] https://learn.microsoft.com/en-us/cpp/preprocessor/predefined-macros?view=msvc-170&viewFallbackFrom=vs-2019#feedback
2022-11-24 17:47:56 +01:00
Philip Withnall
66499d2199 Revert "build: Don’t define redundant built-in variables in pkgconfig calls"
This reverts commit 7e3e591d430e6576292379a4f2d94d5fc0656c36.

The freedesktop SDK, which is used by gnome-build-meta, only has Meson
0.63. Bumping GLib’s Meson dependency to 0.64 means that, at the moment,
GLib is not buildable in gnome-build-meta and hence can’t be tested in
nightly pipelines against other projects, etc.

That’s bad for testing GLib.

It’s arguably bad that we’re restricted to using an older version of
Meson than shipped by Debian Testing, but that’s a separate discussion
to be had.

Revert the Meson 0.64 dependency until the freedesktop SDK ships Meson ≥
0.64. This also means reverting the simplifications to use of
`gnome.mkenum_simple()`.

See https://gitlab.gnome.org/GNOME/glib/-/merge_requests/3077#note_1601064
2022-11-24 12:10:03 +00:00
Simon McVittie
78d0ac4237 Merge branch 'valgrind-fd-fun' into 'main'
gspawn: Ignore invalid FDs when using safe_fdwalk()

See merge request GNOME/glib!3076
2022-11-23 17:34:05 +00:00
Xavier Claessens
6dd5c5002a Merge branch 'wip/pwithnall/meson-0.64' into 'main'
build: Bump Meson dependency to 0.64.0

See merge request GNOME/glib!3077
2022-11-23 11:48:51 +00:00
Philip Withnall
7e3e591d43 build: Don’t define redundant built-in variables in pkgconfig calls
This is deprecated since Meson 0.62.0, since Meson does this
automatically for us.

This fixes a Meson configure warning.

Signed-off-by: Philip Withnall <pwithnall@endlessos.org>
2022-11-23 10:39:58 +00:00
Marco Trevisan
ad755e7489 Merge branch 'c-cxx-std-versions' into 'main'
Expose C and C++ standard versions and add macros to check them

See merge request GNOME/glib!2895
2022-11-22 21:10:34 +00:00
Marco Trevisan (Treviño)
ff49707501 gmacros: Use G_C_STD_CHECK_VERSION to define C-std dependent items 2022-11-22 17:45:56 +01:00
Philip Withnall
e535f22b08 gmem: Fix introspection annotations for g_clear_pointer() and g_clear_fd()
They were mixing up `(optional)` and `(nullable)`, and didn’t correctly
annotate the arguments as `(inout)` or `(transfer full)`.

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

Fixes: #2793
2022-11-22 16:25:39 +00:00
Marco Trevisan (Treviño)
633561ada2 macros: Add a generic way to get and check the supported C standard
Try to get the value of __STDC_VERSION__ if supported, if not just
fallback to the oldest standard that any compiler should handle.
2022-11-22 17:23:14 +01:00
Marco Trevisan (Treviño)
14ba699508 meson: Compile some tests with multiple C standards
We need to ensure that all the expected macros and utilities are working
with all the supported C standards, so just repeat the tests with all
the ones the compiler supports.
2022-11-22 17:23:14 +01:00
Marco Trevisan (Treviño)
641256ea22 gmacros: Prioritize the usage of [[noreturn]] in C++11
We defined G_NO_RETURN as [[noreturn]] in the C++ case, but only after
trying the __attribute__ syntax, so it was never used in GNUC compatible
compilers.

Give it priority instead when supporting a C++11 compiler and onwards.

As per this we need to adapt the code in the places where it was not
properly used (leading to compilation warnings).
2022-11-22 17:23:13 +01:00
Marco Trevisan (Treviño)
372ab7a964 glib: Use G_CXX_STD_VERSION to check how to behave with C++ compilers 2022-11-22 17:23:12 +01:00
Marco Trevisan
3af38a3a1a Merge branch 'coverity-fixes' into 'main'
gthreadpool: Mark an explicit leak as to be ignored

See merge request GNOME/glib!3081
2022-11-22 16:10:01 +00:00