Message ID | 20250310-v5_user_cfi_series-v11-8-86b36cbfb910@rivosinc.com (mailing list archive) |
---|---|
State | New |
Headers | show |
Series | riscv control-flow integrity for usermode | expand |
On Mon, Mar 10, 2025 at 11:42 PM Deepak Gupta <debug@rivosinc.com> wrote: > > pte_mkwrite creates PTEs with WRITE encodings for underlying arch. > Underlying arch can have two types of writeable mappings. One that can be > written using regular store instructions. Another one that can only be > written using specialized store instructions (like shadow stack stores). > pte_mkwrite can select write PTE encoding based on VMA range (i.e. > VM_SHADOW_STACK) > > Signed-off-by: Deepak Gupta <debug@rivosinc.com> > Reviewed-by: Alexandre Ghiti <alexghiti@rivosinc.com> > --- > arch/riscv/include/asm/pgtable.h | 7 +++++++ > arch/riscv/mm/pgtable.c | 17 +++++++++++++++++ > 2 files changed, 24 insertions(+) > > diff --git a/arch/riscv/include/asm/pgtable.h b/arch/riscv/include/asm/pgtable.h > index ede43185ffdf..ccd2fa34afb8 100644 > --- a/arch/riscv/include/asm/pgtable.h > +++ b/arch/riscv/include/asm/pgtable.h > @@ -416,6 +416,10 @@ static inline pte_t pte_wrprotect(pte_t pte) > > /* static inline pte_t pte_mkread(pte_t pte) */ > > +struct vm_area_struct; > +pte_t pte_mkwrite(pte_t pte, struct vm_area_struct *vma); > +#define pte_mkwrite pte_mkwrite > + > static inline pte_t pte_mkwrite_novma(pte_t pte) > { > return __pte(pte_val(pte) | _PAGE_WRITE); > @@ -749,6 +753,9 @@ static inline pmd_t pmd_mkyoung(pmd_t pmd) > return pte_pmd(pte_mkyoung(pmd_pte(pmd))); > } > > +pmd_t pmd_mkwrite(pmd_t pmd, struct vm_area_struct *vma); > +#define pmd_mkwrite pmd_mkwrite > + > static inline pmd_t pmd_mkwrite_novma(pmd_t pmd) > { > return pte_pmd(pte_mkwrite_novma(pmd_pte(pmd))); > diff --git a/arch/riscv/mm/pgtable.c b/arch/riscv/mm/pgtable.c > index 4ae67324f992..be5d38546bb3 100644 > --- a/arch/riscv/mm/pgtable.c > +++ b/arch/riscv/mm/pgtable.c > @@ -155,3 +155,20 @@ pmd_t pmdp_collapse_flush(struct vm_area_struct *vma, > return pmd; > } > #endif /* CONFIG_TRANSPARENT_HUGEPAGE */ > + > +pte_t pte_mkwrite(pte_t pte, struct vm_area_struct *vma) > +{ > + if (vma->vm_flags & VM_SHADOW_STACK) > + return pte_mkwrite_shstk(pte); > + > + return pte_mkwrite_novma(pte); > +} > + > +pmd_t pmd_mkwrite(pmd_t pmd, struct vm_area_struct *vma) > +{ > + if (vma->vm_flags & VM_SHADOW_STACK) > + return pmd_mkwrite_shstk(pmd); > + > + return pmd_mkwrite_novma(pmd); > +} > + > LGTM. Reviewed-by: Zong Li <zong.li@sifive.com> > -- > 2.34.1 > > > _______________________________________________ > linux-riscv mailing list > linux-riscv@lists.infradead.org > http://lists.infradead.org/mailman/listinfo/linux-riscv
diff --git a/arch/riscv/include/asm/pgtable.h b/arch/riscv/include/asm/pgtable.h index ede43185ffdf..ccd2fa34afb8 100644 --- a/arch/riscv/include/asm/pgtable.h +++ b/arch/riscv/include/asm/pgtable.h @@ -416,6 +416,10 @@ static inline pte_t pte_wrprotect(pte_t pte) /* static inline pte_t pte_mkread(pte_t pte) */ +struct vm_area_struct; +pte_t pte_mkwrite(pte_t pte, struct vm_area_struct *vma); +#define pte_mkwrite pte_mkwrite + static inline pte_t pte_mkwrite_novma(pte_t pte) { return __pte(pte_val(pte) | _PAGE_WRITE); @@ -749,6 +753,9 @@ static inline pmd_t pmd_mkyoung(pmd_t pmd) return pte_pmd(pte_mkyoung(pmd_pte(pmd))); } +pmd_t pmd_mkwrite(pmd_t pmd, struct vm_area_struct *vma); +#define pmd_mkwrite pmd_mkwrite + static inline pmd_t pmd_mkwrite_novma(pmd_t pmd) { return pte_pmd(pte_mkwrite_novma(pmd_pte(pmd))); diff --git a/arch/riscv/mm/pgtable.c b/arch/riscv/mm/pgtable.c index 4ae67324f992..be5d38546bb3 100644 --- a/arch/riscv/mm/pgtable.c +++ b/arch/riscv/mm/pgtable.c @@ -155,3 +155,20 @@ pmd_t pmdp_collapse_flush(struct vm_area_struct *vma, return pmd; } #endif /* CONFIG_TRANSPARENT_HUGEPAGE */ + +pte_t pte_mkwrite(pte_t pte, struct vm_area_struct *vma) +{ + if (vma->vm_flags & VM_SHADOW_STACK) + return pte_mkwrite_shstk(pte); + + return pte_mkwrite_novma(pte); +} + +pmd_t pmd_mkwrite(pmd_t pmd, struct vm_area_struct *vma) +{ + if (vma->vm_flags & VM_SHADOW_STACK) + return pmd_mkwrite_shstk(pmd); + + return pmd_mkwrite_novma(pmd); +} +