Received: by 2002:a05:6902:102b:0:0:0:0 with SMTP id x11csp669274ybt; Fri, 10 Jul 2020 09:22:59 -0700 (PDT) X-Google-Smtp-Source: ABdhPJysOmwetSlVhfTQD6fE/nK8uELO/860a3H12y0zYIB3edDoemgKhsetAQCtBDlihCLAGR0o X-Received: by 2002:a50:fd12:: with SMTP id i18mr80131497eds.371.1594398179400; Fri, 10 Jul 2020 09:22:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1594398179; cv=none; d=google.com; s=arc-20160816; b=msh1Fc17182H6pTF3vBYVwXM1CoHaB0Cjmsi1gaj7mjBaeG3ztPArKbC7xLEgLJwxN 5UMIo4g7q5QnVM09iPWl1vgcALyQEIRnilnXE/NlN6tiaQBmIoH8BdEFEqlv0zTbiShD zKOFMHM1EVW7ae1BF05dejH/wSONMgTVIlEhnN5nRi5ts4RgTUiTnvTeOt1VtgH5mj4/ nEj6Cet+vQU/744X42yDyi4VaFVUDsb2jIXZATEitDj2ScGzJtrKW/tYvNPajdUlxQ8x UqyWef1DOY43kVTJUXy3S+Z0uZnz1J/MTl1LyAaS//zfuolM42gNGK1U18c+Aj2cW82b UHog== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=hLi4AHR95MboOC3Pou9TxbMJJpZ7WwsXFl8hUnZwSS8=; b=MAJbb2qR9Ke/fWqSBm22Nxb1s78yppF9TNfHZ0v3cCIaOd6jyo6O+MJfEPkych/esD gWylbNgTGx2/57qHrPjNCqntEQV/pk4m+2suSTMF+4+QOhQ2MQUV1sIZBKek/8HBggUU is3vMJ2XmBGE+nfxGw01j/1wEG8YpwQauiBtNhgYmGj+4Scz0P6exLc/3R+HkPEgyzPS VHt2QUwzD6eZck1e7eajWzkCoKfObM5XOvjbvq0/iN7lJH0I/llj0V2BT61OTOc+b40E zUlfJW4D0MRiTEu6zKigZ6dy5DmduPT2IkoqXDbqtnKw04+delJic7LSDWh8aKEV+9Qc 3/ng== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b="P4/zkk+O"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id p13si4210265edh.504.2020.07.10.09.22.35; Fri, 10 Jul 2020 09:22:59 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b="P4/zkk+O"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728013AbgGJQU0 (ORCPT + 99 others); Fri, 10 Jul 2020 12:20:26 -0400 Received: from mail.kernel.org ([198.145.29.99]:44376 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726820AbgGJQU0 (ORCPT ); Fri, 10 Jul 2020 12:20:26 -0400 Received: from localhost.localdomain (unknown [89.208.247.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 320D420657; Fri, 10 Jul 2020 16:20:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1594398025; bh=ERGigc8QaW07BGajQpKa6kuFdOuBQHa4YxZRxHxyvG4=; h=From:To:Cc:Subject:Date:From; b=P4/zkk+OTc+xvnY6KxP1vHRODZoVLxJJn16uHU7qWli0vWQntfnTEIqIzqPCR2KIm QYTPjJt0sRKFVlTA+hOrmHFUDBcjDujpl8rekHxeXG7nIkAsM4sTSMLqSTvjihJXrs pvZu1XPk4TCvLB4LlpctImEuS5FEGfvJ9IH+aw4M= From: guoren@kernel.org To: palmerdabbelt@google.com, paul.walmsley@sifive.com, anup@brainfault.org, greentime.hu@sifive.com, zong.li@sifive.com, keescook@chromium.org, bjorn.topel@gmail.com, atish.patra@wdc.com, cooper.qu@linux.alibaba.com Cc: linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, linux-csky@vger.kernel.org, guoren@kernel.org, Guo Ren , Albert Ou , Masami Hiramatsu , Greentime Hu Subject: [PATCH v3 1/2] riscv: Add STACKPROTECTOR supported Date: Fri, 10 Jul 2020 16:19:57 +0000 Message-Id: <1594397998-10221-1-git-send-email-guoren@kernel.org> X-Mailer: git-send-email 2.7.4 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Guo Ren The -fstack-protector & -fstack-protector-strong features are from gcc. The patch only add basic kernel support to stack-protector feature and some arch could have its own solution such as ARM64_PTR_AUTH. After enabling STACKPROTECTOR and STACKPROTECTOR_STRONG, the .text size is expanded from 0x7de066 to 0x81fb32 (only 5%) to add canary checking code. Signed-off-by: Guo Ren Reviewed-by: Kees Cook Cc: Paul Walmsley Cc: Palmer Dabbelt Cc: Albert Ou Cc: Masami Hiramatsu Cc: Björn Töpel Cc: Greentime Hu Cc: Atish Patra --- Change v2: - Fixup rsicv32 compile warning Signed-off-by: Guo Ren --- arch/riscv/Kconfig | 1 + arch/riscv/include/asm/stackprotector.h | 33 +++++++++++++++++++++++++++++++++ arch/riscv/kernel/process.c | 6 ++++++ 3 files changed, 40 insertions(+) create mode 100644 arch/riscv/include/asm/stackprotector.h diff --git a/arch/riscv/Kconfig b/arch/riscv/Kconfig index f927a91..4b0e308 100644 --- a/arch/riscv/Kconfig +++ b/arch/riscv/Kconfig @@ -63,6 +63,7 @@ config RISCV select HAVE_PERF_EVENTS select HAVE_PERF_REGS select HAVE_PERF_USER_STACK_DUMP + select HAVE_STACKPROTECTOR select HAVE_SYSCALL_TRACEPOINTS select IRQ_DOMAIN select MODULES_USE_ELF_RELA if MODULES diff --git a/arch/riscv/include/asm/stackprotector.h b/arch/riscv/include/asm/stackprotector.h new file mode 100644 index 00000000..d95f7b2 --- /dev/null +++ b/arch/riscv/include/asm/stackprotector.h @@ -0,0 +1,33 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +#ifndef _ASM_RISCV_STACKPROTECTOR_H +#define _ASM_RISCV_STACKPROTECTOR_H + +#include +#include +#include + +extern unsigned long __stack_chk_guard; + +/* + * Initialize the stackprotector canary value. + * + * NOTE: this must only be called from functions that never return, + * and it must always be inlined. + */ +static __always_inline void boot_init_stack_canary(void) +{ + unsigned long canary; + unsigned long tsc; + + /* Try to get a semi random initial value. */ + get_random_bytes(&canary, sizeof(canary)); + tsc = get_cycles(); + canary += tsc + (tsc << BITS_PER_LONG/2); + canary ^= LINUX_VERSION_CODE; + canary &= CANARY_MASK; + + current->stack_canary = canary; + __stack_chk_guard = current->stack_canary; +} +#endif /* _ASM_RISCV_STACKPROTECTOR_H */ diff --git a/arch/riscv/kernel/process.c b/arch/riscv/kernel/process.c index 824d117..6548929 100644 --- a/arch/riscv/kernel/process.c +++ b/arch/riscv/kernel/process.c @@ -24,6 +24,12 @@ register unsigned long gp_in_global __asm__("gp"); +#ifdef CONFIG_STACKPROTECTOR +#include +unsigned long __stack_chk_guard __read_mostly; +EXPORT_SYMBOL(__stack_chk_guard); +#endif + extern asmlinkage void ret_from_fork(void); extern asmlinkage void ret_from_kernel_thread(void); -- 2.7.4