From patchwork Tue Oct 29 23:44:18 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13855734 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 C6AD5D7494E for ; Tue, 29 Oct 2024 23:45:13 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 352AF6B00B1; Tue, 29 Oct 2024 19:45:09 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 301DA6B00B8; Tue, 29 Oct 2024 19:45:09 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 12E686B00B7; Tue, 29 Oct 2024 19:45:09 -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 DFA4E6B00B1 for ; Tue, 29 Oct 2024 19:45:06 -0400 (EDT) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 8BE208046B for ; Tue, 29 Oct 2024 23:45:06 +0000 (UTC) X-FDA: 82728272424.15.057EA5C Received: from mail-pg1-f172.google.com (mail-pg1-f172.google.com [209.85.215.172]) by imf18.hostedemail.com (Postfix) with ESMTP id 8D1CE1C000D for ; Tue, 29 Oct 2024 23:44:53 +0000 (UTC) Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=kvgarZxq; spf=pass (imf18.hostedemail.com: domain of debug@rivosinc.com designates 209.85.215.172 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1730245423; a=rsa-sha256; cv=none; b=AD4kWb7inwBo80XxRydTmQxCxXFKIx78mEK5dKPG99//IRYv31DD8NsPgo1kEdNyar/tyE F7ibmNnvQeO2mnv99YA3d6qk8JOqD3OXcV/XAe2lwF1UJuycvDtIDOASKcTmj87B+XsexJ JT+VcvXFBToG5iFMhcJCzmJRq2ww7Zc= ARC-Authentication-Results: i=1; imf18.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=kvgarZxq; spf=pass (imf18.hostedemail.com: domain of debug@rivosinc.com designates 209.85.215.172 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=1730245423; 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=6TVX3BLcn87m0lpyR2AYtrzuqtp3pcfnfCPWtAWnF6I=; b=xqcutPU8mYbK5aBT7KJANvvUScu0F+aVuDee8Q71YaxC9BJJYYWtRMfbLCXAQIcQstOrBA T3v9gf01apumD71cQQ5gXMLsTkrRnRsXD655H6HZPkqEPCpbFF2KReoxIbmb7nug1plTYn VXI8JlTzMf+DzOcq4UM805NW32mV6Hs= Received: by mail-pg1-f172.google.com with SMTP id 41be03b00d2f7-7eb0bc007edso3029923a12.3 for ; Tue, 29 Oct 2024 16:45:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1730245503; x=1730850303; 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=6TVX3BLcn87m0lpyR2AYtrzuqtp3pcfnfCPWtAWnF6I=; b=kvgarZxq6EF/g9BQPVDn1gMAEv8aBIb6fFKjMKepMpkoT1nANtJdplRxVkcyvF6L4z q4v28hnczaRKcSq15eLAEh7va1RdGzA1eWbW2nehlb7Zjh9uTq3HMljADpWhA199w8X8 QdULEg2fNQ8UYORk1dSBG2ocu4HjGbKeSVPR5kFM6WWmYM7WWKpPItstdVnDN9P8yBCo DIPNKrP5obLvBuXLTskqkGSHdps9rKCON6DTtN/xTBHFPtNnPMowu7fVHeh5QJuNKOQb myTqxcZGYvp5HeHLmAWfNnZwbwGNcIl60syMH37pIk7IqYDVmt87JFOV/7aHmxoE/3l1 +IKQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730245503; x=1730850303; 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=6TVX3BLcn87m0lpyR2AYtrzuqtp3pcfnfCPWtAWnF6I=; b=iu/mTWgrvsDbl6WkqDl+EPlPpdS6w1rlEoOkj1mG8wpQoAx3qISlx4fN4wgNFYNQi1 nC+TFLKsAfa+XApvgHglG9kziRe/BDpg9D+1tLdqVYNTeo1eP+Kzf6MFSilGKvhBfZ/h GaIROci+oTgXngKnLVPagD3uQ61b+hfwEt2L4NT7Y1uCgmoD2XVYhhfkYKlCZ8fRyfsY SK8DW8/aevOeLrKyxvm6IKHl10w2ro+BCkN7OeA7HU6AS+QB3vqe+ppZwq2DLr7E5J1h bY3h2Wr1C3znEQoawj9oXdQ1c+Id6scinmXq9kp1ulcofExQkEHe59pLUvNpOql5ESrl V40g== X-Forwarded-Encrypted: i=1; AJvYcCVHU4Ni41ydORjJrR+7DA3Kad1+OwFjX9cMhvddeQLv8U/YSvJk/B9n/96w6W0OwFIESUhJv3Ehxg==@kvack.org X-Gm-Message-State: AOJu0YwUPaeWsfae/4tUc2Vuou4BncF8aYoEaf20aO8R6XzJmWCm4+O+ unR3/ZeVVfUUa8c6qTa3H+XVe76BmeBY+fq5kvEaCU0TRL3llQPxzzpE0KrKR/8= X-Google-Smtp-Source: AGHT+IFNg5o6rO3j8JKAbw0m86pKoaLglZmNpPu6e6Ng3OXhO9Lt2u49bgj7cspmJy/j4LUmb5jQTw== X-Received: by 2002:a05:6a21:1813:b0:1d6:d330:2417 with SMTP id adf61e73a8af0-1d9a84d9eb7mr16511530637.40.1730245503345; Tue, 29 Oct 2024 16:45:03 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-72057921863sm8157643b3a.33.2024.10.29.16.45.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 29 Oct 2024 16:45:02 -0700 (PDT) From: Deepak Gupta Date: Tue, 29 Oct 2024 16:44:18 -0700 Subject: [PATCH v7 18/32] riscv: Implements arch agnostic shadow stack prctls MIME-Version: 1.0 Message-Id: <20241029-v5_user_cfi_series-v7-18-2727ce9936cb@rivosinc.com> References: <20241029-v5_user_cfi_series-v7-0-2727ce9936cb@rivosinc.com> In-Reply-To: <20241029-v5_user_cfi_series-v7-0-2727ce9936cb@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: wij346zzm9bo4a6ebkt7qitr5mcpcwya X-Rspamd-Queue-Id: 8D1CE1C000D X-Rspam-User: X-Rspamd-Server: rspam10 X-HE-Tag: 1730245493-265789 X-HE-Meta: U2FsdGVkX1+HsUQNx+zssQ5/FBmREchOAUeZzGrJeLWRNqTH0/7t7+SlRo18iABYeKD0cJxggFacmnvBKUL226Ddx8RQX3p9goT15g/7gyPGXfTqhkdvCEt9ocjSSsyBPTOW2l/wzDjcDp81XS4QRb92m/Dfzno1aaJJrwRGj+kOdMwvBXRhzS5vHZbXGBKA9Q5ZcOcju4tGX6dGg3WdcGrEsUw5hYMjVAyDVH1YM0aL6HobeanIRr/vofDlzH1rS5YQFdBMHY1b4BMupbZQelwBBZs65nhq2FTtkjKBZmpk3BYyQIBQujtdEi6TmgsLoahf4EuzWcPa+LQX8QVpX7Lt/Qlh+DdSONmc+qlihwgPkukj4ppVkbnj/tc9ZqqAEiMUUI+oz0RQ9zIPUChYLwV/fYUuhgL55xLOx/4I4x2c+hzeRKkxwKqHVnPt9hjSyY99B5JnszfJYui9C7UzO9j3kWWD9xnAMBEQnUjXNzh6pEl1j/TccSTBQjb8TA9YiVE9+ZGDwCDZTjtGl0U5mXiLzF0IcdnQ2rD81fFM1CaaMvE/bk5YiLmpdszGHD6l7+5PLOVA3MOWnfqHtaR0O4ZXeoJaS8+32e1EifMX4PFgakao/gHkBjmIgiwygMTmUVkSNb5XwBftKnNnUL2wQ1nYzt3I3pTq9/Y8BATuZI8xovPYln1boFgjIVs8rhJG0XAolyrnGca1vGV1FfQ0UsUO50V4hHVZOT7ey9NCfJPFSiba0emyKitUcfhvt5iu9sbDu4rGJFhPU66bYNEP6xDoScYZYsNZt8QWe+kmpqb+5g/ARE4ShiGu4MLxVvsbzA8wo9dKRSuG0+DinFLB6dABJx2cY+9qVIGYGPwdRZto7AYS0DbYv+WJWqswF31D/YhXlsyglPAxJ2mEQpSSUOsreQzDue1kF0JtgpsnjMuc0D8bo+Hs0iSDRHVL1SKhRnjUe5saVw9NeSA+qLD QNydbI+l pGze8IOJp8vRjb9jnVjBUVV4QD6BhyM34SVk8AinhA9egdAVJpITqjCBebFawT3WLOsvGt4ZiFfTtv67jyet2vQ4YpaLNUFlMfknk+xHMpUAGsI6njOEM6E/Js1xPwBTnz2CvlNmNTWH5fpDUlTW1mw2hpNVfvqBq+D6kWI8115/0mtNZnuSu5cd2sj2MNVB9FzjWqN08MCkofb+c1q/60liNoaiuMoDJKogEx0slhFaQ4dYedZmeVbDbSBCxsEIEttaLDAMhesuABhjCBKO2UNYJG1DBf0HqNEO71UYEjjwkJHyt1uf+iwjm/EzQBFuGULWFoOQzEiUdSxE5O7uJSa0OGz/3g3YaXKA0kzcyvNljZ+S7AuC2Mgi8G6da5nD731wNy+peOjpi88pDiXPhIQXCnhzjo0GyrnUS8Z93/lz58ehsw8c1NAaMRApCyESedGuVPFXVpoxj9eo= 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 632c621682f6..850128427936 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..141ef70b08a3 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) || (arg != 0)) + return -EINVAL; + + set_shstk_lock(task); + + return 0; +}