Skip to contentSkip to conversation

kvm: LAPIC: Restore guard to prevent illegal APIC register access

PATCH In review Jim Mattson 2 participants 5 messages Jun 9, 2021 – Jun 10, 2021

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

PATCH 1/1 kvm: LAPIC: Restore guard to prevent illegal APIC register access

Per the SDM, "any access that touches bytes 4 through 15 of an APIC register may cause undefined behavior and must not be executed." Worse, such an access in kvm_lapic_reg_read can result in a leak of kernel stack contents. Prior to commit 01402cf81051 ("kvm: LAPIC:
write down valid APIC registers"), such an access was explicitly disallowed. Restore the guard that was removed in that commit.

Fixes 01402cf81051 ("kvm: LAPIC: write down valid APIC registers")
---
 arch/x86/kvm/lapic.c | 3 +++
 1 file changed, 3 insertions(+)
arch/x86/kvm/lapic.c +3 -0
index c0ebef560bd1..32fb82bbd63f 100644
@@ -1410,6 +1410,9 @@ int kvm_lapic_reg_read(struct kvm_lapic *apic, u32 offset, int len,
 	if (!apic_x2apic_mode(apic))
 		valid_reg_mask |= APIC_REG_MASK(APIC_ARBPRI);
 
+	if (alignment + len > 4)
+		return 1;
+
 	if (offset > 0x3f0 || !(valid_reg_mask & APIC_REG_MASK(offset)))
 		return 1;
 
Signature · 1 line
2.32.0.272.g935e593368-goog
Reviewed-by on v1 1/1
#1 lore raw
Quoted 19 lines · Jim Mattson

Per the SDM, "any access that touches bytes 4 through 15 of an APIC register may cause undefined behavior and must not be executed." Worse, such an access in kvm_lapic_reg_read can result in a leak of kernel stack contents. Prior to commit 01402cf81051 ("kvm: LAPIC:
write down valid APIC registers"), such an access was explicitly disallowed. Restore the guard that was removed in that commit.

Fixes 01402cf81051 ("kvm: LAPIC: write down valid APIC registers")
---
  arch/x86/kvm/lapic.c | 3 +++
  1 file changed, 3 insertions(+)
arch/x86/kvm/lapic.c +0 -0
index c0ebef560bd1..32fb82bbd63f 100644

arch/x86/kvm/lapic.c int kvm_lapic_reg_read(struct kvm_lapic *apic, u32 offset, int len, · line 1413

Quoted 4 lines
arch/x86/kvm/lapic.c +1 -0
@@ -1410,6 +1410,9 @@ int kvm_lapic_reg_read(struct kvm_lapic *apic, u32 offset, int len,
  	if (!apic_x2apic_mode(apic))
  		valid_reg_mask |= APIC_REG_MASK(APIC_ARBPRI);
 
+	if (alignment + len > 4)

It will be useful for debugging if the apic_debug() call is added back in.

arch/x86/kvm/lapic.c int kvm_lapic_reg_read(struct kvm_lapic *apic, u32 offset, int len, · line 1418

Quoted 5 lines
arch/x86/kvm/lapic.c +2 -0
+		return 1;
+
  	if (offset > 0x3f0 || !(valid_reg_mask & APIC_REG_MASK(offset)))
  		return 1;
 
#2 lore raw
Quoted 25 lines · Krish Sadhukhan
Quoted 22 lines · Jim Mattson

Per the SDM, "any access that touches bytes 4 through 15 of an APIC register may cause undefined behavior and must not be executed." Worse, such an access in kvm_lapic_reg_read can result in a leak of kernel stack contents. Prior to commit 01402cf81051 ("kvm: LAPIC:
write down valid APIC registers"), such an access was explicitly disallowed. Restore the guard that was removed in that commit.

Fixes 01402cf81051 ("kvm: LAPIC: write down valid APIC registers")
---
  arch/x86/kvm/lapic.c | 3 +++
  1 file changed, 3 insertions(+)
arch/x86/kvm/lapic.c +0 -0
index c0ebef560bd1..32fb82bbd63f 100644
@@ -1410,6 +1410,9 @@ int kvm_lapic_reg_read(struct kvm_lapic *apic, u32 offset, int len,
      if (!apic_x2apic_mode(apic))
              valid_reg_mask |= APIC_REG_MASK(APIC_ARBPRI);
 
Quoted 3 lines

arch/x86/kvm/lapic.c int kvm_lapic_reg_read(struct kvm_lapic *apic, u32 offset, int len, · line 1413

Quoted 1 line
arch/x86/kvm/lapic.c +1 -0
+     if (alignment + len > 4)

It will be useful for debugging if the apic_debug() call is added back in.

Are you suggesting that I should revert commit 0d88800d5472 ("kvm:
x86: ioapic and apic debug macros cleanup")?

Quoted 7 lines

arch/x86/kvm/lapic.c int kvm_lapic_reg_read(struct kvm_lapic *apic, u32 offset, int len, · line 1418

Quoted 5 lines
arch/x86/kvm/lapic.c +2 -0
+             return 1;
+
      if (offset > 0x3f0 || !(valid_reg_mask & APIC_REG_MASK(offset)))
              return 1;
 
#3 lore raw
Quoted 26 lines · Jim Mattson
Quoted 24 lines · Krish Sadhukhan
Quoted 22 lines · Jim Mattson

Per the SDM, "any access that touches bytes 4 through 15 of an APIC register may cause undefined behavior and must not be executed." Worse, such an access in kvm_lapic_reg_read can result in a leak of kernel stack contents. Prior to commit 01402cf81051 ("kvm: LAPIC:
write down valid APIC registers"), such an access was explicitly disallowed. Restore the guard that was removed in that commit.

Fixes 01402cf81051 ("kvm: LAPIC: write down valid APIC registers")
---
   arch/x86/kvm/lapic.c | 3 +++
   1 file changed, 3 insertions(+)
arch/x86/kvm/lapic.c +0 -0
index c0ebef560bd1..32fb82bbd63f 100644
@@ -1410,6 +1410,9 @@ int kvm_lapic_reg_read(struct kvm_lapic *apic, u32 offset, int len,
       if (!apic_x2apic_mode(apic))
               valid_reg_mask |= APIC_REG_MASK(APIC_ARBPRI);
 
Quoted 4 lines
Quoted 2 lines

arch/x86/kvm/lapic.c int kvm_lapic_reg_read(struct kvm_lapic *apic, u32 offset, int len, · line 1413

Quoted 1 line
arch/x86/kvm/lapic.c +1 -0
+     if (alignment + len > 4)

It will be useful for debugging if the apic_debug() call is added back in.

Are you suggesting that I should revert commit 0d88800d5472 ("kvm:
x86: ioapic and apic debug macros cleanup")?

Oh, I wasn't aware that commit 0d88800d5472 had removed the debug macros.  The tracepoint in kvm_lapic_reg_read() fires after these error checks pass. A printk may be useful. Or perhaps move the tracepoint up ?

Quoted 7 lines
Quoted 7 lines

arch/x86/kvm/lapic.c int kvm_lapic_reg_read(struct kvm_lapic *apic, u32 offset, int len, · line 1418

Quoted 6 lines
arch/x86/kvm/lapic.c +2 -0
+             return 1;
+
       if (offset > 0x3f0 || !(valid_reg_mask & APIC_REG_MASK(offset)))
               return 1;
 
#4 lore raw
Quoted 31 lines · Krish Sadhukhan
Quoted 28 lines · Jim Mattson
Quoted 26 lines · Krish Sadhukhan
Quoted 19 lines · Jim Mattson

Per the SDM, "any access that touches bytes 4 through 15 of an APIC register may cause undefined behavior and must not be executed." Worse, such an access in kvm_lapic_reg_read can result in a leak of kernel stack contents. Prior to commit 01402cf81051 ("kvm: LAPIC:
write down valid APIC registers"), such an access was explicitly disallowed. Restore the guard that was removed in that commit.

Fixes 01402cf81051 ("kvm: LAPIC: write down valid APIC registers")
---
   arch/x86/kvm/lapic.c | 3 +++
   1 file changed, 3 insertions(+)
arch/x86/kvm/lapic.c +0 -0
index c0ebef560bd1..32fb82bbd63f 100644

arch/x86/kvm/lapic.c int kvm_lapic_reg_read(struct kvm_lapic *apic, u32 offset, int len, · line 1413

Quoted 4 lines
arch/x86/kvm/lapic.c +1 -0
@@ -1410,6 +1410,9 @@ int kvm_lapic_reg_read(struct kvm_lapic *apic, u32 offset, int len,
       if (!apic_x2apic_mode(apic))
               valid_reg_mask |= APIC_REG_MASK(APIC_ARBPRI);
 
+     if (alignment + len > 4)

It will be useful for debugging if the apic_debug() call is added back in.

Quoted 7 lines
Quoted 2 lines

Are you suggesting that I should revert commit 0d88800d5472 ("kvm:
x86: ioapic and apic debug macros cleanup")?

Oh, I wasn't aware that commit 0d88800d5472 had removed the debug macros. The tracepoint in kvm_lapic_reg_read() fires after these error checks pass. A printk may be useful. Or perhaps move the tracepoint up ?

It sounds like you disagree with the claim in commit 0d88800d5472 ("kvm: x86: ioapic and apic debug macros cleanup") that "kvm tracepoints are enough for debugging." I'll let you argue that point with Yi and Paolo separately, as it seems orthogonal to this change.

My personal opinion is that messages that get written to syslog are next to useless. Unless the message goes out to userspace, I have no way of getting the message to my end customers. Similarly, while tracepoints may be useful for developers, they are useless in production, and since I can't usually get my hands on a customer workload to run in a development environment, tracepoints also have quite limited usefulness.