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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id D8883C636D4 for ; Mon, 13 Feb 2023 14:38:21 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229967AbjBMOiT (ORCPT ); Mon, 13 Feb 2023 09:38:19 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58330 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229655AbjBMOiO (ORCPT ); Mon, 13 Feb 2023 09:38:14 -0500 Received: from mail-pg1-x52c.google.com (mail-pg1-x52c.google.com [IPv6:2607:f8b0:4864:20::52c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 27717974A for ; Mon, 13 Feb 2023 06:37:57 -0800 (PST) Received: by mail-pg1-x52c.google.com with SMTP id 24so8236143pgt.7 for ; Mon, 13 Feb 2023 06:37:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20210112.gappssmtp.com; s=20210112; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=v46IGf7aK9w6Ujjdk9pH0Sd2OlQiZacAgD2cBvk1jT0=; b=SzFpKuzH1Wg1EhWOGZPaNWQd2POITNEL5ZP8sLHp25iK+Mn6828HZ6U13HeQCN93Sx +0ZNI0ubnqjUSBiqLTF8TSzT8ESdFmVXQ0TpJZ3Qm5ltt+EMXxjri/+uueQF1nGjGMA4 stDvus1Ojiffvsbq+nnE/UFkipL/riZobKSSy4IW/WVEusJyPLHO1nE6z5Bpy4fnOsgP kJ51zGJGuGpgfXmSA8YquHUgckB/TBqhkvuuQyNOeijvGHmV19pXSOA3v2MlP7wD95ls UEl9GtUVFINyfXgxkowUI5R/zjVUa2yd5uwaFVOYIaNuZiIogxSqqpN2//Dbkl2WDX51 yzPA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=in-reply-to: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=v46IGf7aK9w6Ujjdk9pH0Sd2OlQiZacAgD2cBvk1jT0=; b=4zAKATGEM8/SJVBj0vGd22d/iO4jRzDo21+PuGuykTiZBS5Pp2OnlaCTqCtnC77gT/ fv+qoyMw91A3QBoChBqrJNFVsYlsaqG+1QF4Vm9owvJZqMGcVfG+LgN6Z31PlRCrNfKk ihtfSH1FQzDQDixLlaJRIcduAMS7Lg9WjzK42UbuHqy/5arQvFvhRHDHqGgazY28VluQ VhVWjwCC3jEX+evoA2oXjOpheYUg/94szT9cb/HxPxiCl7GyPvNw8LpMmxbmOrMlncAQ ZTsMtoHpWOzBMtV1aePu4LrAV3RHzsmMYVVA/t/la+3er73DYG+1bQA9sBOpofSb8LxD 1kDg== X-Gm-Message-State: AO0yUKU8thoX4bFBsPlIThhUz2rmGaHZt5doXEFZfE4fWlO9/Me8oxt2 4PB0pxgbc+9P0cypU5IhUmuilg== X-Google-Smtp-Source: AK7set/DKESMToA1CvFzD8s7hPzM1C6fYF5uRXkJ8n+Af4LEgHbtdNDBVTegNt3NTjuOhT2mPBEStg== X-Received: by 2002:a62:4ec3:0:b0:5a8:c40e:891c with SMTP id c186-20020a624ec3000000b005a8c40e891cmr1406360pfb.31.1676299076568; Mon, 13 Feb 2023 06:37:56 -0800 (PST) Received: from debug.ba.rivosinc.com ([66.220.2.162]) by smtp.gmail.com with ESMTPSA id t13-20020a62ea0d000000b0057fec210d33sm8028139pfh.152.2023.02.13.06.37.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 13 Feb 2023 06:37:56 -0800 (PST) Date: Mon, 13 Feb 2023 06:37:54 -0800 From: Deepak Gupta To: David Hildenbrand Cc: linux-kernel@vger.kernel.org, linux-riscv@lists.infradead.org, Andrew Morton , linux-mm@kvack.org Subject: Re: [PATCH v1 RFC Zisslpcfi 11/20] mmu: maybe_mkwrite updated to manufacture shadow stack PTEs Message-ID: <20230213143754.GC3943238@debug.ba.rivosinc.com> References: <20230213045351.3945824-1-debug@rivosinc.com> <20230213045351.3945824-12-debug@rivosinc.com> <2d6eefb8-c7c5-7d32-9a75-ae716f828cd9@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Disposition: inline In-Reply-To: <2d6eefb8-c7c5-7d32-9a75-ae716f828cd9@redhat.com> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Feb 13, 2023 at 01:05:16PM +0100, David Hildenbrand wrote: >On 13.02.23 05:53, Deepak Gupta wrote: >>maybe_mkwrite creates PTEs with WRITE encodings for underlying arch if >>VM_WRITE is turned on in vma->vm_flags. Shadow stack memory is a write- >>able memory except it can only be written by certain specific >>instructions. This patch allows maybe_mkwrite to create shadow stack PTEs >>if vma is shadow stack VMA. Each arch can define which combination of VMA >>flags means a shadow stack. >> >>Additionally pte_mkshdwstk must be provided by arch specific PTE >>construction headers to create shadow stack PTEs. (in arch specific >>pgtable.h). >> >>This patch provides dummy/stub pte_mkshdwstk if CONFIG_USER_SHADOW_STACK >>is not selected. >> >>Signed-off-by: Deepak Gupta >>--- >> include/linux/mm.h | 23 +++++++++++++++++++++-- >> include/linux/pgtable.h | 4 ++++ >> 2 files changed, 25 insertions(+), 2 deletions(-) >> >>diff --git a/include/linux/mm.h b/include/linux/mm.h >>index 8f857163ac89..a7705bc49bfe 100644 >>--- a/include/linux/mm.h >>+++ b/include/linux/mm.h >>@@ -1093,6 +1093,21 @@ static inline unsigned long thp_size(struct page *page) >> void free_compound_page(struct page *page); >> #ifdef CONFIG_MMU >>+ >>+#ifdef CONFIG_USER_SHADOW_STACK >>+bool arch_is_shadow_stack_vma(struct vm_area_struct *vma); >>+#endif >>+ >>+static inline bool >>+is_shadow_stack_vma(struct vm_area_struct *vma) >>+{ >>+#ifdef CONFIG_USER_SHADOW_STACK >>+ return arch_is_shadow_stack_vma(vma); >>+#else >>+ return false; >>+#endif >>+} >>+ >> /* >> * Do pte_mkwrite, but only if the vma says VM_WRITE. We do this when >> * servicing faults for write access. In the normal case, do always want >>@@ -1101,8 +1116,12 @@ void free_compound_page(struct page *page); >> */ >> static inline pte_t maybe_mkwrite(pte_t pte, struct vm_area_struct *vma) >> { >>- if (likely(vma->vm_flags & VM_WRITE)) >>- pte = pte_mkwrite(pte); >>+ if (likely(vma->vm_flags & VM_WRITE)) { >>+ if (unlikely(is_shadow_stack_vma(vma))) >>+ pte = pte_mkshdwstk(pte); >>+ else >>+ pte = pte_mkwrite(pte); >>+ } >> return pte; > >Exactly what we are trying to avoid in the x86 approach right now. >Please see the x86 series on details, we shouldn't try reinventing the >wheel but finding a core-mm approach that fits multiple architectures. > >https://lkml.kernel.org/r/20230119212317.8324-1-rick.p.edgecombe@intel.com Thanks David for comment here. I looked at x86 approach. This patch actually written in a way which is not re-inventing wheel and is following a core-mm approach that fits multiple architectures. Change above checks `is_shadow_stack_vma` and if it returns true then only it manufactures shadow stack pte else it'll make a regular writeable mapping. Now if we look at `is_shadow_stack_vma` implementation, it returns false if `CONFIG_USER_SHADOW_STACK` is not defined. If `CONFIG_USER_SHADOW_STACK is defined then it calls `arch_is_shadow_stack_vma` which should be implemented by arch specific code. This allows each architecture to define their own vma flag encodings for shadow stack (riscv chooses presence of only `VM_WRITE` which is analogous to choosen PTE encodings on riscv W=1,R=0,X=0) Additionally pte_mkshdwstk will be nop if not implemented by architecture. Let me know if this make sense. If I am missing something here, let me know. > >-- >Thanks, > >David / dhildenb >