# Re: \[PATCH v20 11/25\] x86/mm: Update ptep\_set\_wrprotect() and pmdp\_set\_wrprotect() for transition from \_PAGE\_DIRTY to \_PAGE\_COW

- From: Kees Cook \<hidden\>
- Date: 2021-02-10 19:45:13 
- List: linux-doc
- Page: https://ratatoskr.run/linux-doc/2021/02/6266410
- Thread: https://ratatoskr.run/linux-doc/2021/02/6266248/t.md

```text
On Wed, Feb 10, 2021 at 09:56:49AM -0800, Yu-cheng Yu wrote:
> When Shadow Stack is introduced, [R/O + _PAGE_DIRTY] PTE is reserved for
> shadow stack.  Copy-on-write PTEs have [R/O + _PAGE_COW].
> 
> When a PTE goes from [R/W + _PAGE_DIRTY] to [R/O + _PAGE_COW], it could
> become a transient shadow stack PTE in two cases:
> 
> The first case is that some processors can start a write but end up seeing
> a read-only PTE by the time they get to the Dirty bit, creating a transient
> shadow stack PTE.  However, this will not occur on processors supporting
> Shadow Stack, and a TLB flush is not necessary.
> 
> The second case is that when _PAGE_DIRTY is replaced with _PAGE_COW non-
> atomically, a transient shadow stack PTE can be created as a result.
> Thus, prevent that with cmpxchg.
> 
> Dave Hansen, Jann Horn, Andy Lutomirski, and Peter Zijlstra provided many
> insights to the issue.  Jann Horn provided the cmpxchg solution.
> 
> Signed-off-by: Yu-cheng Yu <redacted>

Reviewed-by: Kees Cook <redacted>

-- 
Kees Cook
```
