From patchwork Tue Oct 8 22:37:01 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13827102 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 47293CF0429 for ; Tue, 8 Oct 2024 22:39:00 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B13A66B00B4; Tue, 8 Oct 2024 18:38:44 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id AC3C16B00B5; Tue, 8 Oct 2024 18:38:44 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8C72F6B00B6; Tue, 8 Oct 2024 18:38:44 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 6C8F16B00B4 for ; Tue, 8 Oct 2024 18:38:44 -0400 (EDT) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 75E9E120CDA for ; Tue, 8 Oct 2024 22:38:42 +0000 (UTC) X-FDA: 82651900926.17.D6EE6E0 Received: from mail-pf1-f179.google.com (mail-pf1-f179.google.com [209.85.210.179]) by imf22.hostedemail.com (Postfix) with ESMTP id 13B5FC0010 for ; Tue, 8 Oct 2024 22:38:41 +0000 (UTC) Authentication-Results: imf22.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=VTTIHE0K; dmarc=none; spf=pass (imf22.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.179 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1728427078; a=rsa-sha256; cv=none; b=ZRbkSGbe/VV0u/9bcnVQSqP8qzF5Q+4dNv8N+OBOfdneNBWRKB5JGH1ALGXkQ0egZ77eqf eXHY+/oQsap32I2JPF0FXnarFrjQUDc3rX1+9C2aM2VQtkEvynygEnPbf8khpBwtbsJPWx fptTdQWeLzKmEb22hxu1S2XEpt0SoqI= ARC-Authentication-Results: i=1; imf22.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=VTTIHE0K; dmarc=none; spf=pass (imf22.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.179 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1728427078; 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=+IBv6y5g/XQfRfM7PVd+NrzWS0a9vjXfXxzedLB6inI=; b=IgVPhDERt3Zf0UNBXsaDzeP5nsW7a8kDpp1Fteaw7HhGINxiGZLe09d86Ksll/oJOd0aBv f62bK6R6nnDat6nJzIVT6fc1A2IZSPqpJaLXACYBEWUeo0lHggkG13rsk5zH4MAKZtYIlg S210tl3vtqGQYdSkQEKiDz7S8v2cgXA= Received: by mail-pf1-f179.google.com with SMTP id d2e1a72fcca58-71dfc250001so2440692b3a.2 for ; Tue, 08 Oct 2024 15:38:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1728427121; x=1729031921; 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=+IBv6y5g/XQfRfM7PVd+NrzWS0a9vjXfXxzedLB6inI=; b=VTTIHE0K3PPzQs9QzuAQ3DuqqpXxL5VUe+MC83Iu9jgGUjMjIYpXNJP7eRYWG07KrH i6JQ238YD7Jk96f23sOS3q7CEs4SjM9+9zbdYfSRi3J9U6ifSB6EDUpLoRADkvNGdu3a pgKvNv6ddmdaRFZEmrHyeTC+poFed2i/0JaxQyPUv4c47za10lji78J15ebN3YnyRqK4 QvdHnfP+Sl+ma84QWP86WWuFLjTxsoQWh2+Y4RdxwnAPGFcrq+sQrpt9v6s1kiQ95M8+ vS9/54gRChLQ67nUMmhRePDgXEVGWIyIZ5fvhZsA1+wER4TJM/DACisIqAvAv8isrJgU ShTA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728427121; x=1729031921; 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=+IBv6y5g/XQfRfM7PVd+NrzWS0a9vjXfXxzedLB6inI=; b=dTIaL0UutCkpOQ9v9HW/ie4zvv/3985w/0j2MEzbq3WxuZnQzqK7kYlWZHqxLXBLW5 G97qjETvvnS/T4T8Rp4AwKOrT+oYhCLlSmV5KYZaCOzXonfo7cDpm3nZv34D1FIzAmrH qdJpUYp9bPKbWzRNly9Bqz+EttCQH6u0PmBi9ki0igT9w7C+Jq9ua4RWr/wLuF2Xshbk WNyhAHSZ33Wsv+fkL5gFt+Uf18dkHxiSWdQ83K/EJk+/0PJ3fIQk7HJ/h3RefY6J9OWx gjNOQXeMK3MWp/19WiGjKUsiiGS77aV6cxyubtI/mTeLLjzTDC9q7/PjEeXo1nn9TYs0 qCKQ== X-Forwarded-Encrypted: i=1; AJvYcCW1Nb9Kt6tkwtI3/8AY0bc/2cIS9XyW0PEIsj49as/FGBTVALkOBRVwf6Yb48m0AubbW1cvCGcAuw==@kvack.org X-Gm-Message-State: AOJu0YwKiv+jcRZkx10eAfQHuWmKIxQcUJrbJYIWXuFJ7+m/wXbBacy7 86KH+zkKwRqCCjREGdaqk4lhSQRfFbZTiCTuWD203S2PHw4CsyE19LiZSij3Ll8= X-Google-Smtp-Source: AGHT+IEEDZEG6ZHaENLvzcfnn+S6ck1KUevwKGyZCx7BXAYd7lvydRa0tA4mrbzbJrIZdKFkpkoDsQ== X-Received: by 2002:a05:6a00:2d89:b0:717:945e:effd with SMTP id d2e1a72fcca58-71e1db65b58mr647001b3a.1.1728427120735; Tue, 08 Oct 2024 15:38:40 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-71df0ccc4b2sm6591270b3a.45.2024.10.08.15.38.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 08 Oct 2024 15:38:40 -0700 (PDT) From: Deepak Gupta Date: Tue, 08 Oct 2024 15:37:01 -0700 Subject: [PATCH v6 19/33] riscv: Implements arch agnostic shadow stack prctls MIME-Version: 1.0 Message-Id: <20241008-v5_user_cfi_series-v6-19-60d9fe073f37@rivosinc.com> References: <20241008-v5_user_cfi_series-v6-0-60d9fe073f37@rivosinc.com> In-Reply-To: <20241008-v5_user_cfi_series-v6-0-60d9fe073f37@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-Rspam-User: X-Rspamd-Queue-Id: 13B5FC0010 X-Rspamd-Server: rspam01 X-Stat-Signature: keipj9a9shqwcqgiewrisbfk7ah45ay6 X-HE-Tag: 1728427121-470311 X-HE-Meta: U2FsdGVkX19slhhnjCSkcQ6CUQ3I2+t7N3PaPKXZZ20y/nm0gDQmLSpMr4YinJHB2ZdDPkH7c2zLZ4qHNa8cRoJg8Ku3FnOYTFNnFIyKb0dwRG3fawhhXLdCkSI9XnokjPLJ8u0wMB8CM3VjTzbLahEIln48Ps9POCMb58mU8nvKWo0vp4BHY2KoxOAK6s4y/T4NIi9pWCtecOR45xC6h8/tGHjQa52DEdQnPnLsYaKMRKcGOcNoD8PfFPopudgcBw00XtNlH0zvAa+3br7NcOO0DuotpMF/4o3bw4U2B7ono3RQ8lOG4Sg1pT9LhEdNchmJUdJswMpAdzhpkQluhGnfMUUAfktVao/Gdtou9yY1cb5riQz2RzhLG5yqbfvRAtlGbfpSIqEURS1xUhe0cL807O9cQGJ3nSIZk/HxpncvwTXH/kR/+pXFmFekzEX8PlAJyMH1pJxYC0ta6yEqdgZviluzGDx0EepgI+3zeyEcruFSkcLGVNF91YLvvkkYuhNwxuEose6qdLEl8bQca96/SVSAVFbJdafedpUxJIfT0Hue7XsyQQWHJs/LKb9YVNvCcSGsh2xABblGb8vVWQZjNnuD0nvUHDQGgoNwkBsI6Mj5orL3e97ir+H0afQrW/X69CC2XfTZneb4/wmK7y62zMkp08MB3/OcNkrilJ1EyYpaqIXtO/PVcxURcUvsqjc/jzOFangXLBSdat2qfENFRYrDdqdftPZOfs97ub5rqRuqdtTyBosNTVegXe3LTTn3D2FujSxSkafB3PTQLRsxkl83DYffVi0FI8P1lbghCGR4h6by3LbfRtNeTwy++rkRhb2Oc61K8WtnNvnW0pgE39ezmq01GU24v9ftDyPl8E2dAdm5RmsQWa7AD2g0zKdEXIRo+HcotDjNlzSFaSk3+vFeqtsJTDXl54vP5V10er/ZJgUTvqwvXplflGEZhxM4i7ZJe87AkxFKK7t MnK9/a9E n/kXwtiq7o6LDWzwZ6lDyXpROV0b+ww8bQQZrId+yF37bsmWPrN1/1qaRHHJzc7qjoR7cfPoafGMecvqq+yKSRoli/3bNphKvc2huAblkQRdxIFcC4OhSyg+DhUi/w4+1DI7X6pqQKGhOMYicafoLhRPTDVcb+M/fzH/i7BW/NUBCm/IkEtA4pBtOuIYKzoVRP5q0VcLWKmxkdtK02CnpZuCrELPw3p6m250COTrmcw7dm48gnsLIZfezCOyCllV6N/AoSdSlyOBNLladTLIACWdYVaGoozmTwf2CsI4T9J87pa1tuwIWaQkDHRc5lSdH6d8Ijv4EXS2DWi15YNav4n6DbsQ1MQkYdOZUgsVFyDY+LUHaUytU7XiYlbXcChwBD7EaPySdOqoM04osFIN23//PPhX1i6UTHjp1NbX8AfnJft9Le2dpx4jNGFCjwPBeDpbLX9tZ+SRK0sc= 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 4da9cbc8f9b5..0b3aff008c85 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 6cd166b73316..6ac5e87b4c70 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.envcfg |= ENVCFG_SSE; + else + task->thread.envcfg &= ~ENVCFG_SSE; + + csr_write(CSR_ENVCFG, task->thread.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; +}