PATCH] sms_decode_text(): Sanitize 8-bit data so that it is UTF8-clean.



This keeps ModemManager from crashing deep in the DBus libraries when
a SMS Get() or List() DBus operation finds a message that isn't valid
UTF-8 and/or has embedded NUL characters.

I'll be putting up a separate patch as a proposal for how to avoid
this problem in the new API.

    - Nathan
From b4be9e8cfa79cfb1d63e69a151078c75f38131d9 Mon Sep 17 00:00:00 2001
From: Nathan Williams <njw chromium org>
Date: Fri, 23 Sep 2011 17:21:15 -0400
Subject: [PATCH] sms_decode_text(): Sanitize 8-bit data so that it is UTF8-clean.

When receiving a SMS message with raw 8-bit data, sanitize it by
replacing non-ASCII characters with \xNN escape sequences. This
prevents a problem further down the line where the body of the message
is passed into DBus as a string, and DBus chokes because the string
isn't valid UTF-8.

Once the ModemManager SMS API can support non-string message bodies,
this should be revisited.

BUG=chrome-os-partner:5953
TEST=Run network_ModemManagerSMS.py with the PDU from this bug.

Change-Id: Ic33a365f9a065c49a325e047e4c3f5e81450fa1f
Reviewed-on: http://gerrit.chromium.org/gerrit/8232
Reviewed-by: Eric Shienbrood <ers chromium org>
Tested-by: Nathan J. Williams <njw chromium org>
Commit-Ready: Nathan J. Williams <njw chromium org>
---
 src/mm-sms-utils.c |   21 +++++++++++++++++++--
 1 files changed, 19 insertions(+), 2 deletions(-)

diff --git a/src/mm-sms-utils.c b/src/mm-sms-utils.c
index 3f56a64..89eae4b 100644
--- a/src/mm-sms-utils.c
+++ b/src/mm-sms-utils.c
@@ -13,6 +13,9 @@
  * Copyright (C) 2011 Red Hat, Inc.
  */
 
+#include <ctype.h>
+#include <stdio.h>
+
 #include <glib.h>
 
 #include "mm-charsets.h"
@@ -200,8 +203,22 @@ sms_decode_text (const guint8 *text, int len, SmsEncoding encoding, int bit_offs
         g_free (unpacked);
     } else if (encoding == MM_SMS_ENCODING_UCS2)
         utf8 = g_convert ((char *) text, len, "UTF8", "UCS-2BE", NULL, NULL, NULL);
-    else if (encoding == MM_SMS_ENCODING_8BIT)
-        utf8 = g_strndup ((const char *)text, len);
+    else if (encoding == MM_SMS_ENCODING_8BIT) {
+        /* DBus may choke on non-UTF8 strings, so we have some sanitizing to do */
+        char *p;
+        int i;
+        utf8 = g_malloc0 (4*len+1); /* Worst case: Every byte becomes "\xFF" */
+        p = utf8;
+        for (i = 0 ; i < len ; i++) {
+            if (isascii (text[i]) && text[i] != '\0')
+                *p++ = text[i];
+            else {
+                sprintf(p, "\\x%02x", text[i]);
+                p += 4;
+            }
+        }
+        *p = '\0';
+    }
     else
         utf8 = g_strdup ("");
 
-- 
1.7.3.1



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]