Skip to contentSkip to conversation

Prevent abort on non-UTF-8 data loaded from storage

PATCH New Giuseppe Eletto 1 participant 3 messages Jul 8

lore
Share & tools
lore
#0 lore raw

cover Prevent abort on non-UTF-8 data loaded from storage

While investigating a connmand crash loop on an i.MX91 based device I found that connmand aborts on start with SIGABRT raised from libdbus.

The global settings file contained a WiFi tethering identifier and/or passphrase that were no longer valid UTF-8:

[WiFi]
Tethering.Identifier=@?????
Tethering.Passphrase=PW\215\301\252\252

technology_load() loads these values and append_properties() hands them to D-Bus when the TechnologyAdded signal is emitted. libdbus aborts the whole process when a string that is not valid UTF-8 is appended to a message, so connmand crashes on every start and cannot recover without manually editing or removing the lines from the file:

Aborting (signal 6) [/usr/sbin/connmand]

  #6  libdbus-1.so.3  dbus_message_iter_append_basic
  #7  connmand        connman_dbus_dict_append_basic
  #8  connmand        append_properties
  #9  connmand        technology_added_signal
  #10 connmand        technology_dbus_register
  #11 connmand        technology_get

The same problem applies to the WiFi service name, which is also loaded verbatim from storage and exported over D-Bus.

This series makes connmand resilient to such corrupted data by validating the affected strings with g_utf8_validate() when they are loaded and ignoring the invalid ones. Values sent over D-Bus are always valid UTF-8, so only corrupted on-disk data is affected.

Giuseppe Eletto (2):
technology: Ignore invalid UTF-8 tethering identifier and passphrase
service: Ignore invalid UTF-8 service name

 src/service.c    |  8 ++++++++
 src/technology.c | 16 ++++++++++++++++
 2 files changed, 24 insertions(+)
Signature · 1 line
2.50.1
#1 lore raw

PATCH 1/2 technology: Ignore invalid UTF-8 tethering identifier and passphrase

technology_load() reads the WiFi tethering identifier and passphrase from the global settings file. The passphrase is additionally passed through g_strcompress(), which turns stored octal escapes such as "\215\301\252" back into raw bytes.

When the settings file is corrupted, for example after an unclean shutdown on flash based storage, these strings may no longer be valid UTF-8:

[WiFi]
Tethering.Identifier=@?????
Tethering.Passphrase=PW\215\301\252\252

Both values are later handed to D-Bus in append_properties() when the technology is registered and the TechnologyAdded signal is emitted. libdbus aborts the process when a string that is not valid UTF-8 is appended to a message, so connmand crashes on every start and never recovers on its own:

Aborting (signal 6) [/usr/sbin/connmand]

  #6  libdbus-1.so.3  dbus_message_iter_append_basic
  #7  connmand        connman_dbus_dict_append_basic
  #8  connmand        append_properties
  #9  connmand        technology_added_signal
  #10 connmand        technology_dbus_register
  #11 connmand        technology_get

Validate both values with g_utf8_validate() right after loading them and ignore the ones that are not valid UTF-8.

---
 src/technology.c | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)
src/technology.c +16 -0
index 933070f0..8bdb296e 100644
@@ -481,11 +481,27 @@ static void technology_load(struct connman_technology *technology)
 	technology->tethering_ident = g_key_file_get_string(keyfile,
 				identifier, "Tethering.Identifier", NULL);
 
+	if (technology->tethering_ident &&
+			!g_utf8_validate(technology->tethering_ident, -1, NULL)) {
+		connman_warn("Ignoring invalid Tethering.Identifier for %s",
+								identifier);
+		g_free(technology->tethering_ident);
+		technology->tethering_ident = NULL;
+	}
+
 	enc = g_key_file_get_string(keyfile,
 				identifier, "Tethering.Passphrase", NULL);
 	if (enc) {
 		technology->tethering_passphrase = g_strcompress(enc);
 		g_free(enc);
+
+		if (!g_utf8_validate(technology->tethering_passphrase,
+								-1, NULL)) {
+			connman_warn("Ignoring invalid Tethering.Passphrase "
+						"for %s", identifier);
+			g_free(technology->tethering_passphrase);
+			technology->tethering_passphrase = NULL;
+		}
 	}
 
 	technology->tethering_freq = g_key_file_get_integer(keyfile,
Signature · 1 line
2.50.1
#2 lore raw

PATCH 2/2 service: Ignore invalid UTF-8 service name

In reply to Giuseppe Eletto

service_load() reads the WiFi service name from the per-service settings file and stores it in service->name, which is later sent over D-Bus in append_properties(). Like the tethering identifier and passphrase, a corrupted settings file may contain a name that is not valid UTF-8, and appending such a string to a D-Bus message aborts connmand.

Names obtained from a scan are already sanitized by create_name() in gsupplicant, but the value loaded from storage is not. Validate it with g_utf8_validate() and ignore it when it is not valid UTF-8.

---
 src/service.c | 8 ++++++++
 1 file changed, 8 insertions(+)
src/service.c +8 -0
index d5869b14..067ddd46 100644
@@ -678,6 +678,14 @@ static int service_load(struct connman_service *service)
 
 			name = g_key_file_get_string(keyfile,
 					service->identifier, "Name", NULL);
+
+			if (name && !g_utf8_validate(name, -1, NULL)) {
+				connman_warn("Ignoring invalid Name for %s",
+						service->identifier);
+				g_free(name);
+				name = NULL;
+			}
+
 			if (name) {
 				g_free(service->name);
 				service->name = name;
Signature · 1 line
2.50.1