glib-aux: honor NM_OBFUSCATE_PTR=0 setting for printing raw pointer values

We avoid printing pointer values directly, instead we usually call
NM_HASH_OBFUSCATE_PTR(). This hashes the pointers with a random seed
so they are not directly visible.

That obviously makes it harder to debug. Add an environment variable
to disable that.

  $ NM_OBFUSCATE_PTR=0 LIBNM_CLIENT_DEBUG=trace,stdout nmcli

Note that this flag is only honored in debug builds (WITH_MORE_ASSERTS>0).
This commit is contained in:
Thomas Haller 2022-01-05 22:49:49 +01:00
parent 99f82b4b84
commit 0acb4b685d
No known key found for this signature in database
GPG key ID: 29C2366E4DFC5728
2 changed files with 29 additions and 9 deletions

View file

@ -284,3 +284,31 @@ nm_pg_bytes_equal(gconstpointer a, gconstpointer b)
return g_bytes_equal(*ptr_a, *ptr_b);
}
/*****************************************************************************/
guint64
nm_hash_obfuscate_ptr(guint static_seed, gconstpointer val)
{
NMHashState h;
if (NM_MORE_ASSERTS > 0) {
static int obfuscate_static = -1;
int obfuscate;
again:
obfuscate = g_atomic_int_get(&obfuscate_static);
if (G_UNLIKELY(obfuscate == -1)) {
obfuscate = _nm_utils_ascii_str_to_int64(g_getenv("NM_OBFUSCATE_PTR"), 10, 0, 1, 1);
if (!g_atomic_int_compare_and_exchange(&obfuscate_static, -1, obfuscate))
goto again;
}
if (!obfuscate)
return (uintptr_t) val;
}
nm_hash_init(&h, static_seed);
nm_hash_update_val(&h, val);
return nm_hash_complete_u64(&h);
}

View file

@ -284,15 +284,7 @@ gboolean nm_pg_bytes_equal(gconstpointer a, gconstpointer b);
*
* Note that there is a chance that two different pointer values hash to the same obfuscated
* value. So beware of that when reviewing logs. However, such a collision is very unlikely. */
static inline guint64
nm_hash_obfuscate_ptr(guint static_seed, gconstpointer val)
{
NMHashState h;
nm_hash_init(&h, static_seed);
nm_hash_update_val(&h, val);
return nm_hash_complete_u64(&h);
}
guint64 nm_hash_obfuscate_ptr(guint static_seed, gconstpointer val);
/* if you want to log obfuscated pointer for a certain context (like, NMPRuleManager
* logging user-tags), then you are advised to use nm_hash_obfuscate_ptr() with your