From 0acb4b685d6801aced740c40c8b6b527fc0c9b11 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Wed, 5 Jan 2022 22:49:49 +0100 Subject: [PATCH] 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). --- src/libnm-glib-aux/nm-hash-utils.c | 28 ++++++++++++++++++++++++++++ src/libnm-glib-aux/nm-hash-utils.h | 10 +--------- 2 files changed, 29 insertions(+), 9 deletions(-) diff --git a/src/libnm-glib-aux/nm-hash-utils.c b/src/libnm-glib-aux/nm-hash-utils.c index 5cfc3d4ad5..ed575123f7 100644 --- a/src/libnm-glib-aux/nm-hash-utils.c +++ b/src/libnm-glib-aux/nm-hash-utils.c @@ -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); +} diff --git a/src/libnm-glib-aux/nm-hash-utils.h b/src/libnm-glib-aux/nm-hash-utils.h index d7de2de43a..1eb1d55d79 100644 --- a/src/libnm-glib-aux/nm-hash-utils.h +++ b/src/libnm-glib-aux/nm-hash-utils.h @@ -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