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 03CDDCED26B for ; Tue, 8 Oct 2024 05:31:22 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7C2096B0083; Tue, 8 Oct 2024 01:31:22 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 771576B0085; Tue, 8 Oct 2024 01:31:22 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 611FC6B0088; Tue, 8 Oct 2024 01:31:22 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 3F7FC6B0083 for ; Tue, 8 Oct 2024 01:31:22 -0400 (EDT) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id A09151A0C7F for ; Tue, 8 Oct 2024 05:31:20 +0000 (UTC) X-FDA: 82649311962.26.CD6C3E9 Received: from mail-pl1-f174.google.com (mail-pl1-f174.google.com [209.85.214.174]) by imf26.hostedemail.com (Postfix) with ESMTP id C2987140004 for ; Tue, 8 Oct 2024 05:31:19 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b="C62hl7B/"; spf=pass (imf26.hostedemail.com: domain of debug@rivosinc.com designates 209.85.214.174 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=1728365301; 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=YVWWt1mcV86CG5um5aUopa5TS6G5mg+pb0D64jrI0rI=; b=Skcjt0LxRVfxK237IZk2xL+9sPT8YXf2w6AkBAbnTySZZnzDBZGBHR74/fA1iNp1dZ6Kdx PaNWok9oSxYx7PUstRYbNGIhkh07QBeLyXNxQel/N/wrB/Hs6SNxSmzeOxKeo/l4FzJv33 3sGbmZ8FjcSdldWqa4qADxUfNImuc8k= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b="C62hl7B/"; spf=pass (imf26.hostedemail.com: domain of debug@rivosinc.com designates 209.85.214.174 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1728365301; a=rsa-sha256; cv=none; b=wvzdvXHD7mt/pvtA27OKKjPyRRjax4O9dJoMVLrxa7+cFwCC386ihLOONWDBV+uSP7VvZ6 y1ZdqMXSjL9Dg4vns+up0n3wQi4CMYIjp/9CvX078m1j6OGhrGn9ZgZsWg0LDBm+fdxPmg mBVmAC64U11Ku12ZxM3ctKE2Hh89b0s= Received: by mail-pl1-f174.google.com with SMTP id d9443c01a7336-20b78ee6298so33196295ad.2 for ; Mon, 07 Oct 2024 22:31:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1728365478; x=1728970278; darn=kvack.org; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:references:message-id:subject:cc:to:from:date:from:to :cc:subject:date:message-id:reply-to; bh=YVWWt1mcV86CG5um5aUopa5TS6G5mg+pb0D64jrI0rI=; b=C62hl7B/L3hsv9oNCKoswW9Cp/ic1Y72LvFmggLjlMnfziO/yG7nnpznmICJlkaApy IpHe9bElYTtZuEV0I+wp+grEHz+y020dUNsUHcf5KWDrbxfL2RTYhPRUiJEwhGgZuMzH GB6syuIUwmTc68Imw1HU/8WRm4c0lGWWHDXXMJc+gNy4QQqGPXK/Z1x9E0SZ9ZnC0gmz iGLhMQYObHRMzzmz+glcivNcFuiLIqeCo7c8ZOFMP747oS6LU2p8rMio0EJEZS+hn0r8 JJS16D6OMI4Cq6hCTeW6QDtVKQFZL8r+vX3v3Am8TGJSmP7mk0GNjVviwI3qHvKdQ7j9 rcoA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728365478; x=1728970278; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=YVWWt1mcV86CG5um5aUopa5TS6G5mg+pb0D64jrI0rI=; b=xRzUqot95VYPaV6RDWXDKnw60qy1oZeDqfXdlTfrPvo1ECahru0D2GqwUI2afUR7Vs +okHXVe02jLVvY/CdfLthcEhWFN2C4IpduaV7zSfX5iTRWaKtj5lEfNDf5YDLlqXnA6a AoLVyiOr4htmVq4JS/W1XiH+Jje/Z9vY8w8IwFARBlkLqMfvOIo840vip7myWY3+6y/1 jxq6Sgkm94uY3pJxQ3vDMUL+7Z23VjVu9JlRYV6mf6YijDfa/ph2SaleZzfKyD43YDL6 6CDFSq99B5OK0tCa9Cx93eiSh0jMUay55QUZfP4I3/zlpvlTe69kFmJuHU2OdkmfIrMe CFmA== X-Forwarded-Encrypted: i=1; AJvYcCXyktrZu9hO12c67GllqBLt+jt8lpDohPmOdIS+qYNCC3eugryrKRLd8w6kmpj316gkRuRfpJE0Eg==@kvack.org X-Gm-Message-State: AOJu0YyK+uaoerf8ZQlZxOiAExV4QnsA6Y/yLRsrZYg7T+4r834K7Wbf pTXnzr57EpzQ1USoZTKuBuNRjnvwzLpW50Irx00EIhyNYJY6ICBlxDaoHZB532o= X-Google-Smtp-Source: AGHT+IGthWcJ/4TKf5GSv621dnUUfPBgutAW7rucqDwxG39GX8eeue8mfmIeD0tAu25NcYxCwQyqlA== X-Received: by 2002:a17:902:d4cd:b0:20b:7731:e3f8 with SMTP id d9443c01a7336-20bfe00b926mr216344355ad.26.1728365478346; Mon, 07 Oct 2024 22:31:18 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-20c1393a292sm48470455ad.173.2024.10.07.22.31.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 07 Oct 2024 22:31:18 -0700 (PDT) Date: Mon, 7 Oct 2024 22:31:14 -0700 From: Deepak Gupta To: Zong Li Cc: 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 , 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 Subject: Re: [PATCH 16/33] riscv/shstk: If needed allocate a new shadow stack on clone Message-ID: References: <20241001-v5_user_cfi_series-v1-0-3ba65b6e550f@rivosinc.com> <20241001-v5_user_cfi_series-v1-16-3ba65b6e550f@rivosinc.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Disposition: inline Content-Transfer-Encoding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weMlfqdhsGzzTCJc1Cg4cA5Cz0jpeohg191j G7AML/QZ kIkkj69amM1v3FKDy7vnHTfmRU0cHTCSOwMedSiPwtQK7FJVTUkZV7MfEEwjuqb6M1vpsgumy7FZoolA2+dTTFJkQSNt8M5ZZHNRV0o4xUmSaehHCiJ905BRyE43DUIfwt7vNOS+jd2TtiiGwuw/uW+/0dyZ/gPkjRB5nBo1sGkt5DCOQse9oJDAoL1cPIIEUc8msFclxX7LZmW6wySRz103Hy1qui+yIp5bmJ8xSkSTZ0mlTq4J+423hwZ2oBsbtvtSUmB27pxPfVbgDovJemX6oVwML4BPLgk5ouS/tcRsd6jD+xFTo3q37LrI+5CM0XtsvUJU8LYkxweebSYcw4MXAZEVhW8qLEJoFz+CUJwPvLMaq1CUytfLOMxcKcQB6K0pelYrxPcUkAwXWqU/Oy7YCckeOw8BDS3sSk5bla9+l5hbxom17YuO7RXcEgTPl5oShzmufuMn0kVTuScIp/GhCN1nsisrERhoU1YITb8VahI6qKJOSkIGbLcIP/YzzwklVJrb02hBZaOrsMHs4X113cQ== 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: On Tue, Oct 08, 2024 at 01:16:17PM +0800, Zong Li wrote: >On Tue, Oct 8, 2024 at 7:30 AM Deepak Gupta wrote: >> >> On Mon, Oct 07, 2024 at 04:17:47PM +0800, Zong Li wrote: >> >On Wed, Oct 2, 2024 at 12:20 AM Deepak Gupta wrote: >> >> >> >> 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. >> >> >> >> Signed-off-by: Deepak Gupta >> >> --- >> >> arch/riscv/include/asm/usercfi.h | 25 ++++++++ >> >> ... snipped... >> >> >> + >> >> +/* >> >> + * 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)) >> > >> >Hi Deepak, >> >Should it be '!' is_shstk_enabled(tsk)? >> >> Yes it is a bug. It seems like fork without CLONE_VM or with CLONE_VFORK, it was returning >> 0 anyways. And in the case of CLONE_VM (used by pthread), it was not doing the right thing. > >Hi Deepak, >I'd like to know if I understand correctly. Could I know whether there >might also be a risk when the user program doesn't enable the CFI and >the kernel doesn't activate CFI. Because this flow will still try to >allocate the shadow stack and execute the ssamowap command. Thanks `shstk_alloc_thread_stack` is only called from `copy_thread` and allocates and returns non-zero (positive value) for ssp only if `CLONE_VM` is specified. `CLONE_VM` means that address space is shared and userspace has allocated separate stack. This flow is ensuring that newly created thread with separate data stack gets a separate shadow stack as well. Retruning zero value from `shstk_alloc_thread_stack` means that, no need to allocate a shadow stack. If you look at `copy_thread` function, it simply sets the returned ssp in newly created task's task_struct (if it was non-zero). If returned ssp was zero, `copy_thread` doesn't do anything. Thus whatever is current task settings are that will be copied over to new forked/cloned task. If current task had shadow stack enabled, new task will also get it enabled at same address (to be COWed later). Any task get shadow stack enabled for first time using new prctls (see prctl patches). So only time `ssamoswap` will be exercised will be are - User issues enabling `prctl` (it'll be issued from loader) - fork/clone happens In both cases, it is guarded against checks of whether cpu supports it and task has shadow stack enabled. Let me know if you think I missed any flow. > >> Most of the testing has been with busybox build (independent binaries0 driven via buildroot >> setup. Wondering why it wasn't caught. >> >> Anyways, will fix it. Thanks for catching it. >> >> > >> >> + return 0; >> >> + >> >> + /*