Commit graph

14273 commits

Author SHA1 Message Date
Beniamino Galvani
a3a282677b core: add nm_ip_config_find_first_address()
Replace nm_ip6_config_find_first_address() with a version generic for
IPv4 and IPv6.

(cherry picked from commit ca31cbbc74)
2021-06-21 10:14:19 +02:00
Beniamino Galvani
786ab294db libnm-platform: add nm_platform_ip_address_match()
Replace nm_platform_ip6_address_match() with a version generic for
IPv4 and IPv6.

(cherry picked from commit 376c7f8315)
2021-06-21 10:14:19 +02:00
Beniamino Galvani
21051dc6d8 policy: prefer IPv4 to determine the hostname
When determining the hostname, it is preferable to evaluate devices in
a predictable order to avoid that the hostname changes between
different boots.

The current order is based first on hostname priority, then on the
presence of a best default route, and then on activation order.

The activation order is not a very strong condition, as it is
basically useless for devices that are autoactivated at boot.

As we already prefer IPv4 over IPv6 within the same connection, also
prefer it when 2 connections have the same priority and the same
default route status, to achieve better predictability.

https://bugzilla.redhat.com/show_bug.cgi?id=1970335
https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/merge_requests/895
(cherry picked from commit 637a45e25b)
2021-06-21 08:57:30 +02:00
Beniamino Galvani
6914f4213f nm-daemon-helper: fix indentation
Ops.

Fixes: 9b802ff7f2 ('nm-daemon-helper: fix build with musl libc')
(cherry picked from commit e3e0afb011)
2021-06-16 18:33:59 +02:00
Beniamino Galvani
1d2f8f983c nm-daemon-helper: fix build with musl libc
NSS is not available in musl libc:

./src/nm-daemon-helper/nm-daemon-helper.c:9:10: fatal error: nss.h: No such file or directory
    9 | #include <nss.h>
      |          ^~~~~~~

(cherry picked from commit 9b802ff7f2)
2021-06-16 18:26:07 +02:00
Beniamino Galvani
098b4abe06 firewall: fix double free
Fixes: 9ebdb967de ('firewall: implement masquerading for shared mode with nftables')
(cherry picked from commit 0cd20f94d0)
2021-06-15 19:45:48 +02:00
Thomas Haller
46533e7da4
firewall: use nm_g_main_context_push_thread_default_if_necessary() in _fw_nft_call_communicate_cb()
There is no need to push (and pop) the same main-context.

(cherry picked from commit 654bb92a69)
2021-06-15 19:13:54 +02:00
Thomas Haller
4badc1f33a
firewall: fix signalling timeout error reason from _fw_nft_call()
During timeout we cancel the (internal) GCancellable. But the overall
error reason is not cancellation by the user, it's timeout. Fix
the error reason.

Fixes: 9ebdb967de ('firewall: implement masquerading for shared mode with nftables')
(cherry picked from commit 897c6a5744)
2021-06-15 19:13:53 +02:00
Thomas Haller
eea912dfb3
libnm: expose nm_ip_address_dup(), nm_ip_route_dup() API in libnm
This fixes commit 21c8a6b20e ('libnm-core, all: merge IPv4 and IPv6
address/route types'), which introduced this API but didn't export it
in the library. In practice this API is thus only usable since 1.32.0.

(cherry picked from commit 05f2a0b024)
2021-06-15 19:13:27 +02:00
Thomas Haller
123b408c32
firewall: change default for firewall-backend to "nftables""
It seems that the nftables backend works well. Let's change the default.
This will also be backported to nm-1-32 branch, for 1.32.0 release.

This reverts commit 0609f1f31c.

(cherry picked from commit 79d9441567)
2021-06-14 13:56:21 +02:00
Thomas Haller
6df0c8e9bb
libnm/tests: avoid coverity warning in test_setting_connection_secondaries_verify()
nm_strvarray_get_strv() returns the input pointer itself, if _secondaries is NULL.
It does so intentionally and correctly to create an artificial empty strv array.

Coverity doesn't like this. Try to workaround the warning:

    Error: ARRAY_VS_SINGLETON (CWE-119): [#def484]
    NetworkManager-1.31.90/src/libnm-core-impl/tests/test-setting.c:4544: address_of: Taking address with "&_secondaries" yields a singleton pointer.
    NetworkManager-1.31.90/src/libnm-core-impl/tests/test-setting.c:4544: identity_transfer: Passing "&_secondaries" as argument 1 to function "nm_strvarray_get_strv", which returns that argument.
    NetworkManager-1.31.90/src/libnm-core-impl/tests/test-setting.c:4544: callee_ptr_arith: Passing "_Generic (nm_strvarray_get_strv(&_secondaries, NULL), char const * const * : (char const * const *)nm_strvarray_get_strv(&_secondaries, NULL), char const ** : (char const * const *)nm_strvarray_get_strv(&_secondaries, NULL), char * const * : (char const * const *)nm_strvarray_get_strv(&_secondaries, NULL), char ** : (char const * const *)nm_strvarray_get_strv(&_secondaries, NULL), void const * : (char const * const *)nm_strvarray_get_strv(&_secondaries, NULL), void * : (char const * const *)nm_strvarray_get_strv(&_secondaries, NULL), char const * const * const : (char const * const *)nm_strvarray_get_strv(&_secondaries, NULL), char const ** const : (char const * const *)nm_strvarray_get_strv(&_secondaries, NULL), char * const * const : (char const * const *)nm_strvarray_get_strv(&_secondaries, NULL), char ** const : (char const * const *)nm_strvarray_get_strv(&_secondaries, NULL), void const * const : (char const * const *)nm_strvarray_get_strv(&_secondaries, NULL), void * const : (char const * const *)nm_strvarray_get_strv(&_secondaries, NULL))" to function "_nm_utils_strv_cmp_n" which uses it as an array. This might corrupt or misinterpret adjacent memory locations.
    # 4542|       G_STMT_END
    # 4543|
    # 4544|->         _assert_secondaries(s_con, (const char *const *) arr->pdata);
    # 4545|
    # 4546|           /* reimplement the normalization that we expect to happen and

(cherry picked from commit 14ec96f262)
2021-06-11 22:44:32 +02:00
Thomas Haller
91f5c5e763
cli: avoid coverity warning in do_connection_down()
Error: USE_AFTER_FREE (CWE-416): [#def729] [important]
    NetworkManager-1.31.90/src/nmcli/connections.c:3288: freed_arg: "connection_cb_info_finish" frees "info".
    NetworkManager-1.31.90/src/nmcli/connections.c:3287: pass_freed_arg: Passing freed pointer "info" as an argument to "g_signal_handlers_disconnect_matched".
    # 3285|
    # 3286|               if (info) {
    # 3287|->                 g_signal_handlers_disconnect_by_func(active, down_active_connection_state_cb, info);
    # 3288|                   connection_cb_info_finish(info, active);
    # 3289|               }

(cherry picked from commit 627503ad86)
2021-06-11 22:44:31 +02:00
Thomas Haller
c47c823c9d
glib-aux/tests: avoid coverity warning in test_nm_g_source_sentinel()
Coverity wrongly think there is a use after free in the test:

    Error: USE_AFTER_FREE (CWE-416): [#def559] [important]
    NetworkManager-1.31.90/src/libnm-glib-aux/tests/test-shared-general.c:1305: alias: Assigning: "s1" = "_s". Now both point to the same storage.
    NetworkManager-1.31.90/src/libnm-glib-aux/tests/test-shared-general.c:1324: freed_arg: "g_source_unref" frees "s1".
    NetworkManager-1.31.90/src/libnm-glib-aux/tests/test-shared-general.c:1330: deref_after_free: Dereferencing freed pointer "s1".
    # 1328|               s2 = nm_g_source_sentinel_get(0);
    # 1329|               g_assert(s2 == s1);
    # 1330|->             g_assert_cmpint(g_atomic_int_get(&s1->ref_count), >=, 1);
    # 1331|           }
    # 1332|       }

Rework the code in the hope to avoid the false warning.

(cherry picked from commit 7825609f1f)
2021-06-11 22:44:31 +02:00
Thomas Haller
025a3a60b4
platform: avoid wrong coverity warning in nmp_utils_sysctl_open_netdir()
The warning is wrong, because we already assert for the string length a few
lines earlier.

  Error: STRING_OVERFLOW (CWE-120): [#def595]
  NetworkManager-1.31.90/src/libnm-platform/nm-platform-utils.c:1896: fixed_size_dest: You might overrun the 16-character fixed-size string "ifname_buf_last_try" by copying "ifname" without checking the length.
  # 1894|           if (nm_streq(ifname, ifname_buf_last_try))
  # 1895|               return -1;
  # 1896|->         strcpy(ifname_buf_last_try, ifname);
  # 1897|
  # 1898|           fd_dir = open(sysdir, O_DIRECTORY | O_CLOEXEC);

(cherry picked from commit c87433ebd2)
2021-06-11 22:44:31 +02:00
Thomas Haller
fd2c614b71
libnm: hide NMSimpleConnection type from public headers
(cherry picked from commit 860b280248)
2021-06-11 22:36:51 +02:00
Thomas Haller
65278461a3
libnm: hide NMSetting types from public headers
When subclassing a GObject type, the class and object structs
must be available and defined in the header.

For libnm, and in particular for NMSetting classes, we don't want
users to subclass NMSetting. It also doesn't work, because libnm
has internal code that is necessary to hook up the NMSetting class.
You cannot define your own type and make it work together with
libnm.

Having the structs in public headers limits what we can do with them.
For example, we could embed the private data directly in the structures
and avoid the additional indirection.

This is an API break, but for something that most likely nobody cares
about. Or better, nobody should care about. API is not what is
accidentally defined in a header, API was the library provides to
meaningfully use. Subclassing these types is not meaningful and was
only accidentally possible so far.

Only hide the structs for now. More cleanup is possible later. We shall
however aim to keep the padding and struct layout to not also break ABI.

(cherry picked from commit e46d484fae)
2021-06-11 22:36:50 +02:00
Thomas Haller
f07c135c42
core: avoid accessing opaque NMSetting type
Next, NMSetting will be hidden from public headers and become an opaque type.
We cannot do typeof(*setting) anymore.

(cherry picked from commit 0d6c35a6d9)
2021-06-11 22:36:50 +02:00
Beniamino Galvani
709b497938 device: remove unused variable
Fixes: 620fbb7894 ('device: use nm_device_resolve_address()')
(cherry picked from commit c89ac8f0c7)
2021-06-11 21:59:18 +02:00
Beniamino Galvani
f468e15248 device: use nm_device_resolve_address()
(cherry picked from commit 620fbb7894)
2021-06-11 21:59:12 +02:00
Beniamino Galvani
b26449a9c7 device: add a nm_device_resolve_address()
The new function resolve an address via DNS, first by using
systemd-resolved (disabling synthesized results) and then by spawning
the daemon helper.

Trying systemd-resolved via D-Bus before spawning the helper is
important to get a correct result. Suppose that resolv.conf points to
the local stub listener at 127.0.0.53; if NM only spawns the helper,
the helper will query the local systemd-resolved which could return a
synthesized result.

Therefore, we first query systemd-resolved with NO_SYNTHESIZE and
then, in case of error, we spawn the helper.

(cherry picked from commit 27eae4043b)
2021-06-11 21:59:11 +02:00
Beniamino Galvani
4f9ff47e2d dns: add function to retrieve the systemd-resolved plugin instance
Now that we have nm_dns_systemd_resolved_resolve_address(), we also
need a way to obtain a reference to the plugin.

(cherry picked from commit 7285bc56a9)
2021-06-11 21:59:11 +02:00
Beniamino Galvani
ffd6c5e3f4 nm-daemon-helper: implement 'resolve-address' operation
Implement a new operation to resolve the hostname from an
address. Note the call to __nss_configure_lookup("hosts", "dns"): this
configures the glibc Name Service Switch (NSS) to only use DNS,
bypassing other modules that might be configured in
/etc/nsswitch.conf. Other modules like 'myhostname' or 'resolve' can
return synthesized names, while we are only interested in real DNS
results.

(cherry picked from commit 648b0848f1)
2021-06-11 21:59:11 +02:00
Beniamino Galvani
00126e57b4 core: add infrastructure for spawning a helper process
(cherry picked from commit 6ac21ba916)
2021-06-11 21:59:11 +02:00
Beniamino Galvani
5e5baa0f05 core,nm-dispatcher: use nm_utils_get_process_exit_status_desc()
(cherry picked from commit 326dde6d53)
2021-06-11 21:59:11 +02:00
Beniamino Galvani
227c5ca305 libnm-glib-aux: add nm_utils_get_process_exit_status_desc()
(cherry picked from commit 517852dccd)
2021-06-11 21:59:11 +02:00
Beniamino Galvani
3c96d4bb59 dns/resolved: fix memory leak
'v_name' must be freed. Pass ownership to the result element.

Fixes: e7b76f6757 ('dns/resolved: add API for ResolveAddress call')
(cherry picked from commit 9e69d921c1)
2021-06-11 21:59:11 +02:00
Thomas Haller
c1b8a03598 shared: add nm_ip_addr_init() helper
(cherry picked from commit 5ccc5e10b9)
2021-06-11 21:59:11 +02:00
Andrew Zaborowski
a93653336b
iwd: Don't add connection.interface-name by default
This setting can't be handled IWD (e.g. during autoactivations performed
by IWD) and although for manual activations NM will probably check it,
there's no reason for the IWD backend to restrict new profiles to one
interface plus when running IWD the udev permanent interface naming is
likely to be broken.

https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/merge_requests/886
(cherry picked from commit 35a1d89a96)
2021-06-10 23:32:33 +02:00
Simon McVittie
a90a7b2d5f
libnm: Don't crash if service tells us a new key management mode
The NetworkManager service sometimes adds new key management modes.
If it does, an older client library (perhaps in a container, or loaded
into a pre-existing process before an upgrade) shouldn't crash when
talking to a newer NetworkManager service over D-Bus.

https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/issues/744

Signed-off-by: Simon McVittie <smcv@collabora.com>

https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/merge_requests/887

Fixes: 4c4f59674a ('2007-10-23  Dan Williams  <dcbw@redhat.com>')

(cherry picked from commit 18c76388f0)
2021-06-10 23:25:50 +02:00
Thomas Haller
a25c577556
bluez: fix leak of private data "conn_data_elems" in NMBluezManager
Found by valgrind.

Fixes: 4154d9618c ('bluetooth: refactor BlueZ handling and let NMBluezManager cache ObjectManager data')
(cherry picked from commit 6813a4fe75)
2021-06-10 16:02:25 +02:00
Thomas Haller
80a8a5d16d
supplicant: fix leaking handle in nm_supplicant_manager_create_interface()
Found by valgrind.

Fixes: b83f07916a ('supplicant: large rework of wpa_supplicant handling')
(cherry picked from commit 01df4a5ad0)
2021-06-10 16:02:24 +02:00
Sibo Dong
1e0252fa8f
bash-completion: localize the prev variable
The prev variable is not localzed in the nmcli Bash completion script,
which calls _init_completion.

Even though prev does not appear in the completion script, it should
still be localized. This variable may otherwise appear in the user's
environment and clobber a user-defined variable of the same name, which
is bad.

Localize the prev variable.

https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/issues/741

https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/merge_requests/882
(cherry picked from commit 5c1181c6f3)
2021-06-08 11:38:45 +02:00
Thomas Haller
8d01bf5d80
wifi/iwd: rework warning about invalid state-dir
- always remember priv->last_state_dir that we received via
  D-Bus. Only later, during get_config_path() we will check
  whether the path is valid.

- remember in priv->warned_state_dir the full path for
  which we warned. We want to print a warning for each
  path once, if the path changes, then we also want a new
  warning. A boolean flag cannot express that.
2021-06-04 13:36:38 +02:00
Andrew Zaborowski
cd7213e27c
iwd: Enforce absolute state dir path, print warnings
Validate the state directory path, that we read from the configuration
or from IWD, to be an absolute path.  Print a warning if the value
cannot be used and is not an empty string.

https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/merge_requests/873
2021-06-04 13:09:54 +02:00
Thomas Haller
96ef5dede9
core: belatedly add [main].firewall-backend config to known options
Otherwise we see a warning:

   <warn>  [1622790097.3601] config: unknown key firewall-backend in section [main] of file /etc/NetworkManager/NetworkManager.conf

Fixes: 1da1ad9c99 ('firewall: make firewall-backend configurable via "NetworkManager.conf"')
2021-06-04 10:27:20 +02:00
Thomas Haller
3699c31eb1
libnm/tests: add test for normalizing "connection.secondaries" 2021-06-04 09:42:38 +02:00
Thomas Haller
890df48d14
libnm: verify and normalize "connection.secondaries"
So far, we didn't verify the secondary connections at all.
But these really are supposed to be UUIDs.

As we now also normalize "connection.uuid" to be in a strict
format, the user might have profiles with non-normalized UUIDs.
In that case, the "connection.uuid" would be normalized, but
"connection.secondaries" no longer matches. We can fix that by
also normalizing "connection.secondaries". OK, this is not a very good
reason, because it's unlikely to affect any users in practice ('though
it's easy to reproduce).

A better reason is that the secondary setting really should be well
defined and verified. As we didn't do that so far, we cannot simply
outright reject invalid settings. What this patch does instead, is
silently changing the profile to only contain valid settings.
That has it's own problems, like that the user setting an invalid
value does not get an error nor the desired(?) outcome.
But of all the bad choices, normalizing seems the most sensible
one.

Note that in practice, most client applications don't rely on setting
arbitrary (invalid) "UUIDs". They simply expect to be able to set valid
UUIDs, which they still are. For example, nm-connection-editor presents
a drop down list of VPN profile, and nmcli also resolves connection IDs
to the UUID. That is, clients already have an intimate understanding of
this setting, and don't blindly set arbitrary values. Hence, this
normalization is unlikely to hit users in practice. But what it gives
is the guarantee that a verified connection only contains valid UUIDs.

Now all UUIDs will be normalized, invalid entries removed, and the list
made unique.
2021-06-04 09:29:25 +02:00
Thomas Haller
3acf62f8be
libnm: use GArray to track "connection.secondaries" property instead of GSList
GSList requires an additional allocation for the container struct for each
element. Also, it does not have O(1) direct access. It's a pretty bad
data structure, especially if the underlying data is in form of a strv
array.

Use a GArray instead and the nm_strvarray_*() helpers.
2021-06-04 09:29:24 +02:00
Thomas Haller
92136135ad
libnm: don't reject empty strings in add/remove API
For example for NM_SETTING_CONNECTION_SECONDARIES, the user can set
the GObject property to a string list that includes empty strings.

The C accessors (add/remove-by-value) should also accept any strings that
are accepted otherwise. Asserting against empty strings is wrong. If the
setting wants to reject empty strings, then it should use verify().
2021-06-04 09:29:24 +02:00
Thomas Haller
46533cd15f
libnm: use nm_strvarray_get_strv_non_empty_dup() in "nm-setting-match.c" 2021-06-04 09:29:24 +02:00
Thomas Haller
75c6c4abf8
libnm: use nm_strvarray_get_idx() in "nm-setting-match.c" 2021-06-04 09:29:23 +02:00
Thomas Haller
6f2ae46b37
all: use nm_uuid_is_normalized() for checking valid UUID for "connection.uuid"
"connection.uuid" gets normalized. When we check for a valid UUID, we expect
it to be normalized.
2021-06-04 09:29:23 +02:00
Thomas Haller
423e83b880
keyfile: reject non-normalized UUIDs in nms_keyfile_nmmeta_check_filename()
Since commit 207cf3d5d4 ('libnm: normalize "connection.uuid"') the
"connection.uuid" is normalized to be a valid UUID and all lower case.

That means, if we have .nmmeta files on disk with a previously valid,
but now invalid UUID, the meta file is no longer going to match.

Reject such file outright as invalid. If we really wanted to preserve
backward compatibility, then we would have to also normalize the
filename when we read it. However, that means, that suddenly we might
have any number of compatible .nmmeta files that normalize to the same
UUID, like the files

  71088c75dec54119ab41be71bc10e736aaaabbbb.nmmeta
  F95D40B4-578A-5E68-8597-39392249442B.nmmeta
  f95d40b4-578a-5e68-8597-39392249442b.nmmeta

Having multiple places for the nmmeta file is complicated to handle.

Also, we often have the connection profile (and the normalized UUID)
first, and then check whether it has a .nmmeta file. If we would support
those unnormalized file names, we would have to visit all file names and
try to normalize it, to find those with a matching UUID.

Non-normalized UUIDs really should not be used and they already are not
working anymore for the .nmmeta file. This commit only outright rejects
them. This is a change in behavior, but the behavior change happened
earlier when we started normalizing "connection.uuid".
2021-06-04 09:29:22 +02:00
Thomas Haller
7e8e6836e0
keyfile: fix comparison in nms_keyfile_nmmeta_read()
"uuid" is returned from nms_keyfile_nmmeta_check_filename(),
and contains "$UUID.nmmeta". We must compare only the first
"uuid_len" bytes.

Fixes: 064544cc07 ('settings: support storing "shadowed-storage" to .nmmeta files')
2021-06-04 09:29:22 +02:00
Thomas Haller
25f4d23e13
glib-aux: change nm_uuid_is_valid_full() to nm_uuid_is_normalized_full()
Most of the time, we care about whether we have a normalized UUID.

nm_uuid_is_valid_full() only exists for a particular case where we want
to use the function in a header, without including "nm-uuid.h". In that
case, we actually also care about normalized UUIDs.
2021-06-04 09:29:22 +02:00
Thomas Haller
6ce7b3ca0f
glib-aux: add nm_uuid_is_valid_normalized() helper 2021-06-04 09:29:21 +02:00
Thomas Haller
8c6be1909f
glib-aux: add nm_strvarray_get_strv_non_empty_dup() helper 2021-06-04 09:29:21 +02:00
Thomas Haller
851267b6e7
glib-aux: add nm_strvarray_find_first() helper 2021-06-04 09:29:21 +02:00
Thomas Haller
b0acbe504f
glib-aux: add nm_strvarray_get_idx() helper 2021-06-04 09:29:21 +02:00
Thomas Haller
a266bc15b2
glib-aux: add nm_strv_has_duplicate() helper 2021-06-04 09:29:21 +02:00