From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753590AbdHORhR (ORCPT ); Tue, 15 Aug 2017 13:37:17 -0400 Received: from Galois.linutronix.de ([146.0.238.70]:54583 "EHLO Galois.linutronix.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752412AbdHORhQ (ORCPT ); Tue, 15 Aug 2017 13:37:16 -0400 Date: Tue, 15 Aug 2017 19:37:05 +0200 (CEST) From: Thomas Gleixner To: "Theodore Ts'o" cc: Borislav Petkov , Ingo Molnar , Willy Tarreau , Linus Torvalds , x86-ml , "Jason A. Donenfeld" , lkml , Peter Zijlstra , Nicholas Mc Guire Subject: Re: early x86 unseeded randomness In-Reply-To: Message-ID: References: <20170814180048.m3igiaiunlyb5wur@pd.tnic> <20170814190013.zixopgjyq26ukxcj@pd.tnic> <20170815013124.2afytkibspxrikdn@thunk.org> <20170815064437.GA1986@1wt.eu> <20170815074254.6byayhspc5tdtjb5@gmail.com> <20170815134514.r6qjotjgfmurwh64@pd.tnic> <20170815142528.ne5fewtrnazmjzt6@thunk.org> User-Agent: Alpine 2.20 (DEB 67 2015-01-07) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, 15 Aug 2017, Thomas Gleixner wrote: > On Tue, 15 Aug 2017, Theodore Ts'o wrote: > > On Tue, Aug 15, 2017 at 03:48:18PM +0200, Thomas Gleixner wrote: > > > > > +u64 __init tsc_early_random(void) > > > > > +{ > > > > > + u64 uninitialized_var(res); > > > > > + int i; > > > > > + > > > > > + if (!boot_cpu_has(X86_FEATURE_TSC)) > > > > > + return res; > > > > > + > > > > > + res ^= rdtsc(); > > > > > + for (i = 0; i < BITS_PER_LONG; i++) { > > > > > + res ^= ((rdtsc() & 0x04) >> 2) << i; > > > > > + udelay(2); > > > > > + } > > > > > + return res; > > > > > +} > > > > Reasons why this is probably not the best idea: > > > > 1) Exactly how udelay is implemented varies from architecture to > > architecture and in some cases is different on a subarchitectural > > level. Some of them rely on reading the TSC; others rely on > > operations that will have a constant number of CPU cycles (e.g., they > > aren't doing much if any operations that might even have a tiny > > glimmer of hope of adding unpredictability). > > That's not really true. You can add random shite instead of udelay(2). The > point of this exercise is to somewhat utilize the instruction pipeline, > which causes the TSC readouts to be not even spread over a the loop and > therefor yield random results. Talking about random shite: memset(foo, 0, sizeof(foo)); res ^= rdtsc(); for (i = 0; i < BITS_PER_LONG; i++) { /* Will never happen ... */ if (memchr_inv(foo, i, sizeof(foo))) continue; res ^= ((rdtsc() & 0x04) >> 2) << i; memset(foo, i, sizeof(foo)); wbinvd(); } return res; That exploits the fact that the CPU and caches run at a different non synchronized clock than the memory controller and therefore the execution time for both the wbinvd() and the memchr_inv() measured in TSC cycles is non constant and random enough for the early boot randomization. Thanks, tglx