Skip to contentSkip to conversation

UBSAN fixes

PATCH Superseded Oleksii Kurochko 4 participants 23 messages May 25 – Jun 3 RISC-V

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover UBSAN fixes

During Baptiste Le Duc's work on adding CI stuff for RISC-V several UBSAN issues were found.

This patch series resolves the found issues.

CI tests: https://gitlab.com/xen-project/people/olkur/xen/-/pipelines/2551001500

---
Changes in v2:
- Address comments from ML.
---

Oleksii Kurochko (3):
xen/riscv: fix switch_stack_and_jump() for range beyond 1M xen/domain: fix UBSAN null pointer dereference of d->shared_info
libfdt: fix UBSAN null pointer in fdt_property()

 xen/arch/riscv/include/asm/current.h | 10 +++++-----
 xen/common/domain.c                  |  2 +-
 xen/common/event_fifo.c              |  3 ++-
 xen/common/libfdt/fdt_sw.c           |  3 ++-
 xen/common/time.c                    |  3 +++
 5 files changed, 13 insertions(+), 8 deletions(-)
Signature · 1 line
2.54.0
#1 lore raw

PATCH v2 1/3 xen/riscv: fix switch_stack_and_jump() for range beyond 1M

The j instruction (JAL x0) used in switch_stack_and_jump() is a J-type instruction with only a ±1MB range, and that this can be exceeded in some configurations, causing a linker error:

relocation truncated to fit: R_RISCV_JAL against `<symbol>'

Replace j with jr (JALR x0) via an explicit register, which has unlimited range.

Found in a downstream branch when UBSAN instrumentation was enabled.

Note that the tail instruction looks more natural here, but jr is chosen instead to avoid depending on how the assembler expands tail and which scratch register it uses (t1 in GAS), which would need to be listed in the clobber section of asm volatile.

Fixes e66003e7be199 ("xen/riscv: introduce setup_initial_pages")

---
Changes in v2:
- Update the commit message and subject

---
 xen/arch/riscv/include/asm/current.h | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
xen/arch/riscv/include/asm/current.h +5 -5
index 5fbee8182caa..cc004670d18c 100644
@@ -51,11 +51,11 @@ DECLARE_PER_CPU(struct vcpu *, curr_vcpu);
 #define vcpu_guest_cpu_user_regs(vcpu) \
     (&(vcpu)->arch.cpu_info->guest_cpu_user_regs)
 
-#define switch_stack_and_jump(stack, fn) do {               \
-    asm volatile (                                          \
-            "mv sp, %0\n"                                   \
-            "j " #fn :: "r" (stack), "X" (fn) : "memory" ); \
-    unreachable();                                          \
+#define switch_stack_and_jump(stack, fn) do {                    \
+    asm volatile (                                               \
+            "mv sp, %0\n"                                        \
+            "jr %1" :: "r" (stack), "r" (fn) : "memory" );       \
+    unreachable();                                               \
 } while ( false )
 
 #define get_per_cpu_offset() __per_cpu_offset[smp_processor_id()]
Signature · 1 line
2.54.0
Acked-by on v2 1/3
#2 lore raw

Re: [PATCH v2 1/3] xen/riscv: fix switch_stack_and_jump() for range beyond 1M

Quoted 9 lines · Oleksii Kurochko

The j instruction (JAL x0) used in switch_stack_and_jump() is a J-type instruction with only a ±1MB range, and that this can be exceeded in some configurations, causing a linker error:

relocation truncated to fit: R_RISCV_JAL against `<symbol>'

Replace j with jr (JALR x0) via an explicit register, which has unlimited range.

Quoted 6 lines

Found in a downstream branch when UBSAN instrumentation was enabled.

Note that the tail instruction looks more natural here, but jr is chosen instead to avoid depending on how the assembler expands tail and which scratch register it uses (t1 in GAS), which would need to be listed in the clobber section of asm volatile.

Hmm, looks like this indeed isn't standardized.

Quoted 3 lines
Fixes e66003e7be199 ("xen/riscv: introduce setup_initial_pages")
#3 lore raw

Re: [PATCH v2 1/3] xen/riscv: fix switch_stack_and_jump() for range beyond 1M

In reply to Oleksii Kurochko
Quoted 35 lines · Oleksii Kurochko

The j instruction (JAL x0) used in switch_stack_and_jump() is a J-type instruction with only a ±1MB range, and that this can be exceeded in some configurations, causing a linker error:

relocation truncated to fit: R_RISCV_JAL against `<symbol>'

Replace j with jr (JALR x0) via an explicit register, which has unlimited range.

Found in a downstream branch when UBSAN instrumentation was enabled.

Note that the tail instruction looks more natural here, but jr is chosen instead to avoid depending on how the assembler expands tail and which scratch register it uses (t1 in GAS), which would need to be listed in the clobber section of asm volatile.

Fixes e66003e7be199 ("xen/riscv: introduce setup_initial_pages")

---
Changes in v2:
- Update the commit message and subject

---
 xen/arch/riscv/include/asm/current.h | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
xen/arch/riscv/include/asm/current.h +0 -1
index 5fbee8182caa..cc004670d18c 100644
@@ -51,11 +51,11 @@ DECLARE_PER_CPU(struct vcpu *, curr_vcpu);
 #define vcpu_guest_cpu_user_regs(vcpu) \
     (&(vcpu)->arch.cpu_info->guest_cpu_user_regs)
 
-#define switch_stack_and_jump(stack, fn) do {               \

xen/arch/riscv/include/asm/current.h DECLARE_PER_CPU(struct vcpu *, curr_vcpu); · line 59

Quoted 10 lines
xen/arch/riscv/include/asm/current.h +5 -4
-    asm volatile (                                          \
-            "mv sp, %0\n"                                   \
-            "j " #fn :: "r" (stack), "X" (fn) : "memory" ); \
-    unreachable();                                          \
+#define switch_stack_and_jump(stack, fn) do {                    \
+    asm volatile (                                               \
+            "mv sp, %0\n"                                        \
+            "jr %1" :: "r" (stack), "r" (fn) : "memory" );       \
+    unreachable();                                               \
 } while ( false )

Just as a note, with this simplified again, the \'s can avoid moving, at which point the diff becomes a single line.

Can be fixed on commit.

~Andrew

#4 lore raw

Re: [PATCH v2 1/3] xen/riscv: fix switch_stack_and_jump() for range beyond 1M

Quoted 50 lines · Andrew Cooper
Quoted 35 lines · Oleksii Kurochko

The j instruction (JAL x0) used in switch_stack_and_jump() is a J-type instruction with only a ±1MB range, and that this can be exceeded in some configurations, causing a linker error:

relocation truncated to fit: R_RISCV_JAL against `<symbol>'

Replace j with jr (JALR x0) via an explicit register, which has unlimited range.

Found in a downstream branch when UBSAN instrumentation was enabled.

Note that the tail instruction looks more natural here, but jr is chosen instead to avoid depending on how the assembler expands tail and which scratch register it uses (t1 in GAS), which would need to be listed in the clobber section of asm volatile.

Fixes e66003e7be199 ("xen/riscv: introduce setup_initial_pages")

---
Changes in v2:
- Update the commit message and subject

---
  xen/arch/riscv/include/asm/current.h | 10 +++++-----
  1 file changed, 5 insertions(+), 5 deletions(-)
xen/arch/riscv/include/asm/current.h +0 -1
index 5fbee8182caa..cc004670d18c 100644
@@ -51,11 +51,11 @@ DECLARE_PER_CPU(struct vcpu *, curr_vcpu);
  #define vcpu_guest_cpu_user_regs(vcpu) \
      (&(vcpu)->arch.cpu_info->guest_cpu_user_regs)
 
-#define switch_stack_and_jump(stack, fn) do {               \

xen/arch/riscv/include/asm/current.h DECLARE_PER_CPU(struct vcpu *, curr_vcpu); · line 59

Quoted 10 lines
xen/arch/riscv/include/asm/current.h +5 -4
-    asm volatile (                                          \
-            "mv sp, %0\n"                                   \
-            "j " #fn :: "r" (stack), "X" (fn) : "memory" ); \
-    unreachable();                                          \
+#define switch_stack_and_jump(stack, fn) do {                    \
+    asm volatile (                                               \
+            "mv sp, %0\n"                                        \
+            "jr %1" :: "r" (stack), "r" (fn) : "memory" );       \
+    unreachable();                                               \
  } while ( false )

Just as a note, with this simplified again, the \'s can avoid moving, at which point the diff becomes a single line.

Quoted 1 line

Can be fixed on commit.

I would be happy with that.

Thanks.

~ Oleksii

#5 lore raw

PATCH v2 2/3 xen/domain: fix UBSAN null pointer dereference of d->shared_info

In reply to Oleksii Kurochko

It is legal to have d->shared_info equal to NULL for architectures which support only the FIFO ABI for event channel management.

Having d->shared_info == NULL leads to a UBSAN issue on such architectures:

  UBSAN: Undefined behaviour in common/domain.c:325:10
         member access within null pointer of type 'struct shared_info_t'

vcpu_info_reset() maps v->vcpu_info_area.map to the per-vcpu slot inside the domain's shared_info page for vcpus with id < XEN_LEGACY_MAX_VCPUS, and falls back to dummy_vcpu_info for vcpus beyond that limit. Extend the existing fallback condition to also cover the case where no shared_info page has been allocated, mapping the vcpu to dummy_vcpu_info instead. This is the correct behaviour: dummy_vcpu_info already serves as the safe stand-in for vcpus that have no usable shared_info slot.

Additionally, if an architecture supports only the FIFO ABI, setup_ports() should be updated to avoid a NULL pointer dereference of d->shared_info, since in that case there will be no pending events in shared_info->evtchn_pending and the pending flag of the FIFO event channel does not need to be set to true.
update_domain_wallclock_time() accesses d->shared_info via shared_info() macro. On architectures that do not allocate a shared_info page (currently RISC-V, which runs guests in dom0less mode without the PV ABI), this causes a NULL dereference. The early return is safe: if there is no shared_info page, there is nothing to update. For all existing architectures (x86, ARM) that do allocate it, the guard is never taken and behavior is unchanged.

---
Changes in v2:
- Update commit message + subject.
- Drop Fixes tag.
- Handle migration of pending events from 2L and FIFO ABIs when arch support only FIFO ABI.
- Update the commit message.
- Protect some other places in common code from NULL pointer deref of d->shared_info.
- Drop R-by: Baptiste ... as some extra checks are added.

---
 xen/common/domain.c     | 2 +-
 xen/common/event_fifo.c | 3 ++-
 xen/common/time.c       | 3 +++
 3 files changed, 6 insertions(+), 2 deletions(-)
xen/common/domain.c +1 -1
index bb9e210c2895..e64b7df9b704 100644
@@ -320,7 +320,7 @@ void vcpu_info_reset(struct vcpu *v)
     struct domain *d = v->domain;
 
     v->vcpu_info_area.map =
-        ((v->vcpu_id < XEN_LEGACY_MAX_VCPUS)
+        ((v->vcpu_id < XEN_LEGACY_MAX_VCPUS && d->shared_info)
          ? (vcpu_info_t *)&shared_info(d, vcpu_info[v->vcpu_id])
          : &dummy_vcpu_info);
 }
xen/common/event_fifo.c +2 -1
index 37cba9bc4564..59d9bf4c7ec0 100644
@@ -562,7 +562,8 @@ static void setup_ports(struct domain *d, unsigned int prev_evtchns)
 
         evtchn = evtchn_from_port(d, port);
 
-        if ( guest_test_bit(d, port, &shared_info(d, evtchn_pending)) )
+        if ( d->shared_info &&
+             guest_test_bit(d, port, &shared_info(d, evtchn_pending)) )
             evtchn->pending = true;
 
         evtchn_fifo_set_priority(d, evtchn, EVTCHN_FIFO_PRIORITY_DEFAULT);
xen/common/time.c +3 -0
index 04a65f00b35c..1ee49a8b0d13 100644
@@ -94,6 +94,9 @@ void update_domain_wallclock_time(struct domain *d)
     uint32_t *wc_version;
     uint64_t sec;
 
+    if ( !d->shared_info )
+        return;
+
     spin_lock(&wc_lock);
 
     wc_version = &shared_info(d, wc_version);
Signature · 1 line
2.54.0
#6 lore raw

Re: [PATCH v2 2/3] xen/domain: fix UBSAN null pointer dereference of d->shared_info

Quoted 27 lines · Oleksii Kurochko

It is legal to have d->shared_info equal to NULL for architectures which support only the FIFO ABI for event channel management.

Having d->shared_info == NULL leads to a UBSAN issue on such architectures:

  UBSAN: Undefined behaviour in common/domain.c:325:10
         member access within null pointer of type 'struct shared_info_t'

vcpu_info_reset() maps v->vcpu_info_area.map to the per-vcpu slot inside the domain's shared_info page for vcpus with id < XEN_LEGACY_MAX_VCPUS, and falls back to dummy_vcpu_info for vcpus beyond that limit. Extend the existing fallback condition to also cover the case where no shared_info page has been allocated, mapping the vcpu to dummy_vcpu_info instead. This is the correct behaviour: dummy_vcpu_info already serves as the safe stand-in for vcpus that have no usable shared_info slot.

Additionally, if an architecture supports only the FIFO ABI, setup_ports() should be updated to avoid a NULL pointer dereference of d->shared_info, since in that case there will be no pending events in shared_info->evtchn_pending and the pending flag of the FIFO event channel does not need to be set to true.
update_domain_wallclock_time() accesses d->shared_info via shared_info() macro. On architectures that do not allocate a shared_info page (currently RISC-V, which runs guests in dom0less mode without the PV ABI), this causes a NULL dereference. The early return is safe: if there is no shared_info page, there is nothing to update. For all existing architectures (x86, ARM) that do allocate it, the guard is never taken and behavior is unchanged.

Quoted 7 lines

---
Changes in v2:
- Update commit message + subject.
- Drop Fixes tag.
- Handle migration of pending events from 2L and FIFO ABIs when arch support only FIFO ABI.

What does this item describe? On an arch supporting only FIFO, how could evtchn need migrating from 2L?

Quoted 3 lines

- Update the commit message.
- Protect some other places in common code from NULL pointer deref of d->shared_info.

What I'm still missing is the description clarifying why other uses don't need guarding (or that there simply are no other uses, which - however - I doubt).

xen/common/domain.c void vcpu_info_reset(struct vcpu *v) · line 326

Quoted 11 lines
xen/common/domain.c +1 -1
@@ -320,7 +320,7 @@ void vcpu_info_reset(struct vcpu *v)
     struct domain *d = v->domain;
 
     v->vcpu_info_area.map =
-        ((v->vcpu_id < XEN_LEGACY_MAX_VCPUS)
+        ((v->vcpu_id < XEN_LEGACY_MAX_VCPUS && d->shared_info)
          ? (vcpu_info_t *)&shared_info(d, vcpu_info[v->vcpu_id])
          : &dummy_vcpu_info);
 }

At the example of this: The extra conditionals are dead code on x86 and Arm. While the status of the respective Misra rule is still uncertain for Xen, imo we'd be better off avoiding the introduction of new dead code. Which in turn means we may need some kind of abstraction to have these extra conditionals in place only for arch-es not supporting shared-info at all.

Jan

#7 lore raw

Re: [PATCH v2 2/3] xen/domain: fix UBSAN null pointer dereference of d->shared_info

Quoted 36 lines · Jan Beulich
Quoted 27 lines · Oleksii Kurochko

It is legal to have d->shared_info equal to NULL for architectures which support only the FIFO ABI for event channel management.

Having d->shared_info == NULL leads to a UBSAN issue on such architectures:

   UBSAN: Undefined behaviour in common/domain.c:325:10
          member access within null pointer of type 'struct shared_info_t'

vcpu_info_reset() maps v->vcpu_info_area.map to the per-vcpu slot inside the domain's shared_info page for vcpus with id < XEN_LEGACY_MAX_VCPUS, and falls back to dummy_vcpu_info for vcpus beyond that limit. Extend the existing fallback condition to also cover the case where no shared_info page has been allocated, mapping the vcpu to dummy_vcpu_info instead. This is the correct behaviour: dummy_vcpu_info already serves as the safe stand-in for vcpus that have no usable shared_info slot.

Additionally, if an architecture supports only the FIFO ABI, setup_ports() should be updated to avoid a NULL pointer dereference of d->shared_info, since in that case there will be no pending events in shared_info->evtchn_pending and the pending flag of the FIFO event channel does not need to be set to true.
update_domain_wallclock_time() accesses d->shared_info via shared_info() macro. On architectures that do not allocate a shared_info page (currently RISC-V, which runs guests in dom0less mode without the PV ABI), this causes a NULL dereference. The early return is safe: if there is no shared_info page, there is nothing to update. For all existing architectures (x86, ARM) that do allocate it, the guard is never taken and behavior is unchanged.

Quoted 7 lines

---
Changes in v2:
- Update commit message + subject.
- Drop Fixes tag.
- Handle migration of pending events from 2L and FIFO ABIs when arch support only FIFO ABI.

Quoted 2 lines

What does this item describe? On an arch supporting only FIFO, how could evtchn need migrating from 2L?

Agree this item is inaccurate.

evtchn_init() always calls evtchn_2l_init(d) first (event_channel.c:1627), so every domain starts with 2L regardless of arch (of course, it is just initialization of evtchn_port_ops which aren't really used when only FIFO is supported).

setup_ports() is called during the guest-initiated 2L→FIFO transition (event_fifo.c:637), not at arch init time. There is no arch that supports "only FIFO" as a starting state and that is why it is needed to guard setup_ports() against NULL d->shared_info when migrating 2L pending state to FIFO even 2L wasn't really used by an arch with only FIFO support.

I Will drop this item to not confuse.

Quoted 8 lines
Quoted 4 lines

- Update the commit message.
- Protect some other places in common code from NULL pointer deref of

    d->shared_info.

What I'm still missing is the description clarifying why other uses don't need guarding (or that there simply are no other uses, which - however - I doubt).

I will add an explicit paragraph mentioning that the 2L ops in event_2l.c are unreachable for a domain with no shared_info.

The only place which isn't covered now is domctl.c:108 (virt_to_mfn(d->shared_info)) is only reached via the XEN_DOMCTL_getdomaininfo path and as RISC-V doesn't use it now it could be left as it is what also could be added to commit message.

Quoted 6 lines
Quoted 6 lines
xen/common/domain.c +0 -0
@@ -320,7 +320,7 @@ void vcpu_info_reset(struct vcpu *v)
      struct domain *d = v->domain;
 
Quoted 13 lines

xen/common/domain.c void vcpu_info_reset(struct vcpu *v) · line 326

Quoted 6 lines
xen/common/domain.c +1 -1
      v->vcpu_info_area.map =
-        ((v->vcpu_id < XEN_LEGACY_MAX_VCPUS)
+        ((v->vcpu_id < XEN_LEGACY_MAX_VCPUS && d->shared_info)
           ? (vcpu_info_t *)&shared_info(d, vcpu_info[v->vcpu_id])
           : &dummy_vcpu_info);
  }

At the example of this: The extra conditionals are dead code on x86 and Arm. While the status of the respective Misra rule is still uncertain for Xen, imo we'd be better off avoiding the introduction of new dead code. Which in turn means we may need some kind of abstraction to have these extra conditionals in place only for arch-es not supporting shared-info at all.

What about then add config HAS_SHARED_INFO to xen/common/Kconfig and then:

xen/common/domain.c +7 -3
index e64b7df9b704..58442ce1f952 100644
@@ -319,10 +319,14 @@ void vcpu_info_reset(struct vcpu *v)
 {
     struct domain *d = v->domain;
 
+#ifdef CONFIG_HAS_SHARED_INFO
     v->vcpu_info_area.map =
-        ((v->vcpu_id < XEN_LEGACY_MAX_VCPUS && d->shared_info)
-         ? (vcpu_info_t *)&shared_info(d, vcpu_info[v->vcpu_id])
-         : &dummy_vcpu_info);
+        (v->vcpu_id < XEN_LEGACY_MAX_VCPUS)
+        ? (vcpu_info_t *)&shared_info(d, vcpu_info[v->vcpu_id])
+        : &dummy_vcpu_info;
+#else
+    v->vcpu_info_area.map = &dummy_vcpu_info;
+#endif
 }
 
 static struct domain *alloc_domain_struct(void)
xen/common/event_fifo.c +3 -2
index 59d9bf4c7ec0..3d7104100f0b 100644
@@ -562,9 +562,10 @@ static void setup_ports(struct domain *d, unsigned int prev_evtchns)
 
         evtchn = evtchn_from_port(d, port);
 
-        if ( d->shared_info &&
-             guest_test_bit(d, port, &shared_info(d, evtchn_pending)) )
+#ifdef CONFIG_HAS_SHARED_INFO
+        if ( guest_test_bit(d, port, &shared_info(d, evtchn_pending)) )
             evtchn->pending = true;
+#endif
 
         evtchn_fifo_set_priority(d, evtchn, EVTCHN_FIFO_PRIORITY_DEFAULT);
     }
xen/common/time.c +3 -2
index 1ee49a8b0d13..da8403949102 100644
@@ -94,8 +94,9 @@ void update_domain_wallclock_time(struct domain *d)
     uint32_t *wc_version;
     uint64_t sec;
 
-    if ( !d->shared_info )
-        return;
+#ifndef CONFIG_HAS_SHARED_INFO
+    return;
+#endif
 
     spin_lock(&wc_lock);
 

Thanks.

~ Oleksii

#8 lore raw

Re: [PATCH v2 2/3] xen/domain: fix UBSAN null pointer dereference of d->shared_info

Quoted 9 lines · Oleksii Kurochko
Quoted 8 lines
Quoted 4 lines

  - Update the commit message.
  - Protect some other places in common code from NULL pointer deref of d->shared_info.

What I'm still missing is the description clarifying why other uses don't need guarding (or that there simply are no other uses, which - however - I doubt).

Quoted 5 lines

I will add an explicit paragraph mentioning that the 2L ops in event_2l.c are unreachable for a domain with no shared_info.

The only place which isn't covered now is  domctl.c:108 (virt_to_mfn(d-

Quoted 1 line

shared_info)) is only reached via the XEN_DOMCTL_getdomaininfo path and

as RISC-V doesn't use it now it could be left as it is what also could be added to commit message.

For that part could be considered ...

Quoted 68 lines
Quoted 11 lines
Quoted 11 lines
xen/common/domain.c +0 -0
@@ -320,7 +320,7 @@ void vcpu_info_reset(struct vcpu *v)
       struct domain *d = v->domain;

      v->vcpu_info_area.map =
-        ((v->vcpu_id < XEN_LEGACY_MAX_VCPUS)
+        ((v->vcpu_id < XEN_LEGACY_MAX_VCPUS && d->shared_info) ? (vcpu_info_t *)&shared_info(d, vcpu_info[v->vcpu_id]) : &dummy_vcpu_info);

Quoted 8 lines
Quoted 1 line

  }

At the example of this: The extra conditionals are dead code on x86 and Arm. While the status of the respective Misra rule is still uncertain for Xen, imo we'd be better off avoiding the introduction of new dead code. Which in turn means we may need some kind of abstraction to have these extra conditionals in place only for arch-es not supporting shared-info at all.

What about then add config HAS_SHARED_INFO to xen/common/Kconfig and then:

xen/common/domain.c +7 -3
index e64b7df9b704..58442ce1f952 100644
@@ -319,10 +319,14 @@ void vcpu_info_reset(struct vcpu *v)
  {
      struct domain *d = v->domain;
 
+#ifdef CONFIG_HAS_SHARED_INFO
      v->vcpu_info_area.map =
-        ((v->vcpu_id < XEN_LEGACY_MAX_VCPUS && d->shared_info)
-         ? (vcpu_info_t *)&shared_info(d, vcpu_info[v->vcpu_id])
-         : &dummy_vcpu_info);
+        (v->vcpu_id < XEN_LEGACY_MAX_VCPUS)
+        ? (vcpu_info_t *)&shared_info(d, vcpu_info[v->vcpu_id])
+        : &dummy_vcpu_info;
+#else
+    v->vcpu_info_area.map = &dummy_vcpu_info;
+#endif
  }
 
  static struct domain *alloc_domain_struct(void)
xen/common/event_fifo.c +3 -2
index 59d9bf4c7ec0..3d7104100f0b 100644
@@ -562,9 +562,10 @@ static void setup_ports(struct domain *d, unsigned 
 int prev_evtchns)
 
          evtchn = evtchn_from_port(d, port);
 
-        if ( d->shared_info &&
-             guest_test_bit(d, port, &shared_info(d, evtchn_pending)) )
+#ifdef CONFIG_HAS_SHARED_INFO
+        if ( guest_test_bit(d, port, &shared_info(d, evtchn_pending)) )
              evtchn->pending = true;
+#endif
 
          evtchn_fifo_set_priority(d, evtchn, 

EVTCHN_FIFO_PRIORITY_DEFAULT);
     }

xen/common/time.c +0 -0
index 1ee49a8b0d13..da8403949102 100644

xen/common/time.c void update_domain_wallclock_time(struct domain *d) · line 101

Quoted 10 lines
xen/common/time.c +3 -2
@@ -94,8 +94,9 @@ void update_domain_wallclock_time(struct domain *d)
      uint32_t *wc_version;
      uint64_t sec;
 
-    if ( !d->shared_info )
-        return;
+#ifndef CONFIG_HAS_SHARED_INFO
+    return;
+#endif
 
      spin_lock(&wc_lock);

...

+#ifdef CONFIG_HAS_SHARED_INFO

     info->shared_info_frame =
         gfn_x(mfn_to_gfn(d, _mfn(virt_to_mfn(d->shared_info))));
     BUG_ON(SHARED_M2P(info->shared_info_frame));

+#else
+ info->shared_info_frame = INVALID_GFN_RAW;
+#endif

~ Oleksii

#9 lore raw

Re: [PATCH v2 2/3] xen/domain: fix UBSAN null pointer dereference of d->shared_info

In reply to Oleksii Kurochko
Quoted 47 lines · Oleksii Kurochko
Quoted 36 lines · Jan Beulich
Quoted 27 lines · Oleksii Kurochko

It is legal to have d->shared_info equal to NULL for architectures which support only the FIFO ABI for event channel management.

Having d->shared_info == NULL leads to a UBSAN issue on such architectures:

   UBSAN: Undefined behaviour in common/domain.c:325:10
          member access within null pointer of type 'struct shared_info_t'

vcpu_info_reset() maps v->vcpu_info_area.map to the per-vcpu slot inside the domain's shared_info page for vcpus with id < XEN_LEGACY_MAX_VCPUS, and falls back to dummy_vcpu_info for vcpus beyond that limit. Extend the existing fallback condition to also cover the case where no shared_info page has been allocated, mapping the vcpu to dummy_vcpu_info instead. This is the correct behaviour: dummy_vcpu_info already serves as the safe stand-in for vcpus that have no usable shared_info slot.

Additionally, if an architecture supports only the FIFO ABI, setup_ports() should be updated to avoid a NULL pointer dereference of d->shared_info, since in that case there will be no pending events in shared_info->evtchn_pending and the pending flag of the FIFO event channel does not need to be set to true.
update_domain_wallclock_time() accesses d->shared_info via shared_info() macro. On architectures that do not allocate a shared_info page (currently RISC-V, which runs guests in dom0less mode without the PV ABI), this causes a NULL dereference. The early return is safe: if there is no shared_info page, there is nothing to update. For all existing architectures (x86, ARM) that do allocate it, the guard is never taken and behavior is unchanged.

Quoted 7 lines

---
Changes in v2:
- Update commit message + subject.
- Drop Fixes tag.
- Handle migration of pending events from 2L and FIFO ABIs when arch support only FIFO ABI.

Quoted 2 lines

What does this item describe? On an arch supporting only FIFO, how could evtchn need migrating from 2L?

Agree this item is inaccurate.

evtchn_init() always calls evtchn_2l_init(d) first (event_channel.c:1627), so every domain starts with 2L regardless of arch (of course, it is just initialization of evtchn_port_ops which aren't really used when only FIFO is supported).

Quoted 6 lines

setup_ports() is called during the guest-initiated 2L→FIFO transition (event_fifo.c:637), not at arch init time. There is no arch that supports "only FIFO" as a starting state and that is why it is needed to guard setup_ports() against NULL d->shared_info when migrating 2L pending state to FIFO even 2L wasn't really used by an arch with only FIFO support.

Imo on arch-es not supporting 2L, domains shouldn't start in 2L mode.

Quoted 8 lines
Quoted 7 lines
Quoted 3 lines

- Update the commit message.
- Protect some other places in common code from NULL pointer deref of

    d->shared_info.

What I'm still missing is the description clarifying why other uses don't need guarding (or that there simply are no other uses, which - however - I doubt).

Quoted 8 lines

I will add an explicit paragraph mentioning that the 2L ops in event_2l.c are unreachable for a domain with no shared_info.

The only place which isn't covered now is domctl.c:108 (virt_to_mfn(d->shared_info)) is only reached via the XEN_DOMCTL_getdomaininfo path and
as RISC-V doesn't use it now it could be left as it is what also could be added to commit message.

Or, better yet, deal with that as well. But see also below.

Quoted 19 lines
Quoted 5 lines
Quoted 5 lines
xen/common/domain.c +0 -0
@@ -320,7 +320,7 @@ void vcpu_info_reset(struct vcpu *v)
      struct domain *d = v->domain;
 
Quoted 13 lines

xen/common/domain.c void vcpu_info_reset(struct vcpu *v) · line 326

Quoted 6 lines
xen/common/domain.c +1 -1
      v->vcpu_info_area.map =
-        ((v->vcpu_id < XEN_LEGACY_MAX_VCPUS)
+        ((v->vcpu_id < XEN_LEGACY_MAX_VCPUS && d->shared_info)
           ? (vcpu_info_t *)&shared_info(d, vcpu_info[v->vcpu_id])
           : &dummy_vcpu_info);
  }

At the example of this: The extra conditionals are dead code on x86 and Arm. While the status of the respective Misra rule is still uncertain for Xen, imo we'd be better off avoiding the introduction of new dead code. Which in turn means we may need some kind of abstraction to have these extra conditionals in place only for arch-es not supporting shared-info at all.

Quoted 1 line

What about then add config HAS_SHARED_INFO to xen/common/Kconfig and then:

We're getting closer. Imo we want to go farther, though: shared_info() as a construct should be unavailable when !HAS_SHARED_INFO. _That_ then will make obvious (by causing build failures) that all respective use sites were properly dealt with.

Quoted 8 lines
xen/common/domain.c +1 -0
@@ -319,10 +319,14 @@ void vcpu_info_reset(struct vcpu *v)
  {
      struct domain *d = v->domain;
 
+#ifdef CONFIG_HAS_SHARED_INFO
      v->vcpu_info_area.map =

xen/common/domain.c void vcpu_info_reset(struct vcpu *v) · line 330

Quoted 10 lines
xen/common/domain.c +6 -3
-        ((v->vcpu_id < XEN_LEGACY_MAX_VCPUS && d->shared_info)
-         ? (vcpu_info_t *)&shared_info(d, vcpu_info[v->vcpu_id])
-         : &dummy_vcpu_info);
+        (v->vcpu_id < XEN_LEGACY_MAX_VCPUS)
+        ? (vcpu_info_t *)&shared_info(d, vcpu_info[v->vcpu_id])
+        : &dummy_vcpu_info;
+#else
+    v->vcpu_info_area.map = &dummy_vcpu_info;
+#endif
  }

I agree with #ifdef here.

xen/common/event_fifo.c static void setup_ports(struct domain *d, unsigned · line 569

Quoted 13 lines
xen/common/event_fifo.c +3 -2
@@ -562,9 +562,10 @@ static void setup_ports(struct domain *d, unsigned 
 int prev_evtchns)
 
          evtchn = evtchn_from_port(d, port);
 
-        if ( d->shared_info &&
-             guest_test_bit(d, port, &shared_info(d, evtchn_pending)) )
+#ifdef CONFIG_HAS_SHARED_INFO
+        if ( guest_test_bit(d, port, &shared_info(d, evtchn_pending)) )
              evtchn->pending = true;
+#endif

While as per above shared_info() would best not exist when !HAS_SHARED_INFO (in which case #ifdef may be unavoidable here), an alternative where IS_ENABLED() could be used here may want at least considering. E.g. causing a link-time failure when shared_info() is used (and not compiled out).

xen/common/time.c void update_domain_wallclock_time(struct domain *d) · line 101

Quoted 13 lines
xen/common/time.c +3 -2
@@ -94,8 +94,9 @@ void update_domain_wallclock_time(struct domain *d)
      uint32_t *wc_version;
      uint64_t sec;
 
-    if ( !d->shared_info )
-        return;
+#ifndef CONFIG_HAS_SHARED_INFO
+    return;
+#endif
 
      spin_lock(&wc_lock);

Constructs like this are imo somewhat ugly. Using IS_ENABLED() instead would make things at least a little better (again imo).

Jan

#10 lore raw

Re: [PATCH v2 2/3] xen/domain: fix UBSAN null pointer dereference of d->shared_info

Quoted 48 lines · Jan Beulich
Quoted 47 lines · Oleksii Kurochko
Quoted 36 lines · Jan Beulich
Quoted 27 lines · Oleksii Kurochko

It is legal to have d->shared_info equal to NULL for architectures which support only the FIFO ABI for event channel management.

Having d->shared_info == NULL leads to a UBSAN issue on such architectures:

    UBSAN: Undefined behaviour in common/domain.c:325:10
           member access within null pointer of type 'struct shared_info_t'

vcpu_info_reset() maps v->vcpu_info_area.map to the per-vcpu slot inside the domain's shared_info page for vcpus with id < XEN_LEGACY_MAX_VCPUS, and falls back to dummy_vcpu_info for vcpus beyond that limit. Extend the existing fallback condition to also cover the case where no shared_info page has been allocated, mapping the vcpu to dummy_vcpu_info instead. This is the correct behaviour: dummy_vcpu_info already serves as the safe stand-in for vcpus that have no usable shared_info slot.

Additionally, if an architecture supports only the FIFO ABI, setup_ports() should be updated to avoid a NULL pointer dereference of d->shared_info, since in that case there will be no pending events in shared_info->evtchn_pending and the pending flag of the FIFO event channel does not need to be set to true.
update_domain_wallclock_time() accesses d->shared_info via shared_info() macro. On architectures that do not allocate a shared_info page (currently RISC-V, which runs guests in dom0less mode without the PV ABI), this causes a NULL dereference. The early return is safe: if there is no shared_info page, there is nothing to update. For all existing architectures (x86, ARM) that do allocate it, the guard is never taken and behavior is unchanged.

Quoted 7 lines

---
Changes in v2:
- Update commit message + subject.
- Drop Fixes tag.
- Handle migration of pending events from 2L and FIFO ABIs when arch support only FIFO ABI.

Quoted 2 lines

What does this item describe? On an arch supporting only FIFO, how could evtchn need migrating from 2L?

Agree this item is inaccurate.

evtchn_init() always calls evtchn_2l_init(d) first (event_channel.c:1627), so every domain starts with 2L regardless of arch (of course, it is just initialization of evtchn_port_ops which aren't really used when only FIFO is supported).

Quoted 8 lines
Quoted 6 lines

setup_ports() is called during the guest-initiated 2L→FIFO transition (event_fifo.c:637), not at arch init time. There is no arch that supports "only FIFO" as a starting state and that is why it is needed to guard setup_ports() against NULL d->shared_info when migrating 2L pending state to FIFO even 2L wasn't really used by an arch with only FIFO support.

Imo on arch-es not supporting 2L, domains shouldn't start in 2L mode.

Agree but will it be easy to achieve now with the current code base?

The best what could be done it is avoid calling evtchn_2l_init() now in event_channel.c and:
1. Add a new Kconfig symbol, CONFIG_HAS_EVTCHN_2L (or re-use HAS_SHARED_INFO suggested before), selected by x86 and ARM.
2. In evtchn_init() (event_channel.c:1627), guard the call:

   #ifdef CONFIG_HAS_EVTCHN_2L
      evtchn_2l_init(d);
   #else
      evtchn_none_init(d);

#endif
3. Add a small stub ops table (probably in event_fifo.c or a new event_none.c) with no-op set_pending/clear_pending/unmask, is_pending returning false, is_masked returning true (valid until evtchn_fifo_init_control() replaces them).

Does it make sense?

Quoted 39 lines
Quoted 9 lines
Quoted 8 lines
Quoted 4 lines

- Update the commit message.
- Protect some other places in common code from NULL pointer deref of

     d->shared_info.

What I'm still missing is the description clarifying why other uses don't need guarding (or that there simply are no other uses, which - however - I doubt).

Quoted 8 lines

I will add an explicit paragraph mentioning that the 2L ops in event_2l.c are unreachable for a domain with no shared_info.

The only place which isn't covered now is domctl.c:108 (virt_to_mfn(d->shared_info)) is only reached via the XEN_DOMCTL_getdomaininfo path and
as RISC-V doesn't use it now it could be left as it is what also could be added to commit message.

Or, better yet, deal with that as well. But see also below.

Quoted 19 lines
Quoted 18 lines

xen/common/domain.c void vcpu_info_reset(struct vcpu *v) · line 326

Quoted 11 lines
xen/common/domain.c +1 -1
@@ -320,7 +320,7 @@ void vcpu_info_reset(struct vcpu *v)
       struct domain *d = v->domain;
 
       v->vcpu_info_area.map =
-        ((v->vcpu_id < XEN_LEGACY_MAX_VCPUS)
+        ((v->vcpu_id < XEN_LEGACY_MAX_VCPUS && d->shared_info)
            ? (vcpu_info_t *)&shared_info(d, vcpu_info[v->vcpu_id])
            : &dummy_vcpu_info);
   }

At the example of this: The extra conditionals are dead code on x86 and Arm. While the status of the respective Misra rule is still uncertain for Xen, imo we'd be better off avoiding the introduction of new dead code. Which in turn means we may need some kind of abstraction to have these extra conditionals in place only for arch-es not supporting shared-info at all.

Quoted 6 lines
Quoted 1 line

What about then add config HAS_SHARED_INFO to xen/common/Kconfig and then:

We're getting closer. Imo we want to go farther, though: shared_info() as a construct should be unavailable when !HAS_SHARED_INFO. _That_ then will make obvious (by causing build failures) that all respective use sites were properly dealt with.

I will add then:

+#ifdef CONFIG_HAS_SHARED_INFO
#define shared_info(d, field) __shared_info(d, (d)->shared_info,
field)
+#endif

But with doing that we have only option of using #ifdef HAS_SHARED_INFO in the place where shared_info() is used. If it is fine then I will be happy to do in this way.

Quoted 54 lines
Quoted 9 lines
xen/common/domain.c +1 -0
@@ -319,10 +319,14 @@ void vcpu_info_reset(struct vcpu *v)
   {
       struct domain *d = v->domain;
 
+#ifdef CONFIG_HAS_SHARED_INFO
       v->vcpu_info_area.map =

xen/common/domain.c void vcpu_info_reset(struct vcpu *v) · line 330

Quoted 10 lines
xen/common/domain.c +6 -3
-        ((v->vcpu_id < XEN_LEGACY_MAX_VCPUS && d->shared_info)
-         ? (vcpu_info_t *)&shared_info(d, vcpu_info[v->vcpu_id])
-         : &dummy_vcpu_info);
+        (v->vcpu_id < XEN_LEGACY_MAX_VCPUS)
+        ? (vcpu_info_t *)&shared_info(d, vcpu_info[v->vcpu_id])
+        : &dummy_vcpu_info;
+#else
+    v->vcpu_info_area.map = &dummy_vcpu_info;
+#endif
   }

I agree with #ifdef here.

xen/common/event_fifo.c static void setup_ports(struct domain *d, unsigned · line 569

Quoted 13 lines
xen/common/event_fifo.c +3 -2
@@ -562,9 +562,10 @@ static void setup_ports(struct domain *d, unsigned
 int prev_evtchns)
 
           evtchn = evtchn_from_port(d, port);
 
-        if ( d->shared_info &&
-             guest_test_bit(d, port, &shared_info(d, evtchn_pending)) )
+#ifdef CONFIG_HAS_SHARED_INFO
+        if ( guest_test_bit(d, port, &shared_info(d, evtchn_pending)) )
               evtchn->pending = true;
+#endif

While as per above shared_info() would best not exist when !HAS_SHARED_INFO (in which case #ifdef may be unavoidable here), an alternative where IS_ENABLED() could be used here may want at least considering. E.g. causing a link-time failure when shared_info() is used (and not compiled out).

Quoted 12 lines
xen/common/time.c +3 -2
@@ -94,8 +94,9 @@ void update_domain_wallclock_time(struct domain *d)
       uint32_t *wc_version;
       uint64_t sec;
 
-    if ( !d->shared_info )
-        return;
+#ifndef CONFIG_HAS_SHARED_INFO
+    return;
+#endif
 
Quoted 4 lines

xen/common/time.c void update_domain_wallclock_time(struct domain *d) · line 101

Quoted 1 line
xen/common/time.c +0 -0
       spin_lock(&wc_lock);

Constructs like this are imo somewhat ugly. Using IS_ENABLED() instead would make things at least a little better (again imo).

Considering mentioned above it would be better to #ifdef the whole buddy of the function:

void update_domain_wallclock_time(struct domain *d)
{
+#ifdef CONFIG_HAS_SHARED_INFO
+

     uint32_t *wc_version;
     uint64_t sec;

-#ifndef CONFIG_HAS_SHARED_INFO
- return;
-#endif
-

     spin_lock(&wc_lock);

     wc_version = &shared_info(d, wc_version);
@@ -120,6 +118,8 @@ void update_domain_wallclock_time(struct domain *d)
     *wc_version = version_update_end(*wc_version);

     spin_unlock(&wc_lock);

+
+#endif /* CONFIG_HAS_SHARED_INFO */
}

Considering also that shared_info is expected to be used only for 2L then it would be better to introduce CONFIG_HAS_EVTCHN_2L instead.

~ Oleksii

#11 lore raw

Re: [PATCH v2 2/3] xen/domain: fix UBSAN null pointer dereference of d->shared_info

Quoted 56 lines · Oleksii Kurochko
Quoted 55 lines · Jan Beulich
Quoted 53 lines · Oleksii Kurochko
Quoted 36 lines · Jan Beulich
Quoted 27 lines · Oleksii Kurochko

It is legal to have d->shared_info equal to NULL for architectures which support only the FIFO ABI for event channel management.

Having d->shared_info == NULL leads to a UBSAN issue on such architectures:

    UBSAN: Undefined behaviour in common/domain.c:325:10
           member access within null pointer of type 'struct shared_info_t'

vcpu_info_reset() maps v->vcpu_info_area.map to the per-vcpu slot inside the domain's shared_info page for vcpus with id < XEN_LEGACY_MAX_VCPUS, and falls back to dummy_vcpu_info for vcpus beyond that limit. Extend the existing fallback condition to also cover the case where no shared_info page has been allocated, mapping the vcpu to dummy_vcpu_info instead. This is the correct behaviour: dummy_vcpu_info already serves as the safe stand-in for vcpus that have no usable shared_info slot.

Additionally, if an architecture supports only the FIFO ABI, setup_ports() should be updated to avoid a NULL pointer dereference of d->shared_info, since in that case there will be no pending events in shared_info->evtchn_pending and the pending flag of the FIFO event channel does not need to be set to true.
update_domain_wallclock_time() accesses d->shared_info via shared_info() macro. On architectures that do not allocate a shared_info page (currently RISC-V, which runs guests in dom0less mode without the PV ABI), this causes a NULL dereference. The early return is safe: if there is no shared_info page, there is nothing to update. For all existing architectures (x86, ARM) that do allocate it, the guard is never taken and behavior is unchanged.

Quoted 7 lines

---
Changes in v2:
- Update commit message + subject.
- Drop Fixes tag.
- Handle migration of pending events from 2L and FIFO ABIs when arch support only FIFO ABI.

Quoted 2 lines

What does this item describe? On an arch supporting only FIFO, how could evtchn need migrating from 2L?

Agree this item is inaccurate.

evtchn_init() always calls evtchn_2l_init(d) first (event_channel.c:1627), so every domain starts with 2L regardless of arch (of course, it is just initialization of evtchn_port_ops which aren't really used when only FIFO is supported).

setup_ports() is called during the guest-initiated 2L→FIFO transition (event_fifo.c:637), not at arch init time. There is no arch that supports "only FIFO" as a starting state and that is why it is needed to guard setup_ports() against NULL d->shared_info when migrating 2L pending state to FIFO even 2L wasn't really used by an arch with only FIFO support.

Quoted 8 lines
Quoted 1 line

Imo on arch-es not supporting 2L, domains shouldn't start in 2L mode.

Agree but will it be easy to achieve now with the current code base?

The best what could be done it is avoid calling evtchn_2l_init() now in event_channel.c and:
1. Add a new Kconfig symbol, CONFIG_HAS_EVTCHN_2L (or re-use HAS_SHARED_INFO suggested before), selected by x86 and ARM.

I'd stick to just HAS_SHARED_INFO as long as a separate control for 2- level evtchn isn't strictly needed.

Quoted 10 lines

2. In evtchn_init() (event_channel.c:1627), guard the call:

    #ifdef CONFIG_HAS_EVTCHN_2L
       evtchn_2l_init(d);
    #else
       evtchn_none_init(d);
    #endif

3. Add a small stub ops table (probably in event_fifo.c or a new event_none.c) with no-op set_pending/clear_pending/unmask, is_pending returning false, is_masked returning true (valid until evtchn_fifo_init_control() replaces them).

That's one of the options (the stubs could then as well live in event_channel.c). Another might be to put the FIFO ops in place right away, making sure they can cope with evtchn_fifo_init_control() not having been called yet.

Quoted 28 lines
Quoted 19 lines
Quoted 19 lines
Quoted 18 lines

xen/common/domain.c void vcpu_info_reset(struct vcpu *v) · line 326

Quoted 11 lines
xen/common/domain.c +1 -1
@@ -320,7 +320,7 @@ void vcpu_info_reset(struct vcpu *v)
       struct domain *d = v->domain;
 
       v->vcpu_info_area.map =
-        ((v->vcpu_id < XEN_LEGACY_MAX_VCPUS)
+        ((v->vcpu_id < XEN_LEGACY_MAX_VCPUS && d->shared_info)
            ? (vcpu_info_t *)&shared_info(d, vcpu_info[v->vcpu_id])
            : &dummy_vcpu_info);
   }

At the example of this: The extra conditionals are dead code on x86 and Arm. While the status of the respective Misra rule is still uncertain for Xen, imo we'd be better off avoiding the introduction of new dead code. Which in turn means we may need some kind of abstraction to have these extra conditionals in place only for arch-es not supporting shared-info at all.

Quoted 6 lines
Quoted 1 line

What about then add config HAS_SHARED_INFO to xen/common/Kconfig and then:

We're getting closer. Imo we want to go farther, though: shared_info() as a construct should be unavailable when !HAS_SHARED_INFO. _That_ then will make obvious (by causing build failures) that all respective use sites were properly dealt with.

I will add then:

Quoted 8 lines

+#ifdef CONFIG_HAS_SHARED_INFO
#define shared_info(d, field) __shared_info(d, (d)->shared_info,
field)
+#endif

But with doing that we have only option of using #ifdef HAS_SHARED_INFO in the place where shared_info() is used. If it is fine then I will be happy to do in this way.

Well, I gave a suggestion to avoid such #ifdef-ary, ...

Quoted 28 lines
Quoted 28 lines
Quoted 8 lines
xen/common/domain.c +1 -0
@@ -319,10 +319,14 @@ void vcpu_info_reset(struct vcpu *v)
   {
       struct domain *d = v->domain;
 
+#ifdef CONFIG_HAS_SHARED_INFO
       v->vcpu_info_area.map =

xen/common/domain.c void vcpu_info_reset(struct vcpu *v) · line 330

Quoted 10 lines
xen/common/domain.c +6 -3
-        ((v->vcpu_id < XEN_LEGACY_MAX_VCPUS && d->shared_info)
-         ? (vcpu_info_t *)&shared_info(d, vcpu_info[v->vcpu_id])
-         : &dummy_vcpu_info);
+        (v->vcpu_id < XEN_LEGACY_MAX_VCPUS)
+        ? (vcpu_info_t *)&shared_info(d, vcpu_info[v->vcpu_id])
+        : &dummy_vcpu_info;
+#else
+    v->vcpu_info_area.map = &dummy_vcpu_info;
+#endif
   }

I agree with #ifdef here.

Quoted 7 lines
xen/common/event_fifo.c +0 -0
@@ -562,9 +562,10 @@ static void setup_ports(struct domain *d, unsigned
 int prev_evtchns)
 
           evtchn = evtchn_from_port(d, port);
 
Quoted 12 lines
Quoted 4 lines
Quoted 4 lines
xen/common/event_fifo.c +2 -2
-        if ( d->shared_info &&
-             guest_test_bit(d, port, &shared_info(d, evtchn_pending)) )
+#ifdef CONFIG_HAS_SHARED_INFO
+        if ( guest_test_bit(d, port, &shared_info(d, evtchn_pending)) )
Quoted 8 lines

xen/common/event_fifo.c static void setup_ports(struct domain *d, unsigned int prev_evtchns) · line 568

Quoted 2 lines
xen/common/event_fifo.c +1 -0
               evtchn->pending = true;
+#endif

While as per above shared_info() would best not exist when !HAS_SHARED_INFO (in which case #ifdef may be unavoidable here), an alternative where IS_ENABLED() could be used here may want at least considering. E.g. causing a link-time failure when shared_info() is used (and not compiled out).

... here. There are downsides to this, so which route to go needs settling on.

Quoted 35 lines
Quoted 12 lines
Quoted 12 lines
xen/common/time.c +3 -2
@@ -94,8 +94,9 @@ void update_domain_wallclock_time(struct domain *d)
       uint32_t *wc_version;
       uint64_t sec;
 
-    if ( !d->shared_info )
-        return;
+#ifndef CONFIG_HAS_SHARED_INFO
+    return;
+#endif
 
Quoted 4 lines

xen/common/time.c void update_domain_wallclock_time(struct domain *d) · line 101

Quoted 1 line
xen/common/time.c +0 -0
       spin_lock(&wc_lock);

Constructs like this are imo somewhat ugly. Using IS_ENABLED() instead would make things at least a little better (again imo).

Considering mentioned above it would be better to #ifdef the whole buddy of the function:

void update_domain_wallclock_time(struct domain *d)
{

xen/common/time.c +2 -4
+#ifdef CONFIG_HAS_SHARED_INFO
+
      uint32_t *wc_version;
      uint64_t sec;
 
-#ifndef CONFIG_HAS_SHARED_INFO
-    return;
-#endif
-
      spin_lock(&wc_lock);
 
      wc_version = &shared_info(d, wc_version);

xen/common/time.c void update_domain_wallclock_time(struct domain *d) · line 123

Quoted 6 lines
xen/common/time.c +2 -0
@@ -120,6 +118,8 @@ void update_domain_wallclock_time(struct domain *d)
      *wc_version = version_update_end(*wc_version);
 
      spin_unlock(&wc_lock);
+
+#endif /* CONFIG_HAS_SHARED_INFO */
  }

In which case there's no point having use sites actually call here. I.e. we then may want to have an inline stub when !HAS_SHARED_INFO.

Quoted 2 lines

Considering also that shared_info is expected to be used only for 2L then it would be better to introduce CONFIG_HAS_EVTCHN_2L instead.

How does the event channel model in use matter for update_domain_wallclock_time()?

Jan

#12 lore raw

Re: [PATCH v2 2/3] xen/domain: fix UBSAN null pointer dereference of d->shared_info

Quoted 80 lines · Jan Beulich
Quoted 56 lines · Oleksii Kurochko
Quoted 55 lines · Jan Beulich
Quoted 53 lines · Oleksii Kurochko
Quoted 36 lines · Jan Beulich
Quoted 27 lines · Oleksii Kurochko

It is legal to have d->shared_info equal to NULL for architectures which support only the FIFO ABI for event channel management.

Having d->shared_info == NULL leads to a UBSAN issue on such architectures:

     UBSAN: Undefined behaviour in common/domain.c:325:10
            member access within null pointer of type 'struct shared_info_t'

vcpu_info_reset() maps v->vcpu_info_area.map to the per-vcpu slot inside the domain's shared_info page for vcpus with id < XEN_LEGACY_MAX_VCPUS, and falls back to dummy_vcpu_info for vcpus beyond that limit. Extend the existing fallback condition to also cover the case where no shared_info page has been allocated, mapping the vcpu to dummy_vcpu_info instead. This is the correct behaviour: dummy_vcpu_info already serves as the safe stand-in for vcpus that have no usable shared_info slot.

Additionally, if an architecture supports only the FIFO ABI, setup_ports() should be updated to avoid a NULL pointer dereference of d->shared_info, since in that case there will be no pending events in shared_info->evtchn_pending and the pending flag of the FIFO event channel does not need to be set to true.
update_domain_wallclock_time() accesses d->shared_info via shared_info() macro. On architectures that do not allocate a shared_info page (currently RISC-V, which runs guests in dom0less mode without the PV ABI), this causes a NULL dereference. The early return is safe: if there is no shared_info page, there is nothing to update. For all existing architectures (x86, ARM) that do allocate it, the guard is never taken and behavior is unchanged.

Quoted 7 lines

---
Changes in v2:
- Update commit message + subject.
- Drop Fixes tag.
- Handle migration of pending events from 2L and FIFO ABIs when arch support only FIFO ABI.

Quoted 2 lines

What does this item describe? On an arch supporting only FIFO, how could evtchn need migrating from 2L?

Agree this item is inaccurate.

evtchn_init() always calls evtchn_2l_init(d) first (event_channel.c:1627), so every domain starts with 2L regardless of arch (of course, it is just initialization of evtchn_port_ops which aren't really used when only FIFO is supported).

setup_ports() is called during the guest-initiated 2L→FIFO transition (event_fifo.c:637), not at arch init time. There is no arch that supports "only FIFO" as a starting state and that is why it is needed to guard setup_ports() against NULL d->shared_info when migrating 2L pending state to FIFO even 2L wasn't really used by an arch with only FIFO support.

Quoted 8 lines
Quoted 1 line

Imo on arch-es not supporting 2L, domains shouldn't start in 2L mode.

Agree but will it be easy to achieve now with the current code base?

The best what could be done it is avoid calling evtchn_2l_init() now in event_channel.c and:
1. Add a new Kconfig symbol, CONFIG_HAS_EVTCHN_2L (or re-use HAS_SHARED_INFO suggested before), selected by x86 and ARM.

I'd stick to just HAS_SHARED_INFO as long as a separate control for 2- level evtchn isn't strictly needed.

Quoted 10 lines

2. In evtchn_init() (event_channel.c:1627), guard the call:

     #ifdef CONFIG_HAS_EVTCHN_2L
        evtchn_2l_init(d);
     #else
        evtchn_none_init(d);
     #endif

3. Add a small stub ops table (probably in event_fifo.c or a new event_none.c) with no-op set_pending/clear_pending/unmask, is_pending returning false, is_masked returning true (valid until evtchn_fifo_init_control() replaces them).

Quoted 4 lines

That's one of the options (the stubs could then as well live in event_channel.c). Another might be to put the FIFO ops in place right away, making sure they can cope with evtchn_fifo_init_control() not having been called yet.

Some FIFO ops are dependent on evtchn_fifo_init_control:

evtchn_fifo_word_from_port() is used in FIFO ops, and it dereferences d->evtchn_fifo unconditionally at event_fifo.c:65:
if ( unlikely(port >= d->evtchn_fifo->num_evtchns) )

evtchn_fifo_set_pending() additionally dereferences v->evtchn_fifo->queue[...] at line 202, which is also NULL before per-vcpu FIFO init.

It looks safer to go with no-op operations.

Quoted 81 lines
Quoted 29 lines
Quoted 20 lines
Quoted 20 lines
Quoted 19 lines

xen/common/domain.c void vcpu_info_reset(struct vcpu *v) · line 326

Quoted 12 lines
xen/common/domain.c +1 -1
@@ -320,7 +320,7 @@ void vcpu_info_reset(struct vcpu *v)
        struct domain *d = v->domain;
 
        v->vcpu_info_area.map =
-        ((v->vcpu_id < XEN_LEGACY_MAX_VCPUS)
+        ((v->vcpu_id < XEN_LEGACY_MAX_VCPUS && d->shared_info)
             ? (vcpu_info_t *)&shared_info(d, vcpu_info[v->vcpu_id])
             : &dummy_vcpu_info);
    }

At the example of this: The extra conditionals are dead code on x86 and Arm. While the status of the respective Misra rule is still uncertain for Xen, imo we'd be better off avoiding the introduction of new dead code. Which in turn means we may need some kind of abstraction to have these extra conditionals in place only for arch-es not supporting shared-info at all.

Quoted 6 lines
Quoted 1 line

What about then add config HAS_SHARED_INFO to xen/common/Kconfig and then:

We're getting closer. Imo we want to go farther, though: shared_info() as a construct should be unavailable when !HAS_SHARED_INFO. _That_ then will make obvious (by causing build failures) that all respective use sites were properly dealt with.

I will add then:

Quoted 8 lines

+#ifdef CONFIG_HAS_SHARED_INFO
#define shared_info(d, field) __shared_info(d, (d)->shared_info,
field)
+#endif

But with doing that we have only option of using #ifdef HAS_SHARED_INFO in the place where shared_info() is used. If it is fine then I will be happy to do in this way.

Well, I gave a suggestion to avoid such #ifdef-ary, ...

Quoted 28 lines
Quoted 28 lines
Quoted 8 lines
xen/common/domain.c +1 -0
@@ -319,10 +319,14 @@ void vcpu_info_reset(struct vcpu *v)
    {
        struct domain *d = v->domain;
 
+#ifdef CONFIG_HAS_SHARED_INFO
        v->vcpu_info_area.map =

xen/common/domain.c void vcpu_info_reset(struct vcpu *v) · line 330

Quoted 10 lines
xen/common/domain.c +6 -3
-        ((v->vcpu_id < XEN_LEGACY_MAX_VCPUS && d->shared_info)
-         ? (vcpu_info_t *)&shared_info(d, vcpu_info[v->vcpu_id])
-         : &dummy_vcpu_info);
+        (v->vcpu_id < XEN_LEGACY_MAX_VCPUS)
+        ? (vcpu_info_t *)&shared_info(d, vcpu_info[v->vcpu_id])
+        : &dummy_vcpu_info;
+#else
+    v->vcpu_info_area.map = &dummy_vcpu_info;
+#endif
    }

I agree with #ifdef here.

Quoted 7 lines
xen/common/event_fifo.c +0 -0
@@ -562,9 +562,10 @@ static void setup_ports(struct domain *d, unsigned
 int prev_evtchns)
 
            evtchn = evtchn_from_port(d, port);
 
Quoted 12 lines
Quoted 4 lines
Quoted 4 lines
xen/common/event_fifo.c +2 -2
-        if ( d->shared_info &&
-             guest_test_bit(d, port, &shared_info(d, evtchn_pending)) )
+#ifdef CONFIG_HAS_SHARED_INFO
+        if ( guest_test_bit(d, port, &shared_info(d, evtchn_pending)) )
Quoted 8 lines

xen/common/event_fifo.c static void setup_ports(struct domain *d, unsigned int prev_evtchns) · line 568

Quoted 2 lines
xen/common/event_fifo.c +1 -0
                evtchn->pending = true;
+#endif

While as per above shared_info() would best not exist when !HAS_SHARED_INFO (in which case #ifdef may be unavoidable here), an alternative where IS_ENABLED() could be used here may want at least considering. E.g. causing a link-time failure when shared_info() is used (and not compiled out).

Quoted 2 lines

... here. There are downsides to this, so which route to go needs settling on.

For an alternative approach are you okay with the following introduction:

#ifdef CONFIG_HAS_SHARED_INFO
#define shared_info(d, field) __shared_info(d, (d)->shared_info, field)
#else
void *__shared_info_unavailable(void);

#define shared_info(d, field) \
    (*(typeof(__shared_info(d, (d)->shared_info, field)) 

*)__shared_info_unavailable())
#endif

And then use IS_ENABLED(CONFIG_HAS_SHARED_INFO) everywhere where shared_info() is used including the case above:

v->vcpu_info_area.map =

    IS_ENABLED(CONFIG_HAS_SHARED_INFO) && v->vcpu_id < XEN_LEGACY_MAX_VCPUS
    ? (vcpu_info_t *)&shared_info(d, vcpu_info[v->vcpu_id])
    : &dummy_vcpu_info;

Everything that in event_2l.c could go for now without IS_ENABLED(CONFIG_HAS_SHARED_INFO) where shared_info() is used as that code isn't expected to be called by arch which doesn't support 2L so no linkage error will occur.

Quoted 46 lines
Quoted 36 lines
Quoted 13 lines
Quoted 13 lines
xen/common/time.c +3 -2
@@ -94,8 +94,9 @@ void update_domain_wallclock_time(struct domain *d)
        uint32_t *wc_version;
        uint64_t sec;
 
-    if ( !d->shared_info )
-        return;
+#ifndef CONFIG_HAS_SHARED_INFO
+    return;
+#endif
 
Quoted 4 lines

xen/common/time.c void update_domain_wallclock_time(struct domain *d) · line 101

Quoted 1 line
xen/common/time.c +0 -0
        spin_lock(&wc_lock);

Constructs like this are imo somewhat ugly. Using IS_ENABLED() instead would make things at least a little better (again imo).

Considering mentioned above it would be better to #ifdef the whole buddy of the function:

void update_domain_wallclock_time(struct domain *d)
{

xen/common/time.c +2 -4
+#ifdef CONFIG_HAS_SHARED_INFO
+
       uint32_t *wc_version;
       uint64_t sec;
 
-#ifndef CONFIG_HAS_SHARED_INFO
-    return;
-#endif
-
       spin_lock(&wc_lock);
 
       wc_version = &shared_info(d, wc_version);

xen/common/time.c void update_domain_wallclock_time(struct domain *d) · line 123

Quoted 6 lines
xen/common/time.c +2 -0
@@ -120,6 +118,8 @@ void update_domain_wallclock_time(struct domain *d)
       *wc_version = version_update_end(*wc_version);
 
       spin_unlock(&wc_lock);
+
+#endif /* CONFIG_HAS_SHARED_INFO */
   }

In which case there's no point having use sites actually call here. I.e. we then may want to have an inline stub when !HAS_SHARED_INFO.

Quoted 5 lines
Quoted 2 lines

Considering also that shared_info is expected to be used only for 2L then it would be better to introduce CONFIG_HAS_EVTCHN_2L instead.

How does the event channel model in use matter for update_domain_wallclock_time()?

Just implicitly because of that 2l uses shared_info page but theoretically it is possible that this page will be used for something else or for some other event channel ABI implementation so CONFIG_HAS_SHARED_INFO is better to use.

~ Oleksii

#13 lore raw

Re: [PATCH v2 2/3] xen/domain: fix UBSAN null pointer dereference of d->shared_info

Quoted 44 lines · Oleksii Kurochko
Quoted 25 lines · Jan Beulich
Quoted 11 lines · Oleksii Kurochko
Quoted 10 lines · Jan Beulich
Quoted 9 lines · Oleksii Kurochko
Quoted 1 line · Jan Beulich

On 25.05.2026 15:20, Oleksii Kurochko wrote:

xen/common/event_fifo.c +0 -0
@@ -562,9 +562,10 @@ static void setup_ports(struct domain *d, unsigned
 int prev_evtchns)
 
            evtchn = evtchn_from_port(d, port);
 
Quoted 12 lines
Quoted 4 lines
Quoted 4 lines
xen/common/event_fifo.c +2 -2
-        if ( d->shared_info &&
-             guest_test_bit(d, port, &shared_info(d, evtchn_pending)) )
+#ifdef CONFIG_HAS_SHARED_INFO
+        if ( guest_test_bit(d, port, &shared_info(d, evtchn_pending)) )
Quoted 8 lines

xen/common/event_fifo.c static void setup_ports(struct domain *d, unsigned int prev_evtchns) · line 568

Quoted 2 lines
xen/common/event_fifo.c +1 -0
                evtchn->pending = true;
+#endif

While as per above shared_info() would best not exist when !HAS_SHARED_INFO (in which case #ifdef may be unavoidable here), an alternative where IS_ENABLED() could be used here may want at least considering. E.g. causing a link-time failure when shared_info() is used (and not compiled out).

Quoted 2 lines

... here. There are downsides to this, so which route to go needs settling on.

For an alternative approach are you okay with the following introduction:

#ifdef CONFIG_HAS_SHARED_INFO
#define shared_info(d, field) __shared_info(d, (d)->shared_info, field)
#else
void *__shared_info_unavailable(void);

#define shared_info(d, field) \
     (*(typeof(__shared_info(d, (d)->shared_info, field)) 

*)__shared_info_unavailable())
#endif

And then use IS_ENABLED(CONFIG_HAS_SHARED_INFO) everywhere where shared_info() is used including the case above:

v->vcpu_info_area.map =

Quoted 8 lines
     IS_ENABLED(CONFIG_HAS_SHARED_INFO) && v->vcpu_id < XEN_LEGACY_MAX_VCPUS
     ? (vcpu_info_t *)&shared_info(d, vcpu_info[v->vcpu_id])
     : &dummy_vcpu_info;

Everything that in event_2l.c could go for now without IS_ENABLED(CONFIG_HAS_SHARED_INFO) where shared_info() is used as that code isn't expected to be called by arch which doesn't support 2L so no linkage error will occur.

Yes, this roughly is what I was thinking of. I'd like to remind you though of issues with identifiers with two leading underscores.

Jan

#14 lore raw

Re: [PATCH v2 2/3] xen/domain: fix UBSAN null pointer dereference of d->shared_info

Quoted 49 lines · Jan Beulich
Quoted 48 lines · Oleksii Kurochko
Quoted 25 lines · Jan Beulich
Quoted 11 lines · Oleksii Kurochko
Quoted 10 lines · Jan Beulich
Quoted 9 lines · Oleksii Kurochko
Quoted 1 line · Jan Beulich

On 25.05.2026 15:20, Oleksii Kurochko wrote:

xen/common/event_fifo.c +0 -0
@@ -562,9 +562,10 @@ static void setup_ports(struct domain *d, unsigned
 int prev_evtchns)
 
             evtchn = evtchn_from_port(d, port);
 
Quoted 12 lines
Quoted 4 lines
Quoted 4 lines
xen/common/event_fifo.c +2 -2
-        if ( d->shared_info &&
-             guest_test_bit(d, port, &shared_info(d, evtchn_pending)) )
+#ifdef CONFIG_HAS_SHARED_INFO
+        if ( guest_test_bit(d, port, &shared_info(d, evtchn_pending)) )
Quoted 8 lines

xen/common/event_fifo.c static void setup_ports(struct domain *d, unsigned int prev_evtchns) · line 568

Quoted 2 lines
xen/common/event_fifo.c +1 -0
                 evtchn->pending = true;
+#endif

While as per above shared_info() would best not exist when !HAS_SHARED_INFO (in which case #ifdef may be unavoidable here), an alternative where IS_ENABLED() could be used here may want at least considering. E.g. causing a link-time failure when shared_info() is used (and not compiled out).

Quoted 2 lines

... here. There are downsides to this, so which route to go needs settling on.

For an alternative approach are you okay with the following introduction:

#ifdef CONFIG_HAS_SHARED_INFO
#define shared_info(d, field) __shared_info(d, (d)->shared_info, field)
#else
void *__shared_info_unavailable(void);

#define shared_info(d, field) \
      (*(typeof(__shared_info(d, (d)->shared_info, field))

*)__shared_info_unavailable())
#endif

And then use IS_ENABLED(CONFIG_HAS_SHARED_INFO) everywhere where shared_info() is used including the case above:

v->vcpu_info_area.map =

      IS_ENABLED(CONFIG_HAS_SHARED_INFO) && v->vcpu_id < XEN_LEGACY_MAX_VCPUS
      ? (vcpu_info_t *)&shared_info(d, vcpu_info[v->vcpu_id])
      : &dummy_vcpu_info;
Quoted 7 lines
Quoted 4 lines

Everything that in event_2l.c could go for now without IS_ENABLED(CONFIG_HAS_SHARED_INFO) where shared_info() is used as that code isn't expected to be called by arch which doesn't support 2L so no linkage error will occur.

Yes, this roughly is what I was thinking of. I'd like to remind you though of issues with identifiers with two leading underscores.

For event_2l.c would you be okay to make compilation of it conditional by CONFIG_HAS_SHARED_INFO or it would be better to introduce separate CONFIG_HAS_EVTCHN_2L:
-obj-y += event_2l.o
+obj-$(CONFIG_HAS_EVTCHN_2L) += event_2l.o

~ Oleksii

#15 lore raw

Re: [PATCH v2 2/3] xen/domain: fix UBSAN null pointer dereference of d->shared_info

Quoted 55 lines · Oleksii Kurochko
Quoted 54 lines · Jan Beulich
Quoted 52 lines · Oleksii Kurochko
Quoted 25 lines · Jan Beulich
Quoted 11 lines · Oleksii Kurochko
Quoted 10 lines · Jan Beulich
Quoted 9 lines · Oleksii Kurochko
Quoted 1 line · Jan Beulich

On 25.05.2026 15:20, Oleksii Kurochko wrote:

xen/common/event_fifo.c +0 -0
@@ -562,9 +562,10 @@ static void setup_ports(struct domain *d, unsigned
 int prev_evtchns)
 
             evtchn = evtchn_from_port(d, port);
 
Quoted 12 lines
Quoted 4 lines
Quoted 4 lines
xen/common/event_fifo.c +2 -2
-        if ( d->shared_info &&
-             guest_test_bit(d, port, &shared_info(d, evtchn_pending)) )
+#ifdef CONFIG_HAS_SHARED_INFO
+        if ( guest_test_bit(d, port, &shared_info(d, evtchn_pending)) )
Quoted 8 lines

xen/common/event_fifo.c static void setup_ports(struct domain *d, unsigned · line 569

Quoted 2 lines
xen/common/event_fifo.c +1 -0
                 evtchn->pending = true;
+#endif

While as per above shared_info() would best not exist when !HAS_SHARED_INFO (in which case #ifdef may be unavoidable here), an alternative where IS_ENABLED() could be used here may want at least considering. E.g. causing a link-time failure when shared_info() is used (and not compiled out).

Quoted 2 lines

... here. There are downsides to this, so which route to go needs settling on.

For an alternative approach are you okay with the following introduction:

#ifdef CONFIG_HAS_SHARED_INFO
#define shared_info(d, field) __shared_info(d, (d)->shared_info, field)
#else
void *__shared_info_unavailable(void);

#define shared_info(d, field) \
      (*(typeof(__shared_info(d, (d)->shared_info, field))

*)__shared_info_unavailable())
#endif

And then use IS_ENABLED(CONFIG_HAS_SHARED_INFO) everywhere where shared_info() is used including the case above:

v->vcpu_info_area.map =

      IS_ENABLED(CONFIG_HAS_SHARED_INFO) && v->vcpu_id < XEN_LEGACY_MAX_VCPUS
      ? (vcpu_info_t *)&shared_info(d, vcpu_info[v->vcpu_id])
      : &dummy_vcpu_info;

Everything that in event_2l.c could go for now without IS_ENABLED(CONFIG_HAS_SHARED_INFO) where shared_info() is used as that code isn't expected to be called by arch which doesn't support 2L so no linkage error will occur.

Quoted 8 lines
Quoted 2 lines

Yes, this roughly is what I was thinking of. I'd like to remind you though of issues with identifiers with two leading underscores.

For event_2l.c would you be okay to make compilation of it conditional by CONFIG_HAS_SHARED_INFO or it would be better to introduce separate CONFIG_HAS_EVTCHN_2L:

   -obj-y += event_2l.o
   +obj-$(CONFIG_HAS_EVTCHN_2L) += event_2l.o

See my earlier reply: Let's stick to just HAS_SHARED_INFO unless there's a clear need to distinguish that from 2-level event channels support.

Jan

#16 lore raw

Re: [PATCH v2 2/3] xen/domain: fix UBSAN null pointer dereference of d->shared_info

In reply to Jan Beulich
Quoted 7 lines · Jan Beulich
Quoted 7 lines
xen/common/event_fifo.c +0 -0
@@ -562,9 +562,10 @@ static void setup_ports(struct domain *d, unsigned
 int prev_evtchns)
 
           evtchn = evtchn_from_port(d, port);
 
Quoted 11 lines

xen/common/event_fifo.c static void setup_ports(struct domain *d, unsigned int prev_evtchns) · line 568

Quoted 6 lines
xen/common/event_fifo.c +3 -2
-        if ( d->shared_info &&
-             guest_test_bit(d, port, &shared_info(d, evtchn_pending)) )
+#ifdef CONFIG_HAS_SHARED_INFO
+        if ( guest_test_bit(d, port, &shared_info(d, evtchn_pending)) )
               evtchn->pending = true;
+#endif

While as per above shared_info() would best not exist when !HAS_SHARED_INFO (in which case #ifdef may be unavoidable here), an alternative where IS_ENABLED() could be used here may want at least considering. E.g. causing a link-time failure when shared_info() is used (and not compiled out).

We still want here to have #ifdef instead of IS_ENABLED() as shared_info() shouldn't exist for arch without 2L support so it will end with linkage error. Considering that setup_ports() will be called for such arch we have to avoid this part from compilation.

Alternative is that considering that I suggested in prev emails to introduced stubs for arch which doesn't use 2L:

+#ifndef CONFIG_HAS_SHARED_INFO
+static void cf_check evtchn_none_set_pending(
+ struct vcpu *v, struct evtchn *evtchn) {}
+static void cf_check evtchn_none_clear_pending(
+ struct domain *d, struct evtchn *evtchn) {}
+static void cf_check evtchn_none_unmask(
+ struct domain *d, struct evtchn *evtchn) {}
+static bool cf_check evtchn_none_is_pending(
+ const struct domain *d, const struct evtchn *evtchn) { return false; }
+static bool cf_check evtchn_none_is_masked(
+ const struct domain *d, const struct evtchn *evtchn) { return true; }
+static void cf_check evtchn_none_print_state(
+ struct domain *d, const struct evtchn *evtchn) {}
+
+static const struct evtchn_port_ops evtchn_port_ops_none = {

+    .set_pending   = evtchn_none_set_pending,
+    .clear_pending = evtchn_none_clear_pending,
+    .unmask        = evtchn_none_unmask,
+    .is_pending    = evtchn_none_is_pending,
+    .is_masked     = evtchn_none_is_masked,
+    .print_state   = evtchn_none_print_state,

+};
+
+static void evtchn_none_init(struct domain *d)
+{
+ d->evtchn_port_ops = &evtchn_port_ops_none;
+}
+#endif

For arch without 2L supports .is_pending() will return false we can just do the following instead of ifdef:

-#ifdef CONFIG_HAS_SHARED_INFO
- if ( guest_test_bit(d, port, &shared_info(d, evtchn_pending)) )
- evtchn->pending = true;
-#endif
+ if ( evtchn_is_pending(d, evtchn) )
+ evtchn->pending = true;

Would you be okay with this approach instead of ifdef?

~ Oleksii

#17 lore raw

Re: [PATCH v2 2/3] xen/domain: fix UBSAN null pointer dereference of d->shared_info

Quoted 22 lines · Oleksii Kurochko
Quoted 7 lines · Jan Beulich
Quoted 7 lines
xen/common/event_fifo.c +0 -0
@@ -562,9 +562,10 @@ static void setup_ports(struct domain *d, unsigned
 int prev_evtchns)
 
           evtchn = evtchn_from_port(d, port);
 
Quoted 11 lines

xen/common/event_fifo.c static void setup_ports(struct domain *d, unsigned int prev_evtchns) · line 568

Quoted 6 lines
xen/common/event_fifo.c +3 -2
-        if ( d->shared_info &&
-             guest_test_bit(d, port, &shared_info(d, evtchn_pending)) )
+#ifdef CONFIG_HAS_SHARED_INFO
+        if ( guest_test_bit(d, port, &shared_info(d, evtchn_pending)) )
               evtchn->pending = true;
+#endif

While as per above shared_info() would best not exist when !HAS_SHARED_INFO (in which case #ifdef may be unavoidable here), an alternative where IS_ENABLED() could be used here may want at least considering. E.g. causing a link-time failure when shared_info() is used (and not compiled out).

Quoted 3 lines

We still want here to have #ifdef instead of IS_ENABLED() as shared_info() shouldn't exist for arch without 2L support so it will end with linkage error.

I don't understand this part.

Quoted 9 lines

Considering that setup_ports() will be called for such arch we have to avoid this part from compilation.

Alternative is that considering that I suggested in prev emails to introduced stubs for arch which doesn't use 2L:

(quoted diff) +3 -0
+#ifndef CONFIG_HAS_SHARED_INFO
+static void cf_check evtchn_none_set_pending(
+    struct vcpu *v, struct evtchn *evtchn) {}

Review

Quoted 10 lines
(quoted diff) +10 -0
+static void cf_check evtchn_none_clear_pending(
+    struct domain *d, struct evtchn *evtchn) {}
+static void cf_check evtchn_none_unmask(
+    struct domain *d, struct evtchn *evtchn) {}
+static bool cf_check evtchn_none_is_pending(
+    const struct domain *d, const struct evtchn *evtchn) { return false; }
+static bool cf_check evtchn_none_is_masked(
+    const struct domain *d, const struct evtchn *evtchn) { return true; }
+static void cf_check evtchn_none_print_state(
+    struct domain *d, const struct evtchn *evtchn) {}

This set can be shrunk. For example, the same stub can be used for clear-pending and unmask. For is-pending and is-masked, considering that the precise return value shouldn't matter, a single function (returning false) would likely be good enough as well.

Quoted 25 lines
(quoted diff) +14 -0
+static const struct evtchn_port_ops evtchn_port_ops_none = {
+    .set_pending   = evtchn_none_set_pending,
+    .clear_pending = evtchn_none_clear_pending,
+    .unmask        = evtchn_none_unmask,
+    .is_pending    = evtchn_none_is_pending,
+    .is_masked     = evtchn_none_is_masked,
+    .print_state   = evtchn_none_print_state,
+};
+
+static void evtchn_none_init(struct domain *d)
+{
+    d->evtchn_port_ops = &evtchn_port_ops_none;
+}
+#endif

For arch without 2L supports .is_pending() will return false we can just do the following instead of ifdef:

(quoted diff) +2 -4
-#ifdef CONFIG_HAS_SHARED_INFO
-        if ( guest_test_bit(d, port, &shared_info(d, evtchn_pending)) )
-            evtchn->pending = true;
-#endif
+        if ( evtchn_is_pending(d, evtchn) )
+             evtchn->pending = true;
Quoted 1 line

Would you be okay with this approach instead of ifdef?

I can't really say ahead of seeing the full result (and without it being made clear why FIFO ops can't be put in place right away, with perhaps a few small adjustments to the handlers). While this isn't going to be used for x86, introduction of new cf_check functions always worries me, at least some.

Jan

#18 lore raw

Re: [PATCH v2 2/3] xen/domain: fix UBSAN null pointer dereference of d->shared_info

Quoted 23 lines · Jan Beulich
Quoted 22 lines · Oleksii Kurochko
Quoted 18 lines · Jan Beulich

xen/common/event_fifo.c static void setup_ports(struct domain *d, unsigned · line 569

Quoted 13 lines
xen/common/event_fifo.c +3 -2
@@ -562,9 +562,10 @@ static void setup_ports(struct domain *d, unsigned
 int prev_evtchns)
 
            evtchn = evtchn_from_port(d, port);
 
-        if ( d->shared_info &&
-             guest_test_bit(d, port, &shared_info(d, evtchn_pending)) )
+#ifdef CONFIG_HAS_SHARED_INFO
+        if ( guest_test_bit(d, port, &shared_info(d, evtchn_pending)) )
                evtchn->pending = true;
+#endif

While as per above shared_info() would best not exist when !HAS_SHARED_INFO (in which case #ifdef may be unavoidable here), an alternative where IS_ENABLED() could be used here may want at least considering. E.g. causing a link-time failure when shared_info() is used (and not compiled out).

Quoted 5 lines
Quoted 3 lines

We still want here to have #ifdef instead of IS_ENABLED() as shared_info() shouldn't exist for arch without 2L support so it will end with linkage error.

I don't understand this part.

If the change will look like:

if ( IS_ENABLED(CONFIG_HAS_SHARED_INFO) && guest_test_bit(d, port,
&shared_info(d, evtchn_pending)) )

      evtchn->pending = true;

It will help to avoid NULL pointer dereference of shared info page in case of 2L isn't supported. But considering that shared_info() macros will be reworked in the way which will lead to linkage error in the case when it is used and arch doesn't have shared info page support usage of shared_info() in setup_ports() will lead to linkage error what is wanted to be avoid for arch without 2L support.

Quoted 51 lines
Quoted 10 lines

Considering that setup_ports() will be called for such arch we have to avoid this part from compilation.

Alternative is that considering that I suggested in prev emails to introduced stubs for arch which doesn't use 2L:

(quoted diff) +3 -0
+#ifndef CONFIG_HAS_SHARED_INFO
+static void cf_check evtchn_none_set_pending(
+    struct vcpu *v, struct evtchn *evtchn) {}

Review

Quoted 10 lines
(quoted diff) +10 -0
+static void cf_check evtchn_none_clear_pending(
+    struct domain *d, struct evtchn *evtchn) {}
+static void cf_check evtchn_none_unmask(
+    struct domain *d, struct evtchn *evtchn) {}
+static bool cf_check evtchn_none_is_pending(
+    const struct domain *d, const struct evtchn *evtchn) { return false; }
+static bool cf_check evtchn_none_is_masked(
+    const struct domain *d, const struct evtchn *evtchn) { return true; }
+static void cf_check evtchn_none_print_state(
+    struct domain *d, const struct evtchn *evtchn) {}

This set can be shrunk. For example, the same stub can be used for clear-pending and unmask. For is-pending and is-masked, considering that the precise return value shouldn't matter, a single function (returning false) would likely be good enough as well.

Quoted 25 lines
(quoted diff) +14 -0
+static const struct evtchn_port_ops evtchn_port_ops_none = {
+    .set_pending   = evtchn_none_set_pending,
+    .clear_pending = evtchn_none_clear_pending,
+    .unmask        = evtchn_none_unmask,
+    .is_pending    = evtchn_none_is_pending,
+    .is_masked     = evtchn_none_is_masked,
+    .print_state   = evtchn_none_print_state,
+};
+
+static void evtchn_none_init(struct domain *d)
+{
+    d->evtchn_port_ops = &evtchn_port_ops_none;
+}
+#endif

For arch without 2L supports .is_pending() will return false we can just do the following instead of ifdef:

(quoted diff) +2 -4
-#ifdef CONFIG_HAS_SHARED_INFO
-        if ( guest_test_bit(d, port, &shared_info(d, evtchn_pending)) )
-            evtchn->pending = true;
-#endif
+        if ( evtchn_is_pending(d, evtchn) )
+             evtchn->pending = true;
Quoted 7 lines
Quoted 1 line

Would you be okay with this approach instead of ifdef?

I can't really say ahead of seeing the full result (and without it being made clear why FIFO ops can't be put in place right away, with perhaps a few small adjustments to the handlers). While this isn't going to be used for x86, introduction of new cf_check functions always worries me, at least some.

I looked at FIFO ops again and it looks like it would be enough just to do the following:

xen/common/event_fifo.c +3 -0
@@ -62,6 +62,9 @@ static inline event_word_t *evtchn_fifo_word_from_port(const struct domain *d,
      */
     smp_rmb();
 
+    if ( unlikely(!d->evtchn_fifo) )
+        return NULL;
+
     if ( unlikely(port >= d->evtchn_fifo->num_evtchns) )
         return NULL;
 

as evtchn_fifo_word_from_port() is used at the begining of fifo ops and if its NULL then it will just return without execution of the rest of an operation so even something isn't allocated it is still safe.

The changes above are wrong as evtchn_is_pending(d, evtchn) will be called for FIFO's evtchn but the original check is checking if there is any pending interrupt in 2L before final switch to FIFO. So we still want to have:

+#ifdef CONFIG_HAS_SHARED_INFO
+ if ( guest_test_bit(d, port, &shared_info(d, evtchn_pending)) )
+ evtchn->pending = true;
+#endif

And then probably we can d->evtchn_port_op with FIFO ops in the way how it is done now instead of:

@@ -1660,7 +1629,7 @@ int evtchn_init(struct domain *d, unsigned int max_port)

 #ifdef CONFIG_HAS_SHARED_INFO
     evtchn_2l_init(d);
 #else
+    evtchn_fifo_init_ops(d);
 #endif
     d->max_evtchn_port = min_t(unsigned int, max_port, INT_MAX);

~ Oleksii

#19 lore raw

Re: [PATCH v2 2/3] xen/domain: fix UBSAN null pointer dereference of d->shared_info

Quoted 38 lines · Oleksii Kurochko
Quoted 23 lines · Jan Beulich
Quoted 22 lines · Oleksii Kurochko
Quoted 18 lines · Jan Beulich

xen/common/event_fifo.c static void setup_ports(struct domain *d, unsigned · line 569

Quoted 13 lines
xen/common/event_fifo.c +3 -2
@@ -562,9 +562,10 @@ static void setup_ports(struct domain *d, unsigned
 int prev_evtchns)
 
            evtchn = evtchn_from_port(d, port);
 
-        if ( d->shared_info &&
-             guest_test_bit(d, port, &shared_info(d, evtchn_pending)) )
+#ifdef CONFIG_HAS_SHARED_INFO
+        if ( guest_test_bit(d, port, &shared_info(d, evtchn_pending)) )
                evtchn->pending = true;
+#endif

While as per above shared_info() would best not exist when !HAS_SHARED_INFO (in which case #ifdef may be unavoidable here), an alternative where IS_ENABLED() could be used here may want at least considering. E.g. causing a link-time failure when shared_info() is used (and not compiled out).

Quoted 5 lines
Quoted 3 lines

We still want here to have #ifdef instead of IS_ENABLED() as shared_info() shouldn't exist for arch without 2L support so it will end with linkage error.

I don't understand this part.

If the change will look like:

   if ( IS_ENABLED(CONFIG_HAS_SHARED_INFO) && guest_test_bit(d, port, 
&shared_info(d, evtchn_pending)) )
       evtchn->pending = true;
Quoted 6 lines

It will help to avoid NULL pointer dereference of shared info page in case of 2L isn't supported. But considering that shared_info() macros will be reworked in the way which will lead to linkage error in the case when it is used and arch doesn't have shared info page support usage of shared_info() in setup_ports() will lead to linkage error what is wanted to be avoid for arch without 2L support.

What linking error are you talking of? When !HAS_SHARED_INFO, the compiler will DCE the entire guest_test_bit(), including the shared_info() use. IOW I still don't understand what you're talking of.

Jan

#20 lore raw

Re: [PATCH v2 2/3] xen/domain: fix UBSAN null pointer dereference of d->shared_info

Quoted 46 lines · Jan Beulich
Quoted 38 lines · Oleksii Kurochko
Quoted 23 lines · Jan Beulich
Quoted 22 lines · Oleksii Kurochko
Quoted 18 lines · Jan Beulich

xen/common/event_fifo.c static void setup_ports(struct domain *d, unsigned · line 569

Quoted 13 lines
xen/common/event_fifo.c +3 -2
@@ -562,9 +562,10 @@ static void setup_ports(struct domain *d, unsigned
 int prev_evtchns)
 
             evtchn = evtchn_from_port(d, port);
 
-        if ( d->shared_info &&
-             guest_test_bit(d, port, &shared_info(d, evtchn_pending)) )
+#ifdef CONFIG_HAS_SHARED_INFO
+        if ( guest_test_bit(d, port, &shared_info(d, evtchn_pending)) )
                 evtchn->pending = true;
+#endif

While as per above shared_info() would best not exist when !HAS_SHARED_INFO (in which case #ifdef may be unavoidable here), an alternative where IS_ENABLED() could be used here may want at least considering. E.g. causing a link-time failure when shared_info() is used (and not compiled out).

Quoted 5 lines
Quoted 3 lines

We still want here to have #ifdef instead of IS_ENABLED() as shared_info() shouldn't exist for arch without 2L support so it will end with linkage error.

I don't understand this part.

If the change will look like:

    if ( IS_ENABLED(CONFIG_HAS_SHARED_INFO) && guest_test_bit(d, port,
&shared_info(d, evtchn_pending)) )
        evtchn->pending = true;
Quoted 6 lines

It will help to avoid NULL pointer dereference of shared info page in case of 2L isn't supported. But considering that shared_info() macros will be reworked in the way which will lead to linkage error in the case when it is used and arch doesn't have shared info page support usage of shared_info() in setup_ports() will lead to linkage error what is wanted to be avoid for arch without 2L support.

Quoted 3 lines

What linking error are you talking of? When !HAS_SHARED_INFO, the compiler will DCE the entire guest_test_bit(), including the shared_info() use. IOW I still don't understand what you're talking of.

Oh, you are right. Sorry for the noise.

Thanks.

~ Oleksii

#21 lore raw

PATCH v2 3/3 libfdt: fix UBSAN null pointer in fdt_property()

In reply to Oleksii Kurochko

fdt_property() unconditionally calls memcpy(ptr, val, len) even when len is zero and val is NULL. This is a legitimate calling convention for adding empty FDT properties such as "interrupt-controller", which carry no payload.

However, compilers that treat memcpy as nonnull on its pointer arguments will fire UBSAN before observing that len is zero.

Guard the memcpy() with a check on len so it is skipped entirely when there is no payload to copy, bringing the code in line with the nonnull contract.

Origin: git://git.kernel.org/pub/scm/utils/dtc/dtc.git f57e7df35df4

Fixes f0ea06558068 ("libfdt: add version 1.3.0")

---
Changes in v2:
- Backport patch from https://github.com/dgibson/dtc/commit/f57e7df35df4a301961cbbf9433ba4e85c2ee5ed

---
 xen/common/libfdt/fdt_sw.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
xen/common/libfdt/fdt_sw.c +2 -1
index 4c569ee7eb0d..96d4cf571319 100644
@@ -330,7 +330,8 @@ int fdt_property(void *fdt, const char *name, const void *val, int len)
 	ret = fdt_property_placeholder(fdt, name, len, &ptr);
 	if (ret)
 		return ret;
-	memcpy(ptr, val, len);
+	if (len)
+		memcpy(ptr, val, len);
 	return 0;
 }
 
Signature · 1 line
2.54.0
Reviewed-by on v2 3/3
#22 lore raw

Re: [PATCH v2 3/3] libfdt: fix UBSAN null pointer in fdt_property()

Quoted 12 lines · Oleksii Kurochko

fdt_property() unconditionally calls memcpy(ptr, val, len) even when len is zero and val is NULL. This is a legitimate calling convention for adding empty FDT properties such as "interrupt-controller", which carry no payload.

However, compilers that treat memcpy as nonnull on its pointer arguments will fire UBSAN before observing that len is zero.

Guard the memcpy() with a check on len so it is skipped entirely when there is no payload to copy, bringing the code in line with the nonnull contract.

Quoted 7 lines

Origin: git://git.kernel.org/pub/scm/utils/dtc/dtc.git f57e7df35df4

Fixes f0ea06558068 ("libfdt: add version 1.3.0")

~Michal