Skip to contentSkip to conversation

KVM: arm64: Harden clock for nvhe/pKVM

PATCH Applied Mostafa Saleh 3 participants 6 messages Apr 30 – May 6

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

PATCH 1/1 KVM: arm64: Harden clock for nvhe/pKVM

Sashiko(locally) reports possiblity of division by zero and out-of-bounds bitwise shift in trace_clock_update().

Although the clock update is untrusted, we should at least have some basic checks to avoid the clock value getting out of sync if the host is buggy.

---
 arch/arm64/kvm/hyp/nvhe/clock.c | 3 +++
 1 file changed, 3 insertions(+)
arch/arm64/kvm/hyp/nvhe/clock.c +3 -0
index 32fc4313fe43..a7fc61976fd0 100644
@@ -35,6 +35,9 @@ void trace_clock_update(u32 mult, u32 shift, u64 epoch_ns, u64 epoch_cyc)
 	struct clock_data *clock = &trace_clock_data;
 	u64 bank = clock->cur ^ 1;
 
+	if (!mult || shift >= 64)
+		return;
+
 	clock->data[bank].mult			= mult;
 	clock->data[bank].shift			= shift;
 	clock->data[bank].epoch_ns		= epoch_ns;
Signature · 1 line
2.54.0.545.g6539524ca2-goog
#1 lore raw
Quoted 6 lines · Mostafa Saleh

Sashiko(locally) reports possiblity of division by zero and out-of-bounds bitwise shift in trace_clock_update().

Although the clock update is untrusted, we should at least have some basic checks to avoid the clock value getting out of sync if the host is buggy.

I am not sure about the gain here. The host can still write values that will make it out of sync anyway.

The timestamp is ultimately fed and read by the host.

Quoted 23 lines
---
 arch/arm64/kvm/hyp/nvhe/clock.c | 3 +++
 1 file changed, 3 insertions(+)
arch/arm64/kvm/hyp/nvhe/clock.c +3 -0
index 32fc4313fe43..a7fc61976fd0 100644
@@ -35,6 +35,9 @@ void trace_clock_update(u32 mult, u32 shift, u64 epoch_ns, u64 epoch_cyc)
 	struct clock_data *clock = &trace_clock_data;
 	u64 bank = clock->cur ^ 1;
 
+	if (!mult || shift >= 64)
+		return;
+
 	clock->data[bank].mult			= mult;
 	clock->data[bank].shift			= shift;
 	clock->data[bank].epoch_ns		= epoch_ns;
Signature · 1 line
2.54.0.545.g6539524ca2-goog
#2 lore raw
Quoted 5 lines · Vincent Donnefort
Quoted 3 lines · Mostafa Saleh

Sashiko(locally) reports possiblity of division by zero and out-of-bounds bitwise shift in trace_clock_update().

Quoted 9 lines
Quoted 3 lines

Although the clock update is untrusted, we should at least have some basic checks to avoid the clock value getting out of sync if the host is buggy.

I am not sure about the gain here. The host can still write values that will make it out of sync anyway.

The timestamp is ultimately fed and read by the host.

This is not about having the clock in sync, but to avoid executing undefined behavior, such as division by zero or large shifts in cases if the host is buggy and not malicious.
That was reported by Sashiko
https://sashiko.dev/#/patchset/20260501111928.259252-1-smostafa%40google.com

Thanks,
Mostafa

Quoted 23 lines
Quoted 23 lines
---
 arch/arm64/kvm/hyp/nvhe/clock.c | 3 +++
 1 file changed, 3 insertions(+)
arch/arm64/kvm/hyp/nvhe/clock.c +3 -1
index 32fc4313fe43..a7fc61976fd0 100644
@@ -35,6 +35,9 @@ void trace_clock_update(u32 mult, u32 shift, u64 epoch_ns, u64 epoch_cyc)
      struct clock_data *clock = &trace_clock_data;
      u64 bank = clock->cur ^ 1;
 
+     if (!mult || shift >= 64)
+             return;
+
      clock->data[bank].mult                  = mult;
      clock->data[bank].shift                 = shift;
      clock->data[bank].epoch_ns              = epoch_ns;
--

2.54.0.545.g6539524ca2-goog

Reviewed-by
#3 lore raw
Quoted 17 lines · Mostafa Saleh
Quoted 10 lines · Vincent Donnefort
Quoted 6 lines · Mostafa Saleh

Sashiko(locally) reports possiblity of division by zero and out-of-bounds bitwise shift in trace_clock_update().

Although the clock update is untrusted, we should at least have some basic checks to avoid the clock value getting out of sync if the host is buggy.

Quoted 5 lines

I am not sure about the gain here. The host can still write values that will make it out of sync anyway.

The timestamp is ultimately fed and read by the host.

Quoted 8 lines

This is not about having the clock in sync, but to avoid executing undefined behavior, such as division by zero or large shifts in cases if the host is buggy and not malicious.
That was reported by Sashiko
https://sashiko.dev/#/patchset/20260501111928.259252-1-smostafa%40google.com

Thanks,
Mostafa

I would then reword that to make it clear it just prevents the host triggering UB on the hypervisor. It doesn't really harden much, which is fine because that data isn't relevant for the hypervisor.

Other than that:

Quoted 24 lines
Quoted 24 lines
Quoted 24 lines
---
 arch/arm64/kvm/hyp/nvhe/clock.c | 3 +++
 1 file changed, 3 insertions(+)
arch/arm64/kvm/hyp/nvhe/clock.c +3 -1
index 32fc4313fe43..a7fc61976fd0 100644
@@ -35,6 +35,9 @@ void trace_clock_update(u32 mult, u32 shift, u64 epoch_ns, u64 epoch_cyc)
      struct clock_data *clock = &trace_clock_data;
      u64 bank = clock->cur ^ 1;
 
+     if (!mult || shift >= 64)
+             return;
+
      clock->data[bank].mult                  = mult;
      clock->data[bank].shift                 = shift;
      clock->data[bank].epoch_ns              = epoch_ns;
--

2.54.0.545.g6539524ca2-goog

maintainer
#4 lore raw
Quoted 25 lines · Vincent Donnefort
Quoted 23 lines · Mostafa Saleh
Quoted 10 lines · Vincent Donnefort
Quoted 6 lines · Mostafa Saleh

Sashiko(locally) reports possiblity of division by zero and out-of-bounds bitwise shift in trace_clock_update().

Although the clock update is untrusted, we should at least have some basic checks to avoid the clock value getting out of sync if the host is buggy.

Quoted 5 lines

I am not sure about the gain here. The host can still write values that will make it out of sync anyway.

The timestamp is ultimately fed and read by the host.

This is not about having the clock in sync, but to avoid executing undefined behavior, such as division by zero or large shifts in cases if the host is buggy and not malicious.
That was reported by Sashiko
https://sashiko.dev/#/patchset/20260501111928.259252-1-smostafa%40google.com

Quoted 6 lines
Quoted 2 lines

Thanks,
Mostafa

I would then reword that to make it clear it just prevents the host triggering UB on the hypervisor. It doesn't really harden much, which is fine because that data isn't relevant for the hypervisor.

I can repaint the commit message when applying this.

Quoted 4 lines

Other than that:

Thanks,

	M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
maintainer Applied
#5 lore raw
In reply to Mostafa Saleh
Quoted 8 lines · Mostafa Saleh

Sashiko(locally) reports possiblity of division by zero and out-of-bounds bitwise shift in trace_clock_update().

Although the clock update is untrusted, we should at least have some basic checks to avoid the clock value getting out of sync if the host is buggy.

[...]

Applied to fixes, thanks!

[1/1] KVM: arm64: Harden clock for nvhe/pKVM

      commit: 9a624ea3f26f40c76bd2c7f77cde30659d42efbd

Cheers,

	M.
Signature · 1 line
Without deviation from the norm, progress is not possible.