From 32c26d92e059ea3d0d43747352fa8454081c3ead Mon Sep 17 00:00:00 2001 From: Packit Service Date: Feb 24 2021 15:29:40 +0000 Subject: Apply patch 1017-initrd-parse-rd.net.dhcp.vendor-class-rh1870692.patch patch_name: 1017-initrd-parse-rd.net.dhcp.vendor-class-rh1870692.patch present_in_specfile: true location_in_specfile: 19 --- diff --git a/libnm-core/nm-libnm-core-intern/nm-libnm-core-utils.c b/libnm-core/nm-libnm-core-intern/nm-libnm-core-utils.c index f2c85cc..8be7d91 100644 --- a/libnm-core/nm-libnm-core-intern/nm-libnm-core-utils.c +++ b/libnm-core/nm-libnm-core-intern/nm-libnm-core-utils.c @@ -6,6 +6,8 @@ #include "nm-common-macros.h" +#include "nm-errors.h" + #include /*****************************************************************************/ @@ -257,3 +259,56 @@ NM_UTILS_ENUM2STR_DEFINE (nm_utils_route_type2str, guint8, NM_UTILS_ENUM2STR (RTN_UNREACHABLE, "unreachable"), NM_UTILS_ENUM2STR (RTN_UNSPEC, "unspecified"), ); + +gboolean +nm_utils_validate_dhcp4_vendor_class_id (const char *vci, GError **error) +{ + const char * bin; + gsize unescaped_len; + gs_free char *to_free = NULL; + + g_return_val_if_fail (!error || !(*error), FALSE); + g_return_val_if_fail (vci, FALSE); + + if (vci[0] == '\0') { + g_set_error_literal (error, + NM_CONNECTION_ERROR, + NM_CONNECTION_ERROR_INVALID_PROPERTY, + _ ("property cannot be an empty string")); + g_prefix_error (error, + "%s.%s: ", + NM_SETTING_IP4_CONFIG_SETTING_NAME, + NM_SETTING_IP4_CONFIG_DHCP_VENDOR_CLASS_IDENTIFIER); + return FALSE; + } + + bin = nm_utils_buf_utf8safe_unescape (vci, + NM_UTILS_STR_UTF8_SAFE_FLAG_NONE, + &unescaped_len, + (gpointer *) &to_free); + /* a DHCP option cannot be longer than 255 bytes */ + if (unescaped_len > 255) { + g_set_error_literal (error, + NM_CONNECTION_ERROR, + NM_CONNECTION_ERROR_INVALID_PROPERTY, + _ ("property cannot be longer than 255 bytes")); + g_prefix_error (error, + "%s.%s: ", + NM_SETTING_IP4_CONFIG_SETTING_NAME, + NM_SETTING_IP4_CONFIG_DHCP_VENDOR_CLASS_IDENTIFIER); + return FALSE; + } + if (strlen (bin) != unescaped_len) { + g_set_error_literal (error, + NM_CONNECTION_ERROR, + NM_CONNECTION_ERROR_INVALID_PROPERTY, + _ ("property cannot contain any nul bytes")); + g_prefix_error (error, + "%s.%s: ", + NM_SETTING_IP4_CONFIG_SETTING_NAME, + NM_SETTING_IP4_CONFIG_DHCP_VENDOR_CLASS_IDENTIFIER); + return FALSE; + } + + return TRUE; +} diff --git a/libnm-core/nm-libnm-core-intern/nm-libnm-core-utils.h b/libnm-core/nm-libnm-core-intern/nm-libnm-core-utils.h index bb3fa5f..6c1337d 100644 --- a/libnm-core/nm-libnm-core-intern/nm-libnm-core-utils.h +++ b/libnm-core/nm-libnm-core-intern/nm-libnm-core-utils.h @@ -140,4 +140,6 @@ guint8 nm_utils_route_type_by_name (const char *name); const char *nm_utils_route_type2str (guint8 val, char *buf, gsize len); +gboolean nm_utils_validate_dhcp4_vendor_class_id (const char *vci, GError **error); + #endif /* __NM_LIBNM_SHARED_UTILS_H__ */ diff --git a/libnm-core/nm-setting-ip4-config.c b/libnm-core/nm-setting-ip4-config.c index 0b8dc89..7ffefc2 100644 --- a/libnm-core/nm-setting-ip4-config.c +++ b/libnm-core/nm-setting-ip4-config.c @@ -227,51 +227,9 @@ verify (NMSetting *setting, NMConnection *connection, GError **error) return FALSE; } - if (priv->dhcp_vendor_class_identifier) { - const char * bin; - gsize unescaped_len; - gs_free char *to_free = NULL; - - if (priv->dhcp_vendor_class_identifier[0] == '\0') { - g_set_error_literal (error, - NM_CONNECTION_ERROR, - NM_CONNECTION_ERROR_INVALID_PROPERTY, - _ ("property cannot be an empty string")); - g_prefix_error (error, - "%s.%s: ", - NM_SETTING_IP4_CONFIG_SETTING_NAME, - NM_SETTING_IP4_CONFIG_DHCP_VENDOR_CLASS_IDENTIFIER); - return FALSE; - } - - bin = nm_utils_buf_utf8safe_unescape (priv->dhcp_vendor_class_identifier, - NM_UTILS_STR_UTF8_SAFE_FLAG_NONE, - &unescaped_len, - (gpointer *) &to_free); - /* a DHCP option cannot be longer than 255 bytes */ - if (unescaped_len > 255) { - g_set_error_literal (error, - NM_CONNECTION_ERROR, - NM_CONNECTION_ERROR_INVALID_PROPERTY, - _ ("property cannot be longer than 255 bytes")); - g_prefix_error (error, - "%s.%s: ", - NM_SETTING_IP4_CONFIG_SETTING_NAME, - NM_SETTING_IP4_CONFIG_DHCP_VENDOR_CLASS_IDENTIFIER); - return FALSE; - } - if (strlen (bin) != unescaped_len) { - g_set_error_literal (error, - NM_CONNECTION_ERROR, - NM_CONNECTION_ERROR_INVALID_PROPERTY, - _ ("property cannot contain any nul bytes")); - g_prefix_error (error, - "%s.%s: ", - NM_SETTING_IP4_CONFIG_SETTING_NAME, - NM_SETTING_IP4_CONFIG_DHCP_VENDOR_CLASS_IDENTIFIER); - return FALSE; - } - } + if ( priv->dhcp_vendor_class_identifier + && !nm_utils_validate_dhcp4_vendor_class_id (priv->dhcp_vendor_class_identifier, error)) + return FALSE; /* Failures from here on are NORMALIZABLE_ERROR... */ diff --git a/po/POTFILES.in b/po/POTFILES.in index 25cb5c4..ea2eafa 100644 --- a/po/POTFILES.in +++ b/po/POTFILES.in @@ -59,6 +59,7 @@ libnm-core/nm-dbus-utils.c libnm-core/nm-keyfile/nm-keyfile-utils.c libnm-core/nm-keyfile/nm-keyfile.c libnm-core/nm-libnm-core-aux/nm-libnm-core-aux.c +libnm-core/nm-libnm-core-intern/nm-libnm-core-utils.c libnm-core/nm-setting-6lowpan.c libnm-core/nm-setting-8021x.c libnm-core/nm-setting-adsl.c diff --git a/shared/nm-glib-aux/nm-macros-internal.h b/shared/nm-glib-aux/nm-macros-internal.h index 15bcd7e..57ddee0 100644 --- a/shared/nm-glib-aux/nm-macros-internal.h +++ b/shared/nm-glib-aux/nm-macros-internal.h @@ -216,6 +216,14 @@ NM_AUTO_DEFINE_FCN0 (GError *, gs_local_free_error, g_error_free) #define gs_unref_keyfile nm_auto(gs_local_keyfile_unref) NM_AUTO_DEFINE_FCN0 (GKeyFile *, gs_local_keyfile_unref, g_key_file_unref) +/** + * gs_free_option_context: + * + * Call g_option_context_free() on a variable location when it goes out of scope. + */ +#define gs_free_option_context nm_auto(gs_local_option_context) +NM_AUTO_DEFINE_FCN0 (GOptionContext *, gs_local_option_context, g_option_context_free); + /*****************************************************************************/ #include "nm-glib.h" diff --git a/src/devices/nm-device.c b/src/devices/nm-device.c index 72a2b10..012af4d 100644 --- a/src/devices/nm-device.c +++ b/src/devices/nm-device.c @@ -8754,7 +8754,6 @@ dhcp4_get_vendor_class_identifier (NMDevice *self, NMSettingIP4Config *s_ip4) { gs_free char *config_data_prop = NULL; gs_free char *to_free = NULL; - gboolean validate = FALSE; const char *conn_prop; GBytes *bytes = NULL; const char *bin; @@ -8764,12 +8763,14 @@ dhcp4_get_vendor_class_identifier (NMDevice *self, NMSettingIP4Config *s_ip4) if (!conn_prop) { /* set in NetworkManager.conf ? */ - validate = TRUE; config_data_prop = nm_config_data_get_connection_default ( NM_CONFIG_GET_DATA, NM_CON_DEFAULT ("ipv4.dhcp-vendor-class-identifier"), self); - conn_prop = config_data_prop; + + if ( config_data_prop + && nm_utils_validate_dhcp4_vendor_class_id (config_data_prop, NULL)) + conn_prop = config_data_prop; } if (conn_prop) { @@ -8777,10 +8778,6 @@ dhcp4_get_vendor_class_identifier (NMDevice *self, NMSettingIP4Config *s_ip4) NM_UTILS_STR_UTF8_SAFE_FLAG_NONE, &len, (gpointer *) &to_free); - - if (validate && (bin[0] == '\0' || len > 255 || strlen (bin) != len)) - return NULL; - if (to_free) bytes = g_bytes_new_take (g_steal_pointer (&to_free), len); else diff --git a/src/initrd/nm-initrd-generator.c b/src/initrd/nm-initrd-generator.c index f984ed7..5a93480 100644 --- a/src/initrd/nm-initrd-generator.c +++ b/src/initrd/nm-initrd-generator.c @@ -83,7 +83,7 @@ main (int argc, char *argv[]) { G_OPTION_REMAINING, '\0', 0, G_OPTION_ARG_STRING_ARRAY, &remaining, NULL, NULL }, { NULL } }; - GOptionContext *option_context; + gs_free_option_context GOptionContext *option_context = NULL; gs_free_error GError *error = NULL; gs_free char *hostname = NULL; int errsv; diff --git a/src/initrd/nmi-cmdline-reader.c b/src/initrd/nmi-cmdline-reader.c index be39ef8..ba747b3 100644 --- a/src/initrd/nmi-cmdline-reader.c +++ b/src/initrd/nmi-cmdline-reader.c @@ -28,6 +28,7 @@ typedef struct { /* Parameters to be set for all connections */ gboolean ignore_auto_dns; int dhcp_timeout; + char *dhcp4_vci; } Reader; static Reader * @@ -52,6 +53,7 @@ reader_destroy (Reader *reader, gboolean free_hash) g_ptr_array_unref (reader->array); hash = g_steal_pointer (&reader->hash); nm_clear_g_free (&reader->hostname); + nm_clear_g_free (&reader->dhcp4_vci); nm_g_slice_free (reader); if (!free_hash) return g_steal_pointer (&hash); @@ -95,6 +97,7 @@ reader_create_connection (Reader *reader, NM_SETTING_IP_CONFIG_MAY_FAIL, TRUE, NM_SETTING_IP_CONFIG_IGNORE_AUTO_DNS, reader->ignore_auto_dns, NM_SETTING_IP_CONFIG_DHCP_TIMEOUT, reader->dhcp_timeout, + NM_SETTING_IP4_CONFIG_DHCP_VENDOR_CLASS_IDENTIFIER, reader->dhcp4_vci, NULL); setting = nm_setting_ip6_config_new (); @@ -927,6 +930,9 @@ nmi_cmdline_reader_parse (const char *sysfs_dir, const char *const*argv, char ** else if (nm_streq (tag, "rd.net.timeout.dhcp")) { reader->dhcp_timeout = _nm_utils_ascii_str_to_int64 (argument, 10, 0, G_MAXINT32, 0); + } else if (nm_streq (tag, "rd.net.dhcp.vendor-class")) { + if (nm_utils_validate_dhcp4_vendor_class_id (argument, NULL)) + nm_utils_strdup_reset (&reader->dhcp4_vci, argument); } } diff --git a/src/initrd/tests/test-cmdline-reader.c b/src/initrd/tests/test-cmdline-reader.c index 7787cf5..a11b76e 100644 --- a/src/initrd/tests/test-cmdline-reader.c +++ b/src/initrd/tests/test-cmdline-reader.c @@ -1485,6 +1485,54 @@ test_bootif_off (void) g_assert_cmpstr (hostname, ==, NULL); } +static void +test_dhcp_vendor_class_id (void) +{ + gs_unref_hashtable GHashTable *connections = NULL; + const char *const*ARGV = NM_MAKE_STRV ("rd.net.dhcp.vendor-class=testvci", + "ip=eno1:dhcp"); + NMConnection *connection; + NMSettingIP4Config *s_ip4; + gs_free char *hostname = NULL; + gs_free char *vci_long = NULL; + char vci_arg_long[512] = {0}; + + connections = nmi_cmdline_reader_parse (TEST_INITRD_DIR "/sysfs", ARGV, &hostname); + g_assert (connections); + g_assert_cmpint (g_hash_table_size (connections), ==, 1); + g_assert_cmpstr (hostname, ==, NULL); + + connection = g_hash_table_lookup (connections, "eno1"); + g_assert (connection); + nmtst_assert_connection_verifies_without_normalization (connection); + s_ip4 = NM_SETTING_IP4_CONFIG (nm_connection_get_setting_ip4_config (connection)); + g_assert_cmpstr (nm_setting_ip4_config_get_dhcp_vendor_class_identifier (s_ip4), ==, "testvci"); + + nm_clear_pointer (&connections, g_hash_table_unref); + + ARGV = NM_MAKE_STRV ("rd.net.dhcp.vendor-class", + "ip=eno1:dhcp"); + connections = nmi_cmdline_reader_parse (TEST_INITRD_DIR "/sysfs", ARGV, &hostname); + connection = g_hash_table_lookup (connections, "eno1"); + g_assert (connection); + nmtst_assert_connection_verifies_without_normalization (connection); + s_ip4 = NM_SETTING_IP4_CONFIG (nm_connection_get_setting_ip4_config (connection)); + g_assert (nm_setting_ip4_config_get_dhcp_vendor_class_identifier (s_ip4) == NULL); + + nm_clear_pointer (&connections, g_hash_table_unref); + + memset (vci_arg_long, 'A', 400); + vci_long = g_strdup_printf ("rd.net.dhcp.vendor-class=%s", vci_arg_long); + ARGV = NM_MAKE_STRV (vci_long, + "ip=eno1:dhcp"); + connections = nmi_cmdline_reader_parse (TEST_INITRD_DIR "/sysfs", ARGV, &hostname); + connection = g_hash_table_lookup (connections, "eno1"); + g_assert (connection); + nmtst_assert_connection_verifies_without_normalization (connection); + s_ip4 = NM_SETTING_IP4_CONFIG (nm_connection_get_setting_ip4_config (connection)); + g_assert (nm_setting_ip4_config_get_dhcp_vendor_class_identifier (s_ip4) == NULL); +} + NMTST_DEFINE (); int main (int argc, char **argv) @@ -1521,6 +1569,7 @@ int main (int argc, char **argv) g_test_add_func ("/initrd/cmdline/bootif/hwtype", test_bootif_hwtype); g_test_add_func ("/initrd/cmdline/bootif/off", test_bootif_off); g_test_add_func ("/initrd/cmdline/neednet", test_neednet); + g_test_add_func ("/initrd/cmdline/dhcp/vendor_class_id", test_dhcp_vendor_class_id); return g_test_run (); }