libnm: fix crash validating infiniband profiles for interface-name

A virtual infiniband profile (with p-key>=0) can also contain a
"connection.interface-name". But it is required to match the
f"{parent}.{p-key}" format.

However, such a profile can also set "mac_address" instead of "parent".
In that case, the validation code was crashing.

  nmcli connection add type infiniband \
     infiniband.p-key 6 \
     infiniband.mac-address 52:54:00:86:f4:eb:aa:aa:aa:aa:52:54:00:86:f4:eb:aa:aa:aa:aa \
     connection.interface-name aaaa

The crash was introduced by commit 99d898cf1f ('libnm: rework caching
of virtual-iface-name for infiniband setting'). Previously, it would not
have crashed, because we just called

  g_strdup_printf("%s.%04x", priv->parent, priv->p_key)

with a NULL string. It would still not have validated the connection
and passing NULL as string to printf is wrong. But in practice, it
would have worked mostly fine for users.

Fixes: 99d898cf1f ('libnm: rework caching of virtual-iface-name for infiniband setting')
This commit is contained in:
Thomas Haller 2022-05-12 09:42:16 +02:00
parent b6da925719
commit fd5945b408
No known key found for this signature in database
GPG key ID: 29C2366E4DFC5728

View file

@ -254,17 +254,27 @@ verify(NMSetting *setting, NMConnection *connection, GError **error)
virtual_iface_name =
nm_setting_infiniband_get_virtual_interface_name(NM_SETTING_INFINIBAND(setting));
if (!nm_streq(interface_name, virtual_iface_name)) {
if (!nm_streq0(interface_name, virtual_iface_name)) {
/* We don't support renaming software infiniband devices. Later we might, but
* for now just reject such connections.
**/
g_set_error(error,
NM_CONNECTION_ERROR,
NM_CONNECTION_ERROR_INVALID_PROPERTY,
_("interface name of software infiniband device must be '%s' or unset "
"(instead it is '%s')"),
virtual_iface_name,
interface_name);
if (virtual_iface_name) {
g_set_error(
error,
NM_CONNECTION_ERROR,
NM_CONNECTION_ERROR_INVALID_PROPERTY,
_("interface name of software infiniband device must be '%s' or unset "
"(instead it is '%s')"),
virtual_iface_name,
interface_name);
} else {
g_set_error(error,
NM_CONNECTION_ERROR,
NM_CONNECTION_ERROR_INVALID_PROPERTY,
_("interface name of software infiniband device with MAC address "
"must be unset (instead it is '%s')"),
interface_name);
}
g_prefix_error(error,
"%s.%s: ",
NM_SETTING_CONNECTION_SETTING_NAME,