From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wr1-f41.google.com (mail-wr1-f41.google.com [209.85.221.41]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 15EBB26FA69 for ; Thu, 10 Apr 2025 11:04:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.41 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744283084; cv=none; b=Zv4L9+3KXWv5YCxUsLHPXQLbNkjuv8qjEc9U67AD+ZFEwK9iCaEM8RSOW4wKHWW34FADPJJF+kvJFgjsZcTVR/Jq6syGpRo2up3tFb1uQiFV980ycagvPX2FuaRgCApaPRCJoIhYmkFmQuMCnu09AsBJVaqTO5cpNmGGJacnYGY= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744283084; c=relaxed/simple; bh=kOlPkOa9RAWaHmxwXq9vXkGSssRFc7sUC8EQNbcnfPY=; h=Mime-Version:Content-Type:Date:Message-Id:Cc:To:From:Subject: References:In-Reply-To; b=tKW3atufKIRoFTJP7/u21PoMEDDoNGy9t2PN8k2UAxqD7StxZTJXDXOjsc6MzQFPEgvbgI6FpmgA+Fv+PS5LsVSwKOIY2+0ro++w1VCGU84i+alieos0Cu+YJdcCgdeZEwZrnt5lDHWZY4JMk4LBr6VLJbQ9pOsT/ay9xiMTBg0= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=ventanamicro.com; spf=pass smtp.mailfrom=ventanamicro.com; dkim=pass (2048-bit key) header.d=ventanamicro.com header.i=@ventanamicro.com header.b=pVFHYrgW; arc=none smtp.client-ip=209.85.221.41 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=ventanamicro.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=ventanamicro.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=ventanamicro.com header.i=@ventanamicro.com header.b="pVFHYrgW" Received: by mail-wr1-f41.google.com with SMTP id ffacd0b85a97d-391295490c8so23865f8f.3 for ; Thu, 10 Apr 2025 04:04:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ventanamicro.com; s=google; t=1744283080; x=1744887880; darn=vger.kernel.org; h=in-reply-to:references:subject:from:to:cc:message-id:date :content-transfer-encoding:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=FwTqTcXuZVwmB1gK8gAQzMNft2dcqvTwt7yn+4sQNuo=; b=pVFHYrgW6CsPBVfhDp8H0SHh07ORi0+k92JSM7WHod+l6GpALDm6Af/8R4cZodTjSI tJhmQC0ISSnfE51tew20VXscex2ugzAatyW+gaNQVdqFVsPCNEtL0hzsectLv90gFFtt oVuwwysWrn9jEcSsg7T6KkbCebzr4gntAQJheGXE5uqMjbzNUT2JWYiPhRLq9y69avjt blLUmxwM5ibExPlEoUUbX0wnvPL/rJd7iUzELewMF/309FFzjGxOdZqxZeMaqwE83MP6 zgXi1m4nmEywod9fgORmfD+WzNDeuueMfkonfaKqfm00t7VTa2R1DFvAman71MpISxGx m5fQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1744283080; x=1744887880; h=in-reply-to:references:subject:from:to:cc:message-id:date :content-transfer-encoding:mime-version:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=FwTqTcXuZVwmB1gK8gAQzMNft2dcqvTwt7yn+4sQNuo=; b=fMr9R2AJk6kE8ufPuNkOjsQOFSC9oSmbpxCDEkiP0TMQzDl/Kjc4IYjteh6yIEGC7u cCyjhn3HTGoNg/mEn6Ao0kB728KjArDFa4nftOUY40rwJiZ4qgkV/cgct/xzzYl5rPSQ D/J2ePRcIRqOOkZTFapO5EhjxblUr+V7CeaHo51Q/O0Og3O/uncXV55Mab/McLlWy9m/ VyiOgGPg6qoa8eClTDpbQUSbrF/wtHLt7cn/6gWtcgwXCU1FEnm8GLWhf/YgF65s1kOL pz8CSFKVv++OcsA2vzoAO4E2PogjAAaLnjvOC6gnvtMqo1zfWsvyXDSN7EAePnTLkM1W h0tQ== X-Forwarded-Encrypted: i=1; AJvYcCXxVY6NI1Yarc4xD9NozK4Qnt2OkWqekSqzM9KUp6ZqaaTjNDnHcFGbfOzuNVTQ0fSVLGwDmCxShuMQ@vger.kernel.org X-Gm-Message-State: AOJu0Yz4cBUx5d5LcbDv6m1OlI3Wga8OvpVgipYJmZoBRBMIQFLIs/4s fcZp1CpeePhxrvtHqpiVWDj3bkzASJK7Zs3ul0rTfO8cTSriLosfyImXYWD5LLY= X-Gm-Gg: ASbGncsmXR7kDJkNpvrhu7XaIJtDCOEJb8Wnt5ybLs9SBAqgK/cpMyweR74Z/fBDGqj XJQXXdYaVxYJ1WXh/jqMutNE8PvQFHxX0kKB78jPWnXfA/k8mCjM9nS7GaJ2u3Lswza525Ujkdm 8qs4lyQ5Ug9IqT56taZMsIEW1B2ZAayRU4vqrNcqI806eg0IQ9VGnZFOvo6mZ6ZW8j2xtHqF8xG OAXd4UQDbSa4APwD3IaGbSKl1RMzMG/V85tLIbr/KEd6qxPFrDqMkwNWMEWRw19UCSeGCO7sJOk 7Tk44jyC9k82vxX2tJb+6AFkKPGCNLxQsl45a4m16jMtUVkm X-Google-Smtp-Source: AGHT+IFTHVjvMm7SI9zG+YTo6xP+VuwjrIfNbL14H9cY5sGUQSewGH6hwjaZZsvjM4ZfhfO2SM4RNA== X-Received: by 2002:a05:6000:18ac:b0:39c:13fa:3e0 with SMTP id ffacd0b85a97d-39d87adcd02mr1983393f8f.9.1744283080239; Thu, 10 Apr 2025 04:04:40 -0700 (PDT) Received: from localhost ([2a02:8308:a00c:e200:7d22:13bb:e539:15ee]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-39d89361186sm4476526f8f.14.2025.04.10.04.04.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 10 Apr 2025 04:04:39 -0700 (PDT) Precedence: bulk X-Mailing-List: linux-arch@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=UTF-8 Date: Thu, 10 Apr 2025 13:04:39 +0200 Message-Id: Cc: , , , , , , , , , , , , , , , , , , , , , "Zong Li" , "linux-riscv" To: "Deepak Gupta" , "Thomas Gleixner" , "Ingo Molnar" , "Borislav Petkov" , "Dave Hansen" , , "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" From: =?utf-8?q?Radim_Kr=C4=8Dm=C3=A1=C5=99?= Subject: Re: [PATCH v12 05/28] riscv: usercfi state for task and save/restore of CSR_SSP on trap entry/exit References: <20250314-v5_user_cfi_series-v12-0-e51202b53138@rivosinc.com> <20250314-v5_user_cfi_series-v12-5-e51202b53138@rivosinc.com> In-Reply-To: <20250314-v5_user_cfi_series-v12-5-e51202b53138@rivosinc.com> 2025-03-14T14:39:24-07:00, Deepak Gupta : > diff --git a/arch/riscv/include/asm/thread_info.h b/arch/riscv/include/as= m/thread_info.h > @@ -62,6 +62,9 @@ struct thread_info { > long user_sp; /* User stack pointer */ > int cpu; > unsigned long syscall_work; /* SYSCALL_WORK_ flags */ > +#ifdef CONFIG_RISCV_USER_CFI > + struct cfi_status user_cfi_state; > +#endif I don't think it makes sense to put all the data in thread_info. kernel_ssp and user_ssp is more than enough and the rest can comfortably live elsewhere in task_struct. thread_info is supposed to be as small as possible -- just spanning multiple cache-lines could be noticeable. > diff --git a/arch/riscv/kernel/entry.S b/arch/riscv/kernel/entry.S > @@ -147,6 +147,20 @@ SYM_CODE_START(handle_exception) > =20 > REG_L s0, TASK_TI_USER_SP(tp) > csrrc s1, CSR_STATUS, t0 > + /* > + * If previous mode was U, capture shadow stack pointer and save it awa= y > + * Zero CSR_SSP at the same time for sanitization. > + */ > + ALTERNATIVE("nop; nop; nop; nop", > + __stringify( \ > + andi s2, s1, SR_SPP; \ > + bnez s2, skip_ssp_save; \ > + csrrw s2, CSR_SSP, x0; \ > + REG_S s2, TASK_TI_USER_SSP(tp); \ > + skip_ssp_save:), > + 0, > + RISCV_ISA_EXT_ZICFISS, > + CONFIG_RISCV_USER_CFI) (I'd prefer this closer to the user_sp and kernel_sp swap, it's breaking the flow here. We also already know if we've returned from userspace or not even without SR_SPP, but reusing the information might tangle the logic.) > csrr s2, CSR_EPC > csrr s3, CSR_TVAL > csrr s4, CSR_CAUSE > @@ -236,6 +250,18 @@ SYM_CODE_START_NOALIGN(ret_from_exception) > csrw CSR_SCRATCH, tp > + > + /* > + * Going back to U mode, restore shadow stack pointer > + */ Are we? I think we can be just as well returning back to kernel-space. Similar to how we can enter the exception handler from kernel-space. > + ALTERNATIVE("nop; nop", > + __stringify( \ > + REG_L s3, TASK_TI_USER_SSP(tp); \ > + csrw CSR_SSP, s3), > + 0, > + RISCV_ISA_EXT_ZICFISS, > + CONFIG_RISCV_USER_CFI) > + Thanks. 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 1FA71C3601E for ; Thu, 10 Apr 2025 11:34:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:In-Reply-To:References:Subject:From:To: Cc:Message-Id:Date:Mime-Version:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=Iu0RZDPBzWxTv+yiFbU/Oysdf2xHCbVH2abhNjZZB7Q=; b=NZnOgDWsDC6xD+ 7GIzePrD7spOxy4G6m1ajxK0CarRj/12GouEMGIR/uS7y3H2HInsnGvcy4ZRtl6rIX11FI7yyyQ/E gWXQzZFzBCVb54CG1oyVScFkrcQxbU4qfcx8L5hhdIJ9cl26OQyT9u8dD6+iAlnAIeWcDvIUtnamv y0e6TZsrKZG9SHurXL1iNH4OFMk5DRR7knL2QotO2dPtf2LuF/6jV9sUshOJHjORoxbQ7Uc76+9xq pVTjyEBLX3cJtqkthr8ZX53olC1mrZGlsDKQnZwbOhK6uh/jRFAzdJdhoeFXaTLYgjKCFrXOP1cud XMjjnqX4zpnGn+jO+cuQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98.2 #2 (Red Hat Linux)) id 1u2qAg-0000000AJeN-2jSK; Thu, 10 Apr 2025 11:34:14 +0000 Received: from mail-wm1-x329.google.com ([2a00:1450:4864:20::329]) by bombadil.infradead.org with esmtps (Exim 4.98.2 #2 (Red Hat Linux)) id 1u2pi5-0000000AA2W-3XW2 for linux-riscv@lists.infradead.org; Thu, 10 Apr 2025 11:04:43 +0000 Received: by mail-wm1-x329.google.com with SMTP id 5b1f17b1804b1-43cf89f81c5so928725e9.2 for ; Thu, 10 Apr 2025 04:04:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ventanamicro.com; s=google; t=1744283080; x=1744887880; darn=lists.infradead.org; h=in-reply-to:references:subject:from:to:cc:message-id:date :content-transfer-encoding:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=FwTqTcXuZVwmB1gK8gAQzMNft2dcqvTwt7yn+4sQNuo=; b=UFhkGG6hwgv87sAOrdJ6qXm4FQqVi0lXpCKbzw5g5fhOXss5vwYYgR5GMEJlJVr94D HYEGVeyND4l++4sqLPkpxVs1YEXVUXGMDZNdFo1DJ9sIPWxpdZMKjvmHg1FrVDQaEjxn tqgHs4g/E06tSS92ZLv32Lzl0ATsFR2rnIQdLkVTlqrcvRm4IrKOHcnQO9h2/tfK9ZuQ aTPhht3TegkPI92QFvX6JIcMDlWg9VM/bZhABm6nSddw/sUqtiUgA8tCZIXKzMeCxu1C 30rs6cbA9ksVRh9peLDFclgJSwfhJ/EtIpjTfJhXWn/GREFFBQxXEawUMnpvDU4a1mcV fE+Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1744283080; x=1744887880; h=in-reply-to:references:subject:from:to:cc:message-id:date :content-transfer-encoding:mime-version:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=FwTqTcXuZVwmB1gK8gAQzMNft2dcqvTwt7yn+4sQNuo=; b=R4fiHSbB8VxVnvcu87/LzkweV295T7JrM+X5zGuOIL+0TaRMKh/1jBNpDOH6hdtEcB LTObuj/OtBRHvVt5MylS4uXUlmrrkmujAG9gjo4YxVY0hiu4eGJYKTONmuFtVkA5MmOx W88tiOfzpiTRn8e+ICj+/l2HCR7S16ajLA4H+4R3rYKz2KIiFCfL9p50Vm5nmcX23Skp ORXYMt14nGmc3oOZj7K5RrALuwWOKgxo2zSSSUommcsv6XfTIUkT04bOe1uCt+kDKunI qw3F5g6XkVTW1OqwSCGrBPlmdXx542m5xOrdGmEFMpcyl8ocThqgGAK5VIFwMnJk5107 Aouw== X-Forwarded-Encrypted: i=1; AJvYcCVMeieB1GWi+dYGnFC26IS8VPaZwHiZcpeNYpAeijCqjqG8gLTVAuyL/9KyFVDsXXneEn74orfZB9IpiQ==@lists.infradead.org X-Gm-Message-State: AOJu0Yz0XbGMN28r2v6zN9L+9DqcMBNrlYshjzE0djrDBZZDhRDFBjQq ZbaLLnEFrZFXLQ/RlFTUgCIcEsMqEv9oBuS10hfSMfLRadFsl0drjdVnfUFmT+o= X-Gm-Gg: ASbGncvkER8gRSQ9LLNq07aebz0gppGF9Nm7JTw7UwaFB1NWJuuQ5GG9vkLRNvwEwWm Wohl7EkFjmCLl2eU+PzgL8GljuSEVogeFpYTwjfITmRjbqrMm00Terki++efETJir7LFr51o2BS zwevUaTkhw+0VfvAtevpdG/aV1wtNhuAW+lAMc9nbYx3qrlMRgMMWDK3YQdD727Ier8lM9fuaH1 gmg9COBMEh4EyBsPYhtFhOfZDf7TXETrFpLmiMd5qP5wBvC/nys2NnShulq16X8E16/jGF7//QN tmqKDsc+fjXd1vfSR5FykIYewnQpWHv5UehrvHiV8HxZzLVV X-Google-Smtp-Source: AGHT+IFTHVjvMm7SI9zG+YTo6xP+VuwjrIfNbL14H9cY5sGUQSewGH6hwjaZZsvjM4ZfhfO2SM4RNA== X-Received: by 2002:a05:6000:18ac:b0:39c:13fa:3e0 with SMTP id ffacd0b85a97d-39d87adcd02mr1983393f8f.9.1744283080239; Thu, 10 Apr 2025 04:04:40 -0700 (PDT) Received: from localhost ([2a02:8308:a00c:e200:7d22:13bb:e539:15ee]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-39d89361186sm4476526f8f.14.2025.04.10.04.04.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 10 Apr 2025 04:04:39 -0700 (PDT) Mime-Version: 1.0 Date: Thu, 10 Apr 2025 13:04:39 +0200 Message-Id: Cc: , , , , , , , , , , , , , , , , , , , , , "Zong Li" , "linux-riscv" To: "Deepak Gupta" , "Thomas Gleixner" , "Ingo Molnar" , "Borislav Petkov" , "Dave Hansen" , , "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" From: =?utf-8?q?Radim_Kr=C4=8Dm=C3=A1=C5=99?= Subject: Re: [PATCH v12 05/28] riscv: usercfi state for task and save/restore of CSR_SSP on trap entry/exit References: <20250314-v5_user_cfi_series-v12-0-e51202b53138@rivosinc.com> <20250314-v5_user_cfi_series-v12-5-e51202b53138@rivosinc.com> In-Reply-To: <20250314-v5_user_cfi_series-v12-5-e51202b53138@rivosinc.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20250410_040441_886337_23AC9055 X-CRM114-Status: GOOD ( 12.09 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org 2025-03-14T14:39:24-07:00, Deepak Gupta : > diff --git a/arch/riscv/include/asm/thread_info.h b/arch/riscv/include/asm/thread_info.h > @@ -62,6 +62,9 @@ struct thread_info { > long user_sp; /* User stack pointer */ > int cpu; > unsigned long syscall_work; /* SYSCALL_WORK_ flags */ > +#ifdef CONFIG_RISCV_USER_CFI > + struct cfi_status user_cfi_state; > +#endif I don't think it makes sense to put all the data in thread_info. kernel_ssp and user_ssp is more than enough and the rest can comfortably live elsewhere in task_struct. thread_info is supposed to be as small as possible -- just spanning multiple cache-lines could be noticeable. > diff --git a/arch/riscv/kernel/entry.S b/arch/riscv/kernel/entry.S > @@ -147,6 +147,20 @@ SYM_CODE_START(handle_exception) > > REG_L s0, TASK_TI_USER_SP(tp) > csrrc s1, CSR_STATUS, t0 > + /* > + * If previous mode was U, capture shadow stack pointer and save it away > + * Zero CSR_SSP at the same time for sanitization. > + */ > + ALTERNATIVE("nop; nop; nop; nop", > + __stringify( \ > + andi s2, s1, SR_SPP; \ > + bnez s2, skip_ssp_save; \ > + csrrw s2, CSR_SSP, x0; \ > + REG_S s2, TASK_TI_USER_SSP(tp); \ > + skip_ssp_save:), > + 0, > + RISCV_ISA_EXT_ZICFISS, > + CONFIG_RISCV_USER_CFI) (I'd prefer this closer to the user_sp and kernel_sp swap, it's breaking the flow here. We also already know if we've returned from userspace or not even without SR_SPP, but reusing the information might tangle the logic.) > csrr s2, CSR_EPC > csrr s3, CSR_TVAL > csrr s4, CSR_CAUSE > @@ -236,6 +250,18 @@ SYM_CODE_START_NOALIGN(ret_from_exception) > csrw CSR_SCRATCH, tp > + > + /* > + * Going back to U mode, restore shadow stack pointer > + */ Are we? I think we can be just as well returning back to kernel-space. Similar to how we can enter the exception handler from kernel-space. > + ALTERNATIVE("nop; nop", > + __stringify( \ > + REG_L s3, TASK_TI_USER_SSP(tp); \ > + csrw CSR_SSP, s3), > + 0, > + RISCV_ISA_EXT_ZICFISS, > + CONFIG_RISCV_USER_CFI) > + Thanks. _______________________________________________ linux-riscv mailing list linux-riscv@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-riscv