From: Stephen Hemminger <stephen@networkplumber.org>
To: dev@dpdk.org
Cc: Stephen Hemminger <stephen@networkplumber.org>,
Yipeng Wang <yipeng1.wang@intel.com>,
Sameh Gobriel <sameh.gobriel@intel.com>,
Bruce Richardson <bruce.richardson@intel.com>,
Vladimir Medvedkin <vladimir.medvedkin@intel.com>
Subject: [PATCH 07/18] hash: use secure random for Toeplitz hash key
Date: Tue, 15 Sep 2026 12:21:05 -0700 [thread overview]
Message-ID: <20260915192207.3926217-8-stephen@networkplumber.org> (raw)
In-Reply-To: <20260915192207.3926217-1-stephen@networkplumber.org>
When no key is supplied by the application, rte_thash_init_ctx()
generated the Toeplitz hash key with rte_rand().
That generator is not meant to be unpredictable and its internal state
can be recovered from a small number of outputs, so an attacker able to
observe other random values can derive the RSS key and craft flows that
all land on a single receive queue.
Use rte_random_bytes() for the hash key and for the LFSR state.
The hash key is generated once at context creation. It is now drawn
before the tailq lock is taken, since the system random generator can
block until the kernel pool is initialized and that should not be
done while holding a global lock.
The LFSR state is also set from alloc_lfsr(), which besides context
creation is reached from rte_thash_add_helper() and
rte_thash_adjust_tuple(). Those paths report a failure of the random
source as -ENOMEM, which is what they already return when alloc_lfsr()
fails.
No longer use rte_memcpy() just use memcpy().
Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
lib/hash/rte_thash.c | 46 +++++++++++++++++++++++++++++++++++---------
lib/hash/rte_thash.h | 4 +++-
2 files changed, 40 insertions(+), 10 deletions(-)
diff --git a/lib/hash/rte_thash.c b/lib/hash/rte_thash.c
index fcd41248b9..7730ee053b 100644
--- a/lib/hash/rte_thash.c
+++ b/lib/hash/rte_thash.c
@@ -9,7 +9,6 @@
#include <rte_thash.h>
#include <rte_tailq.h>
#include <rte_random.h>
-#include <rte_memcpy.h>
#include <rte_errno.h>
#include <rte_eal_memconfig.h>
#include <rte_log.h>
@@ -176,7 +175,17 @@ alloc_lfsr(uint32_t poly_degree)
lfsr->deg = poly_degree;
lfsr->poly = thash_get_rand_poly(lfsr->deg);
do {
- lfsr->state = rte_rand() & ((1 << lfsr->deg) - 1);
+ uint32_t rnd;
+
+ if (rte_random_bytes(&rnd, sizeof(rnd)) != 0) {
+ rte_free(lfsr);
+ return NULL;
+ }
+
+ /* deg can be 32, so the mask must not be computed by
+ * shifting an int by the degree.
+ */
+ lfsr->state = rnd & RTE_GENMASK32(lfsr->deg - 1, 0);
} while (lfsr->state == 0);
/* init reverse order polynomial */
lfsr->rev_poly = get_rev_poly(lfsr->poly, lfsr->deg);
@@ -215,13 +224,35 @@ rte_thash_init_ctx(const char *name, uint32_t key_len, uint32_t reta_sz,
struct rte_thash_ctx *ctx;
struct rte_tailq_entry *te;
struct rte_thash_list *thash_list;
- uint32_t i;
+ uint8_t *rand_key = NULL;
if ((name == NULL) || (key_len == 0) || !RETA_SZ_IN_RANGE(reta_sz)) {
rte_errno = EINVAL;
return NULL;
}
+ /* Draw the random key before taking the tailq lock, the system
+ * random generator can block until it is initialized.
+ */
+ if (key == NULL) {
+ rand_key = rte_zmalloc(NULL, key_len, 0);
+ if (rand_key == NULL) {
+ HASH_LOG(ERR, "thash ctx %s key allocation failed",
+ name);
+ rte_errno = ENOMEM;
+ return NULL;
+ }
+
+ if (rte_random_bytes(rand_key, key_len) != 0) {
+ HASH_LOG(ERR,
+ "Cannot generate hash key for thash context %s",
+ name);
+ rte_free(rand_key);
+ rte_errno = EIO;
+ return NULL;
+ }
+ }
+
thash_list = RTE_TAILQ_CAST(rte_thash_tailq.head, rte_thash_list);
rte_mcfg_tailq_write_lock();
@@ -262,12 +293,7 @@ rte_thash_init_ctx(const char *name, uint32_t key_len, uint32_t reta_sz,
LIST_INIT(&ctx->head);
ctx->flags = flags;
- if (key)
- rte_memcpy(ctx->hash_key, key, key_len);
- else {
- for (i = 0; i < key_len; i++)
- ctx->hash_key[i] = rte_rand();
- }
+ memcpy(ctx->hash_key, key ? key : rand_key, key_len);
if (rte_thash_gfni_supported()) {
ctx->matrices = rte_zmalloc(NULL, key_len * sizeof(uint64_t),
@@ -286,6 +312,7 @@ rte_thash_init_ctx(const char *name, uint32_t key_len, uint32_t reta_sz,
TAILQ_INSERT_TAIL(thash_list, te, next);
rte_mcfg_tailq_write_unlock();
+ rte_free(rand_key);
return ctx;
@@ -295,6 +322,7 @@ rte_thash_init_ctx(const char *name, uint32_t key_len, uint32_t reta_sz,
rte_free(te);
exit:
rte_mcfg_tailq_write_unlock();
+ rte_free(rand_key);
return NULL;
}
diff --git a/lib/hash/rte_thash.h b/lib/hash/rte_thash.h
index 8bca430663..4387eb4a07 100644
--- a/lib/hash/rte_thash.h
+++ b/lib/hash/rte_thash.h
@@ -295,7 +295,9 @@ struct rte_thash_subtuple_helper;
* the reta entry.
* @param key
* Pointer to the key used to init an internal key state.
- * Could be NULL, in this case internal key will be inited with random.
+ * Could be NULL, in this case the internal key is generated from the
+ * random source of the operating system, and context creation fails
+ * if that source is unavailable.
* @param flags
* Supported flags are:
* RTE_THASH_IGNORE_PERIOD_OVERFLOW
--
2.53.0
next prev parent reply other threads:[~2026-09-15 19:22 UTC|newest]
Thread overview: 27+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-09-15 19:20 [PATCH 00/18] random number generator improvements Stephen Hemminger
2026-09-15 19:20 ` [PATCH 01/18] eal/windows: add getentropy shim Stephen Hemminger
2026-09-15 19:21 ` [PATCH 02/18] random: always use getentropy Stephen Hemminger
2026-09-15 19:21 ` [PATCH 03/18] eal: add 32 bit random number function Stephen Hemminger
2026-09-15 19:21 ` [PATCH 04/18] sched: fix data race in RED random number generation Stephen Hemminger
2026-09-15 19:21 ` [PATCH 05/18] eal/windows: add getrandom shim Stephen Hemminger
2026-09-15 19:21 ` [PATCH 06/18] eal: add function to get random bytes Stephen Hemminger
2026-09-15 19:21 ` Stephen Hemminger [this message]
2026-09-15 19:21 ` [PATCH 08/18] pipeline: use secure random for IPsec salt Stephen Hemminger
2026-09-15 19:21 ` [PATCH 09/18] net: use secure random for random MAC address Stephen Hemminger
2026-09-15 19:21 ` [PATCH 10/18] ipsec: use secure random for SAD hash seed Stephen Hemminger
2026-09-15 19:21 ` [PATCH 11/18] net/intel: use secure random for default RSS key Stephen Hemminger
2026-09-15 19:21 ` [PATCH 12/18] random: use all 64 bits of the seed Stephen Hemminger
2026-09-15 19:21 ` [PATCH 13/18] drivers/net: use secure random for RSS key Stephen Hemminger
2026-09-15 19:21 ` [PATCH 14/18] drivers/net: use common random MAC address helper Stephen Hemminger
2026-09-15 19:21 ` [PATCH 15/18] random: remove RDSEED fallback for initial seed Stephen Hemminger
2026-09-16 7:44 ` Bruce Richardson
2026-09-15 19:21 ` [PATCH 16/18] test: allow setting random seed for tests Stephen Hemminger
2026-09-15 19:21 ` [PATCH 17/18] test: add random number generator tests Stephen Hemminger
2026-09-15 19:21 ` [PATCH 18/18] doc: recommend secure random for unpredictable values Stephen Hemminger
2026-09-22 12:32 ` Aaron Conole
2026-10-10 2:27 ` [PATCH v2 0/5] random: fix initial seed entropy Stephen Hemminger
2026-10-10 2:27 ` [PATCH v2 1/5] eal/windows: add getentropy shim Stephen Hemminger
2026-10-10 2:27 ` [PATCH v2 2/5] random: always use getentropy Stephen Hemminger
2026-10-10 2:27 ` [PATCH v2 3/5] random: use all 64 bits of the seed Stephen Hemminger
2026-10-10 2:27 ` [PATCH v2 4/5] random: remove RDSEED fallback for initial seed Stephen Hemminger
2026-10-10 2:27 ` [PATCH v2 5/5] test: allow setting random seed for tests Stephen Hemminger
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260915192207.3926217-8-stephen@networkplumber.org \
--to=stephen@networkplumber.org \
--cc=bruce.richardson@intel.com \
--cc=dev@dpdk.org \
--cc=sameh.gobriel@intel.com \
--cc=vladimir.medvedkin@intel.com \
--cc=yipeng1.wang@intel.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox