Received: by 2002:a05:6358:16cc:b0:ea:6187:17c9 with SMTP id r12csp5372019rwl; Sun, 8 Jan 2023 13:57:59 -0800 (PST) X-Google-Smtp-Source: AMrXdXueauDz1RKQJ41lyhal867/kz6Z8J9d57kz2lGBEanqOfY5n8BV2sUokaBe2gt1xFjK1VWL X-Received: by 2002:a17:907:6e16:b0:7c0:9f6f:6d8 with SMTP id sd22-20020a1709076e1600b007c09f6f06d8mr69734595ejc.2.1673215079540; Sun, 08 Jan 2023 13:57:59 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1673215079; cv=none; d=google.com; s=arc-20160816; b=Do9yHtBXxKigCxWCoL2OUcUCt7Tey0vCuSPRIfztbJhE+hfBwjDZdxFzXmFYMYGKTY SrLwawKd9zztXfLkHLhkd4oTtzsnJQOAuoEBkW+2aWjUcM+hca026HyIMuBtWPxMettx omYnohQSDN8dDX+h7Ek8cJgdK6KiCTqIZ3pBvggp+mt1GY2QohMz9fIMYhyvU3teJuog 5Q1eZKUyFaQMjL5s/OVPaAzi5VrdI9/HBUJbMfATdWsrjfi5rPSJL6Egiompikb2UFyq cFL4UOww1NS+vwDGFO8lktIjrZFVCLrgqCg8TQGPC2U0oE1plUVp1ocV3NLApLiav95R 4fpA== 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:message-id:subject:cc:to :from:date; bh=XopVmPTTt95wHQ+mscc51mAD0MQarp9zoQicnSz3iQU=; b=n877jB7SmhhzvozdP7lJjurMWdB4Fvv3TkTcLKjLeDS+kvXiR4QTTlEuGeJtbIiRWR jPYgAcjaDfIFU39nM/Bqbfn2yIDgnwOqKJ6zg+1+jkHwRwEHPZIQ+txkoj5CVJvxX6yC QcoQa8IsbVqVsxAKFLyGku6qMh4we/dLGKmdx960k31J+Hckuu74B3XCzu5Tvrqdsoh5 cOumR+rQ6BBaMvx2LZqinRk7cw0QWdag0vU4wSO2dxkO6nSK3EFmmCVQEara2t29/p4t iqLWYrnn+Ns/zKeToZmVExqoAKpVJyRyFn6TEAodNFxyPdGpG0iJz/1MmbFzSGF9p1fE 1ABg== 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 dn14-20020a17090794ce00b007c11b897491si8620694ejc.833.2023.01.08.13.57.47; Sun, 08 Jan 2023 13:57:59 -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 S233795AbjAHV0Q (ORCPT + 51 others); Sun, 8 Jan 2023 16:26:16 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37836 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230211AbjAHV0O (ORCPT ); Sun, 8 Jan 2023 16:26:14 -0500 Received: from angie.orcam.me.uk (angie.orcam.me.uk [78.133.224.34]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id D09C7F5 for ; Sun, 8 Jan 2023 13:26:12 -0800 (PST) Received: by angie.orcam.me.uk (Postfix, from userid 500) id 9A06F92009C; Sun, 8 Jan 2023 22:26:11 +0100 (CET) Received: from localhost (localhost [127.0.0.1]) by angie.orcam.me.uk (Postfix) with ESMTP id 9436892009B; Sun, 8 Jan 2023 21:26:11 +0000 (GMT) Date: Sun, 8 Jan 2023 21:26:11 +0000 (GMT) From: "Maciej W. Rozycki" To: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , "H. Peter Anvin" cc: x86@kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v2] x86: Disable kernel stack offset randomization for !TSC Message-ID: 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,T_HDRS_LCASE 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 For x86 kernel stack offset randomization uses the RDTSC instruction, which causes an invalid opcode exception with hardware that does not implement this instruction: process '/sbin/init' started with executable stack invalid opcode: 0000 [#1] CPU: 0 PID: 1 Comm: init Not tainted 6.1.0-rc4+ #1 EIP: exit_to_user_mode_prepare+0x90/0xe1 Code: 30 02 00 75 ad 0f ba e3 16 73 05 e8 a7 a5 fc ff 0f ba e3 0e 73 05 e8 3e af fc ff a1 c4 c6 51 c0 85 c0 7e 13 8b 0d ac 01 53 c0 <0f> 31 0f b6 c0 31 c1 89 0d ac 01 53 c0 83 3d 30 ed 62 c0 00 75 33 EAX: 00000001 EBX: 00004000 ECX: 00000000 EDX: 000004ff ESI: c10253c0 EDI: 00000000 EBP: c1027f98 ESP: c1027f8c DS: 007b ES: 007b FS: 0000 GS: 0000 SS: 0068 EFLAGS: 00010002 CR0: 80050033 CR2: bfe8659b CR3: 012e0000 CR4: 00000000 Call Trace: ? rest_init+0x72/0x72 syscall_exit_to_user_mode+0x15/0x27 ret_from_fork+0x10/0x30 EIP: 0xb7f74800 Code: Unable to access opcode bytes at 0xb7f747d6. EAX: 00000000 EBX: 00000000 ECX: 00000000 EDX: 00000000 ESI: 00000000 EDI: 00000000 EBP: 00000000 ESP: bfe864b0 DS: 007b ES: 007b FS: 0000 GS: 0000 SS: 007b EFLAGS: 00000200 ---[ end trace 0000000000000000 ]--- EIP: exit_to_user_mode_prepare+0x90/0xe1 Code: 30 02 00 75 ad 0f ba e3 16 73 05 e8 a7 a5 fc ff 0f ba e3 0e 73 05 e8 3e af fc ff a1 c4 c6 51 c0 85 c0 7e 13 8b 0d ac 01 53 c0 <0f> 31 0f b6 c0 31 c1 89 0d ac 01 53 c0 83 3d 30 ed 62 c0 00 75 33 EAX: 00000001 EBX: 00004000 ECX: 00000000 EDX: 000004ff ESI: c10253c0 EDI: 00000000 EBP: c1027f98 ESP: c1027f8c DS: 007b ES: 007b FS: 0000 GS: 0000 SS: 0068 EFLAGS: 00010002 CR0: 80050033 CR2: b7f747d6 CR3: 012e0000 CR4: 00000000 Kernel panic - not syncing: Fatal exception Therefore do not use randomization where the CPU does not have the TSC feature. Signed-off-by: Maciej W. Rozycki --- Changes from v1: - Disable randomization at run time rather than in configuration. --- arch/x86/include/asm/entry-common.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) linux-x86-randomize-kstack-offset-tsc.diff 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 #include +#include #include #include #include @@ -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); } #define arch_exit_to_user_mode_prepare arch_exit_to_user_mode_prepare