[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH v8 63/65] xen: assert that data handed to other CPUs is not on a CPU's stack



With per-CPU stacks, each pCPU runs on its stack at an address of its
own, and the page-tables of a cpu_stack vCPU map there the stack of the
pCPU it runs on and no other.  Code that hands a pointer into one pCPU's
stack to another pCPU then faults when the other pCPU dereferences it
on such page-tables, and on all other page-tables reaches into a stack
in use by someone else without anyone noticing.  No caller does so
today; the call-function and stop-machine paths assert it for the
caller's own stack.  Nothing checks the other places that hand
pointers to other CPUs.

Add ptr_on_a_cpu_stack(): whether a pointer lies in any CPU's stack,
where the architecture keeps every CPU's stack in an area of its own
and says so (arch_ptr_on_cpu_stacks()), and in any case in the stack
the caller runs on, as ptr_on_current_stack() tells, which need not lie
in that area: x86's BSP runs on cpu0_stack until the end of
__start_xen().  On x86 with per-CPU stacks the area is the per-CPU
stack sub-area, so the test is one subtraction and one compare,
whichever CPU's stack the pointer is in, then the current-stack test;
without them, and on other architectures, it is the caller's stack
only.  Another mapping of a stack's pages, such as the directmap alias,
is not caught, as before.

Use it, in ASSERT()s, where a pointer is handed to code that may run on
another CPU:

 - on_selected_cpus() for a by-pointer payload, and
   stop_machine_run(): the existing checks, widened from the caller's
   stack to every CPU's.

 - tasklet_init(): the tasklet is queued on whichever CPU it is
   scheduled on (tasklet_schedule_on_cpu()), and @func runs there with
   @data.

 - init_timer(): the timer is queued on @cpu, where @function runs with
   @data, and migrate_timer() and kill_timer() reach it from any CPU.

 - setup_irq() (x86): the irqaction is reached through the IRQ
   descriptor by whichever CPU takes the interrupt, and its handler gets
   dev_id.  request_irq() goes through it.

 - notifier_chain_register(): the block stays linked into the chain,
   which is called on whichever CPU raises the event (CPU hotplug, for
   the CPU notifiers).

 - vpci_add_register_mask(), and so vpci_add_register(): @data is kept
   with the register and passed to its handlers on whichever CPU the
   guest accesses the device's configuration space from.

continue_hypercall_on_cpu() hands its @data to @func on another CPU
too, but is left alone: callers pass integers in it as well as
pointers, one of them a value from the hypercall's arguments
(XENPF_change_freq's frequency), which no test of the value alone can
tell from a stack address.

call_rcu() is left alone as well.  It queues the head on this CPU's
lists, which another CPU takes over only when this one goes offline
(rcu_offline_cpu()).  The one head in the tree on a stack,
rcu_barrier()'s, is safe all the same: its callback runs on this CPU
before rcu_barrier_action() returns, and rcu_barrier() holds
get_cpu_maps() throughout, so no CPU goes offline meanwhile.  Any other
caller would have to do the same, or the callback would run after its
frame was gone.

All are debug-build checks of a few instructions each.  Most of the
sites set an object up once; on_selected_cpus() checks each
call.  A stack address inside a by-value payload, or in a
structure none of these sees, is not caught.

Assisted-by: Claude Code:claude-opus-5-5
Signed-off-by: George Dunlap <gwd@xxxxxxxxxxxxxx>
---
Changes in v8:
- New in this version.

Changes since previously posted version ("x86: adventures in Address Space 
Isolation", v2):
- New: v2 instead mapped the calling CPU's stack around each
  call-function handler (arch_smp_pre_callfunc() and
  arch_smp_post_callfunc()).
---
 tools/tests/vpci/emul.h        |  2 ++
 xen/arch/x86/include/asm/smp.h | 12 ++++++++++++
 xen/arch/x86/irq.c             |  2 ++
 xen/common/notifier.c          |  3 +++
 xen/common/smp.c               |  2 +-
 xen/common/stop_machine.c      |  2 +-
 xen/common/tasklet.c           |  3 +++
 xen/common/timer.c             |  4 ++++
 xen/drivers/vpci/vpci.c        |  2 ++
 xen/include/xen/smp.h          | 17 +++++++++++++++++
 10 files changed, 47 insertions(+), 2 deletions(-)

diff --git a/tools/tests/vpci/emul.h b/tools/tests/vpci/emul.h
index 38211afbb8..4fd24fc9b7 100644
--- a/tools/tests/vpci/emul.h
+++ b/tools/tests/vpci/emul.h
@@ -116,6 +116,8 @@ typedef union {
 #define BUG() assert(0)
 #define ASSERT_UNREACHABLE() assert(0)
 
+#define ptr_on_a_cpu_stack(p) ((void)(p), false)
+
 #endif
 
 /*
diff --git a/xen/arch/x86/include/asm/smp.h b/xen/arch/x86/include/asm/smp.h
index ec243c229a..52d2ef11cc 100644
--- a/xen/arch/x86/include/asm/smp.h
+++ b/xen/arch/x86/include/asm/smp.h
@@ -85,6 +85,18 @@ extern bool cpu_stack_enabled;
 #define cpu_stack_enabled false
 #endif
 
+/*
+ * With per-CPU stacks, does @p point into the per-CPU stack sub-area, which
+ * holds every CPU's stack and nothing else?
+ */
+static inline bool arch_ptr_on_cpu_stacks(const void *p)
+{
+    return cpu_stack_enabled &&
+           (unsigned long)p - PCPU_STACK_VIRT_START <
+           PCPU_STACK_VIRT_END - PCPU_STACK_VIRT_START;
+}
+#define arch_ptr_on_cpu_stacks arch_ptr_on_cpu_stacks
+
 struct page_info *cpu_alloc_stack(unsigned int cpu);
 
 /* Give the idle domain the per-domain area mapping every CPU's stack. */
diff --git a/xen/arch/x86/irq.c b/xen/arch/x86/irq.c
index 5d0ae02c15..c74406d39c 100644
--- a/xen/arch/x86/irq.c
+++ b/xen/arch/x86/irq.c
@@ -1107,6 +1107,8 @@ int __init setup_irq(unsigned int irq, unsigned int 
irqflags,
     unsigned long flags;
 
     ASSERT(irqflags == 0);
+    ASSERT(!ptr_on_a_cpu_stack(new));
+    ASSERT(!ptr_on_a_cpu_stack(new->dev_id));
 
     desc = irq_to_desc(irq);
  
diff --git a/xen/common/notifier.c b/xen/common/notifier.c
index 0f9aa0f93f..8b059dceef 100644
--- a/xen/common/notifier.c
+++ b/xen/common/notifier.c
@@ -9,6 +9,7 @@
 
 #include <xen/init.h>
 #include <xen/notifier.h>
+#include <xen/smp.h>
 
 /**
  * notifier_chain_register - Add notifier to a raw notifier chain
@@ -24,6 +25,8 @@ void __init notifier_chain_register(
     struct list_head *chain = &nh->head;
     struct notifier_block *nb;
 
+    ASSERT(!ptr_on_a_cpu_stack(n));
+
     while ( chain->next != &nh->head )
     {
         nb = list_entry(chain->next, struct notifier_block, chain);
diff --git a/xen/common/smp.c b/xen/common/smp.c
index ef068c014e..6b623ced9b 100644
--- a/xen/common/smp.c
+++ b/xen/common/smp.c
@@ -72,7 +72,7 @@ static void _on_selected_cpus(
      * CPU's stack need not be: pass such payloads by value (@size != 0), or
      * move the datum off the stack.
      */
-    ASSERT(size || !info || !ptr_on_current_stack(info));
+    ASSERT(size || !info || !ptr_on_a_cpu_stack(info));
 
     spin_lock(&call_lock);
 
diff --git a/xen/common/stop_machine.c b/xen/common/stop_machine.c
index e7cdc4ce5a..758d34a929 100644
--- a/xen/common/stop_machine.c
+++ b/xen/common/stop_machine.c
@@ -86,7 +86,7 @@ int stop_machine_run(int (*fn)(void *data), void *data, 
unsigned int cpu)
      * must not point into a CPU's stack, which other CPUs need not be able
      * to reach.
      */
-    ASSERT(!data || !ptr_on_current_stack(data));
+    ASSERT(!data || !ptr_on_a_cpu_stack(data));
 
     /* cpu_online_map must not change. */
     if ( !get_cpu_maps() )
diff --git a/xen/common/tasklet.c b/xen/common/tasklet.c
index 4c8d87a338..fd3c44aa53 100644
--- a/xen/common/tasklet.c
+++ b/xen/common/tasklet.c
@@ -201,6 +201,9 @@ static void migrate_tasklets_from_cpu(unsigned int cpu, 
struct list_head *list)
 
 void tasklet_init(struct tasklet *t, void (*func)(void *data), void *data)
 {
+    ASSERT(!ptr_on_a_cpu_stack(t));
+    ASSERT(!ptr_on_a_cpu_stack(data));
+
     memset(t, 0, sizeof(*t));
     INIT_LIST_HEAD(&t->list);
     t->scheduled_on = -1;
diff --git a/xen/common/timer.c b/xen/common/timer.c
index da0d069cc6..2cbd233b59 100644
--- a/xen/common/timer.c
+++ b/xen/common/timer.c
@@ -299,6 +299,10 @@ void init_timer(
     unsigned int  cpu)
 {
     unsigned long flags;
+
+    ASSERT(!ptr_on_a_cpu_stack(timer));
+    ASSERT(!ptr_on_a_cpu_stack(data));
+
     memset(timer, 0, sizeof(*timer));
     timer->function = function;
     timer->data = data;
diff --git a/xen/drivers/vpci/vpci.c b/xen/drivers/vpci/vpci.c
index 9e2c27e3a3..cf0b72cb95 100644
--- a/xen/drivers/vpci/vpci.c
+++ b/xen/drivers/vpci/vpci.c
@@ -271,6 +271,8 @@ int vpci_add_register_mask(struct vpci *vpci, vpci_read_t 
*read_handler,
     struct list_head *prev;
     struct vpci_register *r;
 
+    ASSERT(!ptr_on_a_cpu_stack(data));
+
     /* Some sanity checks. */
     if ( (size != 1 && size != 2 && size != 4) ||
          offset >= PCI_CFG_SPACE_EXP_SIZE || (offset & (size - 1)) ||
diff --git a/xen/include/xen/smp.h b/xen/include/xen/smp.h
index 4b9afa18d9..23aadc8102 100644
--- a/xen/include/xen/smp.h
+++ b/xen/include/xen/smp.h
@@ -142,6 +142,23 @@ static inline bool ptr_on_current_stack(const void *p)
     return !(((unsigned long)p ^ here) & ~(STACK_SIZE - 1));
 }
 
+/*
+ * Does @p point into a CPU's stack, as far as its address alone can tell:
+ * into any CPU's where the architecture keeps every CPU's stack in an area of
+ * its own, and in any case into the stack we are running on, which need not
+ * lie in that area (x86's BSP runs on cpu0_stack until the end of
+ * __start_xen()).  For checks on data handed to other CPUs.
+ */
+static inline bool ptr_on_a_cpu_stack(const void *p)
+{
+#ifdef arch_ptr_on_cpu_stacks
+    if ( arch_ptr_on_cpu_stacks(p) )
+        return true;
+#endif
+
+    return ptr_on_current_stack(p);
+}
+
 /*
  * Call a function on the current CPU
  */
-- 
2.56.0




 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.