Blob Blame History Raw
From 84cd78aee89120953e703dde308d850ddaadbfc4 Mon Sep 17 00:00:00 2001
From: Beniamino Galvani <bgalvani@redhat.com>
Date: Fri, 3 Jul 2020 15:26:28 +0200
Subject: [PATCH] initrd: fix generating default BOOTIF= connection

There is a bug when parsing a BOOTIF= without any existing
connection. The generated connection doesn't have wired setting and
later we try to access it:

 # nm-initrd-generator --stdout -- BOOTIF=01-50-50-00-9f-21-21
  (nm-initrd-generator:1546): libnm-CRITICAL **: ((libnm-core/nm-setting-wired.c:205)): assertion '<dropped>' failed
  (nm-initrd-generator:1546): GLib-GObject-CRITICAL **: g_object_set: assertion 'G_IS_OBJECT (object)' failed

Fix this.

https://bugzilla.redhat.com/show_bug.cgi?id=1853277

Fixes: 25a2b6e14ff5 ('initrd: rework command line parsing')
(cherry picked from commit 3023c70e4e1e50ddf5e7dae727fc3d67bfa18b32)
(cherry picked from commit b8246ea367a0041cb1527a08f47247877a4c07bd)
---
 src/initrd/nmi-cmdline-reader.c        |  1 +
 src/initrd/tests/test-cmdline-reader.c | 41 ++++++++++++++++++++++++--
 2 files changed, 40 insertions(+), 2 deletions(-)

diff --git a/src/initrd/nmi-cmdline-reader.c b/src/initrd/nmi-cmdline-reader.c
index 69e5e56d0..418e837d8 100644
--- a/src/initrd/nmi-cmdline-reader.c
+++ b/src/initrd/nmi-cmdline-reader.c
@@ -132,6 +132,7 @@ reader_get_default_connection (Reader *reader)
 		                                NULL,
 		                                NM_SETTING_WIRED_SETTING_NAME,
 		                                NM_CONNECTION_MULTI_CONNECT_MULTIPLE);
+		nm_connection_add_setting (con, nm_setting_wired_new ());
 		reader->default_connection = con;
 	}
 	return reader->default_connection;
diff --git a/src/initrd/tests/test-cmdline-reader.c b/src/initrd/tests/test-cmdline-reader.c
index 110770554..b25f49e22 100644
--- a/src/initrd/tests/test-cmdline-reader.c
+++ b/src/initrd/tests/test-cmdline-reader.c
@@ -1064,7 +1064,7 @@ test_rd_znet_no_ip (void)
 }
 
 static void
-test_bootif (void)
+test_bootif_ip (void)
 {
 	gs_unref_hashtable GHashTable *connections = NULL;
 	const char *const*ARGV = NM_MAKE_STRV ("BOOTIF=00:53:AB:cd:02:03",
@@ -1101,6 +1101,42 @@ test_bootif (void)
 	g_assert (!nm_setting_ip_config_get_ignore_auto_dns (s_ip6));
 }
 
+static void
+test_bootif_no_ip (void)
+{
+	gs_unref_hashtable GHashTable *connections = NULL;
+	const char *const*ARGV = NM_MAKE_STRV ("BOOTIF=00:53:AB:cd:02:03");
+	NMConnection *connection;
+	NMSettingWired *s_wired;
+	NMSettingIPConfig *s_ip4;
+	NMSettingIPConfig *s_ip6;
+	gs_free char *hostname = NULL;
+
+	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, "default_connection");
+	g_assert (connection);
+	nmtst_assert_connection_verifies_without_normalization (connection);
+	g_assert_cmpstr (nm_connection_get_id (connection), ==, "Wired Connection");
+
+	s_wired = nm_connection_get_setting_wired (connection);
+	g_assert_cmpstr (nm_setting_wired_get_mac_address (s_wired), ==, "00:53:AB:CD:02:03");
+	g_assert (s_wired);
+
+	s_ip4 = nm_connection_get_setting_ip4_config (connection);
+	g_assert (s_ip4);
+	g_assert_cmpstr (nm_setting_ip_config_get_method (s_ip4), ==, NM_SETTING_IP4_CONFIG_METHOD_AUTO);
+	g_assert (nm_setting_ip_config_get_may_fail (s_ip4));
+
+	s_ip6 = nm_connection_get_setting_ip6_config (connection);
+	g_assert (s_ip6);
+	g_assert_cmpstr (nm_setting_ip_config_get_method (s_ip6), ==, NM_SETTING_IP6_CONFIG_METHOD_AUTO);
+	g_assert (nm_setting_ip_config_get_may_fail (s_ip6));
+}
+
 static void
 test_bootif_hwtype (void)
 {
@@ -1255,7 +1291,8 @@ int main (int argc, char **argv)
 	g_test_add_func ("/initrd/cmdline/rd_znet", test_rd_znet);
 	g_test_add_func ("/initrd/cmdline/rd_znet/legacy", test_rd_znet_legacy);
 	g_test_add_func ("/initrd/cmdline/rd_znet/no_ip", test_rd_znet_no_ip);
-	g_test_add_func ("/initrd/cmdline/bootif", test_bootif);
+	g_test_add_func ("/initrd/cmdline/bootif/ip", test_bootif_ip);
+	g_test_add_func ("/initrd/cmdline/bootif/no_ip", test_bootif_no_ip);
 	g_test_add_func ("/initrd/cmdline/bootif/hwtype", test_bootif_hwtype);
 	g_test_add_func ("/initrd/cmdline/bootif/off", test_bootif_off);
 
-- 
2.26.2