Received: by 2002:a05:7412:f589:b0:e2:908c:2ebd with SMTP id eh9csp719593rdb; Tue, 31 Oct 2023 23:46:18 -0700 (PDT) X-Google-Smtp-Source: AGHT+IENVxmi5CZnGrK7vn5qJTOF2Nk+CBXmMhKu2KiRZHvq/CH9DINmTJunpLB9bhHThY6Xn+sx X-Received: by 2002:a17:902:ce8f:b0:1cc:467a:338f with SMTP id f15-20020a170902ce8f00b001cc467a338fmr6832886plg.4.1698821178523; Tue, 31 Oct 2023 23:46:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698821178; cv=none; d=google.com; s=arc-20160816; b=U/HMyAm+N8A6aLPrdXEehs7qvuI7emd4BaKD4l4q8w+Py/rghey7001yfvJC6G2s+7 cmK7DLCSyi6r9ad+t9KpkU8y9zwvnKuwL8e70B6nNUN9cgxA8LYgDe+FUEWEwQepr+Az rXCvCVEhn9qY9R551+1JAq4IMnWi3W7N4PtPIddYtu7b8wHwt9HkroESBJyu0eXtrdep jqavhqa/8CLJ4i1WO6lfl8v4ZkvvGPw2wQhhTVHwJOqQ5DHl30yAsDhqTZmb6C1HPhar +ljrcJVDX894Vb8a3i1sGPOKgZdeBfyvz4Q+cANk6CSTOd1l6sHXD2NLwDcjIdBlsKLM Q/JQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:feedback-id:content-transfer-encoding :mime-version:message-id:date:subject:cc:to:from; bh=YdTopmSrV4DA4qzNviZdAVPvlsua/k4xrba0BfZpxYs=; fh=pH4CTAF1ADkgGk/ceXDnZ1VQxVwQaG3fHsdH5SPONdk=; b=HudJ25qpIJwtYNdfxcz7Qa2DxZO+mE8dmePe1evap1pT25Ss5QexyYlzbCuk/I7E1Y NX0Kfsiclqz/sDDAthWBhtjM3kbmYld7DgLaHA+p5xVLvRBDurQ53k9bwr7ZpwaixL1s iIT6bqnWgmq/WP5qeyeL+VWOATiJm+ahD4NfMKfrUSqhCnR7aoq6b8wA8P4dzSU3Og6H vvaa/VyhFadmHO4r9R/xnTPN/1gKkNMqRNL7WLKatdqUjJLhLjScFAaoEK9iH7+xBYtw MvipR2eHHHqmVl1h/UGMrb1OPh2UJEoZnb7OkxsGLHDb0K2gUr1oRKvl/lJsNN643UhZ MbZA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from howler.vger.email (howler.vger.email. [2620:137:e000::3:4]) by mx.google.com with ESMTPS id ky6-20020a170902f98600b001c9b15bf936si2274725plb.220.2023.10.31.23.46.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 31 Oct 2023 23:46:18 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) client-ip=2620:137:e000::3:4; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by howler.vger.email (Postfix) with ESMTP id 838088022B70; Tue, 31 Oct 2023 23:46:12 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at howler.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1343937AbjKAGpq (ORCPT + 99 others); Wed, 1 Nov 2023 02:45:46 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44012 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1343684AbjKAGpo (ORCPT ); Wed, 1 Nov 2023 02:45:44 -0400 Received: from bg4.exmail.qq.com (bg4.exmail.qq.com [43.155.65.254]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 18B1E103; Tue, 31 Oct 2023 23:45:37 -0700 (PDT) X-QQ-mid: bizesmtp68t1698821091tvb7u4r1 Received: from localhost.localdomain ( [58.240.82.166]) by bizesmtp.qq.com (ESMTP) with id ; Wed, 01 Nov 2023 14:44:48 +0800 (CST) X-QQ-SSF: 01200000000000B0B000000A0000000 X-QQ-FEAT: 3M0okmaRx3gxeEyH4BlqDeuloEyl32NdDWDCAwDSyK64HTzX/3liUnbfNDiz+ sZcpbjY9p5B/dZFk8H1lKOIoJPxcVegVfzk/66o6v+sV5lnjG+djsJVpkmIsWpTilez6PcS wG3rH0s0qeISm34gMMW7Go0eab9EqbwaAlsjo94pCL5VpmuSmwp2+iwsJ5sRc0YjK2EEyDN 4R+SsAo1eIxe5XgTejVkfuUJ6+ZT1PHSIaZss+kefmd4Xf9X2WUwuwkgOX38HUg15MtAUpP PZf2IJt4k0wS46++jcBsUH9D7+VWfWyVlvdL17lk8A2jZTCExwkSVprmWwImL8NK2Ejo5ZT kmTeZiNuWGehdme6164H/Yg2vxnQE5o7szoeegp X-QQ-GoodBg: 0 X-BIZMAIL-ID: 1492668164630438734 From: Song Shuai To: paul.walmsley@sifive.com, palmer@dabbelt.com, aou@eecs.berkeley.edu, keescook@chromium.org, guoren@kernel.org, bjorn@rivosinc.com, jszhang@kernel.org, conor.dooley@microchip.com, andy.chiu@sifive.com, samitolvanen@google.com, songshuaishuai@tinylab.org, coelacanthushex@gmail.com Cc: linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org Subject: [PATCH] riscv: Support RANDOMIZE_KSTACK_OFFSET Date: Wed, 1 Nov 2023 14:44:23 +0800 Message-Id: <20231101064423.1906122-1-songshuaishuai@tinylab.org> X-Mailer: git-send-email 2.20.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-QQ-SENDSIZE: 520 Feedback-ID: bizesmtp:tinylab.org:qybglogicsvrsz:qybglogicsvrsz4a-0 X-Spam-Status: No, score=-0.8 required=5.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE, URIBL_BLOCKED autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on howler.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (howler.vger.email [0.0.0.0]); Tue, 31 Oct 2023 23:46:12 -0700 (PDT) Inspired from arm64's implement -- commit 70918779aec9 ("arm64: entry: Enable random_kstack_offset support") Add support of kernel stack offset randomization while handling syscall, the offset is defaultly limited by KSTACK_OFFSET_MAX() (i.e. 10 bits). In order to avoid trigger stack canaries (due to __builtin_alloca) and slowing down the entry path, use __no_stack_protector attribute to disable stack protector for do_trap_ecall_u() at the function level. Signed-off-by: Song Shuai --- Testing with randomize_kstack_offset=y cmdline, lkdtm/stack-entropy.sh showed appropriate stack offset instead of zero. --- arch/riscv/Kconfig | 1 + arch/riscv/kernel/traps.c | 18 +++++++++++++++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/arch/riscv/Kconfig b/arch/riscv/Kconfig index d607ab0f7c6d..0e843de33f0c 100644 --- a/arch/riscv/Kconfig +++ b/arch/riscv/Kconfig @@ -100,6 +100,7 @@ config RISCV select HAVE_ARCH_KGDB_QXFER_PKT select HAVE_ARCH_MMAP_RND_BITS if MMU select HAVE_ARCH_MMAP_RND_COMPAT_BITS if COMPAT + select HAVE_ARCH_RANDOMIZE_KSTACK_OFFSET select HAVE_ARCH_SECCOMP_FILTER select HAVE_ARCH_THREAD_STRUCT_WHITELIST select HAVE_ARCH_TRACEHOOK diff --git a/arch/riscv/kernel/traps.c b/arch/riscv/kernel/traps.c index 19807c4d3805..3f869b2d47c3 100644 --- a/arch/riscv/kernel/traps.c +++ b/arch/riscv/kernel/traps.c @@ -6,6 +6,7 @@ #include #include #include +#include #include #include #include @@ -296,9 +297,11 @@ asmlinkage __visible __trap_section void do_trap_break(struct pt_regs *regs) } } -asmlinkage __visible __trap_section void do_trap_ecall_u(struct pt_regs *regs) +asmlinkage __visible __trap_section __no_stack_protector +void do_trap_ecall_u(struct pt_regs *regs) { if (user_mode(regs)) { + long syscall = regs->a7; regs->epc += 4; @@ -308,10 +311,23 @@ asmlinkage __visible __trap_section void do_trap_ecall_u(struct pt_regs *regs) syscall = syscall_enter_from_user_mode(regs, syscall); + add_random_kstack_offset(); + if (syscall >= 0 && syscall < NR_syscalls) syscall_handler(regs, syscall); else if (syscall != -1) regs->a0 = -ENOSYS; + /* + * Ultimately, this value will get limited by KSTACK_OFFSET_MAX(), + * so the maximum stack offset is 1k bytes (10 bits). + * + * The actual entropy will be further reduced by the compiler when + * applying stack alignment constraints: 16-byte (i.e. 4-bit) aligned + * for RV32I or RV64I. + * + * The resulting 6 bits of entropy is seen in SP[9:4]. + */ + choose_random_kstack_offset(get_random_u16()); syscall_exit_to_user_mode(regs); } else { -- 2.20.1