Commit Graph

21088 Commits

Author SHA1 Message Date
Thomas Haller
1181f88ef1 wifi/iwd: various minor cleanups in nm-iwd-manager.c
- prefer "gsize" instead of "size_t".
2018-09-05 15:24:04 +02:00
Thomas Haller
ccf36ff4ce wifi/iwd: use NMHashState (siphash24) for hashing
We shall use nm_hash_*() functions everywhere where
we need a hash for a dictionary.
2018-09-05 15:24:04 +02:00
Thomas Haller
be875fe382 wifi/iwd: in manager's interface_added() ensure known-network ID is not wrongly destroyed
Calling g_hash_table_insert() with a key which is already hashed
will destroy the *new* key. Since @id is used below, that would
be use after free.

Fixes: d635caf940551f8f5b52683b8379a1f81c58f8fc
2018-09-05 15:24:04 +02:00
Andrew Zaborowski
2c8161868e wifi/iwd: Create connections for IWD-side known networks
IWD's mechanism for connecting to EAP networks requires a network config
file to be present in IWD's storage.  NM and its clients however won't
allow a connection to be attempted until a valid NMConnection is created
on the NM side for the network.  To avoid duplicating the settings from
the IWD-side profiles in NM, automatically create NMSettingConnections
for EAP networks preconfigured on the IWD side, unless a matching
connection already exists.  These connections will use the "external"
EAP method to mean their EAP settings can't be modified through NM, also
they won't be valid for devices configured to use the wpa_supplicant
backend unfortunately.

Those nm-generated connections can be modified by NM users (makes sense
for settings not related to the wifi authentication) in which case they
get saved as normal profiles and will not be recreated as nm-generated
connections on the next run.

I want to additionally handle deleting connections from NM clients so
that they're also forgotten by IWD, in a later patch.
2018-09-05 15:24:04 +02:00
Andrew Zaborowski
977d298c5f libnm-core: 8021x: Allow a new eap value "external"
To allow connections that mirror IWD's configured WPA-Enterprise
networks to be seen as valid by NM, add a new value for the eap key in
802-1x settings.  802-1x.eap stores EAP method names.  In the IWD
connections we don't know what EAP method is configured and we don't
have any of the other 802-1x properties that would be required for the
settings to verify.

These connections can't be activated on devices managed by wpa_supplicant.
2018-09-05 15:24:04 +02:00
Andrew Zaborowski
43ea446a50 wifi: Move get_connection_iwd_security to nm-wifi-utils.c
Make this function public.  I'm not sure if at this point it makes
much sense to add a new file for iwd-specific utilities.
While there add a way for the function to return error if security
type can't be mapped to an IWD-supported security type.
2018-09-05 15:24:04 +02:00
Andrew Zaborowski
142d83b019 wifi/iwd: Track known networks using interface-added/-removed signals
The known networks hash table is indexed by the (ssid, security) tuple
for fast lookups both on DBus signals related to an IWD known network
and local NMConnection signals such as on removal.
2018-09-05 15:24:04 +02:00
Andrew Zaborowski
78303e1ab8 wifi/iwd: Convert manager.known_networks to a GHashTable 2018-09-05 15:24:04 +02:00
Andrew Zaborowski
2f941c0790 wifi/iwd: Drop nm_iwd_manager_network_connected
There's no need anymore for NMIwdManager to know when a network has been
connected to, InterfaceAdded signals are now emitted when a network is
saved as a Known Network.
2018-09-05 15:24:04 +02:00
Andrew Zaborowski
eec61a8e81 wifi/iwd: Drop usage of the KnownNetworks IWD API
Before 0.5 IWD has changed the known networks API to expose separate
objects for each known network and dropped the KnownNetworks
manager-like interface so stop using that interface.  Following
patches will add tracking of the known networks through
ObjectManager.
2018-09-05 15:24:04 +02:00
Andrew Zaborowski
f2be625a07 wifi/iwd: Check g_dbus_proxy_get_cached_property return values
Instead of passing the return values to g_variant_get_string or
g_variant_boolean and then checking the return value of that call,
add wrappers that first check's whether the variant is non-NULL
and of the right type.
g_variant_get_string doesn't allow a NULL parameter and will also never
return NULL according to the docs.

For the State property we assume a state "unknown" and emit a warning
if the property can't be read, "unknown" is also a string in IWD itself
which would be returned if something went really wrong.  In any case
this shouldn't happen.

[thaller@redhat.com: fix missing initialization of nm_auto() variable
  interfaces.]
2018-09-05 15:24:04 +02:00
Beniamino Galvani
5ef8284a01 core: add test for nm_wildcard_match_check()
https://github.com/NetworkManager/NetworkManager/pull/181
2018-09-05 15:12:39 +02:00
Thomas Haller
ac6d62cd2d keyfile: fix crash wrongly treating NMSettingsConnection as NMConnection
Fixes: d17d26887c
2018-09-05 15:01:28 +02:00
Michael Biebl
e4c7a60854 ifupdown: properly handle special "none" keyword for bridge_ports
If this option is set, we should not add a device named "none" but
simply don't add any devices at all.

https://manpages.debian.org/testing/bridge-utils/bridge-utils-interfaces.5.en.html

https://gitlab.freedesktop.org/NetworkManager/NetworkManager/merge_requests/11
2018-09-05 09:14:52 +02:00
Thomas Haller
c25d54d0fd contrib/rpm: fix invalid %if condition for building on RHEL
Fixes: 5ef81dc0fb
2018-09-04 09:54:42 +02:00
Thomas Haller
4b5680898f crypto: merge branch 'th/crypto-secrets'
https://github.com/NetworkManager/NetworkManager/pull/191
2018-09-04 07:39:52 +02:00
Thomas Haller
e3ac45c026 ifcfg-rh: don't use 802-1x certifcate setter functions
The certificate setter function like nm_setting_802_1x_set_ca_cert()
actually load the file from disk, and validate whether it is a valid
certificate. That is very wrong to do.

For one, the certificates are external files, which are not embedded
into the NMConnection. That means, strongly validating the files while
loading the ifcfg files, is wrong because:
 - if validation fails, loading the file fails in its entirety with
   a warning in the log. That is not helpful to the user, who now
   can no longer use nmcli to fix the path of the certificate (because
   the profile failed to load in the first place).
 - even if the certificate is valid at load-time, there is no guarantee
   that it is valid later on, when we actually try to use the file. What
   good does such a validation do? nm_setting_802_1x_set_ca_cert() might
   make sense during nmcli_connection_modify(). At the moment when we
   create or update the profile, we do want to validate the input and
   be helpful to the user. Validating the file later on, when reloading
   the profile from disk seems undesirable.
 - note how keyfile also does not perform such validations (for good
   reasons, I presume).

Also, there is so much wrong with how ifcfg reader handles EAP files.
There is a lot of duplication, and trying to be too smart. I find it
wrong how the "eap_readers" are nested. E.g. both eap_peap_reader() and
"tls" method call to eap_tls_reader(), making it look like that
NMSetting8021x can handle multiple EAP profiles separately. But it cannot. The
802-1x profile is a flat set of properties like ca-cert and others. All
EAP methods share these properties, so having this complex parsing
is not only complicated, but also wrong. The reader should simply parse
the shell variables, and let NMSetting8021x::verify() handle validation
of the settings. Anyway, the patch does not address that.

Also, the setting of the likes of NM_SETTING_802_1X_CLIENT_CERT_PASSWORD was
awkwardly only done when
     privkey_format != NM_SETTING_802_1X_CK_FORMAT_PKCS12
  && scheme == NM_SETTING_802_1X_CK_SCHEME_PKCS11
It is too smart. Just read it from file, if it contains invalid data, let
verify() reject it. That is only partly addressed.

Also note, how writer never actually writes the likes of
IEEE_8021X_CLIENT_CERT_PASSWORD. That is another bug and not fixed
either.
2018-09-04 07:38:30 +02:00
Thomas Haller
6b763af1b7 ifcfg-rh: rework parsing secrets
- rename secret related functions to have a "_secret" prefix.
  Also, rename read_8021x_password() because it's not only useful
  for 802-1x.

- In particular, this patch adds _secret_read_ifcfg() helper (formerly
  read_8021x_password()), which is smart enough to obtain secrets from
  the keys ifcfg file. We have other places where we don't get this
  right.

- on a minor note, the patch also makes an effort to clear passwords
  and certifcate data from memory. Yes, there are countless places
  where we don't do that, but in this case, it's done and is as simple
  as replacing gs_free with nm_auto_free_secret, etc.
2018-09-04 07:38:30 +02:00
Thomas Haller
4b6aa207b2 ifcfg-rh/trivial: rename variable for ifcfg keys file
The term "keys" is used ambigiously. Rename occurances which reference
the "keys" ifcfg-rh file.

While at it, rename the file "parsed" to "main_ifcfg". It follows the
same pattern as the "keys_ifcfg" name.
2018-09-04 07:38:30 +02:00
Thomas Haller
a9406ca4a7 libnm-core: expose _nm_utils_str2bin_full() as internal API
We only exposed wrappers around this function, but all of them have
different behavior, and none exposes all possible features. For example,
nm_utils_hexstr2bin() strips leading "0x", but it does not clear
the data on failure (nm_explicit_bzero()). Instead of adding more
wrappers, expose the underlying implementation, so that callers may
use the function the way they want it.
2018-09-04 07:38:30 +02:00
Thomas Haller
b8ea61d26e libnm/keyfile: clear memory when reading certificates from keyfile
Of course, there are countless places where we get it wrong to clear
the memory. In particular, glib's GKeyFile implementation does
not care about that.

Anyway, the keyfile do contain private sensitive data. Adjust
a few places to zero out such data from memory.
2018-09-04 07:38:30 +02:00
Thomas Haller
9e3ebfdebb libnm/802-1x: refactor getting private-key format
Move similar code to a helper function/macro.
2018-09-04 07:38:30 +02:00
Thomas Haller
068d316822 libnm/802-1x: refactor setting certificate from path
NMSetting8021x has various utility functions to set
the certificate:
  - nm_setting_802_1x_set_ca_cert()
  - nm_setting_802_1x_set_client_cert()
  - nm_setting_802_1x_set_private_key()
  - nm_setting_802_1x_set_phase2_ca_cert()
  - nm_setting_802_1x_set_phase2_client_cert()
  - nm_setting_802_1x_set_phase2_private_key()

They support:

 - accepting a plain PKCS11 URI, with scheme set to
   NM_SETTING_802_1X_CK_SCHEME_PKCS11.
 - accepting a filename, with scheme set to
   NM_SETTING_802_1X_CK_SCHEME_BLOB or
   NM_SETTING_802_1X_CK_SCHEME_PATH.

In the latter case, the function tries to load the file and verify it.
In case of the private-key setters, this also involves accepting a
password. Depending on whether the scheme is BLOB or PATH, the function
will either set the certificate to a PATH blob, or take the blob that
was read from file.

The functions seem misdesigned to me, because their behavior is
rather obscure. E.g. they behave fundamentally different, depending
on whether scheme is PKCS11 or BLOB/PATH.

Anyway, improve them:

- refactor the common code into a function _cert_impl_set(). Previously,
  their non-trivial implementations were copy+pasted several times,
  now they all use the same implementation.
- if the function is going to fail, don't touch the setting. Previously,
  the functions would first clear the certificate before trying to
  validate the input. It's more logical, that if a functions is going
  to fail to check for failure first and don't modify the settings.
- not every blob can be represented. For example, if we have a blob
  which starts with "file://", then there is no way to set it, simply
  because we don't support a prefix for blobs (like "data:;base64,").
  This means, if we try to set the certificate to a particular binary,
  we must check that the binary is interpreted with the expected scheme.
  Add this check.
2018-09-04 07:38:30 +02:00
Thomas Haller
f33dec3067 libnm/802-1x: cleanup NMSetting8021x:verify() 2018-09-04 07:38:30 +02:00
Thomas Haller
5ab6875d4e libnm/802-1x: don't verify certificates in GObject property setter
First of all, g_warning() is not a suitable error handling. In
particular, note how this code is reached when obtaining a setting
from D-Bus, that is, the user is not at fault.

The proper way to handle this, is allowing the setter to set the invalid
value. Only later, during verify() we will fail. This way,
NetworkManager can extend the format and older libnm clients don't
break. This is how forward-compatibility (with older libnm vs. newer
daemon) is supposed to work.
2018-09-04 07:38:30 +02:00
Thomas Haller
53ca365407 libnm/802-1x: refactor certificate handling in settings
- all this code duplication. Add functions and macros to simplify
  the implementation of certificate properties.

Overall, pretty trival. Replace code with a macro.
2018-09-04 07:38:30 +02:00
Thomas Haller
6d6016f83d libnm/802-1x: call g_bytes_unref() directly without checking for NULL
Since its inception, g_bytes_unref() is fine with NULL argument.
2018-09-04 07:38:30 +02:00
Thomas Haller
d0a26c6713 libnm/802-1x/trivial: reorder code 2018-09-04 07:38:30 +02:00
Thomas Haller
1cd1bf7d2f libnm/802-1x: refactor GObject properties of NMSetting8021x 2018-09-04 07:38:30 +02:00
Thomas Haller
fa4f27372c libnm/crypto: mark nm_crypto_make_des_aes_key() as test-only function 2018-09-04 07:38:30 +02:00
Thomas Haller
116ee7a4bf libnm/crypto: clean crypto implementations for gnutls/nss
- refactor to use cleanup attribute and return-early

- reorder some code
2018-09-04 07:38:30 +02:00
Thomas Haller
08c80dd2e3 libnm/crypto: rework endianness detection for crypto_verify_pkcs12()
At other places, we already use __BYTE_ORDER define to detect endianness.
We don't need multiple mechanisms.

Also note that meson did not do the correct thing as AC_C_BIGENDIAN,
so meson + nss + big-endian was possibly broken.
2018-09-04 07:38:30 +02:00
Thomas Haller
858d5c3e91 libnm/crypto: refactor to use enum for supported ciphers
We need to (and already did) define our own identifier for ciphers,
because the gnutls/nss identifiers must be abstracted.

Don't use a string for that. The number of supported ciphers
is not generic but fixed and known at compiler time. An enum
is better suited.
2018-09-04 07:38:30 +02:00
Thomas Haller
1f550790bb libnm/crypto: remove unused argument key_type for decrypt functions 2018-09-04 07:38:30 +02:00
Thomas Haller
10724b38a8 libnm/crypto: don't initialize buffer for nm_crypto_make_des_aes_key() with zero
@key is directly passed to nm_crypto_md5_hash(), which cannot (by API design)
fail. No need to initialize it.

Also, no need to allocate an additional trailing NUL byte. The key is
binary, every attempt to use it as a string will horribly fail.
2018-09-04 07:38:30 +02:00
Thomas Haller
2456298da0 libnm/crypto: adjust signature of crypto functions
- avoid "const gsize" as type for function arguments.

- consistently use "guint8 *" type for binary data, instead
  of "char *", which indicates a NUL terminated C string.
2018-09-04 07:38:30 +02:00
Thomas Haller
67f36f880e libnm/crypto: rename crypto functions used for testing only
- drop nm_crypto_encrypt(). It's not actually used outside of
  "nm-crypto.c".
- rename internal _nm_crypto_*() functions that are only used
  in tests. It's so much nicer to visually recognize functions
  that are used for testing only.
2018-09-04 07:38:30 +02:00
Thomas Haller
b91e60b1d6 libnm: cleanup conversion from NMCryptoFileFormat to NMSetting8021xCKFormat enum 2018-09-04 07:38:30 +02:00
Thomas Haller
44ceb16195 libnm: fix race in nm-setting-x802-1x's setting private key functions
Do not first load the file during nm_crypto_verify_private_key(),
and later re-load it, in case we are setting a blob. Instead,
ensure we only load the file once.

This fixes a race, and also the very wrong assertion:

    priv->phase2_private_key = nm_crypto_read_file (value, NULL);
    nm_assert (priv->phase2_private_key);

We should never assert that an IO operation succeeds.

Also, we encode blobs, paths, and pkcs11 URIs all inside a binary
field. Unfortunately, there is no defined prefix for blobs (TODO).
That means, if you have a blob that happens to start with "file://"
it cannot be expressed. At least, check that the binary field that
we are setting gets detected as correct scheme type.
2018-09-04 07:38:30 +02:00
Thomas Haller
b6377b8082 libnm: clear private-key passwords in NMSetting8021x
Yes, there are countless other places where we don't get this right
and leave sensitive data in memory. Anyway, fix these places.
2018-09-04 07:38:30 +02:00
Thomas Haller
0fdd42e24c libnm/keyfile: avoid GByteArray to construct path uri in nm_keyfile_detect_unqualified_path_scheme() 2018-09-04 07:38:30 +02:00
Thomas Haller
b0c3af6c84 libnm: avoid intermediate GByteArray in path_to_scheme_value()
It's not that to directly initialize the GBytes without an
intermediate GByteArray.
2018-09-04 07:38:30 +02:00
Thomas Haller
2be0bb8287 libnm/crypto: fix loading certificates from file securely
file_to_secure_bytes() tried to load the file from disk and ensure that
the data will be cleared. It did so poorely, because g_file_get_contents()
cannot be used for that.

Add a helper function nm_crypto_read_file() to get this right.
2018-09-04 07:38:30 +02:00
Thomas Haller
b5abc8a1d5 libnm/crypto: ensure not leaking sensitive information when loading files
g_file_get_contents() may use re-alloc to load the file. Each time
it re-allocated the buffer, it does not bother clearing the loaded
buffer from memory.

Alternatively, g_file_get_contents() may use stat() and only allocate
one buffer. But also in this mode, without realloc(), it does not
clear the buffer if reading the file fails with IO error later.

Use nm_utils_file_get_contents() which does that.

While at it, don't load files larger that 100 MB.
2018-09-04 07:38:30 +02:00
Thomas Haller
c0a1f09a26 libnm/crypto: refactor nmtst_crypto_rsa_key_encrypt() and clear memory
It's only used for testing, so this change is not very relevant.
Anyway, I think our crypto code should succeed in not leaving
key material in memory. Refactor the code to do that, though,
how the pem file gets composed is quite a hack (for tests good
enough though).
2018-09-04 07:38:30 +02:00
Thomas Haller
f961dcb806 libnm/crypto: move and mark nm_utils_rsa_key_encrypt() as test code
nm_utils_rsa_key_encrypt() is internal API which is only uesd for testing.
Move it to nm-crypto.h (where it fits better) and rename it to make the
testing-aspect obvious.
2018-09-04 07:38:30 +02:00
Thomas Haller
9153d9e2ea libnm-core/trivial: rename testing related functions in crypto code
In nm-crypto.c we have functions that are only called from tests.
Maybe these functions should move away from libnm-core to the
test.

Leave it, but at least rename them to make it clear that these
functions are not relevant for libnm's actual usage. For a
reviewer that makes a big difference as crypto functions in libnm
have a significantly higher requirement for quality.

There is nothing new here. We already have other *nmtst* functions
beside our regular code. The concention is, that functions that
are only for testing are named explicitly ("nmtst"), and that they
can only be called by test functions themselves.
2018-09-04 07:38:30 +02:00
Thomas Haller
639e6de6e3 libnm/crypto: refactor crypto test functions to return GBytes
Using GBytes consistently simplifies the code. Also use it
for the test related functions.
2018-09-04 07:38:30 +02:00
Thomas Haller
896a47da53 libnm/crypto: refactor nm_crypto_load_and_verify_certificate() and return GBytes
The GBytes has a suitable cleanup function, which zeros the certificate
from memory.

Also, all callers that require the certificate, actually later converted
it into a GBytes anyway. This way, they can re-used the same instance
(avoiding an additionaly copying of the data), and they will properly
clear the memory when freed.
2018-09-04 07:38:30 +02:00
Thomas Haller
4c996da5bc libnm/crypto: use nm_explicit_bzero() instead of plain memset() 2018-09-04 07:38:30 +02:00