From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from dpdk.org (dpdk.org [92.243.14.124]) by dpdk.space (Postfix) with ESMTP id F0E06A00E6 for ; Fri, 17 May 2019 21:28:02 +0200 (CEST) Received: from [92.243.14.124] (localhost [127.0.0.1]) by dpdk.org (Postfix) with ESMTP id 15E342BCE; Fri, 17 May 2019 21:28:02 +0200 (CEST) Received: from smtp.tuxdriver.com (charlotte.tuxdriver.com [70.61.120.58]) by dpdk.org (Postfix) with ESMTP id 4B4352BB8 for ; Fri, 17 May 2019 21:28:00 +0200 (CEST) Received: from cpe-2606-a000-111b-405a-0-0-0-162e.dyn6.twc.com ([2606:a000:111b:405a::162e] helo=localhost) by smtp.tuxdriver.com with esmtpsa (TLSv1:AES256-SHA:256) (Exim 4.63) (envelope-from ) id 1hRiWI-0004mU-43; Fri, 17 May 2019 15:27:57 -0400 Date: Fri, 17 May 2019 15:27:25 -0400 From: Neil Horman To: Mattias =?iso-8859-1?Q?R=F6nnblom?= Cc: dev@dpdk.org, stephen@networkplumber.org, david.marchand@redhat.com, bruce.richardson@intel.com Message-ID: <20190517192725.GA4077@hmswarspite.think-freely.org> References: <20190508181014.7dde7580@xps13> <20190514092046.30808-1-mattias.ronnblom@ericsson.com> <20190514092046.30808-2-mattias.ronnblom@ericsson.com> <20190514141615.GA12163@hmswarspite.think-freely.org> <8522b2d7-8361-3e34-7750-88b66d61b98a@ericsson.com> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <8522b2d7-8361-3e34-7750-88b66d61b98a@ericsson.com> User-Agent: Mutt/1.11.3 (2019-02-01) X-Spam-Score: -2.9 (--) X-Spam-Status: No Subject: Re: [dpdk-dev] [PATCH 1/6] eal: replace libc-based random number generation with LFSR X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org Sender: "dev" On Tue, May 14, 2019 at 04:53:08PM +0200, Mattias Rönnblom wrote: > On 2019-05-14 16:16, Neil Horman wrote: > > On Tue, May 14, 2019 at 11:20:41AM +0200, Mattias Rönnblom wrote: > > > This commit replaces rte_rand()'s use of lrand48() with a DPDK-native > > > combined Linear Feedback Shift Register (LFSR) (also known as > > > Tausworthe) pseudo-random number generator. > > > > > > This generator is faster and produces better-quality random numbers > > > than the linear congruential generator (LCG) of lib's lrand48(). The > > > implementation, as opposed to lrand48(), is multi-thread safe in > > > regards to concurrent rte_rand() calls from different lcore threads. > > > A LCG is still used, but only to seed the five per-lcore LFSR > > > sequences. > > > > > > In addition, this patch also addresses the issue of the legacy > > > implementation only producing 62 bits of pseudo randomness, while the > > > API requires all 64 bits to be random. > > > > > > This pseudo-random number generator is not cryptographically secure - > > > just like lrand48(). > > > > > > Bugzilla ID: 114 > > > Bugzilla ID: 276 > > > > > > Signed-off-by: Mattias Rönnblom > > > --- > > > lib/librte_eal/common/include/rte_random.h | 29 ++--- > > > lib/librte_eal/common/meson.build | 1 + > > > lib/librte_eal/common/rte_random.c | 139 +++++++++++++++++++++ > > > lib/librte_eal/freebsd/eal/Makefile | 1 + > > > lib/librte_eal/freebsd/eal/eal.c | 2 - > > > lib/librte_eal/linux/eal/Makefile | 1 + > > > lib/librte_eal/linux/eal/eal.c | 2 - > > > lib/librte_eal/rte_eal_version.map | 8 ++ > > > 8 files changed, 161 insertions(+), 22 deletions(-) > > > create mode 100644 lib/librte_eal/common/rte_random.c > > > > > > diff --git a/lib/librte_eal/common/include/rte_random.h b/lib/librte_eal/common/include/rte_random.h > > > index b2ca1c209..66dfe8ae7 100644 > > > --- a/lib/librte_eal/common/include/rte_random.h > > > +++ b/lib/librte_eal/common/include/rte_random.h > > > @@ -16,7 +16,6 @@ extern "C" { > > > #endif > > > > > > #include > > > -#include > > > > > > /** > > > * Seed the pseudo-random generator. > > > @@ -25,34 +24,28 @@ extern "C" { > > > * value. It may need to be re-seeded by the user with a real random > > > * value. > > > * > > > + * This function is not multi-thread safe in regards to other > > > + * rte_srand() calls, nor is it in relation to concurrent rte_rand() > > > + * calls. > > > + * > > > * @param seedval > > > * The value of the seed. > > > */ > > > -static inline void > > > -rte_srand(uint64_t seedval) > > > -{ > > > - srand48((long)seedval); > > > -} > > > +void > > > +rte_srand(uint64_t seedval); > > > > > > /** > > > * Get a pseudo-random value. > > > * > > > - * This function generates pseudo-random numbers using the linear > > > - * congruential algorithm and 48-bit integer arithmetic, called twice > > > - * to generate a 64-bit value. > > > + * The generator is not cryptographically secure. > > > + * > > > + * If called from lcore threads, this function is thread-safe. > > > * > > > * @return > > > * A pseudo-random value between 0 and (1<<64)-1. > > > */ > > > -static inline uint64_t > > > -rte_rand(void) > > > -{ > > > - uint64_t val; > > > - val = (uint64_t)lrand48(); > > > - val <<= 32; > > > - val += (uint64_t)lrand48(); > > > - return val; > > > -} > > > +uint64_t > > > +rte_rand(void); > > > > > > #ifdef __cplusplus > > > } > > > diff --git a/lib/librte_eal/common/meson.build b/lib/librte_eal/common/meson.build > > > index 0670e4102..bafd23207 100644 > > > --- a/lib/librte_eal/common/meson.build > > > +++ b/lib/librte_eal/common/meson.build > > > @@ -35,6 +35,7 @@ common_sources = files( > > > 'rte_keepalive.c', > > > 'rte_malloc.c', > > > 'rte_option.c', > > > + 'rte_random.c', > > > 'rte_reciprocal.c', > > > 'rte_service.c' > > > ) > > > diff --git a/lib/librte_eal/common/rte_random.c b/lib/librte_eal/common/rte_random.c > > > new file mode 100644 > > > index 000000000..4d3cf5226 > > > --- /dev/null > > > +++ b/lib/librte_eal/common/rte_random.c > > > @@ -0,0 +1,139 @@ > > > +/* SPDX-License-Identifier: BSD-3-Clause > > > + * Copyright(c) 2019 Ericsson AB > > > + */ > > > + > > > +#include > > > + > > > +#include > > > +#include > > > +#include > > > +#include > > > +#include > > > +#include > > > + > > > +struct rte_rand_state { > > > + uint64_t z1; > > > + uint64_t z2; > > > + uint64_t z3; > > > + uint64_t z4; > > > + uint64_t z5; > > > +} __rte_cache_aligned; > > > + > > > +static struct rte_rand_state rand_states[RTE_MAX_LCORE]; > > > + > > > > It just occured to me that this variable embodies all the state of the rng. > > Whats to stop an attacker from digging through ram to get this info and > > predicting what the output will be? I understand that this rng probably > > shouldn't be considered secure for cryptographic use, but it is used in the > > ipsec test example, so it could be mistaken for an rng that is. > > > > rte_rand() was never safe for use in cryptography, and now it's spelled out > in the API documentation. > > If the IPsec example uses rte_rand() for anything security-related, it's a > bug or at least should be accompanied by a comment, in my opinion. > I would agree with that, but the fact remains that the examples use rte_rand in a context that is explicitly in violation of what you are documenting, which certainly seems confusing. > That said, if you have an attacker who's already broken into your DPDK > process' virtual memory, I'm not sure I understand why he would care much > about the state of the PRNG. Wouldn't he just read your private keys, your > messages in clear text or whatever other secrets you might have in memory? > Because the term "Breaking in" is a misnomer here. In unix system, IIRC, global variables are shared accross processes. Breaking in to get this information is as simple as writing a program that links against the dpdk shared library, or just dlopens it and looks up the requisite symbol address. And while thats true of any symbol, given the prior example which uses rte_rand for a cryptographic purpose, it seems like this would be a particularly tempting target to exploit. Either way, asserting that its fine to do this because any method that gives you access to it gives you access to other information is really not a great argument. We should protect application data wherever we can, and the integrity of the randomness of data seems like a case in point. Neil