From patchwork Tue Oct 1 16:06:24 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13818422 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 81F27CEACE4 for ; Tue, 1 Oct 2024 16:07:59 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id DB8382800AE; Tue, 1 Oct 2024 12:07:54 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D6889280068; Tue, 1 Oct 2024 12:07:54 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id BBAFD2800AE; Tue, 1 Oct 2024 12:07:54 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 9977C280068 for ; Tue, 1 Oct 2024 12:07:54 -0400 (EDT) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 5F4C81C73ED for ; Tue, 1 Oct 2024 16:07:54 +0000 (UTC) X-FDA: 82625514468.07.400F16D Received: from mail-pj1-f43.google.com (mail-pj1-f43.google.com [209.85.216.43]) by imf05.hostedemail.com (Postfix) with ESMTP id 5FA15100016 for ; Tue, 1 Oct 2024 16:07:52 +0000 (UTC) Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=YTyRvbno; spf=pass (imf05.hostedemail.com: domain of debug@rivosinc.com designates 209.85.216.43 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1727798744; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=NXAvbRYD1Q1ho1rLmqewAH55Q8KmYmnFeXkf6c6XC6M=; b=ZjpWYvEZDg4qomojx/fqIZm4XGgnS2i9jxrfa0DVshhoLjWOtOIFGjlUWZTSDtCTgNNjIp VZISnEWIfaIsSmiqFrnE68QRmpibeHTAZxs3/RVDBHHy01cAltunBabSWHPtpRzCVPd1S+ YOaSAxardAiWczLuk4eZewuXJfpY6gc= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1727798744; a=rsa-sha256; cv=none; b=H+wRg2MQG3XYuvou+QKDQodxnYcSCXekTpYhd6wSmob1NEw4vhVgb1jZVgt7bForrfkqt7 w0YPWNc/b91ZeGZlkt7NOK8cx8MqBqwHdDN6jL4HVtP/35XQsrLdhSnydoxFBrRS3ONIko Gzj0lhsSyUk+oDMYXUD4SwXo0rubT64= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=YTyRvbno; spf=pass (imf05.hostedemail.com: domain of debug@rivosinc.com designates 209.85.216.43 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none Received: by mail-pj1-f43.google.com with SMTP id 98e67ed59e1d1-2e09f67bc39so4696528a91.1 for ; Tue, 01 Oct 2024 09:07:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1727798871; x=1728403671; darn=kvack.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=NXAvbRYD1Q1ho1rLmqewAH55Q8KmYmnFeXkf6c6XC6M=; b=YTyRvbno3aF+/rxLSR7HFhs+h0dptPEcGjgU5cTQcmTO4cPvTbTnef9L6SUIygdrWw /OjnobE+LHniZlIzZagTtXyIDbwwv98u3+wR3Mf4iWURjsDdBAHecgWYq/iI4vie1Jrz FMFmp3lHj4wt8q816Rc11DOvPEzo4Qf2biNtjDFj0PubjEP4D6D1Rclt7rESkDw7/swI Tm7BT3WqdPVfjRYX2kenRYEknI6GvKM75n5njziguZwEK1T8iPP+FKq8Z/VrpyqAx8h4 9IBMN/mhtZKLFYePkQmBvfjo5Ib1vmUas8tBR9a9qu2ML1DNYpi9/e2UU0JyrBUiJXpX jEaQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727798871; x=1728403671; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=NXAvbRYD1Q1ho1rLmqewAH55Q8KmYmnFeXkf6c6XC6M=; b=Y8RFfYbG47DGw9y8UWVUE5QWRvLW+V95gzKl4ThMo4gLF0IqkTcSxcz9nWlJptCan3 8a3Ktas+xv2gBQ/5xsWXwzmY7eqZkkXD0FQ9LFsifaecQJWtDNJ+HPdgpc0huU6PPW7K OOgSXDieAepWZVN9TzzQqq7i2GyN2C1p6rWPNwpGzsX0WsVG3S2zv1Wf688Em0H9+sNf qSRMY0HzEQ4LG8StQEzIIJ3yLtgK+cpug34w8tCXiNQBaICIJ+DcSR+x+m7dbEL+UJHx onQPLfvSXSPeG7TyAnYWn+b4oN5rMP7Y6iOtovVJU7xZtFrzpXQ44GadZuuPy+LgCP62 hh8Q== X-Forwarded-Encrypted: i=1; AJvYcCU6yLdtj9+dlp434I2kTJ5coEVNbVatrvsZpmXCK5wqKXjzX9S9sGdYlpV5bnfvUkfWJyExW+YQvw==@kvack.org X-Gm-Message-State: AOJu0Yx8LtBaLQEkrptthMXRQt7Han0yk+Zk8j2zcw9zHyOLU7CfW67f dmUlnI49r8nyilRXwULUEfH85UDjOQ4Et9kkzzOmqUeV9JCjZuoDEpwAGzkcNvw= X-Google-Smtp-Source: AGHT+IEdFiTPZh83JuVojwJkJAxLnQhFX7Kjf5puPeQo/q6uacYSkkP+0Jjd72vQ2I4kXed1EqwuJg== X-Received: by 2002:a17:90b:224c:b0:2d8:8808:5154 with SMTP id 98e67ed59e1d1-2e1849eca9amr165298a91.40.1727798871001; Tue, 01 Oct 2024 09:07:51 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2e06e1d7d47sm13843973a91.28.2024.10.01.09.07.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 01 Oct 2024 09:07:50 -0700 (PDT) From: Deepak Gupta Date: Tue, 01 Oct 2024 09:06:24 -0700 Subject: [PATCH 19/33] riscv: Implements arch agnostic shadow stack prctls MIME-Version: 1.0 Message-Id: <20241001-v5_user_cfi_series-v1-19-3ba65b6e550f@rivosinc.com> References: <20241001-v5_user_cfi_series-v1-0-3ba65b6e550f@rivosinc.com> In-Reply-To: <20241001-v5_user_cfi_series-v1-0-3ba65b6e550f@rivosinc.com> To: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Andrew Morton , "Liam R. Howlett" , Vlastimil Babka , Lorenzo Stoakes , Paul Walmsley , Palmer Dabbelt , Albert Ou , Conor Dooley , Rob Herring , Krzysztof Kozlowski , Arnd Bergmann , Christian Brauner , Peter Zijlstra , Oleg Nesterov , Eric Biederman , Kees Cook , Jonathan Corbet , Shuah Khan Cc: linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-riscv@lists.infradead.org, devicetree@vger.kernel.org, linux-arch@vger.kernel.org, linux-doc@vger.kernel.org, linux-kselftest@vger.kernel.org, alistair.francis@wdc.com, richard.henderson@linaro.org, jim.shu@sifive.com, andybnac@gmail.com, kito.cheng@sifive.com, charlie@rivosinc.com, atishp@rivosinc.com, evan@rivosinc.com, cleger@rivosinc.com, alexghiti@rivosinc.com, samitolvanen@google.com, broonie@kernel.org, rick.p.edgecombe@intel.com, Deepak Gupta X-Mailer: b4 0.14.0 X-Stat-Signature: a4s94ffkhrcm65gj6dhahg147br6c373 X-Rspamd-Queue-Id: 5FA15100016 X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1727798872-468645 X-HE-Meta: U2FsdGVkX19DYgLSHH3rQbbTXZ4SBplvFQfAjMU4FQKWWYT0Ivf4Y1PBdZdKw5ujUMMQXIhQJSDVPxnAVSNNunK2Afp/FcehNNHZKlDs95oIjASND1bH5LpGpd9//VDgYYJgFaZ0BgzS9hz5tPIo1MPutWB2YLo6ceNiEwdSlS/s56d8PKifPJ1+uQEyuUaz+Ai/6678td6EmTSHcOzQ4ZYiWYdRSo0QY1syACeZ+HQW/A+ErIsyzBJnro3/yyrbN/7k0m8cHTo+MJUx3ySRR69a9cK9jsofjTctPHzq1wVouBPRSjCLQRH20rkpQbDylLVRUIx426pcNATA2HSGP/Cc3QN5mteXbx/0el134Yp1KuI3sQz1CtI7qBaWPDKNWTTuIitRt+FW00q4V8qgb5ttBdk9Xk+zZtho0l10l9ChQqIMTrpf67DtnuwfwyVQO4XJK+lSZgMQMvCdnzK0qUMM0nqVDLYUhW+RHkNHAhO05PjBF7GSkaCQ4LJXJEGqDXI7m+becYQHGV9O4l8yC3oL7V38ygI9QEqLrpA7G8si8tJLzL5dn06rvlOMGbh5z/EHPHB5FDP1qZFTPLkx7zGfVSABzfV3UYme2B2SA1vJycXeSlzTK1JIEnwdrJ9FfwfMDrmGCB0zHEvNe/hVCPOczl+HqQCb0hIcZ9o0pfTTB6CUvdYgTpps4memNWQg5kBuxZlelHRXl58mFSWgm+Ml9oxOCHdvxqOexCarxHzfPcYVL5PCDooF8GzuOaJrBiG1JYARikvxXByuaMQqm4eCBMIcjs0WrRp1HSs9nG9TRyBygSeAT92KBtwBL2+wM26mwUKXTmbCr9eAua2SSntVd0DZQGma7bJtxOBqT0ldDOwSLTkrLd4w9z7z5LixB65sINsarsovZeNBlMs2jC4privpGxAJiuiu+chxmuFhzrDIQNDJR+xW+7OiRe+xY8cUa0zH8ezQ07T+ZOA AP1mMIti hk3WCLEJB6J/OvHxxGZ9tPEIO+tqPXz9SaHPbo633RGerJh8Mx3+AXe+WMaNBZv9g7mOcQo8D2Zku+U+59ovqC/jAOVP4SBS0f/1VpneCeEolM3ol295HjAlKFxwPWdw87bskRy7a4bV4BwZz2ti/fbbH0PZuAVmrMX9U7wx24I3TYwAFyWB8zjTW8Ftr/LICY1ehUoUMJiydxrQxbJbqmDxNO67M7HzzO4nl3M5OBrK/kDuWRfEdLqSszaA9JzxGVosGyIhcYFDZ8romm7pqs33/l6gIchXXwK6SW5jESuOoDvxjAFb6Cb3TWDBizJ+/p96amj6bX/9m5oHheeL/HYrWwWeXldaxdQzwLhMan/94UDud/ea2iMEN+8myJ0T4s3x8lhqB1GA+V664lLD9fr6JGjCr57Uc0HeWRajViHo4H3dctmevQY2IllMMikaeSLNokckD7jDF/7w= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Implement architecture agnostic prctls() interface for setting and getting shadow stack status. prctls implemented are PR_GET_SHADOW_STACK_STATUS, PR_SET_SHADOW_STACK_STATUS and PR_LOCK_SHADOW_STACK_STATUS. As part of PR_SET_SHADOW_STACK_STATUS/PR_GET_SHADOW_STACK_STATUS, only PR_SHADOW_STACK_ENABLE is implemented because RISCV allows each mode to write to their own shadow stack using `sspush` or `ssamoswap`. PR_LOCK_SHADOW_STACK_STATUS locks current configuration of shadow stack enabling. Signed-off-by: Deepak Gupta --- arch/riscv/include/asm/usercfi.h | 18 ++++++- arch/riscv/kernel/process.c | 8 +++ arch/riscv/kernel/usercfi.c | 107 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 132 insertions(+), 1 deletion(-) diff --git a/arch/riscv/include/asm/usercfi.h b/arch/riscv/include/asm/usercfi.h index 719e28e043c8..52850a2c79cf 100644 --- a/arch/riscv/include/asm/usercfi.h +++ b/arch/riscv/include/asm/usercfi.h @@ -7,6 +7,7 @@ #ifndef __ASSEMBLY__ #include +#include struct task_struct; struct kernel_clone_args; @@ -14,7 +15,8 @@ struct kernel_clone_args; #ifdef CONFIG_RISCV_USER_CFI struct cfi_status { unsigned long ubcfi_en : 1; /* Enable for backward cfi. */ - unsigned long rsvd : ((sizeof(unsigned long)*8) - 1); + unsigned long ubcfi_locked : 1; + unsigned long rsvd : ((sizeof(unsigned long)*8) - 2); unsigned long user_shdw_stk; /* Current user shadow stack pointer */ unsigned long shdw_stk_base; /* Base address of shadow stack */ unsigned long shdw_stk_size; /* size of shadow stack */ @@ -27,6 +29,12 @@ void set_shstk_base(struct task_struct *task, unsigned long shstk_addr, unsigned unsigned long get_shstk_base(struct task_struct *task, unsigned long *size); void set_active_shstk(struct task_struct *task, unsigned long shstk_addr); bool is_shstk_enabled(struct task_struct *task); +bool is_shstk_locked(struct task_struct *task); +bool is_shstk_allocated(struct task_struct *task); +void set_shstk_lock(struct task_struct *task); +void set_shstk_status(struct task_struct *task, bool enable); + +#define PR_SHADOW_STACK_SUPPORTED_STATUS_MASK (PR_SHADOW_STACK_ENABLE) #else @@ -42,6 +50,14 @@ bool is_shstk_enabled(struct task_struct *task); #define is_shstk_enabled(task) false +#define is_shstk_locked(task) false + +#define is_shstk_allocated(task) false + +#define set_shstk_lock(task) + +#define set_shstk_status(task, enable) + #endif /* CONFIG_RISCV_USER_CFI */ #endif /* __ASSEMBLY__ */ diff --git a/arch/riscv/kernel/process.c b/arch/riscv/kernel/process.c index f6f58b1ed905..f7dec532657f 100644 --- a/arch/riscv/kernel/process.c +++ b/arch/riscv/kernel/process.c @@ -152,6 +152,14 @@ void start_thread(struct pt_regs *regs, unsigned long pc, regs->epc = pc; regs->sp = sp; + /* + * clear shadow stack state on exec. + * libc will set it later via prctl. + */ + set_shstk_status(current, false); + set_shstk_base(current, 0, 0); + set_active_shstk(current, 0); + #ifdef CONFIG_64BIT regs->status &= ~SR_UXL; diff --git a/arch/riscv/kernel/usercfi.c b/arch/riscv/kernel/usercfi.c index 7a7f0b57b2d4..c77abe552c88 100644 --- a/arch/riscv/kernel/usercfi.c +++ b/arch/riscv/kernel/usercfi.c @@ -24,6 +24,16 @@ bool is_shstk_enabled(struct task_struct *task) return task->thread_info.user_cfi_state.ubcfi_en ? true : false; } +bool is_shstk_allocated(struct task_struct *task) +{ + return task->thread_info.user_cfi_state.shdw_stk_base ? true : false; +} + +bool is_shstk_locked(struct task_struct *task) +{ + return task->thread_info.user_cfi_state.ubcfi_locked ? true : false; +} + void set_shstk_base(struct task_struct *task, unsigned long shstk_addr, unsigned long size) { task->thread_info.user_cfi_state.shdw_stk_base = shstk_addr; @@ -42,6 +52,23 @@ void set_active_shstk(struct task_struct *task, unsigned long shstk_addr) task->thread_info.user_cfi_state.user_shdw_stk = shstk_addr; } +void set_shstk_status(struct task_struct *task, bool enable) +{ + task->thread_info.user_cfi_state.ubcfi_en = enable ? 1 : 0; + + if (enable) + task->thread_info.envcfg |= ENVCFG_SSE; + else + task->thread_info.envcfg &= ~ENVCFG_SSE; + + csr_write(CSR_ENVCFG, task->thread_info.envcfg); +} + +void set_shstk_lock(struct task_struct *task) +{ + task->thread_info.user_cfi_state.ubcfi_locked = 1; +} + /* * If size is 0, then to be compatible with regular stack we want it to be as big as * regular stack. Else PAGE_ALIGN it and return back @@ -264,3 +291,83 @@ void shstk_release(struct task_struct *tsk) vm_munmap(base, size); set_shstk_base(tsk, 0, 0); } + +int arch_get_shadow_stack_status(struct task_struct *t, unsigned long __user *status) +{ + unsigned long bcfi_status = 0; + + if (!cpu_supports_shadow_stack()) + return -EINVAL; + + /* this means shadow stack is enabled on the task */ + bcfi_status |= (is_shstk_enabled(t) ? PR_SHADOW_STACK_ENABLE : 0); + + return copy_to_user(status, &bcfi_status, sizeof(bcfi_status)) ? -EFAULT : 0; +} + +int arch_set_shadow_stack_status(struct task_struct *t, unsigned long status) +{ + unsigned long size = 0, addr = 0; + bool enable_shstk = false; + + if (!cpu_supports_shadow_stack()) + return -EINVAL; + + /* Reject unknown flags */ + if (status & ~PR_SHADOW_STACK_SUPPORTED_STATUS_MASK) + return -EINVAL; + + /* bcfi status is locked and further can't be modified by user */ + if (is_shstk_locked(t)) + return -EINVAL; + + enable_shstk = status & PR_SHADOW_STACK_ENABLE; + /* Request is to enable shadow stack and shadow stack is not enabled already */ + if (enable_shstk && !is_shstk_enabled(t)) { + /* shadow stack was allocated and enable request again + * no need to support such usecase and return EINVAL. + */ + if (is_shstk_allocated(t)) + return -EINVAL; + + size = calc_shstk_size(0); + addr = allocate_shadow_stack(0, size, 0, false); + if (IS_ERR_VALUE(addr)) + return -ENOMEM; + set_shstk_base(t, addr, size); + set_active_shstk(t, addr + size); + } + + /* + * If a request to disable shadow stack happens, let's go ahead and release it + * Although, if CLONE_VFORKed child did this, then in that case we will end up + * not releasing the shadow stack (because it might be needed in parent). Although + * we will disable it for VFORKed child. And if VFORKed child tries to enable again + * then in that case, it'll get entirely new shadow stack because following condition + * are true + * - shadow stack was not enabled for vforked child + * - shadow stack base was anyways pointing to 0 + * This shouldn't be a big issue because we want parent to have availability of shadow + * stack whenever VFORKed child releases resources via exit or exec but at the same + * time we want VFORKed child to break away and establish new shadow stack if it desires + * + */ + if (!enable_shstk) + shstk_release(t); + + set_shstk_status(t, enable_shstk); + return 0; +} + +int arch_lock_shadow_stack_status(struct task_struct *task, + unsigned long arg) +{ + /* If shtstk not supported or not enabled on task, nothing to lock here */ + if (!cpu_supports_shadow_stack() || + !is_shstk_enabled(task)) + return -EINVAL; + + set_shstk_lock(task); + + return 0; +}