Received: by 2002:a05:6358:16cc:b0:ea:6187:17c9 with SMTP id r12csp6991561rwl; Mon, 9 Jan 2023 16:18:01 -0800 (PST) X-Google-Smtp-Source: AMrXdXsDyhRjm0MEmx5ENZuxUm8BDh2h+/LbxzqsstcLAktOTJQsBeUljWb8eY+wQpLeFqULkPlS X-Received: by 2002:a17:907:1b0b:b0:7c1:6344:84a with SMTP id mp11-20020a1709071b0b00b007c16344084amr90476688ejc.5.1673309881475; Mon, 09 Jan 2023 16:18:01 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1673309881; cv=none; d=google.com; s=arc-20160816; b=nsNQ6HfvmjAVH3KZ0u0jK+yxG2JdaD/cK1T6zNLICYvwNH1oyHH9xF76SnR8i/YpkT Lr0cUr+epnem6ZaDX5kzJnEUKqOyHfcD+6HBW10va0V4lm9qiHJLSTTQcPmW9QqW0ZXs xfV8ArIwtZuVVp2wZoirriuenPswVoZj22ZXdOkb5F8dgGxtBlkCSrqgyOaUDjJu845m 8PRTNgGlwPYpW126yI0OLSot8zNrjKafG3FAtq5nlWIiUjIGnZ8e8u6QvHsnUu/6sKwQ vUduNx1mamOUAh9rsNswPrdUd8IrHpk5PS+7dmABY4h4AcDez3hoNbtrwlzbEezn4xvC uvYA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:user-agent:references:message-id :in-reply-to:subject:cc:to:from:date; bh=judeXvpOaWCESbavM/aBGTHlHAkX3JU7jJw0cXKcmUE=; b=bUd2hgXT6HTlO7qQVThohS4E0SB075utV9XgtHm1oYZJFJqE2kXQigLBjuariDy/N7 sHWaTn+IAL+weLYyJN2rtk+pC+QH63vrkVlX1y/cO/H5ZsmSfR5wSfEldgao5dZKuz4F +t1FvJYp3BxIRbad3ff9mVMEr2fd63koaBBnlrujSuvesUdOtuum03q5LF5WMwzM97yw s0feG0cBiKHIyQAtVfSqqroNzkh09xB15Ts9pcE+vnEX9hPGRWakbrSIG7nXVyxwj8rw xWlpoxabHHm9TGA3EQsAUu3iMwY5XOPHvw+Y8DQXCzBGzEmtmgYXB6cVhMH82B+L/TV1 ndjQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id ji21-20020a170907981500b007c14a639de3si10837545ejc.480.2023.01.09.16.17.48; Mon, 09 Jan 2023 16:18:01 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237260AbjAIWxT (ORCPT + 53 others); Mon, 9 Jan 2023 17:53:19 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36660 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237536AbjAIWxP (ORCPT ); Mon, 9 Jan 2023 17:53:15 -0500 Received: from angie.orcam.me.uk (angie.orcam.me.uk [78.133.224.34]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id BBAD9140F5 for ; Mon, 9 Jan 2023 14:53:14 -0800 (PST) Received: by angie.orcam.me.uk (Postfix, from userid 500) id 330CD92009D; Mon, 9 Jan 2023 23:53:12 +0100 (CET) Received: from localhost (localhost [127.0.0.1]) by angie.orcam.me.uk (Postfix) with ESMTP id 2FB4B92009C; Mon, 9 Jan 2023 22:53:12 +0000 (GMT) Date: Mon, 9 Jan 2023 22:53:12 +0000 (GMT) From: "Maciej W. Rozycki" To: "Jason A. Donenfeld" , Ingo Molnar cc: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , "H. Peter Anvin" , x86@kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH v2] x86: Disable kernel stack offset randomization for !TSC In-Reply-To: Message-ID: References: User-Agent: Alpine 2.21 (DEB 202 2017-01-01) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,SPF_HELO_NONE, SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Jason, Would you mind commenting on the below? On Mon, 9 Jan 2023, Ingo Molnar wrote: > > For x86 kernel stack offset randomization uses the RDTSC instruction, > > which causes an invalid opcode exception with hardware that does not > > implement this instruction: > > > @@ -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); > > } > > While this is an obscure corner case, falling back to 0 offset silently > feels a bit wrong - could we at least attempt to generate some > unpredictability in this case? > > It's not genuine entropy, but we could pass in a value that varies from > task to task and which is not an 'obviously known' constant value like the > 0 fallback? > > For example the lowest 8 bits of the virtual page number of the current > task plus the lowest 8 bits of jiffies should vary from task to task, has > some time dependence and is cheap to compute: > > (((unsigned long)current >> 12) + jiffies) & 0xFF > > This combined with the per-CPU forward storage of previous offsets: > > #define choose_random_kstack_offset(rand) do { \ > if (static_branch_maybe(CONFIG_RANDOMIZE_KSTACK_OFFSET_DEFAULT, \ > &randomize_kstack_offset)) { \ > u32 offset = raw_cpu_read(kstack_offset); \ > offset ^= (rand); \ > raw_cpu_write(kstack_offset, offset); \ > } \ > > Should make this reasonably hard to guess for long-running tasks even if > there's no TSC - and make it hard to guess even for tasks whose creation an > attacker controls, unless there's an info-leak to rely on. Sure, I'm fine implementing it, even in such a way so as not to cause a code size/performance regression for X86_TSC configurations. But is the calculation really unpredictable enough? I don't feel competent enough to decide. Jason, what do you think? Maciej