Skip to contentSkip to conversation

arm64: mte: Do not set PG_mte_tagged if tags were not initialized

PATCH Applied Peter Collingbourne 4 participants 4 messages Apr 20, 2023 – May 16, 2023

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 arm64: mte: Do not set PG_mte_tagged if tags were not initialized

The mte_sync_page_tags() function sets PG_mte_tagged if it initializes page tags. Then we return to mte_sync_tags(), which sets PG_mte_tagged again. At best, this is redundant. However, it is possible for mte_sync_page_tags() to return without having initialized tags for the page, i.e. in the case where check_swap is true (non-compound page), is_swap_pte(old_pte) is false and pte_is_tagged is false. So at worst, we set PG_mte_tagged on a page with uninitialized tags. This can happen if, for example, page migration causes a PTE for an untagged page to be replaced. If the userspace program subsequently uses mprotect() to enable PROT_MTE for that page, the uninitialized tags will be exposed to userspace.

Fix it by removing the redundant call to set_page_mte_tagged().

Fixes e059853d14ca ("arm64: mte: Fix/clarify the PG_mte_tagged semantics")
---
 arch/arm64/kernel/mte.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
arch/arm64/kernel/mte.c +2 -5
index f5bcb0dc6267..7e89968bd282 100644
@@ -66,13 +66,10 @@ void mte_sync_tags(pte_t old_pte, pte_t pte)
 		return;
 
 	/* if PG_mte_tagged is set, tags have already been initialised */
-	for (i = 0; i < nr_pages; i++, page++) {
-		if (!page_mte_tagged(page)) {
+	for (i = 0; i < nr_pages; i++, page++)
+		if (!page_mte_tagged(page))
 			mte_sync_page_tags(page, old_pte, check_swap,
 					   pte_is_tagged);
-			set_page_mte_tagged(page);
-		}
-	}
 
 	/* ensure the tags are visible before the PTE is set */
 	smp_wmb();
Signature · 1 line
2.40.0.634.g4ca3ef3211-goog
maintainer Reviewed-by on v1 1/1
#1 lore raw
Quoted 29 lines · Peter Collingbourne

The mte_sync_page_tags() function sets PG_mte_tagged if it initializes page tags. Then we return to mte_sync_tags(), which sets PG_mte_tagged again. At best, this is redundant. However, it is possible for mte_sync_page_tags() to return without having initialized tags for the page, i.e. in the case where check_swap is true (non-compound page), is_swap_pte(old_pte) is false and pte_is_tagged is false. So at worst, we set PG_mte_tagged on a page with uninitialized tags. This can happen if, for example, page migration causes a PTE for an untagged page to be replaced. If the userspace program subsequently uses mprotect() to enable PROT_MTE for that page, the uninitialized tags will be exposed to userspace.

Fix it by removing the redundant call to set_page_mte_tagged().

Fixes e059853d14ca ("arm64: mte: Fix/clarify the PG_mte_tagged semantics")
---
 arch/arm64/kernel/mte.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
arch/arm64/kernel/mte.c +0 -0
index f5bcb0dc6267..7e89968bd282 100644
@@ -66,13 +66,10 @@ void mte_sync_tags(pte_t old_pte, pte_t pte)
 		return;
 

arch/arm64/kernel/mte.c void mte_sync_tags(pte_t old_pte, pte_t pte) · line 75

Quoted 10 lines
arch/arm64/kernel/mte.c +2 -5
 	/* if PG_mte_tagged is set, tags have already been initialised */
-	for (i = 0; i < nr_pages; i++, page++) {
-		if (!page_mte_tagged(page)) {
+	for (i = 0; i < nr_pages; i++, page++)
+		if (!page_mte_tagged(page))
 			mte_sync_page_tags(page, old_pte, check_swap,
 					   pte_is_tagged);
-			set_page_mte_tagged(page);
-		}
-	}

It makes sense, not sure why I added it here when mte_sync_page_tags() was already setting the flag if needed.

Reviewed-by on v1 1/1
#2 lore raw
In reply to Peter Collingbourne

Hi Peter,

Regardless of the nitpick below, the patch looks correct to me:

Quoted 27 lines · Peter Collingbourne

The mte_sync_page_tags() function sets PG_mte_tagged if it initializes page tags. Then we return to mte_sync_tags(), which sets PG_mte_tagged again. At best, this is redundant. However, it is possible for mte_sync_page_tags() to return without having initialized tags for the page, i.e. in the case where check_swap is true (non-compound page), is_swap_pte(old_pte) is false and pte_is_tagged is false. So at worst, we set PG_mte_tagged on a page with uninitialized tags. This can happen if, for example, page migration causes a PTE for an untagged page to be replaced. If the userspace program subsequently uses mprotect() to enable PROT_MTE for that page, the uninitialized tags will be exposed to userspace.

Fix it by removing the redundant call to set_page_mte_tagged().

Fixes e059853d14ca ("arm64: mte: Fix/clarify the PG_mte_tagged semantics")
---
 arch/arm64/kernel/mte.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
arch/arm64/kernel/mte.c +0 -0
index f5bcb0dc6267..7e89968bd282 100644

arch/arm64/kernel/mte.c void mte_sync_tags(pte_t old_pte, pte_t pte) · line 70

Quoted 5 lines
arch/arm64/kernel/mte.c +0 -2
@@ -66,13 +66,10 @@ void mte_sync_tags(pte_t old_pte, pte_t pte)
 		return;
 
 	/* if PG_mte_tagged is set, tags have already been initialised */
-	for (i = 0; i < nr_pages; i++, page++) {
-		if (!page_mte_tagged(page)) {

Both braces could have been kept (they define multiline blocks), and as a bonus the diff becomes only one line removed. Not sure that's worth a respin.

Thanks,
Alex

Quoted 19 lines
arch/arm64/kernel/mte.c +2 -3
+	for (i = 0; i < nr_pages; i++, page++)
 
+		if (!page_mte_tagged(page))
 			mte_sync_page_tags(page, old_pte, check_swap,
 					   pte_is_tagged);
-			set_page_mte_tagged(page);
-		}
-	}
 
 	/* ensure the tags are visible before the PTE is set */

smp_wmb();

Signature · 7 lines
2.40.0.634.g4ca3ef3211-goog


_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
maintainer Applied
#3 lore raw
In reply to Peter Collingbourne
Quoted 12 lines · Peter Collingbourne

The mte_sync_page_tags() function sets PG_mte_tagged if it initializes page tags. Then we return to mte_sync_tags(), which sets PG_mte_tagged again. At best, this is redundant. However, it is possible for mte_sync_page_tags() to return without having initialized tags for the page, i.e. in the case where check_swap is true (non-compound page), is_swap_pte(old_pte) is false and pte_is_tagged is false. So at worst, we set PG_mte_tagged on a page with uninitialized tags. This can happen if, for example, page migration causes a PTE for an untagged page to be replaced. If the userspace program subsequently uses mprotect() to enable PROT_MTE for that page, the uninitialized tags will be exposed to userspace.

Quoted 1 line

[...]

Applied to arm64 (for-next/fixes), thanks!

[1/1] arm64: mte: Do not set PG_mte_tagged if tags were not initialized

      https://git.kernel.org/arm64/c/c4c597f1b367

Cheers,

Signature · 5 lines
Will

https://fixes.arm64.dev
https://next.arm64.dev
https://will.arm64.dev