Skip to contentSkip to conversation

KVM: s390: Limit adapter indicator access to page

PATCH Superseded Janosch Frank 3 participants 7 messages Feb 17 – Feb 27

Subsystems (4)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Janosch Frank

author maintainer
#0 lore raw

cover KVM: s390: Limit adapter indicator access to page

We currently check the address of the indicator fields but not the sum of address and offset. This patch remedies that problem and limits the address + offset combination to a single page.

The selftest is very rudimentary but it's a start.

Janosch Frank (2):
KVM: s390: Limit adapter indicator access to mapped page
KVM: s390: selftests: Add IRQ routing address offset tests

 arch/s390/kvm/interrupt.c                     | 12 +++
 tools/testing/selftests/kvm/Makefile.kvm      |  1 +
 .../testing/selftests/kvm/s390/irq_routing.c  | 75 +++++++++++++++++++
 3 files changed, 88 insertions(+)
 create mode 100644 tools/testing/selftests/kvm/s390/irq_routing.c
Signature · 1 line
2.53.0
author maintainer
#1 lore raw

PATCH 1/2 KVM: s390: Limit adapter indicator access to mapped page

While we check the address for errors, we don't seem to check the bit offsets and since they are 32 and 64 bits a lot of memory can be reached indirectly via those offsets.

Fixes 84223598778b ("KVM: s390: irq routing for adapter interrupts.")
---
 arch/s390/kvm/interrupt.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)
arch/s390/kvm/interrupt.c +12 -0
index 1c2bb5cd7e12..cd4851e33a5b 100644
@@ -2724,6 +2724,9 @@ static unsigned long get_ind_bit(__u64 addr, unsigned long bit_nr, bool swap)
 
 	bit = bit_nr + (addr % PAGE_SIZE) * 8;
 
+	/* kvm_set_routing_entry() should never allow this to happen */
+	WARN_ON_ONCE(bit > (PAGE_SIZE * BITS_PER_BYTE - 1));
+
 	return swap ? (bit ^ (BITS_PER_LONG - 1)) : bit;
 }
 
@@ -2852,6 +2855,7 @@ int kvm_set_routing_entry(struct kvm *kvm,
 			  struct kvm_kernel_irq_routing_entry *e,
 			  const struct kvm_irq_routing_entry *ue)
 {
+	const struct kvm_irq_routing_s390_adapter *adapter;
 	u64 uaddr_s, uaddr_i;
 	int idx;
 
@@ -2862,6 +2866,14 @@ int kvm_set_routing_entry(struct kvm *kvm,
 			return -EINVAL;
 		e->set = set_adapter_int;
 
+		adapter = &ue->u.adapter;
+		if (adapter->summary_addr + BITS_TO_BYTES(adapter->summary_offset) >=
+		    (adapter->summary_addr & PAGE_MASK) + PAGE_SIZE)
+			return -EINVAL;
+		if (adapter->ind_addr + BITS_TO_BYTES(adapter->ind_offset) >=
+		    (adapter->ind_addr & PAGE_MASK) + PAGE_SIZE)
+			return -EINVAL;
+
 		idx = srcu_read_lock(&kvm->srcu);
 		uaddr_s = gpa_to_hva(kvm, ue->u.adapter.summary_addr);
 		uaddr_i = gpa_to_hva(kvm, ue->u.adapter.ind_addr);
Signature · 1 line
2.53.0
#2 lore raw

Re: [PATCH 1/2] KVM: s390: Limit adapter indicator access to mapped page

Quoted 34 lines · Janosch Frank

While we check the address for errors, we don't seem to check the bit offsets and since they are 32 and 64 bits a lot of memory can be reached indirectly via those offsets.

Fixes 84223598778b ("KVM: s390: irq routing for adapter interrupts.")
---
 arch/s390/kvm/interrupt.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)
arch/s390/kvm/interrupt.c +4 -0
index 1c2bb5cd7e12..cd4851e33a5b 100644
@@ -2724,6 +2724,9 @@ static unsigned long get_ind_bit(__u64 addr, unsigned long bit_nr, bool swap)
 
 	bit = bit_nr + (addr % PAGE_SIZE) * 8;
 
+	/* kvm_set_routing_entry() should never allow this to happen */
+	WARN_ON_ONCE(bit > (PAGE_SIZE * BITS_PER_BYTE - 1));
+
 	return swap ? (bit ^ (BITS_PER_LONG - 1)) : bit;
 }
 
@@ -2852,6 +2855,7 @@ int kvm_set_routing_entry(struct kvm *kvm,
 			  struct kvm_kernel_irq_routing_entry *e,
 			  const struct kvm_irq_routing_entry *ue)
 {
+	const struct kvm_irq_routing_s390_adapter *adapter;
 	u64 uaddr_s, uaddr_i;
 	int idx;
 
@@ -2862,6 +2866,14 @@ int kvm_set_routing_entry(struct kvm *kvm,
 			return -EINVAL;

arch/s390/kvm/interrupt.c int kvm_set_routing_entry(struct kvm *kvm, · line 2876

Quoted 10 lines
arch/s390/kvm/interrupt.c +8 -0
 		e->set = set_adapter_int;
 
+		adapter = &ue->u.adapter;
+		if (adapter->summary_addr + BITS_TO_BYTES(adapter->summary_offset) >=
+		    (adapter->summary_addr & PAGE_MASK) + PAGE_SIZE)
+			return -EINVAL;
+		if (adapter->ind_addr + BITS_TO_BYTES(adapter->ind_offset) >=
+		    (adapter->ind_addr & PAGE_MASK) + PAGE_SIZE)
+			return -EINVAL;
+

I think this is slightly off.

The offset should indicate a bit offset from the beginning of the byte, so offsets 0-7 are all within the same byte as the specified address, 8-15 are in the next byte, etc.

I hacked QEMU and tested something like...
1) addr 8126efff offset 7 -- this would be the very last bit in the page.
2) addr 8126efff offset 8 -- this would be the very first bit in the next page.
3) addr 8126efff offset 9 -- this would be the 2nd bit in the next page.

I expected (1) to pass while (2) and (3) were rejected, but all 3 were rejected by your check.

I think the problem is that BITS_TO_BYTES rounds up. So:
BITS_TO_BYTES(0) = 0
BITS_TO_BYTES(1..8) = 1
BITS_TO_BYTES(9..16) = 2
and so on.

But your offset check expects
0..7 = 0
8..15 = 1
and so on.

AFAICT replacing BITS_TO_BYTES(offset) with (offset / 8) would work.

Quoted 3 lines
arch/s390/kvm/interrupt.c +0 -0
 		idx = srcu_read_lock(&kvm->srcu);
 		uaddr_s = gpa_to_hva(kvm, ue->u.adapter.summary_addr);
 		uaddr_i = gpa_to_hva(kvm, ue->u.adapter.ind_addr);
author maintainer
#3 lore raw

Re: [PATCH 1/2] KVM: s390: Limit adapter indicator access to mapped page

Quoted 56 lines · Matthew Rosato
Quoted 34 lines · Janosch Frank

While we check the address for errors, we don't seem to check the bit offsets and since they are 32 and 64 bits a lot of memory can be reached indirectly via those offsets.

Fixes 84223598778b ("KVM: s390: irq routing for adapter interrupts.")
---
  arch/s390/kvm/interrupt.c | 12 ++++++++++++
  1 file changed, 12 insertions(+)
arch/s390/kvm/interrupt.c +4 -0
index 1c2bb5cd7e12..cd4851e33a5b 100644
@@ -2724,6 +2724,9 @@ static unsigned long get_ind_bit(__u64 addr, unsigned long bit_nr, bool swap)
 
  	bit = bit_nr + (addr % PAGE_SIZE) * 8;
 
+	/* kvm_set_routing_entry() should never allow this to happen */
+	WARN_ON_ONCE(bit > (PAGE_SIZE * BITS_PER_BYTE - 1));
+
  	return swap ? (bit ^ (BITS_PER_LONG - 1)) : bit;
  }
 
@@ -2852,6 +2855,7 @@ int kvm_set_routing_entry(struct kvm *kvm,
  			  struct kvm_kernel_irq_routing_entry *e,
  			  const struct kvm_irq_routing_entry *ue)
  {
+	const struct kvm_irq_routing_s390_adapter *adapter;
  	u64 uaddr_s, uaddr_i;
  	int idx;
 
@@ -2862,6 +2866,14 @@ int kvm_set_routing_entry(struct kvm *kvm,
  			return -EINVAL;

arch/s390/kvm/interrupt.c int kvm_set_routing_entry(struct kvm *kvm, · line 2876

Quoted 10 lines
arch/s390/kvm/interrupt.c +8 -0
  		e->set = set_adapter_int;
 
+		adapter = &ue->u.adapter;
+		if (adapter->summary_addr + BITS_TO_BYTES(adapter->summary_offset) >=
+		    (adapter->summary_addr & PAGE_MASK) + PAGE_SIZE)
+			return -EINVAL;
+		if (adapter->ind_addr + BITS_TO_BYTES(adapter->ind_offset) >=
+		    (adapter->ind_addr & PAGE_MASK) + PAGE_SIZE)
+			return -EINVAL;
+

I think this is slightly off.

The offset should indicate a bit offset from the beginning of the byte, so offsets 0-7 are all within the same byte as the specified address, 8-15 are in the next byte, etc.

I hacked QEMU and tested something like...
1) addr 8126efff offset 7 -- this would be the very last bit in the page.
2) addr 8126efff offset 8 -- this would be the very first bit in the next page.
3) addr 8126efff offset 9 -- this would be the 2nd bit in the next page.

Quoted 4 lines

I expected (1) to pass while (2) and (3) were rejected, but all 3 were rejected by your check.

I think the problem is that BITS_TO_BYTES rounds up. So:
BITS_TO_BYTES(0) = 0

ugh, right.

Quoted 10 lines

BITS_TO_BYTES(1..8) = 1
BITS_TO_BYTES(9..16) = 2
and so on.

But your offset check expects
0..7 = 0
8..15 = 1
and so on.

AFAICT replacing BITS_TO_BYTES(offset) with (offset / 8) would work.

Yeah, rounding down should work.
I wanted to have a fancy macro but didn't work out...

I'll re-spin and make sure the test will catch this problem as well as the issue I'm trying to fix.

maintainer
#4 lore raw

Re: [PATCH 1/2] KVM: s390: Limit adapter indicator access to mapped page

In reply to Janosch Frank
Quoted 6 lines · On Tue, 17 Feb 2026 09:54:22 +0100 Janosch Frank <[email protected]> wrote:

While we check the address for errors, we don't seem to check the bit offsets and since they are 32 and 64 bits a lot of memory can be reached indirectly via those offsets.

Fixes 84223598778b ("KVM: s390: irq routing for adapter interrupts.")

:)

Quoted 41 lines
---
 arch/s390/kvm/interrupt.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)
arch/s390/kvm/interrupt.c +12 -0
index 1c2bb5cd7e12..cd4851e33a5b 100644
@@ -2724,6 +2724,9 @@ static unsigned long get_ind_bit(__u64 addr, unsigned long bit_nr, bool swap)
 
 	bit = bit_nr + (addr % PAGE_SIZE) * 8;
 
+	/* kvm_set_routing_entry() should never allow this to happen */
+	WARN_ON_ONCE(bit > (PAGE_SIZE * BITS_PER_BYTE - 1));
+
 	return swap ? (bit ^ (BITS_PER_LONG - 1)) : bit;
 }
 
@@ -2852,6 +2855,7 @@ int kvm_set_routing_entry(struct kvm *kvm,
 			  struct kvm_kernel_irq_routing_entry *e,
 			  const struct kvm_irq_routing_entry *ue)
 {
+	const struct kvm_irq_routing_s390_adapter *adapter;
 	u64 uaddr_s, uaddr_i;
 	int idx;
 
@@ -2862,6 +2866,14 @@ int kvm_set_routing_entry(struct kvm *kvm,
 			return -EINVAL;
 		e->set = set_adapter_int;
 
+		adapter = &ue->u.adapter;
+		if (adapter->summary_addr + BITS_TO_BYTES(adapter->summary_offset) >=
+		    (adapter->summary_addr & PAGE_MASK) + PAGE_SIZE)
+			return -EINVAL;
+		if (adapter->ind_addr + BITS_TO_BYTES(adapter->ind_offset) >=
+		    (adapter->ind_addr & PAGE_MASK) + PAGE_SIZE)
+			return -EINVAL;
+
 		idx = srcu_read_lock(&kvm->srcu);
 		uaddr_s = gpa_to_hva(kvm, ue->u.adapter.summary_addr);
 		uaddr_i = gpa_to_hva(kvm, ue->u.adapter.ind_addr);
author maintainer
#5 lore raw

Re: [PATCH 1/2] KVM: s390: Limit adapter indicator access to mapped page

Quoted 7 lines · Claudio Imbrenda
Quoted 4 lines · On Tue, 17 Feb 2026 09:54:22 +0100 Janosch Frank <[email protected]> wrote:

While we check the address for errors, we don't seem to check the bit offsets and since they are 32 and 64 bits a lot of memory can be reached indirectly via those offsets.

Quoted 6 lines
Quoted 2 lines
Fixes 84223598778b ("KVM: s390: irq routing for adapter interrupts.")

Just spoke with Claudio, he confused this patch with another one.

Since he suggested a fix after I reported the issue to Christian and him I'll keep the suggested-by though.

author maintainer
#6 lore raw

PATCH 2/2 KVM: s390: selftests: Add IRQ routing address offset tests

In reply to Janosch Frank

This test tries to setup routes which have address + offset combinations which cross a page.

---
 tools/testing/selftests/kvm/Makefile.kvm      |  1 +
 .../testing/selftests/kvm/s390/irq_routing.c  | 75 +++++++++++++++++++
 2 files changed, 76 insertions(+)
 create mode 100644 tools/testing/selftests/kvm/s390/irq_routing.c
tools/testing/selftests/kvm/Makefile.kvm +1 -0
index 7cfdfe7edfbf..c757704a0cb7 100644
@@ -201,6 +201,7 @@ TEST_GEN_PROGS_s390 += s390/ucontrol_test
 TEST_GEN_PROGS_s390 += s390/user_operexec
 TEST_GEN_PROGS_s390 += s390/keyop
 TEST_GEN_PROGS_s390 += rseq_test
+TEST_GEN_PROGS_s390 += s390/irq_routing
 
 TEST_GEN_PROGS_riscv = $(TEST_GEN_PROGS_COMMON)
 TEST_GEN_PROGS_riscv += riscv/sbi_pmu_test
tools/testing/selftests/kvm/s390/irq_routing.c new file +75 -0

75 lines not on this page · raw message

Signature · 1 line
2.53.0