shared: add NM_UTILS_STR_UTF8_SAFE_FLAG_SECRET flag
The new flag tells that as we re-allocate data buffers during escaping, we bzero the memory to avoid leaking secrets.
This commit is contained in:
@@ -7545,10 +7545,11 @@ _do_test_utils_str_utf8safe (const char *str, gsize str_len, const char *expecte
|
||||
gs_free char *str_free_7 = NULL;
|
||||
gs_free char *str_free_8 = NULL;
|
||||
gboolean str_has_nul = FALSE;
|
||||
#define RND_FLAG ((nmtst_get_rand_bool ()) ? NM_UTILS_STR_UTF8_SAFE_FLAG_NONE : NM_UTILS_STR_UTF8_SAFE_FLAG_SECRET)
|
||||
|
||||
buf_safe = nm_utils_buf_utf8safe_escape (str, str_len, flags, &str_free_1);
|
||||
buf_safe = nm_utils_buf_utf8safe_escape (str, str_len, flags | RND_FLAG, &str_free_1);
|
||||
|
||||
str_safe = nm_utils_str_utf8safe_escape (str, flags, &str_free_2);
|
||||
str_safe = nm_utils_str_utf8safe_escape (str, flags | RND_FLAG, &str_free_2);
|
||||
|
||||
if (str_len == 0) {
|
||||
g_assert (buf_safe == NULL);
|
||||
@@ -7568,7 +7569,7 @@ _do_test_utils_str_utf8safe (const char *str, gsize str_len, const char *expecte
|
||||
} else
|
||||
str_has_nul = TRUE;
|
||||
|
||||
str_free_3 = nm_utils_str_utf8safe_escape_cp (str, flags);
|
||||
str_free_3 = nm_utils_str_utf8safe_escape_cp (str, flags | RND_FLAG);
|
||||
g_assert_cmpstr (str_free_3, ==, str_safe);
|
||||
g_assert ((!str && !str_free_3) || (str != str_free_3));
|
||||
|
||||
|
@@ -2204,12 +2204,14 @@ nm_g_type_find_implementing_class_for_property (GType gtype,
|
||||
/*****************************************************************************/
|
||||
|
||||
static void
|
||||
_str_append_escape (GString *s, char ch)
|
||||
_str_buf_append_c_escape_octal (NMStrBuf *strbuf,
|
||||
char ch)
|
||||
{
|
||||
g_string_append_c (s, '\\');
|
||||
g_string_append_c (s, '0' + ((((guchar) ch) >> 6) & 07));
|
||||
g_string_append_c (s, '0' + ((((guchar) ch) >> 3) & 07));
|
||||
g_string_append_c (s, '0' + ( ((guchar) ch) & 07));
|
||||
nm_str_buf_append_c4 (strbuf,
|
||||
'\\',
|
||||
'0' + ((char) ((((guchar) ch) >> 6) & 07)),
|
||||
'0' + ((char) ((((guchar) ch) >> 3) & 07)),
|
||||
'0' + ((char) ((((guchar) ch) ) & 07)));
|
||||
}
|
||||
|
||||
gconstpointer
|
||||
@@ -2338,7 +2340,7 @@ nm_utils_buf_utf8safe_escape (gconstpointer buf, gssize buflen, NMUtilsStrUtf8Sa
|
||||
const char *p = NULL;
|
||||
const char *s;
|
||||
gboolean nul_terminated = FALSE;
|
||||
GString *gstr;
|
||||
NMStrBuf strbuf;
|
||||
|
||||
g_return_val_if_fail (to_free, NULL);
|
||||
|
||||
@@ -2369,7 +2371,9 @@ nm_utils_buf_utf8safe_escape (gconstpointer buf, gssize buflen, NMUtilsStrUtf8Sa
|
||||
return str;
|
||||
}
|
||||
|
||||
gstr = g_string_sized_new (buflen + 5);
|
||||
nm_str_buf_init (&strbuf,
|
||||
buflen + 5,
|
||||
NM_FLAGS_HAS (flags, NM_UTILS_STR_UTF8_SAFE_FLAG_SECRET));
|
||||
|
||||
s = str;
|
||||
do {
|
||||
@@ -2379,21 +2383,22 @@ nm_utils_buf_utf8safe_escape (gconstpointer buf, gssize buflen, NMUtilsStrUtf8Sa
|
||||
for (; s < p; s++) {
|
||||
char ch = s[0];
|
||||
|
||||
nm_assert (ch);
|
||||
if (ch == '\\')
|
||||
g_string_append (gstr, "\\\\");
|
||||
nm_str_buf_append_c2 (&strbuf, '\\', '\\');
|
||||
else if ( ( NM_FLAGS_HAS (flags, NM_UTILS_STR_UTF8_SAFE_FLAG_ESCAPE_CTRL) \
|
||||
&& ch < ' ') \
|
||||
|| ( NM_FLAGS_HAS (flags, NM_UTILS_STR_UTF8_SAFE_FLAG_ESCAPE_NON_ASCII) \
|
||||
&& ((guchar) ch) >= 127))
|
||||
_str_append_escape (gstr, ch);
|
||||
_str_buf_append_c_escape_octal (&strbuf, ch);
|
||||
else
|
||||
g_string_append_c (gstr, ch);
|
||||
nm_str_buf_append_c (&strbuf, ch);
|
||||
}
|
||||
|
||||
if (buflen <= 0)
|
||||
break;
|
||||
|
||||
_str_append_escape (gstr, p[0]);
|
||||
_str_buf_append_c_escape_octal (&strbuf, p[0]);
|
||||
|
||||
buflen--;
|
||||
if (buflen == 0)
|
||||
@@ -2403,8 +2408,7 @@ nm_utils_buf_utf8safe_escape (gconstpointer buf, gssize buflen, NMUtilsStrUtf8Sa
|
||||
(void) g_utf8_validate (s, buflen, &p);
|
||||
} while (TRUE);
|
||||
|
||||
*to_free = g_string_free (gstr, FALSE);
|
||||
return *to_free;
|
||||
return (*to_free = nm_str_buf_finalize (&strbuf, NULL));
|
||||
}
|
||||
|
||||
const char *
|
||||
|
@@ -1024,6 +1024,7 @@ typedef enum {
|
||||
NM_UTILS_STR_UTF8_SAFE_FLAG_NONE = 0,
|
||||
NM_UTILS_STR_UTF8_SAFE_FLAG_ESCAPE_CTRL = 0x0001,
|
||||
NM_UTILS_STR_UTF8_SAFE_FLAG_ESCAPE_NON_ASCII = 0x0002,
|
||||
NM_UTILS_STR_UTF8_SAFE_FLAG_SECRET = 0x0004,
|
||||
} NMUtilsStrUtf8SafeFlags;
|
||||
|
||||
const char *nm_utils_buf_utf8safe_escape (gconstpointer buf, gssize buflen, NMUtilsStrUtf8SafeFlags flags, char **to_free);
|
||||
|
Reference in New Issue
Block a user