* [PATCH] mm/hmm: reject shadow-stack mappings
@ 2026-11-13 19:55 Gregory Price (Meta)
2026-11-13 19:57 ` Lorenzo Stoakes (ARM)
` (2 more replies)
0 siblings, 3 replies; 22+ messages in thread
From: Gregory Price (Meta) @ 2026-11-13 19:55 UTC (permalink / raw)
To: linux-mm
Cc: linux-kernel, kernel-team, jgg, leon, akpm, david, kas,
dave.hansen, bp, rppt, stable, Lorenzo Stoakes (ARM)
HMM can grant ordinary device write access to a userspace shadow stack.
On x86, pte_write() is true for a shadow-stack PTE, so a populated page
can be returned with HMM_PFN_WRITE without taking a CPU write fault.
Nouveau translates that flag into a writable GPU mapping.
Reject shadow-stack VMAs in hmm_vma_walk_test(). Use the existing
unsupported-range handling: a requested fault returns -EFAULT, while a
snapshot records HMM_PFN_ERROR.
Link: https://lore.kernel.org/linux-mm/20261111-mason-migrate-et-al-fixes-v1-10-3d2454362e8d@kernel.org/
Fixes: 54007f818206 ("mm: Introduce VM_SHADOW_STACK for shadow stack memory")
Cc: stable@vger.kernel.org
Cc: "Lorenzo Stoakes (ARM)" <ljs@kernel.org>
Signed-off-by: Gregory Price (Meta) <gourry@gourry.net>
---
mm/hmm.c | 3 +++
1 file changed, 3 insertions(+)
diff --git a/mm/hmm.c b/mm/hmm.c
index 2f1e98c6b644..fa61ca59d927 100644
--- a/mm/hmm.c
+++ b/mm/hmm.c
@@ -596,6 +596,7 @@ static int hmm_vma_walk_test(unsigned long start, unsigned long end,
struct vm_area_struct *vma = walk->vma;
if (!(vma->vm_flags & (VM_IO | VM_PFNMAP)) &&
+ !vma_test_single_mask(vma, VMA_SHADOW_STACK) &&
vma->vm_flags & VM_READ)
return 0;
@@ -607,6 +608,8 @@ static int hmm_vma_walk_test(unsigned long start, unsigned long end,
* allow write access either. HMM does not support architectures that
* allow write without read.
*
+ * Devices must not gain ordinary write access to shadow stacks.
+ *
* If a fault is requested for an unsupported range then it is a hard
* failure.
*/
--
2.56.0
^ permalink raw reply related [flat|nested] 22+ messages in thread* Re: [PATCH] mm/hmm: reject shadow-stack mappings 2026-11-13 19:55 [PATCH] mm/hmm: reject shadow-stack mappings Gregory Price (Meta) @ 2026-11-13 19:57 ` Lorenzo Stoakes (ARM) 2026-11-13 20:15 ` Gregory Price (Meta) 2026-11-13 20:03 ` David Hildenbrand (Arm) 2026-11-13 20:03 ` Jason Gunthorpe 2 siblings, 1 reply; 22+ messages in thread From: Lorenzo Stoakes (ARM) @ 2026-11-13 19:57 UTC (permalink / raw) To: Gregory Price (Meta) Cc: linux-mm, linux-kernel, kernel-team, jgg, leon, akpm, david, kas, dave.hansen, bp, rppt, stable On Fri, Nov 13, 2026 at 02:55:36PM -0500, Gregory Price (Meta) wrote: > HMM can grant ordinary device write access to a userspace shadow stack. > On x86, pte_write() is true for a shadow-stack PTE, so a populated page > can be returned with HMM_PFN_WRITE without taking a CPU write fault. > > Nouveau translates that flag into a writable GPU mapping. > > Reject shadow-stack VMAs in hmm_vma_walk_test(). Use the existing > unsupported-range handling: a requested fault returns -EFAULT, while a > snapshot records HMM_PFN_ERROR. > > Link: https://lore.kernel.org/linux-mm/20261111-mason-migrate-et-al-fixes-v1-10-3d2454362e8d@kernel.org/ > Fixes: 54007f818206 ("mm: Introduce VM_SHADOW_STACK for shadow stack memory") > Cc: stable@vger.kernel.org > Cc: "Lorenzo Stoakes (ARM)" <ljs@kernel.org> > Signed-off-by: Gregory Price (Meta) <gourry@gourry.net> Thanks for doing the HMM side of this, LGTM and: Reviewed-by: Lorenzo Stoakes (ARM) <ljs@kernel.org> > --- > mm/hmm.c | 3 +++ > 1 file changed, 3 insertions(+) > > diff --git a/mm/hmm.c b/mm/hmm.c > index 2f1e98c6b644..fa61ca59d927 100644 > --- a/mm/hmm.c > +++ b/mm/hmm.c > @@ -596,6 +596,7 @@ static int hmm_vma_walk_test(unsigned long start, unsigned long end, > struct vm_area_struct *vma = walk->vma; > > if (!(vma->vm_flags & (VM_IO | VM_PFNMAP)) && > + !vma_test_single_mask(vma, VMA_SHADOW_STACK) && > vma->vm_flags & VM_READ) > return 0; > > @@ -607,6 +608,8 @@ static int hmm_vma_walk_test(unsigned long start, unsigned long end, > * allow write access either. HMM does not support architectures that > * allow write without read. > * > + * Devices must not gain ordinary write access to shadow stacks. > + * > * If a fault is requested for an unsupported range then it is a hard > * failure. > */ > -- > 2.56.0 > -- Cheers, Lorenzo ^ permalink raw reply [flat|nested] 22+ messages in thread
* Re: [PATCH] mm/hmm: reject shadow-stack mappings 2026-11-13 19:57 ` Lorenzo Stoakes (ARM) @ 2026-11-13 20:15 ` Gregory Price (Meta) 0 siblings, 0 replies; 22+ messages in thread From: Gregory Price (Meta) @ 2026-11-13 20:15 UTC (permalink / raw) To: Lorenzo Stoakes (ARM) Cc: Gregory Price (Meta), linux-mm, linux-kernel, kernel-team, jgg, leon, akpm, david, kas, dave.hansen, bp, rppt, stable On 2026-11-13 19:57 +0000, Lorenzo Stoakes (ARM) wrote: > On Fri, Nov 13, 2026 at 02:55:36PM -0500, Gregory Price (Meta) wrote: > > HMM can grant ordinary device write access to a userspace shadow stack. > > On x86, pte_write() is true for a shadow-stack PTE, so a populated page > > can be returned with HMM_PFN_WRITE without taking a CPU write fault. > > > > Nouveau translates that flag into a writable GPU mapping. > > > > Reject shadow-stack VMAs in hmm_vma_walk_test(). Use the existing > > unsupported-range handling: a requested fault returns -EFAULT, while a > > snapshot records HMM_PFN_ERROR. > > > > Link: https://lore.kernel.org/linux-mm/20261111-mason-migrate-et-al-fixes-v1-10-3d2454362e8d@kernel.org/ > > Fixes: 54007f818206 ("mm: Introduce VM_SHADOW_STACK for shadow stack memory") > > Cc: stable@vger.kernel.org > > Cc: "Lorenzo Stoakes (ARM)" <ljs@kernel.org> > > Signed-off-by: Gregory Price (Meta) <gourry@gourry.net> > > Thanks for doing the HMM side of this, LGTM and: > Fell out of my testing harness when reviewing yours :] Stuff like this is becoming much lower effort than yesteryear. > Reviewed-by: Lorenzo Stoakes (ARM) <ljs@kernel.org> thanks! -- ~Gregory ^ permalink raw reply [flat|nested] 22+ messages in thread
* Re: [PATCH] mm/hmm: reject shadow-stack mappings 2026-11-13 19:55 [PATCH] mm/hmm: reject shadow-stack mappings Gregory Price (Meta) 2026-11-13 19:57 ` Lorenzo Stoakes (ARM) @ 2026-11-13 20:03 ` David Hildenbrand (Arm) 2026-11-13 20:42 ` Gregory Price (Meta) 2026-11-13 20:03 ` Jason Gunthorpe 2 siblings, 1 reply; 22+ messages in thread From: David Hildenbrand (Arm) @ 2026-11-13 20:03 UTC (permalink / raw) To: Gregory Price (Meta), linux-mm Cc: linux-kernel, kernel-team, jgg, leon, akpm, kas, dave.hansen, bp, rppt, stable, Lorenzo Stoakes (ARM) On 11/13/26 20:55, Gregory Price (Meta) wrote: > HMM can grant ordinary device write access to a userspace shadow stack. > On x86, pte_write() is true for a shadow-stack PTE, so a populated page > can be returned with HMM_PFN_WRITE without taking a CPU write fault. > > Nouveau translates that flag into a writable GPU mapping. > > Reject shadow-stack VMAs in hmm_vma_walk_test(). Use the existing > unsupported-range handling: a requested fault returns -EFAULT, while a > snapshot records HMM_PFN_ERROR. > > Link: https://lore.kernel.org/linux-mm/20261111-mason-migrate-et-al-fixes-v1-10-3d2454362e8d@kernel.org/ > Fixes: 54007f818206 ("mm: Introduce VM_SHADOW_STACK for shadow stack memory") > Cc: stable@vger.kernel.org > Cc: "Lorenzo Stoakes (ARM)" <ljs@kernel.org> > Signed-off-by: Gregory Price (Meta) <gourry@gourry.net> > --- > mm/hmm.c | 3 +++ > 1 file changed, 3 insertions(+) > > diff --git a/mm/hmm.c b/mm/hmm.c > index 2f1e98c6b644..fa61ca59d927 100644 > --- a/mm/hmm.c > +++ b/mm/hmm.c > @@ -596,6 +596,7 @@ static int hmm_vma_walk_test(unsigned long start, unsigned long end, > struct vm_area_struct *vma = walk->vma; > > if (!(vma->vm_flags & (VM_IO | VM_PFNMAP)) && > + !vma_test_single_mask(vma, VMA_SHADOW_STACK) && > vma->vm_flags & VM_READ) > return 0; > > @@ -607,6 +608,8 @@ static int hmm_vma_walk_test(unsigned long start, unsigned long end, > * allow write access either. HMM does not support architectures that > * allow write without read. > * > + * Devices must not gain ordinary write access to shadow stacks. > + * > * If a fault is requested for an unsupported range then it is a hard > * failure. > */ Cool, so I can drop e27dc0754e3f4 mm/hmm: refuse shadow stack vmas in hmm_range_fault() :) Acked-by: David Hildenbrand (Arm) <david@kernel.org> I used the comment: + * Shadow stack VMAs must not be written, and there is no particular + * reason to allow read access to shadow stacks from devices, so just + * disallow it entirely. + * -- Cheers, David ^ permalink raw reply [flat|nested] 22+ messages in thread
* Re: [PATCH] mm/hmm: reject shadow-stack mappings 2026-11-13 20:03 ` David Hildenbrand (Arm) @ 2026-11-13 20:42 ` Gregory Price (Meta) 2026-11-13 21:19 ` David Hildenbrand (Arm) 0 siblings, 1 reply; 22+ messages in thread From: Gregory Price (Meta) @ 2026-11-13 20:42 UTC (permalink / raw) To: David Hildenbrand (Arm) Cc: Gregory Price (Meta), linux-mm, linux-kernel, kernel-team, jgg, leon, akpm, kas, dave.hansen, bp, rppt, stable, Lorenzo Stoakes (ARM) On 2026-11-13 21:03 +0100, David Hildenbrand (Arm) wrote: > On 11/13/26 20:55, Gregory Price (Meta) wrote: > Cool, so I can drop > > e27dc0754e3f4 mm/hmm: refuse shadow stack vmas in hmm_range_fault() > > :) > Haven't seen that one :], i suppose *yoink* > Acked-by: David Hildenbrand (Arm) <david@kernel.org> > > > I used the comment: > > + * Shadow stack VMAs must not be written, and there is no particular > + * reason to allow read access to shadow stacks from devices, so just > + * disallow it entirely. > + * > Seems reasonable, maybe addresses Jason's request for additional context. -- ~Gregory ^ permalink raw reply [flat|nested] 22+ messages in thread
* Re: [PATCH] mm/hmm: reject shadow-stack mappings 2026-11-13 20:42 ` Gregory Price (Meta) @ 2026-11-13 21:19 ` David Hildenbrand (Arm) 0 siblings, 0 replies; 22+ messages in thread From: David Hildenbrand (Arm) @ 2026-11-13 21:19 UTC (permalink / raw) To: Gregory Price (Meta) Cc: linux-mm, linux-kernel, kernel-team, jgg, leon, akpm, kas, dave.hansen, bp, rppt, stable, Lorenzo Stoakes (ARM) On 11/13/26 21:42, Gregory Price (Meta) wrote: > On 2026-11-13 21:03 +0100, David Hildenbrand (Arm) wrote: >> On 11/13/26 20:55, Gregory Price (Meta) wrote: >> Cool, so I can drop >> >> e27dc0754e3f4 mm/hmm: refuse shadow stack vmas in hmm_range_fault() >> >> :) >> > > Haven't seen that one :], i suppose *yoink* Not posted yet, I'm preparing a bigger set again, but I'm happy about anything I have to drop! :) -- Cheers, David ^ permalink raw reply [flat|nested] 22+ messages in thread
* Re: [PATCH] mm/hmm: reject shadow-stack mappings 2026-11-13 19:55 [PATCH] mm/hmm: reject shadow-stack mappings Gregory Price (Meta) 2026-11-13 19:57 ` Lorenzo Stoakes (ARM) 2026-11-13 20:03 ` David Hildenbrand (Arm) @ 2026-11-13 20:03 ` Jason Gunthorpe 2026-11-13 20:41 ` Gregory Price (Meta) 2 siblings, 1 reply; 22+ messages in thread From: Jason Gunthorpe @ 2026-11-13 20:03 UTC (permalink / raw) To: Gregory Price (Meta) Cc: linux-mm, linux-kernel, kernel-team, leon, akpm, david, kas, dave.hansen, bp, rppt, stable, Lorenzo Stoakes (ARM) On Fri, Nov 13, 2026 at 02:55:36PM -0500, Gregory Price (Meta) wrote: > HMM can grant ordinary device write access to a userspace shadow stack. > On x86, pte_write() is true for a shadow-stack PTE, so a populated page > can be returned with HMM_PFN_WRITE without taking a CPU write fault. I'm not sure I understand this? The pte is writable but you don't want only DMA to write to it? Is there something special on x86 that makes the shadow stack pte non-writable by the CPU? A few more words here would help Regardless, this seems reasonable to me, there is no use case to write to the shadow stack.. Reviewed-by: Jason Gunthorpe <jgg@nvidia.com> Jason ^ permalink raw reply [flat|nested] 22+ messages in thread
* Re: [PATCH] mm/hmm: reject shadow-stack mappings 2026-11-13 20:03 ` Jason Gunthorpe @ 2026-11-13 20:41 ` Gregory Price (Meta) 2026-11-14 0:20 ` Jason Gunthorpe 0 siblings, 1 reply; 22+ messages in thread From: Gregory Price (Meta) @ 2026-11-13 20:41 UTC (permalink / raw) To: Jason Gunthorpe Cc: Gregory Price (Meta), linux-mm, linux-kernel, kernel-team, leon, akpm, david, kas, dave.hansen, bp, rppt, stable, Lorenzo Stoakes (ARM) On 2026-11-13 16:03 -0400, Jason Gunthorpe wrote: > On Fri, Nov 13, 2026 at 02:55:36PM -0500, Gregory Price (Meta) wrote: > > HMM can grant ordinary device write access to a userspace shadow stack. > > On x86, pte_write() is true for a shadow-stack PTE, so a populated page > > can be returned with HMM_PFN_WRITE without taking a CPU write fault. > > I'm not sure I understand this? The pte is writable but you don't want > only DMA to write to it? > > Is there something special on x86 that makes the shadow stack pte > non-writable by the CPU? > Some of the context is here: https://lore.kernel.org/all/20261111-mason-migrate-et-al-fixes-v1-10-3d2454362e8d@kernel.org/ We had a similar issue with migration, but this should handle the DMA case as well. It's "technically writable" but the kernel is supposed to prevent that. I will say my understanding of the larger HMM picture is limited, so there might be a better way than special-casing it like this. -- ~Gregory ^ permalink raw reply [flat|nested] 22+ messages in thread
* Re: [PATCH] mm/hmm: reject shadow-stack mappings 2026-11-13 20:41 ` Gregory Price (Meta) @ 2026-11-14 0:20 ` Jason Gunthorpe 2026-11-14 2:10 ` Andrew Morton 2026-11-14 7:25 ` Gregory Price (Meta) 0 siblings, 2 replies; 22+ messages in thread From: Jason Gunthorpe @ 2026-11-14 0:20 UTC (permalink / raw) To: Gregory Price (Meta) Cc: linux-mm, linux-kernel, kernel-team, leon, akpm, david, kas, dave.hansen, bp, rppt, stable, Lorenzo Stoakes (ARM) On Fri, Nov 13, 2026 at 03:41:33PM -0500, Gregory Price (Meta) wrote: > On 2026-11-13 16:03 -0400, Jason Gunthorpe wrote: > > On Fri, Nov 13, 2026 at 02:55:36PM -0500, Gregory Price (Meta) wrote: > > > HMM can grant ordinary device write access to a userspace shadow stack. > > > On x86, pte_write() is true for a shadow-stack PTE, so a populated page > > > can be returned with HMM_PFN_WRITE without taking a CPU write fault. > > > > I'm not sure I understand this? The pte is writable but you don't want > > only DMA to write to it? > > > > Is there something special on x86 that makes the shadow stack pte > > non-writable by the CPU? > > > > Some of the context is here: > https://lore.kernel.org/all/20261111-mason-migrate-et-al-fixes-v1-10-3d2454362e8d@kernel.org/ > > We had a similar issue with migration, but this should handle the DMA > case as well. > > It's "technically writable" but the kernel is supposed to prevent that. Bah, it's not writable at all. The CPU PTE has write disabled and someone thought it was a good idea to make pte_write() return true because the CPU allows writes through a special instruction on this special PTE? So now all the places like this think its writable, when it really isn't. You could also fix this by having something like pte_real_write() or whatever that doesn't lie about the write bit in the PTE :| The patch is still fine, in that it matches what everything else does, but I'd include these details someplace.. Jason ^ permalink raw reply [flat|nested] 22+ messages in thread
* Re: [PATCH] mm/hmm: reject shadow-stack mappings 2026-11-14 0:20 ` Jason Gunthorpe @ 2026-11-14 2:10 ` Andrew Morton 2026-11-15 0:00 ` Jason Gunthorpe 2026-11-14 7:25 ` Gregory Price (Meta) 1 sibling, 1 reply; 22+ messages in thread From: Andrew Morton @ 2026-11-14 2:10 UTC (permalink / raw) To: Jason Gunthorpe Cc: Gregory Price (Meta), linux-mm, linux-kernel, kernel-team, leon, david, kas, dave.hansen, bp, rppt, stable, Lorenzo Stoakes (ARM) On Fri, 13 Nov 2026 20:20:27 -0400 Jason Gunthorpe <jgg@nvidia.com> wrote: > On Fri, Nov 13, 2026 at 03:41:33PM -0500, Gregory Price (Meta) wrote: > > On 2026-11-13 16:03 -0400, Jason Gunthorpe wrote: > > > On Fri, Nov 13, 2026 at 02:55:36PM -0500, Gregory Price (Meta) wrote: > > > > HMM can grant ordinary device write access to a userspace shadow stack. > > > > On x86, pte_write() is true for a shadow-stack PTE, so a populated page > > > > can be returned with HMM_PFN_WRITE without taking a CPU write fault. > > > > > > I'm not sure I understand this? The pte is writable but you don't want > > > only DMA to write to it? > > > > > > Is there something special on x86 that makes the shadow stack pte > > > non-writable by the CPU? > > > > > > > Some of the context is here: > > https://lore.kernel.org/all/20261111-mason-migrate-et-al-fixes-v1-10-3d2454362e8d@kernel.org/ > > > > We had a similar issue with migration, but this should handle the DMA > > case as well. > > > > It's "technically writable" but the kernel is supposed to prevent that. > > Bah, it's not writable at all. The CPU PTE has write disabled and > someone thought it was a good idea to make pte_write() return true > because the CPU allows writes through a special instruction on this > special PTE? So now all the places like this think its writable, when > it really isn't. > > You could also fix this by having something like pte_real_write() or > whatever that doesn't lie about the write bit in the PTE :| > > The patch is still fine, in that it matches what everything else does, > but I'd include these details someplace.. How does this look for an altered changelog? : On x86, a shadow-stack PTE has its ordinary write bit clear. The CPU : rejects ordinary stores to it, but pte_write() reports it writable because : shadow-stack instructions can write it. HMM consequently sets : HMM_PFN_WRITE, which Nouveau turns into ordinary GPU write permission. A : process with GPU access can then overwrite its own shadow stack, : potentially corrupting return addresses or bypassing that protection. : : Reject shadow-stack VMAs in hmm_vma_walk_test(), including read access. A : requested fault returns -EFAULT; a snapshot records HMM_PFN_ERROR. ^ permalink raw reply [flat|nested] 22+ messages in thread
* Re: [PATCH] mm/hmm: reject shadow-stack mappings 2026-11-14 2:10 ` Andrew Morton @ 2026-11-15 0:00 ` Jason Gunthorpe 2026-11-15 1:01 ` Andrew Morton 0 siblings, 1 reply; 22+ messages in thread From: Jason Gunthorpe @ 2026-11-15 0:00 UTC (permalink / raw) To: Andrew Morton Cc: Gregory Price (Meta), linux-mm, linux-kernel, kernel-team, leon, david, kas, dave.hansen, bp, rppt, stable, Lorenzo Stoakes (ARM) On Fri, Nov 13, 2026 at 06:10:25PM -0800, Andrew Morton wrote: > How does this look for an altered changelog? > > : On x86, a shadow-stack PTE has its ordinary write bit clear. The CPU > : rejects ordinary stores to it, but pte_write() reports it writable because > : shadow-stack instructions can write it. HMM consequently sets > : HMM_PFN_WRITE, which Nouveau turns into ordinary GPU write permission. A > : process with GPU access can then overwrite its own shadow stack, > : potentially corrupting return addresses or bypassing that protection. > : > : Reject shadow-stack VMAs in hmm_vma_walk_test(), including read access. A > : requested fault returns -EFAULT; a snapshot records HMM_PFN_ERROR. Yes, but it is not just nouveau, RDMA user can do this too and that is far more common.. Thanks, Jason ^ permalink raw reply [flat|nested] 22+ messages in thread
* Re: [PATCH] mm/hmm: reject shadow-stack mappings 2026-11-15 0:00 ` Jason Gunthorpe @ 2026-11-15 1:01 ` Andrew Morton 2026-11-15 23:27 ` Jason Gunthorpe ` (2 more replies) 0 siblings, 3 replies; 22+ messages in thread From: Andrew Morton @ 2026-11-15 1:01 UTC (permalink / raw) To: Jason Gunthorpe Cc: Gregory Price (Meta), linux-mm, linux-kernel, kernel-team, leon, david, kas, dave.hansen, bp, rppt, stable, Lorenzo Stoakes (ARM) On Sat, 14 Nov 2026 20:00:05 -0400 Jason Gunthorpe <jgg@nvidia.com> wrote: > On Fri, Nov 13, 2026 at 06:10:25PM -0800, Andrew Morton wrote: > > > How does this look for an altered changelog? > > > > : On x86, a shadow-stack PTE has its ordinary write bit clear. The CPU > > : rejects ordinary stores to it, but pte_write() reports it writable because > > : shadow-stack instructions can write it. HMM consequently sets > > : HMM_PFN_WRITE, which Nouveau turns into ordinary GPU write permission. A > > : process with GPU access can then overwrite its own shadow stack, > > : potentially corrupting return addresses or bypassing that protection. > > : > > : Reject shadow-stack VMAs in hmm_vma_walk_test(), including read access. A > > : requested fault returns -EFAULT; a snapshot records HMM_PFN_ERROR. > > Yes, but it is not just nouveau, RDMA user can do this too and > that is far more common.. Thanks. This? On x86, a shadow-stack PTE has its ordinary write bit clear. The CPU rejects ordinary stores, but pte_write() reports it writable because shadow-stack instructions can write it. HMM consequently sets HMM_PFN_WRITE. Nouveau uses that flag for GPU write permission, and RDMA on-demand paging can use it to grant device write permission to a registered memory region. A device, or an RDMA peer with write access to that region, can then overwrite the process's shadow stack, potentially corrupting return addresses or bypassing that protection. Reject shadow-stack VMAs in hmm_vma_walk_test(), including read access. A requested fault returns -EFAULT; a snapshot records HMM_PFN_ERROR. ^ permalink raw reply [flat|nested] 22+ messages in thread
* Re: [PATCH] mm/hmm: reject shadow-stack mappings 2026-11-15 1:01 ` Andrew Morton @ 2026-11-15 23:27 ` Jason Gunthorpe 2026-11-19 20:05 ` Dave Hansen 2026-11-19 20:33 ` Edgecombe, Rick P 2 siblings, 0 replies; 22+ messages in thread From: Jason Gunthorpe @ 2026-11-15 23:27 UTC (permalink / raw) To: Andrew Morton Cc: Gregory Price (Meta), linux-mm, linux-kernel, kernel-team, leon, david, kas, dave.hansen, bp, rppt, stable, Lorenzo Stoakes (ARM) On Sat, Nov 14, 2026 at 05:01:27PM -0800, Andrew Morton wrote: > On Sat, 14 Nov 2026 20:00:05 -0400 Jason Gunthorpe <jgg@nvidia.com> wrote: > > > On Fri, Nov 13, 2026 at 06:10:25PM -0800, Andrew Morton wrote: > > > > > How does this look for an altered changelog? > > > > > > : On x86, a shadow-stack PTE has its ordinary write bit clear. The CPU > > > : rejects ordinary stores to it, but pte_write() reports it writable because > > > : shadow-stack instructions can write it. HMM consequently sets > > > : HMM_PFN_WRITE, which Nouveau turns into ordinary GPU write permission. A > > > : process with GPU access can then overwrite its own shadow stack, > > > : potentially corrupting return addresses or bypassing that protection. > > > : > > > : Reject shadow-stack VMAs in hmm_vma_walk_test(), including read access. A > > > : requested fault returns -EFAULT; a snapshot records HMM_PFN_ERROR. > > > > Yes, but it is not just nouveau, RDMA user can do this too and > > that is far more common.. > > Thanks. This? > > On x86, a shadow-stack PTE has its ordinary write bit clear. The CPU > rejects ordinary stores, but pte_write() reports it writable because > shadow-stack instructions can write it. HMM consequently sets > HMM_PFN_WRITE. Nouveau uses that flag for GPU write permission, and > RDMA on-demand paging can use it to grant device write permission to a > registered memory region. A device, or an RDMA peer with write access > to that region, can then overwrite the process's shadow stack, > potentially corrupting return addresses or bypassing that protection. > > Reject shadow-stack VMAs in hmm_vma_walk_test(), including read access. > A requested fault returns -EFAULT; a snapshot records HMM_PFN_ERROR. Sure, thanks Jason ^ permalink raw reply [flat|nested] 22+ messages in thread
* Re: [PATCH] mm/hmm: reject shadow-stack mappings 2026-11-15 1:01 ` Andrew Morton 2026-11-15 23:27 ` Jason Gunthorpe @ 2026-11-19 20:05 ` Dave Hansen 2026-11-19 20:16 ` David Hildenbrand (Arm) 2026-11-19 20:40 ` Edgecombe, Rick P 2026-11-19 20:33 ` Edgecombe, Rick P 2 siblings, 2 replies; 22+ messages in thread From: Dave Hansen @ 2026-11-19 20:05 UTC (permalink / raw) To: Andrew Morton, Jason Gunthorpe Cc: Gregory Price (Meta), linux-mm, linux-kernel, kernel-team, leon, david, kas, dave.hansen, bp, rppt, stable, Lorenzo Stoakes (ARM) On 11/14/26 17:01, Andrew Morton wrote: > On x86, a shadow-stack PTE has its ordinary write bit clear. The CPU > rejects ordinary stores, but pte_write() reports it writable because > shadow-stack instructions can write it. HMM consequently sets > HMM_PFN_WRITE. Nouveau uses that flag for GPU write permission, and > RDMA on-demand paging can use it to grant device write permission to a > registered memory region. A device, or an RDMA peer with write access > to that region, can then overwrite the process's shadow stack, > potentially corrupting return addresses or bypassing that protection. > > Reject shadow-stack VMAs in hmm_vma_walk_test(), including read access. > A requested fault returns -EFAULT; a snapshot records HMM_PFN_ERROR. This is a fun one. I almost wonder if we want to go one step further and disallow GUPs with FOLL_WRITE on these PTEs. There should be precious few reasons for devices to be writing to shadow stack pages. Isn't this a bigger issue than HMM? Should the fix be in HMM? ^ permalink raw reply [flat|nested] 22+ messages in thread
* Re: [PATCH] mm/hmm: reject shadow-stack mappings 2026-11-19 20:05 ` Dave Hansen @ 2026-11-19 20:16 ` David Hildenbrand (Arm) 2026-11-19 20:40 ` Edgecombe, Rick P 1 sibling, 0 replies; 22+ messages in thread From: David Hildenbrand (Arm) @ 2026-11-19 20:16 UTC (permalink / raw) To: Dave Hansen, Andrew Morton, Jason Gunthorpe Cc: Gregory Price (Meta), linux-mm, linux-kernel, kernel-team, leon, kas, dave.hansen, bp, rppt, stable, Lorenzo Stoakes (ARM) On 11/19/26 21:05, Dave Hansen wrote: > On 11/14/26 17:01, Andrew Morton wrote: >> On x86, a shadow-stack PTE has its ordinary write bit clear. The CPU >> rejects ordinary stores, but pte_write() reports it writable because >> shadow-stack instructions can write it. HMM consequently sets >> HMM_PFN_WRITE. Nouveau uses that flag for GPU write permission, and >> RDMA on-demand paging can use it to grant device write permission to a >> registered memory region. A device, or an RDMA peer with write access >> to that region, can then overwrite the process's shadow stack, >> potentially corrupting return addresses or bypassing that protection. >> >> Reject shadow-stack VMAs in hmm_vma_walk_test(), including read access. >> A requested fault returns -EFAULT; a snapshot records HMM_PFN_ERROR. > > This is a fun one. > > I almost wonder if we want to go one step further and disallow GUPs with > FOLL_WRITE on these PTEs. There should be precious few reasons for > devices to be writing to shadow stack pages. > > Isn't this a bigger issue than HMM? Should the fix be in HMM? I think we discussed this excessively after I raised it and it's even partially documented in the relevant commit messages. -- Cheers, David ^ permalink raw reply [flat|nested] 22+ messages in thread
* Re: [PATCH] mm/hmm: reject shadow-stack mappings 2026-11-19 20:05 ` Dave Hansen 2026-11-19 20:16 ` David Hildenbrand (Arm) @ 2026-11-19 20:40 ` Edgecombe, Rick P 2026-11-19 22:19 ` David Hildenbrand (Arm) 1 sibling, 1 reply; 22+ messages in thread From: Edgecombe, Rick P @ 2026-11-19 20:40 UTC (permalink / raw) To: jgg@nvidia.com, Hansen, Dave, akpm@linux-foundation.org Cc: ljs@kernel.org, dave.hansen@linux.intel.com, david@kernel.org, linux-mm@kvack.org, kernel-team@meta.com, linux-kernel@vger.kernel.org, rppt@kernel.org, bp@alien8.de, kas@kernel.org, leon@kernel.org, stable@vger.kernel.org, gourry@gourry.net On Thu, 2026-11-19 at 12:05 -0800, Dave Hansen wrote: > On 11/14/26 17:01, Andrew Morton wrote: > > On x86, a shadow-stack PTE has its ordinary write bit clear. The CPU > > rejects ordinary stores, but pte_write() reports it writable because > > shadow-stack instructions can write it. HMM consequently sets > > HMM_PFN_WRITE. Nouveau uses that flag for GPU write permission, and > > RDMA on-demand paging can use it to grant device write permission to a > > registered memory region. A device, or an RDMA peer with write access > > to that region, can then overwrite the process's shadow stack, > > potentially corrupting return addresses or bypassing that protection. > > > > Reject shadow-stack VMAs in hmm_vma_walk_test(), including read access. > > A requested fault returns -EFAULT; a snapshot records HMM_PFN_ERROR. > > This is a fun one. > > I almost wonder if we want to go one step further and disallow GUPs with > FOLL_WRITE on these PTEs. There should be precious few reasons for > devices to be writing to shadow stack pages. > > Isn't this a bigger issue than HMM? Should the fix be in HMM? We do reject GUPs to shadow stack PTEs with FOLL_WRITE, unless FOLL_FORCE is passed. The tricky thing as mentioned my others in this thread is that shadow stack memory *is* writable by userspace. But also the kernel should not treat it as such A false negative for pte_write() is dangerous when the kernel needs to decide when to revoke access. (i.e. fork) I think a mistake there is somewhat worse then missing an exotic way to cajole the kernel into writing to a user's shadow stack like this. So I think pte_write() being true for shadow stacks is the correct implementation. But, maybe the problem is that pte_write() is doing the job of two separate concepts. What if there was a pte_userspace_could_write() and a pte_kernel_should_not_write()? Names would need some work... ^ permalink raw reply [flat|nested] 22+ messages in thread
* Re: [PATCH] mm/hmm: reject shadow-stack mappings 2026-11-19 20:40 ` Edgecombe, Rick P @ 2026-11-19 22:19 ` David Hildenbrand (Arm) 0 siblings, 0 replies; 22+ messages in thread From: David Hildenbrand (Arm) @ 2026-11-19 22:19 UTC (permalink / raw) To: Edgecombe, Rick P, jgg@nvidia.com, Hansen, Dave, akpm@linux-foundation.org Cc: ljs@kernel.org, dave.hansen@linux.intel.com, linux-mm@kvack.org, kernel-team@meta.com, linux-kernel@vger.kernel.org, rppt@kernel.org, bp@alien8.de, kas@kernel.org, leon@kernel.org, stable@vger.kernel.org, gourry@gourry.net On 11/19/26 21:40, Edgecombe, Rick P wrote: > On Thu, 2026-11-19 at 12:05 -0800, Dave Hansen wrote: >> On 11/14/26 17:01, Andrew Morton wrote: >>> On x86, a shadow-stack PTE has its ordinary write bit clear. The CPU >>> rejects ordinary stores, but pte_write() reports it writable because >>> shadow-stack instructions can write it. HMM consequently sets >>> HMM_PFN_WRITE. Nouveau uses that flag for GPU write permission, and >>> RDMA on-demand paging can use it to grant device write permission to a >>> registered memory region. A device, or an RDMA peer with write access >>> to that region, can then overwrite the process's shadow stack, >>> potentially corrupting return addresses or bypassing that protection. >>> >>> Reject shadow-stack VMAs in hmm_vma_walk_test(), including read access. >>> A requested fault returns -EFAULT; a snapshot records HMM_PFN_ERROR. >> >> This is a fun one. >> >> I almost wonder if we want to go one step further and disallow GUPs with >> FOLL_WRITE on these PTEs. There should be precious few reasons for >> devices to be writing to shadow stack pages. >> >> Isn't this a bigger issue than HMM? Should the fix be in HMM? > > We do reject GUPs to shadow stack PTEs with FOLL_WRITE, unless FOLL_FORCE is > passed. The tricky thing as mentioned my others in this thread is that shadow > stack memory *is* writable by userspace. But also the kernel should not treat it > as such > > A false negative for pte_write() is dangerous when the kernel needs to decide > when to revoke access. (i.e. fork) I think a mistake there is somewhat worse > then missing an exotic way to cajole the kernel into writing to a user's shadow > stack like this. So I think pte_write() being true for shadow stacks is the > correct implementation. > > But, maybe the problem is that pte_write() is doing the job of two separate > concepts. What if there was a pte_userspace_could_write() and a > pte_kernel_should_not_write()? Names would need some work... Gah, nak, pte_write() just says "something might be able to write to it". If that's actually true depends on other factors. Like the access+dirty on HW that manages that in software. I repeat: *no code* in Linux besides core-mm should ever interpret "pte_write()" as "cool, I can write to it". The exception is GUP and unfortunately HMM. -- Cheers, David ^ permalink raw reply [flat|nested] 22+ messages in thread
* Re: [PATCH] mm/hmm: reject shadow-stack mappings 2026-11-15 1:01 ` Andrew Morton 2026-11-15 23:27 ` Jason Gunthorpe 2026-11-19 20:05 ` Dave Hansen @ 2026-11-19 20:33 ` Edgecombe, Rick P 2 siblings, 0 replies; 22+ messages in thread From: Edgecombe, Rick P @ 2026-11-19 20:33 UTC (permalink / raw) To: jgg@nvidia.com, akpm@linux-foundation.org Cc: ljs@kernel.org, dave.hansen@linux.intel.com, david@kernel.org, linux-mm@kvack.org, kernel-team@meta.com, linux-kernel@vger.kernel.org, rppt@kernel.org, bp@alien8.de, kas@kernel.org, leon@kernel.org, stable@vger.kernel.org, gourry@gourry.net On Sat, 2026-11-14 at 17:01 -0800, Andrew Morton wrote: > Thanks. This? > > On x86, a shadow-stack PTE has its ordinary write bit clear. The CPU > rejects ordinary stores, but pte_write() reports it writable because > shadow-stack instructions can write it. HMM consequently sets > HMM_PFN_WRITE. Nouveau uses that flag for GPU write permission, and > RDMA on-demand paging can use it to grant device write permission to a > registered memory region. A device, or an RDMA peer with write access > to that region, can then overwrite the process's shadow stack, > potentially corrupting return addresses or bypassing that protection. > > Reject shadow-stack VMAs in hmm_vma_walk_test(), including read access. > A requested fault returns -EFAULT; a snapshot records HMM_PFN_ERROR. BTW, this is not just an x86 thing. Arm and riscv have the same pte_write() shadow stack behavior for the same reasons. Despite not having the Write=0,Dirty=1 thing, which *is* weird. But they still try to prevent the kernel from writing it via logic in gup, etc. ^ permalink raw reply [flat|nested] 22+ messages in thread
* Re: [PATCH] mm/hmm: reject shadow-stack mappings 2026-11-14 0:20 ` Jason Gunthorpe 2026-11-14 2:10 ` Andrew Morton @ 2026-11-14 7:25 ` Gregory Price (Meta) 2026-11-14 11:11 ` David Hildenbrand (Arm) 1 sibling, 1 reply; 22+ messages in thread From: Gregory Price (Meta) @ 2026-11-14 7:25 UTC (permalink / raw) To: Jason Gunthorpe Cc: Gregory Price (Meta), linux-mm, linux-kernel, kernel-team, leon, akpm, david, kas, dave.hansen, bp, rppt, stable, Lorenzo Stoakes (ARM) On 2026-11-13 20:20 -0400, Jason Gunthorpe wrote: > On Fri, Nov 13, 2026 at 03:41:33PM -0500, Gregory Price (Meta) wrote: > > On 2026-11-13 16:03 -0400, Jason Gunthorpe wrote: > > > On Fri, Nov 13, 2026 at 02:55:36PM -0500, Gregory Price (Meta) wrote: > > > > HMM can grant ordinary device write access to a userspace shadow stack. > > > > On x86, pte_write() is true for a shadow-stack PTE, so a populated page > > > > can be returned with HMM_PFN_WRITE without taking a CPU write fault. > > > > > > I'm not sure I understand this? The pte is writable but you don't want > > > only DMA to write to it? > > > > > > Is there something special on x86 that makes the shadow stack pte > > > non-writable by the CPU? > > > > > > > Some of the context is here: > > https://lore.kernel.org/all/20261111-mason-migrate-et-al-fixes-v1-10-3d2454362e8d@kernel.org/ > > > > We had a similar issue with migration, but this should handle the DMA > > case as well. > > > > It's "technically writable" but the kernel is supposed to prevent that. > > Bah, it's not writable at all. The CPU PTE has write disabled and > someone thought it was a good idea to make pte_write() return true > because the CPU allows writes through a special instruction on this > special PTE? So now all the places like this think its writable, when > it really isn't. > I completely missed this, that will teach me to trust pte_write() I guess... what a bizarre design choice. I'm sure there's "reasons". > You could also fix this by having something like pte_real_write() or > whatever that doesn't lie about the write bit in the PTE :| > or rename pte_write() to pte_fake_write() instead and restore pte_write() to its former truthful glory :| indeed > The patch is still fine, in that it matches what everything else does, > but I'd include these details someplace.. > Thank you for taking a closer look, I think Andrew's changelog adjustment is an improvement if you're ok with it. -- ~Gregory ^ permalink raw reply [flat|nested] 22+ messages in thread
* Re: [PATCH] mm/hmm: reject shadow-stack mappings 2026-11-14 7:25 ` Gregory Price (Meta) @ 2026-11-14 11:11 ` David Hildenbrand (Arm) 2026-11-15 23:48 ` Alistair Popple 0 siblings, 1 reply; 22+ messages in thread From: David Hildenbrand (Arm) @ 2026-11-14 11:11 UTC (permalink / raw) To: Gregory Price (Meta), Jason Gunthorpe Cc: linux-mm, linux-kernel, kernel-team, leon, akpm, kas, dave.hansen, bp, rppt, stable, Lorenzo Stoakes (ARM) On 11/14/26 08:25, Gregory Price (Meta) wrote: > On 2026-11-13 20:20 -0400, Jason Gunthorpe wrote: >> On Fri, Nov 13, 2026 at 03:41:33PM -0500, Gregory Price (Meta) wrote: >>> >>> Some of the context is here: >>> https://lore.kernel.org/all/20261111-mason-migrate-et-al-fixes-v1-10-3d2454362e8d@kernel.org/ >>> >>> We had a similar issue with migration, but this should handle the DMA >>> case as well. >>> >>> It's "technically writable" but the kernel is supposed to prevent that. >> >> Bah, it's not writable at all. The CPU PTE has write disabled and >> someone thought it was a good idea to make pte_write() return true >> because the CPU allows writes through a special instruction on this >> special PTE? So now all the places like this think its writable, when >> it really isn't. >> > > I completely missed this, that will teach me to trust pte_write() I > guess... what a bizarre design choice. I'm sure there's "reasons". > >> You could also fix this by having something like pte_real_write() or >> whatever that doesn't lie about the write bit in the PTE :| >> > > or rename pte_write() to pte_fake_write() instead and restore > pte_write() to its former truthful glory It's writable by the hardware, that's what matters to core-mm, for example, when it comes to things like fork(), mproetect() etc. *nobody* besides selected core-mm code should ever write to random PTEs. They shouldn't ever even care about pte_write(). We have GUP for that, which takes care of that oddity properly when deciding whether we can *actually* write to a mapped page. Well, and we have HMM that reimplements part of GUP in ugly ways (sorry) that we missed. -- Cheers, David ^ permalink raw reply [flat|nested] 22+ messages in thread
* Re: [PATCH] mm/hmm: reject shadow-stack mappings 2026-11-14 11:11 ` David Hildenbrand (Arm) @ 2026-11-15 23:48 ` Alistair Popple 2026-11-16 16:09 ` Gregory Price (Meta) 0 siblings, 1 reply; 22+ messages in thread From: Alistair Popple @ 2026-11-15 23:48 UTC (permalink / raw) To: David Hildenbrand (Arm) Cc: Gregory Price (Meta), Jason Gunthorpe, linux-mm, linux-kernel, kernel-team, leon, akpm, kas, dave.hansen, bp, rppt, stable, Lorenzo Stoakes (ARM) On 2026-11-14 at 22:11 +1100, "David Hildenbrand (Arm)" <david@kernel.org> wrote... > On 11/14/26 08:25, Gregory Price (Meta) wrote: > > On 2026-11-13 20:20 -0400, Jason Gunthorpe wrote: > >> On Fri, Nov 13, 2026 at 03:41:33PM -0500, Gregory Price (Meta) wrote: > >>> > >>> Some of the context is here: > >>> https://lore.kernel.org/all/20261111-mason-migrate-et-al-fixes-v1-10-3d2454362e8d@kernel.org/ > >>> > >>> We had a similar issue with migration, but this should handle the DMA > >>> case as well. Yeah, I asked the HMM question there - https://lore.kernel.org/linux-mm/avT2FNTZoEuijEZP@nvdebian.thelocal/ > >>> > >>> It's "technically writable" but the kernel is supposed to prevent that. > >> > >> Bah, it's not writable at all. The CPU PTE has write disabled and > >> someone thought it was a good idea to make pte_write() return true > >> because the CPU allows writes through a special instruction on this > >> special PTE? So now all the places like this think its writable, when > >> it really isn't. > >> > > > > I completely missed this, that will teach me to trust pte_write() I > > guess... what a bizarre design choice. I'm sure there's "reasons". Yeah, that surprised me as well when I looked into how the shadow stack worked. > >> You could also fix this by having something like pte_real_write() or > >> whatever that doesn't lie about the write bit in the PTE :| > >> > > > > or rename pte_write() to pte_fake_write() instead and restore > > pte_write() to its former truthful glory > > It's writable by the hardware, that's what matters to core-mm, for example, when > it comes to things like fork(), mproetect() etc. Except they're not universally writable by HW, after all _PAGE_RW = 0. So pte_write() is documented to return if it is logically writable, which of course is what hmm_range_fault() also needs for certain values of "logic" :-) So it's easy to see how this is confusing and got missed. Anyway this fix looks good: Reviewed-by: Alistair Popple <apopple@nvidia.com> > *nobody* besides selected core-mm code should ever write to random PTEs. They > shouldn't ever even care about pte_write(). > > We have GUP for that, which takes care of that oddity properly when deciding > whether we can *actually* write to a mapped page. > > Well, and we have HMM that reimplements part of GUP in ugly ways (sorry) that we > missed. > > -- > Cheers, > > David > ^ permalink raw reply [flat|nested] 22+ messages in thread
* Re: [PATCH] mm/hmm: reject shadow-stack mappings 2026-11-15 23:48 ` Alistair Popple @ 2026-11-16 16:09 ` Gregory Price (Meta) 0 siblings, 0 replies; 22+ messages in thread From: Gregory Price (Meta) @ 2026-11-16 16:09 UTC (permalink / raw) To: Alistair Popple Cc: David Hildenbrand (Arm), Gregory Price (Meta), Jason Gunthorpe, linux-mm, linux-kernel, kernel-team, leon, akpm, kas, dave.hansen, bp, rppt, stable, Lorenzo Stoakes (ARM) On 2026-11-16 10:48 +1100, Alistair Popple wrote: > On 2026-11-14 at 22:11 +1100, "David Hildenbrand (Arm)" <david@kernel.org> wrote... > > It's writable by the hardware, that's what matters to core-mm, for example, when > > it comes to things like fork(), mproetect() etc. > > Except they're not universally writable by HW, after all _PAGE_RW = 0. So > pte_write() is documented to return if it is logically writable, which of course > is what hmm_range_fault() also needs for certain values of "logic" :-) > > So it's easy to see how this is confusing and got missed. Anyway this fix looks > good: > > Reviewed-by: Alistair Popple <apopple@nvidia.com> > Thank's Alistair! -- ~Gregory ^ permalink raw reply [flat|nested] 22+ messages in thread
end of thread, other threads:[~2026-11-19 22:19 UTC | newest] Thread overview: 22+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2026-11-13 19:55 [PATCH] mm/hmm: reject shadow-stack mappings Gregory Price (Meta) 2026-11-13 19:57 ` Lorenzo Stoakes (ARM) 2026-11-13 20:15 ` Gregory Price (Meta) 2026-11-13 20:03 ` David Hildenbrand (Arm) 2026-11-13 20:42 ` Gregory Price (Meta) 2026-11-13 21:19 ` David Hildenbrand (Arm) 2026-11-13 20:03 ` Jason Gunthorpe 2026-11-13 20:41 ` Gregory Price (Meta) 2026-11-14 0:20 ` Jason Gunthorpe 2026-11-14 2:10 ` Andrew Morton 2026-11-15 0:00 ` Jason Gunthorpe 2026-11-15 1:01 ` Andrew Morton 2026-11-15 23:27 ` Jason Gunthorpe 2026-11-19 20:05 ` Dave Hansen 2026-11-19 20:16 ` David Hildenbrand (Arm) 2026-11-19 20:40 ` Edgecombe, Rick P 2026-11-19 22:19 ` David Hildenbrand (Arm) 2026-11-19 20:33 ` Edgecombe, Rick P 2026-11-14 7:25 ` Gregory Price (Meta) 2026-11-14 11:11 ` David Hildenbrand (Arm) 2026-11-15 23:48 ` Alistair Popple 2026-11-16 16:09 ` Gregory Price (Meta)
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox