Skip to contentSkip to conversation

arm64: mops: Do not dereference src reg for a set operation

PATCH In review Keir Fraser 3 participants 4 messages Mar 26, 2025

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

PATCH 1/1 arm64: mops: Do not dereference src reg for a set operation

The register is not defined and reading it can result in a UBSAN out-of-bounds array access error, specifically when the srcreg field value is 31.

Cc Kristina Martsenko <[email protected]>
Cc Catalin Marinas <[email protected]>
Cc Mark Rutland <[email protected]>
Cc Will Deacon <[email protected]>
Cc Marc Zyngier <[email protected]>
---
 arch/arm64/include/asm/traps.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
arch/arm64/include/asm/traps.h +2 -2
index d780d1bd2eac..82cf1f879c61 100644
@@ -109,10 +109,9 @@ static inline void arm64_mops_reset_regs(struct user_pt_regs *regs, unsigned lon
 	int dstreg = ESR_ELx_MOPS_ISS_DESTREG(esr);
 	int srcreg = ESR_ELx_MOPS_ISS_SRCREG(esr);
 	int sizereg = ESR_ELx_MOPS_ISS_SIZEREG(esr);
-	unsigned long dst, src, size;
+	unsigned long dst, size;
 
 	dst = regs->regs[dstreg];
-	src = regs->regs[srcreg];
 	size = regs->regs[sizereg];
 
 	/*
@@ -129,6 +128,7 @@ static inline void arm64_mops_reset_regs(struct user_pt_regs *regs, unsigned lon
 		}
 	} else {
 		/* CPY* instruction */
+		unsigned long src = regs->regs[srcreg];
 		if (!(option_a ^ wrong_option)) {
 			/* Format is from Option B */
 			if (regs->pstate & PSR_N_BIT) {
Signature · 1 line
2.49.0.395.g12beb8f557-goog
Reviewed-by on v1 1/1
#1 lore raw
Quoted 4 lines · Keir Fraser

The register is not defined and reading it can result in a UBSAN out-of-bounds array access error, specifically when the srcreg field value is 31.

Gah, XZR/SP encoding strikes back...

Quoted 29 lines
Cc Kristina Martsenko <[email protected]>
Cc Catalin Marinas <[email protected]>
Cc Mark Rutland <[email protected]>
Cc Will Deacon <[email protected]>
Cc Marc Zyngier <[email protected]>
---
 arch/arm64/include/asm/traps.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
arch/arm64/include/asm/traps.h +1 -2
index d780d1bd2eac..82cf1f879c61 100644
@@ -109,10 +109,9 @@ static inline void arm64_mops_reset_regs(struct user_pt_regs *regs, unsigned lon
 	int dstreg = ESR_ELx_MOPS_ISS_DESTREG(esr);
 	int srcreg = ESR_ELx_MOPS_ISS_SRCREG(esr);
 	int sizereg = ESR_ELx_MOPS_ISS_SIZEREG(esr);
-	unsigned long dst, src, size;
+	unsigned long dst, size;
 
 	dst = regs->regs[dstreg];
-	src = regs->regs[srcreg];
 	size = regs->regs[sizereg];
 
 	/*

arch/arm64/include/asm/traps.h static inline void arm64_mops_reset_regs(struct user_pt_regs *regs, unsigned lon · line 134

Quoted 7 lines
arch/arm64/include/asm/traps.h +1 -0
@@ -129,6 +128,7 @@ static inline void arm64_mops_reset_regs(struct user_pt_regs *regs, unsigned lon
 		}
 	} else {
 		/* CPY* instruction */
+		unsigned long src = regs->regs[srcreg];
 		if (!(option_a ^ wrong_option)) {
 			/* Format is from Option B */
 			if (regs->pstate & PSR_N_BIT) {
	M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
reviewer
#2 lore raw
In reply to Keir Fraser
Quoted 3 lines · Keir Fraser

The register is not defined and reading it can result in a UBSAN out-of-bounds array access error, specifically when the srcreg field value is 31.

I'm assuming this is for a MOPS exception taken from a SET* sequence with XZR as the source?

It'd be nice to say that explicitly, as this is the only case where any of the src/dst/size fields in the ESR can be reported as 31. In all other cases where a CPY* or SET* instruction takes register 31 as an argument, the behaviour is CONSTRAINED UNPREDICTABLE and cannot generate a MOPS exception.

Note that in ARM DDI 0487 L.a there's a bug where:

* The prose says that SET* taking XZR as a src is CONSTRAINED UNPREDICTABLE, per K1.2.17.1.1 linked from C6.2.332.

The title for the K1.2.17.1.1 is "Memory Copy and Memory Set CPY*", which looks like an editing error.

* The pseudocode is explicit that the CONSTRAINED UNPREDICTABLE behaviours differ for CPY* and SET* per J1.1.3.121 CheckCPYConstrainedUnpredictable() and J1.1.3.125 CheckSETConstrainedUnpredictable().

... and I'll go file a ticket about that soon if someone doesn't beat me to it.

Quoted 6 lines
Cc Kristina Martsenko <[email protected]>
Cc Catalin Marinas <[email protected]>
Cc Mark Rutland <[email protected]>
Cc Will Deacon <[email protected]>
Cc Marc Zyngier <[email protected]>

Looks like this should have:

Fixes 2de451a329cf662b ("KVM: arm64: Add handler for MOPS exceptions")

Prior to that, the code in do_el0_mops() was benign as the use of pt_regs_read_reg() prevented the out-of-bounds access. It'd also be nice to note that in the commit message.

Mark.

Quoted 32 lines
---
 arch/arm64/include/asm/traps.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
arch/arm64/include/asm/traps.h +2 -2
index d780d1bd2eac..82cf1f879c61 100644
@@ -109,10 +109,9 @@ static inline void arm64_mops_reset_regs(struct user_pt_regs *regs, unsigned lon
 	int dstreg = ESR_ELx_MOPS_ISS_DESTREG(esr);
 	int srcreg = ESR_ELx_MOPS_ISS_SRCREG(esr);
 	int sizereg = ESR_ELx_MOPS_ISS_SIZEREG(esr);
-	unsigned long dst, src, size;
+	unsigned long dst, size;
 
 	dst = regs->regs[dstreg];
-	src = regs->regs[srcreg];
 	size = regs->regs[sizereg];
 
 	/*
@@ -129,6 +128,7 @@ static inline void arm64_mops_reset_regs(struct user_pt_regs *regs, unsigned lon
 		}
 	} else {
 		/* CPY* instruction */
+		unsigned long src = regs->regs[srcreg];
 		if (!(option_a ^ wrong_option)) {
 			/* Format is from Option B */
 			if (regs->pstate & PSR_N_BIT) {
Signature · 1 line
2.49.0.395.g12beb8f557-goog
#3 lore raw
Quoted 7 lines · Mark Rutland
Quoted 3 lines · Keir Fraser

The register is not defined and reading it can result in a UBSAN out-of-bounds array access error, specifically when the srcreg field value is 31.

I'm assuming this is for a MOPS exception taken from a SET* sequence with XZR as the source?

Yes.

Quoted 5 lines

It'd be nice to say that explicitly, as this is the only case where any of the src/dst/size fields in the ESR can be reported as 31. In all other cases where a CPY* or SET* instruction takes register 31 as an argument, the behaviour is CONSTRAINED UNPREDICTABLE and cannot generate a MOPS exception.

Okay, will do.

Quoted 23 lines

Note that in ARM DDI 0487 L.a there's a bug where:

* The prose says that SET* taking XZR as a src is CONSTRAINED UNPREDICTABLE, per K1.2.17.1.1 linked from C6.2.332.

The title for the K1.2.17.1.1 is "Memory Copy and Memory Set CPY*", which looks like an editing error.

* The pseudocode is explicit that the CONSTRAINED UNPREDICTABLE behaviours differ for CPY* and SET* per J1.1.3.121 CheckCPYConstrainedUnpredictable() and J1.1.3.125 CheckSETConstrainedUnpredictable().

... and I'll go file a ticket about that soon if someone doesn't beat me to it.

Quoted 6 lines
Cc Kristina Martsenko <[email protected]>
Cc Catalin Marinas <[email protected]>
Cc Mark Rutland <[email protected]>
Cc Will Deacon <[email protected]>
Cc Marc Zyngier <[email protected]>
Quoted 7 lines

Looks like this should have:

Fixes 2de451a329cf662b ("KVM: arm64: Add handler for MOPS exceptions")

Prior to that, the code in do_el0_mops() was benign as the use of pt_regs_read_reg() prevented the out-of-bounds access. It'd also be nice to note that in the commit message.

I will add this too. And Marc's reviewed-by. And re-send as v2. Thanks!

Keir

Quoted 34 lines

Mark.

Quoted 32 lines
---
 arch/arm64/include/asm/traps.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
arch/arm64/include/asm/traps.h +2 -2
index d780d1bd2eac..82cf1f879c61 100644
@@ -109,10 +109,9 @@ static inline void arm64_mops_reset_regs(struct user_pt_regs *regs, unsigned lon
 	int dstreg = ESR_ELx_MOPS_ISS_DESTREG(esr);
 	int srcreg = ESR_ELx_MOPS_ISS_SRCREG(esr);
 	int sizereg = ESR_ELx_MOPS_ISS_SIZEREG(esr);
-	unsigned long dst, src, size;
+	unsigned long dst, size;
 
 	dst = regs->regs[dstreg];
-	src = regs->regs[srcreg];
 	size = regs->regs[sizereg];
 
 	/*
@@ -129,6 +128,7 @@ static inline void arm64_mops_reset_regs(struct user_pt_regs *regs, unsigned lon
 		}
 	} else {
 		/* CPY* instruction */
+		unsigned long src = regs->regs[srcreg];
 		if (!(option_a ^ wrong_option)) {
 			/* Format is from Option B */
 			if (regs->pstate & PSR_N_BIT) {
Signature · 1 line
2.49.0.395.g12beb8f557-goog