All of lore.kernel.org
 help / color / mirror / Atom feed
From: "H. Peter Anvin" <hpa@zytor.com>
To: "Maciej W. Rozycki" <macro@orcam.me.uk>,
	"Jason A. Donenfeld" <Jason@zx2c4.com>
Cc: Thomas Gleixner <tglx@linutronix.de>,
	Ingo Molnar <mingo@redhat.com>, Borislav Petkov <bp@alien8.de>,
	Dave Hansen <dave.hansen@linux.intel.com>,
	x86@kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH v2] x86: Disable kernel stack offset randomization for !TSC
Date: Wed, 11 Jan 2023 17:53:37 -0800	[thread overview]
Message-ID: <B71587C5-21E8-4F7C-94FB-92E2AA9F840A@zytor.com> (raw)
In-Reply-To: <alpine.DEB.2.21.2301120129020.65308@angie.orcam.me.uk>

On January 11, 2023 5:34:29 PM PST, "Maciej W. Rozycki" <macro@orcam.me.uk> wrote:
>On Tue, 10 Jan 2023, Jason A. Donenfeld wrote:
>
>> > Index: linux-macro/arch/x86/include/asm/entry-common.h
>> > ===================================================================
>> > --- linux-macro.orig/arch/x86/include/asm/entry-common.h
>> > +++ linux-macro/arch/x86/include/asm/entry-common.h
>> > @@ -5,6 +5,7 @@
>> >  #include <linux/randomize_kstack.h>
>> >  #include <linux/user-return-notifier.h>
>> >  
>> > +#include <asm/cpufeature.h>
>> >  #include <asm/nospec-branch.h>
>> >  #include <asm/io_bitmap.h>
>> >  #include <asm/fpu/api.h>
>> > @@ -85,7 +86,8 @@ static inline void arch_exit_to_user_mod
>> >  	 * Therefore, final stack offset entropy will be 5 (x86_64) or
>> >  	 * 6 (ia32) bits.
>> >  	 */
>> > -	choose_random_kstack_offset(rdtsc() & 0xFF);
>> > +	if (cpu_feature_enabled(X86_FEATURE_TSC))
>> > +		choose_random_kstack_offset(rdtsc() & 0xFF);
>> 
>> What would happen if you just called `get_random_u8()` here?
>
> Thank you for your input.  I've had a look at the function and it seems a 
>bit heavyweight compared to a mere single CPU instruction, but I guess why 
>not.  Do you have any performance figures (in terms of CPU cycles) for the 
>usual cases?  Offhand I'm not sure how I could benchmark it myself.
>
> I have made a patch and of course it makes the system boot too, although 
>it's not clear to me how I can actually verify randomisation works.  I can 
>assume it does I suppose.
>
>  Maciej

Not to mention that we could use rdrand here if it is available (although it is slower than rdtsc.)

RDTSC isn't a super fast instruction either, but what is *way* more significant is that this use of RDTSC is NOT safe: in certain power states it may very well be that stone number of lower bits of TSC contain no entropy at all.

At the very least one should do a rotating multiply with a large (32-bit) prime number.

  reply	other threads:[~2023-01-12  1:54 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-01-08 21:26 [PATCH v2] x86: Disable kernel stack offset randomization for !TSC Maciej W. Rozycki
2023-01-09 10:40 ` Ingo Molnar
2023-01-09 22:53   ` Maciej W. Rozycki
2023-01-10 10:47     ` Ingo Molnar
2023-01-10 13:56       ` David Laight
2023-01-10 15:19 ` Jason A. Donenfeld
2023-01-12  1:34   ` Maciej W. Rozycki
2023-01-12  1:53     ` H. Peter Anvin [this message]
2023-01-12 11:30       ` Borislav Petkov
2023-01-12 11:58         ` Maciej W. Rozycki
2023-01-30 20:43       ` Maciej W. Rozycki
2023-01-13 15:33     ` Jason A. Donenfeld
2023-01-30 20:43       ` Maciej W. Rozycki

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=B71587C5-21E8-4F7C-94FB-92E2AA9F840A@zytor.com \
    --to=hpa@zytor.com \
    --cc=Jason@zx2c4.com \
    --cc=bp@alien8.de \
    --cc=dave.hansen@linux.intel.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=macro@orcam.me.uk \
    --cc=mingo@redhat.com \
    --cc=tglx@linutronix.de \
    --cc=x86@kernel.org \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.