arm64: Also reset KASAN tag if page is not PG_mte_tagged
Subsystems (3)
PATCH 1/1 arm64: Also reset KASAN tag if page is not PG_mte_tagged
Consider the following sequence of events:
1) A page in a PROT_READ|PROT_WRITE VMA is faulted.
2) Page migration allocates a page with the KASAN allocator, causing it to receive a non-match-all tag, and uses it
to replace the page faulted in 1.
3) The program uses mprotect() to enable PROT_MTE on the page faulted in 1.
As a result of step 3, we are left with a non-match-all tag for a page with tags accessible to userspace, which can lead to the same kind of tag check faults that commit e74a68468062 ("arm64: Reset KASAN tag in copy_highpage with HW tags only") intended to fix.
The general invariant that we have for pages in a VMA with VM_MTE_ALLOWED is that they cannot have a non-match-all tag. As a result of step 2, the invariant is broken. This means that the fix in the referenced commit was incomplete and we also need to reset the tag for pages without PG_mte_tagged.
--- arch/arm64/mm/copypage.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
arch/arm64/mm/copypage.c +3 -2
@@ -21,9 +21,10 @@ void copy_highpage(struct page *to, struct page *from)copy_page (kto, kfrom); +if (kasan_hw_tags_enabled ()) +page_kasan_tag_reset (to); +if (system_supports_mte () &&page_mte_tagged (from)) { -if (kasan_hw_tags_enabled ()) -page_kasan_tag_reset (to);/* It's a new page, shouldn't have been tagged yet */ WARN_ON_ONCE (!try_page_mte_tagging (to));mte_copy_page_tags (kto, kfrom);
Signature · 1 line
2.40.0.396.gfff15efe05-goog
Catalin Marinas
maintainerQuoted 7 lines · Peter Collingbourne
Consider the following sequence of events:
1) A page in a PROT_READ|PROT_WRITE VMA is faulted.
2) Page migration allocates a page with the KASAN allocator, causing it to receive a non-match-all tag, and uses it
to replace the page faulted in 1.
3) The program usesmprotect()to enablePROT_MTEon the page faulted in 1.
Ah, so there is no race here, it's simply because the page allocation for migration has a non-match-all kasan tag in page->flags.
How do we handle the non-migration case with mprotect()? IIRC post_alloc_hook() always resets the page->flags since GFP_HIGHUSER_MOVABLE has the __GFP_SKIP_KASAN_UNPOISON flag.
Quoted 5 lines
As a result of step 3, we are left with a non-match-all tag for a page with tags accessible to userspace, which can lead to the same kind of tag check faults that commit
e74a68468062("arm64: Reset KASAN tag incopy_highpagewith HW tags only") intended to fix.
Quoted 7 lines
The general invariant that we have for pages in a VMA with
VM_MTE_ALLOWEDis that they cannot have a non-match-all tag. As a result of step 2, the invariant is broken. This means that the fix in the referenced commit was incomplete and we also need to reset the tag for pages withoutPG_mte_tagged.Fixes e5b8d9218951 ("arm64: mte: reset the page tag in page->flags")
This commit was reverted in 20794545c146 (arm64: kasan: Revert "arm64:
mte: reset the page tag in page->flags"). It looks a bit strange to fix it up.
Quoted 5 lines
arch/arm64/mm/copypage.c +0
-0
arch/arm64/mm/copypage.c void copy_highpage(struct page *to, struct page *from) · line 26
Quoted 9 lines
arch/arm64/mm/copypage.c +3
-2@@ -21,9 +21,10 @@ void copy_highpage(struct page *to, struct page *from)copy_page (kto, kfrom); +if (kasan_hw_tags_enabled ()) +page_kasan_tag_reset (to); +if (system_supports_mte () &&page_mte_tagged (from)) { -if (kasan_hw_tags_enabled ()) -page_kasan_tag_reset (to);
This should work but can we not do this at allocation time like we do for the source page and remove any page_kasan_tag_reset() here altogether?
Signature · 1 line
Catalin
Quoted 10 lines · Catalin Marinas
Quoted 7 lines · Peter Collingbourne
Consider the following sequence of events:
1) A page in a PROT_READ|PROT_WRITE VMA is faulted.
2) Page migration allocates a page with the KASAN allocator, causing it to receive a non-match-all tag, and uses it
to replace the page faulted in 1.
3) The program usesmprotect()to enablePROT_MTEon the page faulted in 1.
Quoted 6 lines
Ah, so there is no race here, it's simply because the page allocation for migration has a non-match-all kasan tag in
page->flags.How do we handle the non-migration case with
mprotect()? IIRCpost_alloc_hook()always resets thepage->flagssinceGFP_HIGHUSER_MOVABLEhas the__GFP_SKIP_KASAN_UNPOISONflag.
Yes, that's how it normally works.
Quoted 11 lines
Quoted 11 lines
As a result of step 3, we are left with a non-match-all tag for a page with tags accessible to userspace, which can lead to the same kind of tag check faults that commit
e74a68468062("arm64: Reset KASAN tag incopy_highpagewith HW tags only") intended to fix.The general invariant that we have for pages in a VMA with
VM_MTE_ALLOWEDis that they cannot have a non-match-all tag. As a result of step 2, the invariant is broken. This means that the fix in the referenced commit was incomplete and we also need to reset the tag for pages withoutPG_mte_tagged.
Quoted 5 lines
Quoted 1 line
Fixes e5b8d9218951 ("arm64: mte: reset the page tag in page->flags")This commit was reverted in
20794545c146(arm64: kasan: Revert "arm64:
mte: reset the page tag inpage->flags"). It looks a bit strange to fix it up.
It does seem strange but I think it is correct because that is when the bug (resetting tag only if PG_mte_tagged) was introduced. The revert preserved the bug because it did not account for the migration case, which means that it didn't account for migration+mprotect either.
Quoted 8 lines
Quoted 8 lines
arch/arm64/mm/copypage.c +0
-0@@ -21,9 +21,10 @@ void copy_highpage(struct page *to, struct page *from)copy_page (kto, kfrom);
Quoted 10 lines
arch/arm64/mm/copypage.c void copy_highpage(struct page *to, struct page *from) · line 26
Quoted 6 lines
arch/arm64/mm/copypage.c +3
-2+if (kasan_hw_tags_enabled ()) +page_kasan_tag_reset (to); +if (system_supports_mte () &&page_mte_tagged (from)) { -if (kasan_hw_tags_enabled ()) -page_kasan_tag_reset (to);This should work but can we not do this at allocation time like we do for the source page and remove any
page_kasan_tag_reset()here altogether?
That would be difficult because of the number of different ways that the page can be allocated. That's why we also decided to reset it here in commit e74a68468062.
Peter
Quoted 63 lines · Peter Collingbourne
Quoted 10 lines · Catalin Marinas
Quoted 7 lines · Peter Collingbourne
Consider the following sequence of events:
1) A page in a PROT_READ|PROT_WRITE VMA is faulted.
2) Page migration allocates a page with the KASAN allocator, causing it to receive a non-match-all tag, and uses it
to replace the page faulted in 1.
3) The program usesmprotect()to enablePROT_MTEon the page faulted in 1.Quoted 6 lines
Ah, so there is no race here, it's simply because the page allocation for migration has a non-match-all kasan tag in
page->flags.How do we handle the non-migration case with
mprotect()? IIRCpost_alloc_hook()always resets thepage->flagssinceGFP_HIGHUSER_MOVABLEhas the__GFP_SKIP_KASAN_UNPOISONflag.Yes, that's how it normally works.
Quoted 11 lines
Quoted 11 lines
As a result of step 3, we are left with a non-match-all tag for a page with tags accessible to userspace, which can lead to the same kind of tag check faults that commit
e74a68468062("arm64: Reset KASAN tag incopy_highpagewith HW tags only") intended to fix.The general invariant that we have for pages in a VMA with
VM_MTE_ALLOWEDis that they cannot have a non-match-all tag. As a result of step 2, the invariant is broken. This means that the fix in the referenced commit was incomplete and we also need to reset the tag for pages withoutPG_mte_tagged.Quoted 5 lines
Quoted 1 line
Fixes e5b8d9218951 ("arm64: mte: reset the page tag in page->flags")This commit was reverted in
20794545c146(arm64: kasan: Revert "arm64:
mte: reset the page tag inpage->flags"). It looks a bit strange to fix it up.It does seem strange but I think it is correct because that is when the bug (resetting tag only if
PG_mte_tagged) was introduced. The revert preserved the bug because it did not account for the migration case, which means that it didn't account for migration+mprotect either.Quoted 8 lines
Quoted 8 lines
arch/arm64/mm/copypage.c +0
-0@@ -21,9 +21,10 @@ void copy_highpage(struct page *to, struct page *from)copy_page (kto, kfrom);Quoted 10 lines
arch/arm64/mm/copypage.c void copy_highpage(struct page *to, struct page *from) · line 26
Quoted 6 lines
arch/arm64/mm/copypage.c +3
-2+if (kasan_hw_tags_enabled ()) +page_kasan_tag_reset (to); +if (system_supports_mte () &&page_mte_tagged (from)) { -if (kasan_hw_tags_enabled ()) -page_kasan_tag_reset (to);This should work but can we not do this at allocation time like we do for the source page and remove any
page_kasan_tag_reset()here altogether?
Quoted 3 lines
That would be difficult because of the number of different ways that the page can be allocated. That's why we also decided to reset it here in commit
e74a68468062.
Ping.
Peter
Quoted 19 lines · Peter Collingbourne
Consider the following sequence of events:
1) A page in a PROT_READ|PROT_WRITE VMA is faulted.
2) Page migration allocates a page with the KASAN allocator, causing it to receive a non-match-all tag, and uses it
to replace the page faulted in 1.
3) The program usesmprotect()to enablePROT_MTEon the page faulted in 1.As a result of step 3, we are left with a non-match-all tag for a page with tags accessible to userspace, which can lead to the same kind of tag check faults that commit
e74a68468062("arm64: Reset KASAN tag incopy_highpagewith HW tags only") intended to fix.The general invariant that we have for pages in a VMA with
VM_MTE_ALLOWEDis that they cannot have a non-match-all tag. As a result of step 2, the invariant is broken. This means that the fix in the referenced commit was incomplete and we also need to reset the tag for pages withoutPG_mte_tagged.
Quoted 4 lines
Fixes e5b8d9218951 ("arm64: mte: reset the page tag in page->flags")Cc <[email protected]> # 5.15Signed-off-by Peter Collingbourne <[email protected]>
Sorry, forgot to reply:
Quoted 9 lines · Peter Collingbourne
Consider the following sequence of events:
1) A page in a PROT_READ|PROT_WRITE VMA is faulted.
2) Page migration allocates a page with the KASAN allocator, causing it to receive a non-match-all tag, and uses it
to replace the page faulted in 1.
3) The program usesmprotect()to enablePROT_MTEon the page faulted in 1.[...]
Applied to arm64 (for-next/fixes), thanks!
[1/1] arm64: Also reset KASAN tag if page is not PG_mte_tagged
https://git.kernel.org/arm64/c/2efbafb91e12
Cheers,
Signature · 5 lines
Will https://fixes.arm64.dev https://next.arm64.dev https://will.arm64.dev