From mboxrd@z Thu Jan 1 00:00:00 1970 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 BE15EC3DA6D for ; Fri, 23 May 2025 05:31:54 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id EB2606B00A2; Fri, 23 May 2025 01:31:52 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id E63046B00A4; Fri, 23 May 2025 01:31:52 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D2CAA6B00A5; Fri, 23 May 2025 01:31:52 -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 B20926B00A2 for ; Fri, 23 May 2025 01:31:52 -0400 (EDT) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 70489BD690 for ; Fri, 23 May 2025 05:31:52 +0000 (UTC) X-FDA: 83473050864.21.B576DFD Received: from mail-pf1-f176.google.com (mail-pf1-f176.google.com [209.85.210.176]) by imf05.hostedemail.com (Postfix) with ESMTP id 80E88100003 for ; Fri, 23 May 2025 05:31:50 +0000 (UTC) Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=eVvUeasZ; dmarc=none; spf=pass (imf05.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.176 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1747978310; a=rsa-sha256; cv=none; b=r6I9DEIP22YuiDfZIZrG8LTO9mbyUitgPyz0i1/y0/YQzDvixIwLL0aXQjTGBbu8laINND Dlv/VOuw2Y4HRhW9JwlBzAygfeom+BnFHCxf+swH6Y5/t57rSeu6HpImd+7jpY1nmAElmz QQGKSa4/TrHd+f9IecDQtxlQYWFpsm0= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=eVvUeasZ; dmarc=none; spf=pass (imf05.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.176 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=1747978310; 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=HZxLHlD3l8KQyqfBm0pNowD5Eu+jDmaJ7iNXDIvukjQ=; b=taieySOuvkLj4TT1ML7FEi0Yyxil1k026IKBtq1QPk1hIUdq9KBCpAg6CNdhEoh/ZLAlKn jBRdU17N4pqXfwvsABmMrzxI35MHx+iIe+UKuaC4mOZhIOTG0QWeHUYKPq9MGnWVuofvll s0GDVskrhIjAhPyLRg2KzyAaNLs1BtQ= Received: by mail-pf1-f176.google.com with SMTP id d2e1a72fcca58-742b614581dso7273476b3a.3 for ; Thu, 22 May 2025 22:31:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1747978309; x=1748583109; 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=HZxLHlD3l8KQyqfBm0pNowD5Eu+jDmaJ7iNXDIvukjQ=; b=eVvUeasZUkIr/pHDa0OozlUU+OwN1T479aRAs+FcL45sFGmnLL8OQLIhAWcRHshing HfYsCeViE3qfUQ+Olku3W0E8HW+Gh0aZ2vjLcwsOQ60lLhG1gC7F7t1SCBXR7+hfTPKE 3Nt3SLplpdcEtDXKHKzZX64C16a2GbgFT6ZjyxHhLZYAgG6FBgiZ44YQaeY5CEikK1PJ 13QwK9ljqI5qJ6xbNK4Lg6+UaRw93cnygFuzsAMx4WJ1LtRinrpHcUotHhJ03MpCY3OR s227+W1W3+pCc/r4rNeOofTofQViU5R515u0tjpndUvgaAuj6vSU2+tZ9mpzzaHQH4cw aIGQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747978309; x=1748583109; 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=HZxLHlD3l8KQyqfBm0pNowD5Eu+jDmaJ7iNXDIvukjQ=; b=ayzpIjQcy4RQfLhVJJjaFXpsSfocI1vAVZv2xoLEEXL/Tm4hiEgm7C+ggkry3/7Bfu /7oHBT0pe9+kymq+hKAj9VqoY3Y2jcjO3JbQjiR1PsYimUJMBXjUwx4nTdw6oorJ7bbT 6EXPS5q3KV1E+bBsCdaDE16Pf0/+Ptyc3KiGO5MENwyB6lSv1+1hLP3KxWn+aL0RXmu/ P3ILglwHNbOeQjwfQQ6D9eKIgvxL+BsdLOpIhw7vzkw60/BQu9irYJTAH1murGNZedb6 nsTwlIFlubWesr7SGWQlcw5OZwKRmUdYoy48FLXd1Dfhj7dKnKiV1N61jNLnqCF4x8cl +sEQ== X-Forwarded-Encrypted: i=1; AJvYcCVzNy7UFolqf89YgBbNOg/IwU4/qryDOMYq+fGOHWvDHUnzva1ZuKRAs0njJ/NiPmaMpqMS7b65dg==@kvack.org X-Gm-Message-State: AOJu0Ywe3ENmSZI4oOjBJ2N6AD2LL2zcqaWKhOdP+bkuTxQQAT8fX2QZ 96stfTkSwtbNON9nhRVyPeF5676pJ6v22+3373SWN39DyzhR3AvppOa3xG2mDKW+anQ= X-Gm-Gg: ASbGncuFqdOZtbBzb9Br6LhgJpKxBg82BznxlTKXv6rzMS6i3DiYnl/enttZE9SGHYt rTzKpULoqYUZpSgb+jhuWShKAfQDJnAdJzVVkIH9NIEz/W7GH2Gh9wqFHk4nfJ4ZeI83KwbWQGc 6am++P7SgbGf5vR5QdqN7uTHHyaAxwH2EOKhGlr7vvEX0SXt7HSgjxOzDEowGekYGfbHKqK1357 If0LrbINxoo72hwPpmdEF+ldoCswe2TP1tATJISzxLguczXW5rWkWIuai7Bw86FobCh8eu9ivO5 8J8MCCqDqJvzKR5FjkUpEaROjDOUuIx3gXfTs2vGkAzte0Dr/O8Ote64PFGxZg== X-Google-Smtp-Source: AGHT+IF7LlCkN573rUCYieuRP59J7Jo55tO0RornjZjOo1vL8FXqn/alTmbMVyBz8jvWktxK3/sRxw== X-Received: by 2002:a05:6a00:1305:b0:736:31cf:2590 with SMTP id d2e1a72fcca58-742acd50f2fmr36796510b3a.16.1747978309300; Thu, 22 May 2025 22:31:49 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-742a982a0a4sm12474336b3a.101.2025.05.22.22.31.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 May 2025 22:31:48 -0700 (PDT) From: Deepak Gupta Date: Thu, 22 May 2025 22:31:14 -0700 Subject: [PATCH v16 11/27] riscv/shstk: If needed allocate a new shadow stack on clone MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20250522-v5_user_cfi_series-v16-11-64f61a35eee7@rivosinc.com> References: <20250522-v5_user_cfi_series-v16-0-64f61a35eee7@rivosinc.com> In-Reply-To: <20250522-v5_user_cfi_series-v16-0-64f61a35eee7@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 , Jann Horn , Conor Dooley , Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross 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, rust-for-linux@vger.kernel.org, Zong Li , Deepak Gupta X-Mailer: b4 0.13.0 X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: 80E88100003 X-Stat-Signature: onp4jqce5gfr8mmm3wxqxjnuoohiuh1f X-Rspam-User: X-HE-Tag: 1747978310-899807 X-HE-Meta: U2FsdGVkX1/zyrrsd1O/Vv/+6VoanBrqwEIYil4dmuimF1EHiSdEqdzkOGiFTh2kEAqH4+ncnbPcEHhSsTykc2BFBmA6qzbnZL79QxFpEd4SyYm/aLcV6ziqkiy0VkR3qbdM0TepSv1D06QOimvLlYEBfzXyFkS1DFT34oUIaorTB3KPi+4Ya3jiAdRx9Vn/qxNtGWXBrQCXRhDBDd/GXqLaJk14r40wYlFWZqShDHoLq59nKGqulvTE1rUKPlOdnLAhXNWNZNYHoHmJJ68CgRmDpe+AKQ3YrUFjNhL68NKFrscYvdFv5yt3uUYTXM7b/brurFOr1z2SUrbs9Jn666SnuGVbMDVmhEaSTG7q1kTTKLfnGeTAuEI+Ppd5FiT1URtmlgy4zkL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1pGsNLjqL/oLRpY7qvqvN8S2tmt8kDalwFlBEsvD6f50= 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: Userspace specifies CLONE_VM to share address space and spawn new thread. `clone` allow userspace to specify a new stack for new thread. However there is no way to specify new shadow stack base address without changing API. This patch allocates a new shadow stack whenever CLONE_VM is given. In case of CLONE_VFORK, parent is suspended until child finishes and thus can child use parent shadow stack. In case of !CLONE_VM, COW kicks in because entire address space is copied from parent to child. `clone3` is extensible and can provide mechanisms using which shadow stack as an input parameter can be provided. This is not settled yet and being extensively discussed on mailing list. Once that's settled, this commit will adapt to that. Reviewed-by: Zong Li Signed-off-by: Deepak Gupta --- arch/riscv/include/asm/mmu_context.h | 7 ++ arch/riscv/include/asm/usercfi.h | 25 ++++++++ arch/riscv/kernel/process.c | 9 +++ arch/riscv/kernel/usercfi.c | 120 +++++++++++++++++++++++++++++++++++ 4 files changed, 161 insertions(+) diff --git a/arch/riscv/include/asm/mmu_context.h b/arch/riscv/include/asm/mmu_context.h index 8c4bc49a3a0f..dbf27a78df6c 100644 --- a/arch/riscv/include/asm/mmu_context.h +++ b/arch/riscv/include/asm/mmu_context.h @@ -48,6 +48,13 @@ static inline unsigned long mm_untag_mask(struct mm_struct *mm) } #endif +#define deactivate_mm deactivate_mm +static inline void deactivate_mm(struct task_struct *tsk, + struct mm_struct *mm) +{ + shstk_release(tsk); +} + #include #endif /* _ASM_RISCV_MMU_CONTEXT_H */ diff --git a/arch/riscv/include/asm/usercfi.h b/arch/riscv/include/asm/usercfi.h index 94b214c295c0..0581587490c2 100644 --- a/arch/riscv/include/asm/usercfi.h +++ b/arch/riscv/include/asm/usercfi.h @@ -8,6 +8,9 @@ #ifndef __ASSEMBLY__ #include +struct task_struct; +struct kernel_clone_args; + #ifdef CONFIG_RISCV_USER_CFI struct cfi_state { unsigned long ubcfi_en : 1; /* Enable for backward cfi. */ @@ -16,6 +19,28 @@ struct cfi_state { unsigned long shdw_stk_size; /* size of shadow stack */ }; +unsigned long shstk_alloc_thread_stack(struct task_struct *tsk, + const struct kernel_clone_args *args); +void shstk_release(struct task_struct *tsk); +void set_shstk_base(struct task_struct *task, unsigned long shstk_addr, unsigned long size); +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); + +#else + +#define shstk_alloc_thread_stack(tsk, args) 0 + +#define shstk_release(tsk) + +#define get_shstk_base(task, size) 0UL + +#define set_shstk_base(task, shstk_addr, size) + +#define set_active_shstk(task, shstk_addr) + +#define is_shstk_enabled(task) false + #endif /* CONFIG_RISCV_USER_CFI */ #endif /* __ASSEMBLY__ */ diff --git a/arch/riscv/kernel/process.c b/arch/riscv/kernel/process.c index 7c244de77180..99acb6342a37 100644 --- a/arch/riscv/kernel/process.c +++ b/arch/riscv/kernel/process.c @@ -29,6 +29,7 @@ #include #include #include +#include #if defined(CONFIG_STACKPROTECTOR) && !defined(CONFIG_STACKPROTECTOR_PER_TASK) #include @@ -211,6 +212,7 @@ int copy_thread(struct task_struct *p, const struct kernel_clone_args *args) unsigned long clone_flags = args->flags; unsigned long usp = args->stack; unsigned long tls = args->tls; + unsigned long ssp = 0; struct pt_regs *childregs = task_pt_regs(p); /* Ensure all threads in this mm have the same pointer masking mode. */ @@ -229,11 +231,18 @@ int copy_thread(struct task_struct *p, const struct kernel_clone_args *args) p->thread.s[0] = (unsigned long)args->fn; p->thread.s[1] = (unsigned long)args->fn_arg; } else { + /* allocate new shadow stack if needed. In case of CLONE_VM we have to */ + ssp = shstk_alloc_thread_stack(p, args); + if (IS_ERR_VALUE(ssp)) + return PTR_ERR((void *)ssp); + *childregs = *(current_pt_regs()); /* Turn off status.VS */ riscv_v_vstate_off(childregs); if (usp) /* User fork */ childregs->sp = usp; + /* if needed, set new ssp */ + ssp ? set_active_shstk(p, ssp) : 0; if (clone_flags & CLONE_SETTLS) childregs->tp = tls; childregs->a0 = 0; /* Return value of fork() */ diff --git a/arch/riscv/kernel/usercfi.c b/arch/riscv/kernel/usercfi.c index 0b3bbb41490a..ec3d78efd6f3 100644 --- a/arch/riscv/kernel/usercfi.c +++ b/arch/riscv/kernel/usercfi.c @@ -19,6 +19,41 @@ #define SHSTK_ENTRY_SIZE sizeof(void *) +bool is_shstk_enabled(struct task_struct *task) +{ + return task->thread_info.user_cfi_state.ubcfi_en; +} + +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; + task->thread_info.user_cfi_state.shdw_stk_size = size; +} + +unsigned long get_shstk_base(struct task_struct *task, unsigned long *size) +{ + if (size) + *size = task->thread_info.user_cfi_state.shdw_stk_size; + return task->thread_info.user_cfi_state.shdw_stk_base; +} + +void set_active_shstk(struct task_struct *task, unsigned long shstk_addr) +{ + task->thread_info.user_cfi_state.user_shdw_stk = shstk_addr; +} + +/* + * 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 + */ +static unsigned long calc_shstk_size(unsigned long size) +{ + if (size) + return PAGE_ALIGN(size); + + return PAGE_ALIGN(min_t(unsigned long long, rlimit(RLIMIT_STACK), SZ_4G)); +} + /* * Writes on shadow stack can either be `sspush` or `ssamoswap`. `sspush` can happen * implicitly on current shadow stack pointed to by CSR_SSP. `ssamoswap` takes pointer to @@ -141,3 +176,88 @@ SYSCALL_DEFINE3(map_shadow_stack, unsigned long, addr, unsigned long, size, unsi return allocate_shadow_stack(addr, aligned_size, size, set_tok); } + +/* + * This gets called during clone/clone3/fork. And is needed to allocate a shadow stack for + * cases where CLONE_VM is specified and thus a different stack is specified by user. We + * thus need a separate shadow stack too. How does separate shadow stack is specified by + * user is still being debated. Once that's settled, remove this part of the comment. + * This function simply returns 0 if shadow stack are not supported or if separate shadow + * stack allocation is not needed (like in case of !CLONE_VM) + */ +unsigned long shstk_alloc_thread_stack(struct task_struct *tsk, + const struct kernel_clone_args *args) +{ + unsigned long addr, size; + + /* If shadow stack is not supported, return 0 */ + if (!cpu_supports_shadow_stack()) + return 0; + + /* + * If shadow stack is not enabled on the new thread, skip any + * switch to a new shadow stack. + */ + if (!is_shstk_enabled(tsk)) + return 0; + + /* + * For CLONE_VFORK the child will share the parents shadow stack. + * Set base = 0 and size = 0, this is special means to track this state + * so the freeing logic run for child knows to leave it alone. + */ + if (args->flags & CLONE_VFORK) { + set_shstk_base(tsk, 0, 0); + return 0; + } + + /* + * For !CLONE_VM the child will use a copy of the parents shadow + * stack. + */ + if (!(args->flags & CLONE_VM)) + return 0; + + /* + * reaching here means, CLONE_VM was specified and thus a separate shadow + * stack is needed for new cloned thread. Note: below allocation is happening + * using current mm. + */ + size = calc_shstk_size(args->stack_size); + addr = allocate_shadow_stack(0, size, 0, false); + if (IS_ERR_VALUE(addr)) + return addr; + + set_shstk_base(tsk, addr, size); + + return addr + size; +} + +void shstk_release(struct task_struct *tsk) +{ + unsigned long base = 0, size = 0; + /* If shadow stack is not supported or not enabled, nothing to release */ + if (!cpu_supports_shadow_stack() || !is_shstk_enabled(tsk)) + return; + + /* + * When fork() with CLONE_VM fails, the child (tsk) already has a + * shadow stack allocated, and exit_thread() calls this function to + * free it. In this case the parent (current) and the child share + * the same mm struct. Move forward only when they're same. + */ + if (!tsk->mm || tsk->mm != current->mm) + return; + + /* + * We know shadow stack is enabled but if base is NULL, then + * this task is not managing its own shadow stack (CLONE_VFORK). So + * skip freeing it. + */ + base = get_shstk_base(tsk, &size); + if (!base) + return; + + vm_munmap(base, size); + set_shstk_base(tsk, 0, 0); +} -- 2.43.0