All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v3 stable 4.14 4.19 0/3] Backport "KVM: arm64: Filter out invalid core registers IDs in KVM_GET_REG_LIST"
@ 2023-04-04 10:30 Takahiro Itazuri
  2023-04-04 10:30 ` [PATCH v3 stable 4.14 4.19 1/3] KVM: arm64: Factor out core register ID enumeration Takahiro Itazuri
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: Takahiro Itazuri @ 2023-04-04 10:30 UTC (permalink / raw)
  To: stable
  Cc: Greg Kroah-Hartman, Sasha Levin, Dave Martin, Andrew Jones,
	Marc Zyngier, Julien Thierry, zhang . lei, Takahiro Itazuri,
	Takahiro Itazuri

Hi stable maintainers,

This is a backport patch for commit df205b5c6328 ("KVM: arm64: Filter
out invalid core register IDs in KVM_GET_REG_LIST") to 4.14 and 4.19.
This commit was not applied to the 4.14-stable tree due to merge
conflict [1]. To backport this, cherry-picked commit be25bbb392fa
("KVM: arm64: Factor out core register ID enumeration") that has no
functional changes but makes it easy to merge, and commit 5d8d4af24460
("arm64: KVM: Fix system register enumeration") that is a fix patch for
the commit.

I'd appreciate it if you could consider backporting this to 4.14 and
4.19.

Best regards,
Takahiro

[1] https://lore.kernel.org/all/1560343489-22906-1-git-send-email-Dave.Martin@arm.com/

Dave Martin (2):
  KVM: arm64: Factor out core register ID enumeration
  KVM: arm64: Filter out invalid core register IDs in KVM_GET_REG_LIST

 arch/arm64/kvm/guest.c | 79 ++++++++++++++++++++++++++++++++++--------
 1 file changed, 65 insertions(+), 14 deletions(-)

-- 
2.39.2

v2 -> v3:
* Cherry-pick an additional fix patch.
* Link to v2: https://lore.kernel.org/all/20230404034649.77915-1-itazur@amazon.com/

v1 -> v2:
* Fix a compile error for core_reg_size_from_offset().
* Link to v1: https://lore.kernel.org/all/20230403223028.45131-1-itazur@amazon.com/


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH v3 stable 4.14 4.19 1/3] KVM: arm64: Factor out core register ID enumeration
  2023-04-04 10:30 [PATCH v3 stable 4.14 4.19 0/3] Backport "KVM: arm64: Filter out invalid core registers IDs in KVM_GET_REG_LIST" Takahiro Itazuri
@ 2023-04-04 10:30 ` Takahiro Itazuri
  2023-04-04 10:30 ` [PATCH v3 stable 4.14 4.19 2/3] KVM: arm64: Filter out invalid core register IDs in KVM_GET_REG_LIST Takahiro Itazuri
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: Takahiro Itazuri @ 2023-04-04 10:30 UTC (permalink / raw)
  To: stable
  Cc: Greg Kroah-Hartman, Sasha Levin, Dave Martin, Andrew Jones,
	Marc Zyngier, Julien Thierry, zhang . lei, Takahiro Itazuri,
	Takahiro Itazuri

From: Dave Martin <Dave.Martin@arm.com>

[ Upstream commit be25bbb392fad3a721d6d21b78639b60612b5439 ]

In preparation for adding logic to filter out some KVM_REG_ARM_CORE
registers from the KVM_GET_REG_LIST output, this patch factors out
the core register enumeration into a separate function and rebuilds
num_core_regs() on top of it.

This may be a little more expensive (depending on how good a job
the compiler does of specialising the code), but KVM_GET_REG_LIST
is not a hot path.

This will make it easier to consolidate ID filtering code in one
place.

No functional change.

Signed-off-by: Dave Martin <Dave.Martin@arm.com>
Reviewed-by: Julien Thierry <julien.thierry@arm.com>
Tested-by: zhang.lei <zhang.lei@jp.fujitsu.com>
Signed-off-by: Marc Zyngier <marc.zyngier@arm.com>
Signed-off-by: Takahiro Itazuri <itazur@amazon.com>
---
 arch/arm64/kvm/guest.c | 32 ++++++++++++++++++++++++--------
 1 file changed, 24 insertions(+), 8 deletions(-)

diff --git a/arch/arm64/kvm/guest.c b/arch/arm64/kvm/guest.c
index 76d27edf33cb..d29cb34a828f 100644
--- a/arch/arm64/kvm/guest.c
+++ b/arch/arm64/kvm/guest.c
@@ -193,9 +193,28 @@ int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
 	return -EINVAL;
 }
 
+static int kvm_arm_copy_core_reg_indices(u64 __user *uindices)
+{
+	unsigned int i;
+	int n = 0;
+	const u64 core_reg = KVM_REG_ARM64 | KVM_REG_SIZE_U64 | KVM_REG_ARM_CORE;
+
+	for (i = 0; i < sizeof(struct kvm_regs) / sizeof(__u32); i++) {
+		if (uindices) {
+			if (put_user(core_reg | i, uindices))
+				return -EFAULT;
+			uindices++;
+		}
+
+		n++;
+	}
+
+	return n;
+}
+
 static unsigned long num_core_regs(void)
 {
-	return sizeof(struct kvm_regs) / sizeof(__u32);
+	return kvm_arm_copy_core_reg_indices(NULL);
 }
 
 /**
@@ -269,15 +288,12 @@ unsigned long kvm_arm_num_regs(struct kvm_vcpu *vcpu)
  */
 int kvm_arm_copy_reg_indices(struct kvm_vcpu *vcpu, u64 __user *uindices)
 {
-	unsigned int i;
-	const u64 core_reg = KVM_REG_ARM64 | KVM_REG_SIZE_U64 | KVM_REG_ARM_CORE;
 	int ret;
 
-	for (i = 0; i < sizeof(struct kvm_regs) / sizeof(__u32); i++) {
-		if (put_user(core_reg | i, uindices))
-			return -EFAULT;
-		uindices++;
-	}
+	ret = kvm_arm_copy_core_reg_indices(uindices);
+	if (ret)
+		return ret;
+	uindices += ret;
 
 	ret = kvm_arm_copy_fw_reg_indices(vcpu, uindices);
 	if (ret)
-- 
2.39.2


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH v3 stable 4.14 4.19 2/3] KVM: arm64: Filter out invalid core register IDs in KVM_GET_REG_LIST
  2023-04-04 10:30 [PATCH v3 stable 4.14 4.19 0/3] Backport "KVM: arm64: Filter out invalid core registers IDs in KVM_GET_REG_LIST" Takahiro Itazuri
  2023-04-04 10:30 ` [PATCH v3 stable 4.14 4.19 1/3] KVM: arm64: Factor out core register ID enumeration Takahiro Itazuri
@ 2023-04-04 10:30 ` Takahiro Itazuri
  2023-04-04 10:30 ` [PATCH v3 stable 4.14 4.19 3/3] arm64: KVM: Fix system register enumeration Takahiro Itazuri
  2023-04-18 10:24 ` [PATCH v3 stable 4.14 4.19 0/3] Backport "KVM: arm64: Filter out invalid core registers IDs in KVM_GET_REG_LIST" Greg Kroah-Hartman
  3 siblings, 0 replies; 5+ messages in thread
From: Takahiro Itazuri @ 2023-04-04 10:30 UTC (permalink / raw)
  To: stable
  Cc: Greg Kroah-Hartman, Sasha Levin, Dave Martin, Andrew Jones,
	Marc Zyngier, Julien Thierry, zhang . lei, Takahiro Itazuri,
	Takahiro Itazuri

From: Dave Martin <Dave.Martin@arm.com>

[ Upstream commit df205b5c63281e4f32caac22adda18fd68795e80 ]

Since commit d26c25a9d19b ("arm64: KVM: Tighten guest core register
access from userspace"), KVM_{GET,SET}_ONE_REG rejects register IDs
that do not correspond to a single underlying architectural register.

KVM_GET_REG_LIST was not changed to match however: instead, it
simply yields a list of 32-bit register IDs that together cover the
whole kvm_regs struct.  This means that if userspace tries to use
the resulting list of IDs directly to drive calls to KVM_*_ONE_REG,
some of those calls will now fail.

This was not the intention.  Instead, iterating KVM_*_ONE_REG over
the list of IDs returned by KVM_GET_REG_LIST should be guaranteed
to work.

This patch fixes the problem by splitting validate_core_offset()
into a backend core_reg_size_from_offset() which does all of the
work except for checking that the size field in the register ID
matches, and kvm_arm_copy_reg_indices() and num_core_regs() are
converted to use this to enumerate the valid offsets.

kvm_arm_copy_reg_indices() now also sets the register ID size field
appropriately based on the value returned, so the register ID
supplied to userspace is fully qualified for use with the register
access ioctls.

Cc: stable@vger.kernel.org
Fixes: d26c25a9d19b ("arm64: KVM: Tighten guest core register access from userspace")
Signed-off-by: Dave Martin <Dave.Martin@arm.com>
Reviewed-by: Andrew Jones <drjones@redhat.com>
Tested-by: Andrew Jones <drjones@redhat.com>
Signed-off-by: Marc Zyngier <marc.zyngier@arm.com>
Signed-off-by: Takahiro Itazuri <itazur@amazon.com>
---
 arch/arm64/kvm/guest.c | 51 +++++++++++++++++++++++++++++++++++-------
 1 file changed, 43 insertions(+), 8 deletions(-)

diff --git a/arch/arm64/kvm/guest.c b/arch/arm64/kvm/guest.c
index d29cb34a828f..6835ddf598a7 100644
--- a/arch/arm64/kvm/guest.c
+++ b/arch/arm64/kvm/guest.c
@@ -57,9 +57,8 @@ static u64 core_reg_offset_from_id(u64 id)
 	return id & ~(KVM_REG_ARCH_MASK | KVM_REG_SIZE_MASK | KVM_REG_ARM_CORE);
 }
 
-static int validate_core_offset(const struct kvm_one_reg *reg)
+static int core_reg_size_from_offset(u64 off)
 {
-	u64 off = core_reg_offset_from_id(reg->id);
 	int size;
 
 	switch (off) {
@@ -89,11 +88,24 @@ static int validate_core_offset(const struct kvm_one_reg *reg)
 		return -EINVAL;
 	}
 
-	if (KVM_REG_SIZE(reg->id) == size &&
-	    IS_ALIGNED(off, size / sizeof(__u32)))
-		return 0;
+	if (!IS_ALIGNED(off, size / sizeof(__u32)))
+		return -EINVAL;
 
-	return -EINVAL;
+	return size;
+}
+
+static int validate_core_offset(const struct kvm_one_reg *reg)
+{
+	u64 off = core_reg_offset_from_id(reg->id);
+	int size = core_reg_size_from_offset(off);
+
+	if (size < 0)
+		return -EINVAL;
+
+	if (KVM_REG_SIZE(reg->id) != size)
+		return -EINVAL;
+
+	return 0;
 }
 
 static int get_core_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
@@ -197,11 +209,34 @@ static int kvm_arm_copy_core_reg_indices(u64 __user *uindices)
 {
 	unsigned int i;
 	int n = 0;
-	const u64 core_reg = KVM_REG_ARM64 | KVM_REG_SIZE_U64 | KVM_REG_ARM_CORE;
 
 	for (i = 0; i < sizeof(struct kvm_regs) / sizeof(__u32); i++) {
+		u64 reg = KVM_REG_ARM64 | KVM_REG_ARM_CORE | i;
+		int size = core_reg_size_from_offset(i);
+
+		if (size < 0)
+			continue;
+
+		switch (size) {
+		case sizeof(__u32):
+			reg |= KVM_REG_SIZE_U32;
+			break;
+
+		case sizeof(__u64):
+			reg |= KVM_REG_SIZE_U64;
+			break;
+
+		case sizeof(__uint128_t):
+			reg |= KVM_REG_SIZE_U128;
+			break;
+
+		default:
+			WARN_ON(1);
+			continue;
+		}
+
 		if (uindices) {
-			if (put_user(core_reg | i, uindices))
+			if (put_user(reg, uindices))
 				return -EFAULT;
 			uindices++;
 		}
-- 
2.39.2


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH v3 stable 4.14 4.19 3/3] arm64: KVM: Fix system register enumeration
  2023-04-04 10:30 [PATCH v3 stable 4.14 4.19 0/3] Backport "KVM: arm64: Filter out invalid core registers IDs in KVM_GET_REG_LIST" Takahiro Itazuri
  2023-04-04 10:30 ` [PATCH v3 stable 4.14 4.19 1/3] KVM: arm64: Factor out core register ID enumeration Takahiro Itazuri
  2023-04-04 10:30 ` [PATCH v3 stable 4.14 4.19 2/3] KVM: arm64: Filter out invalid core register IDs in KVM_GET_REG_LIST Takahiro Itazuri
@ 2023-04-04 10:30 ` Takahiro Itazuri
  2023-04-18 10:24 ` [PATCH v3 stable 4.14 4.19 0/3] Backport "KVM: arm64: Filter out invalid core registers IDs in KVM_GET_REG_LIST" Greg Kroah-Hartman
  3 siblings, 0 replies; 5+ messages in thread
From: Takahiro Itazuri @ 2023-04-04 10:30 UTC (permalink / raw)
  To: stable
  Cc: Greg Kroah-Hartman, Sasha Levin, Dave Martin, Andrew Jones,
	Marc Zyngier, Julien Thierry, zhang . lei, Takahiro Itazuri,
	Takahiro Itazuri

From: Marc Zyngier <marc.zyngier@arm.com>

[ Upstream commit 5d8d4af24460d079ecdb190254b14b528add1228 ]

The introduction of the SVE registers to userspace started with a
refactoring of the way we expose any register via the ONE_REG
interface.

Unfortunately, this change doesn't exactly behave as expected
if the number of registers is non-zero and consider everything
to be an error. The visible result is that QEMU barfs very early
when creating vcpus.

Make sure we only exit early in case there is an actual error, rather
than a positive number of registers...

Fixes: be25bbb392fa ("KVM: arm64: Factor out core register ID enumeration")
Signed-off-by: Marc Zyngier <marc.zyngier@arm.com>
Signed-off-by: Takahiro Itazuri <itazur@amazon.com>
---
 arch/arm64/kvm/guest.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/arch/arm64/kvm/guest.c b/arch/arm64/kvm/guest.c
index 6835ddf598a7..8ae0f408b89c 100644
--- a/arch/arm64/kvm/guest.c
+++ b/arch/arm64/kvm/guest.c
@@ -326,17 +326,17 @@ int kvm_arm_copy_reg_indices(struct kvm_vcpu *vcpu, u64 __user *uindices)
 	int ret;
 
 	ret = kvm_arm_copy_core_reg_indices(uindices);
-	if (ret)
+	if (ret < 0)
 		return ret;
 	uindices += ret;
 
 	ret = kvm_arm_copy_fw_reg_indices(vcpu, uindices);
-	if (ret)
+	if (ret < 0)
 		return ret;
 	uindices += kvm_arm_get_fw_num_regs(vcpu);
 
 	ret = copy_timer_indices(vcpu, uindices);
-	if (ret)
+	if (ret < 0)
 		return ret;
 	uindices += NUM_TIMER_REGS;
 
-- 
2.39.2


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH v3 stable 4.14 4.19 0/3] Backport "KVM: arm64: Filter out invalid core registers IDs in KVM_GET_REG_LIST"
  2023-04-04 10:30 [PATCH v3 stable 4.14 4.19 0/3] Backport "KVM: arm64: Filter out invalid core registers IDs in KVM_GET_REG_LIST" Takahiro Itazuri
                   ` (2 preceding siblings ...)
  2023-04-04 10:30 ` [PATCH v3 stable 4.14 4.19 3/3] arm64: KVM: Fix system register enumeration Takahiro Itazuri
@ 2023-04-18 10:24 ` Greg Kroah-Hartman
  3 siblings, 0 replies; 5+ messages in thread
From: Greg Kroah-Hartman @ 2023-04-18 10:24 UTC (permalink / raw)
  To: Takahiro Itazuri
  Cc: stable, Sasha Levin, Dave Martin, Andrew Jones, Marc Zyngier,
	Julien Thierry, zhang . lei, Takahiro Itazuri

On Tue, Apr 04, 2023 at 11:30:47AM +0100, Takahiro Itazuri wrote:
> Hi stable maintainers,
> 
> This is a backport patch for commit df205b5c6328 ("KVM: arm64: Filter
> out invalid core register IDs in KVM_GET_REG_LIST") to 4.14 and 4.19.
> This commit was not applied to the 4.14-stable tree due to merge
> conflict [1]. To backport this, cherry-picked commit be25bbb392fa
> ("KVM: arm64: Factor out core register ID enumeration") that has no
> functional changes but makes it easy to merge, and commit 5d8d4af24460
> ("arm64: KVM: Fix system register enumeration") that is a fix patch for
> the commit.
> 
> I'd appreciate it if you could consider backporting this to 4.14 and
> 4.19.

All now queued up, thanks.

greg k-h

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2023-04-18 10:25 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-04-04 10:30 [PATCH v3 stable 4.14 4.19 0/3] Backport "KVM: arm64: Filter out invalid core registers IDs in KVM_GET_REG_LIST" Takahiro Itazuri
2023-04-04 10:30 ` [PATCH v3 stable 4.14 4.19 1/3] KVM: arm64: Factor out core register ID enumeration Takahiro Itazuri
2023-04-04 10:30 ` [PATCH v3 stable 4.14 4.19 2/3] KVM: arm64: Filter out invalid core register IDs in KVM_GET_REG_LIST Takahiro Itazuri
2023-04-04 10:30 ` [PATCH v3 stable 4.14 4.19 3/3] arm64: KVM: Fix system register enumeration Takahiro Itazuri
2023-04-18 10:24 ` [PATCH v3 stable 4.14 4.19 0/3] Backport "KVM: arm64: Filter out invalid core registers IDs in KVM_GET_REG_LIST" Greg Kroah-Hartman

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.