UBSAN fixes
v3 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/2573666715
---
Changes in v3:
- Reorder patches in the way that ready to be merged go first.
- Merged to upstream/staging:
xen/riscv: fix switch_stack_and_jump() for range beyond 1M
- Add R-by for patch "libfdt: fix UBSAN null pointer in fdt_property()".
- Address the comments from ML.
---
Changes in v2:
- Address comments from ML.
---
Oleksii Kurochko (2):
libfdt: fix UBSAN null pointer in fdt_property()
xen: introduce CONFIG_HAS_SHARED_INFO for archs without a shared page
xen/arch/arm/Kconfig | 1 + xen/arch/x86/Kconfig | 1 + xen/common/Kconfig | 3 +++ xen/common/Makefile | 2 +- xen/common/domain.c | 6 +++--- xen/common/domctl.c | 11 ++++++++--- xen/common/event_channel.c | 14 +++++++++++--- xen/common/event_channel.h | 6 ++++++ xen/common/event_fifo.c | 11 ++++++++++- xen/common/libfdt/fdt_sw.c | 3 ++- xen/common/time.c | 2 ++ xen/include/xen/shared.h | 7 +++++++ xen/include/xen/time.h | 4 ++++ 13 files changed, 59 insertions(+), 12 deletions(-)
Signature · 1 line
2.54.0
PATCH v3 1/2 libfdt: fix UBSAN null pointer in fdt_property()
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
---
Changes in v3:
- Add Reviewed-by: Michal Orzel <[email protected]>.
- Add Release-Acked-by.
---
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
@@ -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
PATCH v3 2/2 xen: introduce CONFIG_HAS_SHARED_INFO for archs without a shared page
In reply to Oleksii KurochkoOn architectures that run guests in dom0less mode without the PV ABI (currently RISC-V), no shared_info page is allocated and d->shared_info remains NULL throughout the domain lifetime. Several places in common code access d->shared_info through the shared_info() macro or directly, causing UBSAN null-pointer errors on such architectures.
Rather than adding runtime NULL guards that are logically unreachable on x86 and Arm (where shared_info is always allocated), introduce a new Kconfig symbol CONFIG_HAS_SHARED_INFO selected by x86 and Arm.
On !HAS_SHARED_INFO the shared_info() macro expands to a dereference of a pointer returned by shared_info_absent(), which is declared but intentionally never defined. Any use of shared_info() that is not dead-code-eliminated will therefore cause a link-time failure, making missed guards impossible to overlook.
The 2L event-channel ops call shared_info() and must not be compiled on architectures without a shared_info page, so event_2l.o is gated on CONFIG_HAS_SHARED_INFO. On such architectures evtchn_init() installs the FIFO ops as a placeholder instead; evtchn_fifo_word_from_port() is guarded against uninitialised d->evtchn_fifo so the FIFO ops are safe before evtchn_fifo_init_control() is called by the guest.
---
With having in Xen:
info->shared_info_frame = INVALID_GFN_RAW;
probably we want to add at least ASSERT() in tools/libs/guest code as xc_map_foreign_range() doesn't check if mfn arguemnt is correct or not.
---
Changes in v3:
- Introduce CONFIG_HAS_SHARED_INFO Kconfig symbol selected by x86 and Arm; RISC-V does not select it.
- Gate shared_info() macro on CONFIG_HAS_SHARED_INFO; on !HAS_SHARED_INFO it calls shared_info_absent() (declared, never defined) so any unguarded use produces a link-time error.
- Replace runtime if (!d->shared_info) guards with IS_ENABLED() at call sites so both branches type-check and dead code is eliminated.
- Guard shared_info_frame assignment in domctl.c.
- Gate event_2l.o on CONFIG_HAS_SHARED_INFO; use FIFO ops as placeholder on !HAS_SHARED_INFO archs instead of dedicated stub ops; guard evtchn_fifo_word_from_port() against uninitialised d->evtchn_fifo.
- Add static inline stubs for evtchn_2l_init() (!HAS_SHARED_INFO) and evtchn_fifo_init_ops() (!EVTCHN_FIFO) so call sites can use IS_ENABLED() without #ifdef.
- Drop inaccurate changelog entry about "only FIFO ABI" migration.
- Update the commit message.
- Drop R-by: Baptiste ... as some extra checks are added.
---
Changes in v2:
- Update commit message + subject.
- Drop Fixes tag.
--- xen/arch/arm/Kconfig | 1 + xen/arch/x86/Kconfig | 1 + xen/common/Kconfig | 3 +++ xen/common/Makefile | 2 +- xen/common/domain.c | 6 +++--- xen/common/domctl.c | 11 ++++++++--- xen/common/event_channel.c | 14 +++++++++++--- xen/common/event_channel.h | 6 ++++++ xen/common/event_fifo.c | 11 ++++++++++- xen/common/time.c | 2 ++ xen/include/xen/shared.h | 7 +++++++ xen/include/xen/time.h | 4 ++++ 12 files changed, 57 insertions(+), 11 deletions(-)
xen/arch/arm/Kconfig +1 -0
@@ -20,6 +20,7 @@ config ARM select HAS_DEVICE_TREE_DISCOVERY select HAS_DOM0LESS select HAS_GRANT_CACHE_FLUSH if GRANT_TABLE + select HAS_SHARED_INFO select HAS_STACK_PROTECTOR select HAS_UBSAN
xen/arch/x86/Kconfig +1 -0
@@ -29,6 +29,7 @@ config X86 select HAS_PCI_MSI select HAS_PIRQ select HAS_SCHED_GRANULARITY + select HAS_SHARED_INFO imply HAS_SOFT_RESET select HAS_UBSAN select HAS_VMAP
xen/common/Kconfig +3 -0
@@ -158,6 +158,9 @@ config HAS_PMAP config HAS_SCHED_GRANULARITY bool +config HAS_SHARED_INFO + bool + config HAS_SOFT_RESET bool
xen/common/Makefile +1 -1
@@ -12,7 +12,7 @@ obj-$(CONFIG_DEVICE_TREE_PARSE) += device-tree/ obj-$(CONFIG_IOREQ_SERVER) += dm.o obj-y += domain.o obj-y += domid.o -obj-y += event_2l.o +obj-$(CONFIG_HAS_SHARED_INFO) += event_2l.o obj-y += event_channel.o obj-$(CONFIG_EVTCHN_FIFO) += event_fifo.o obj-$(CONFIG_GRANT_TABLE) += grant_table.o
xen/common/domain.c +3 -3
@@ -320,9 +320,9 @@ void vcpu_info_reset(struct vcpu *v)struct domain *d = v->domain; v->vcpu_info_area.map = - ((v->vcpu_id <XEN_LEGACY_MAX_VCPUS ) - ? (vcpu_info_t *)&shared_info (d, vcpu_info[v->vcpu_id]) - : &dummy_vcpu_info); +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; }static struct domain *alloc_domain_struct (void )
xen/common/domctl.c +8 -3
@@ -104,9 +104,14 @@ void getdomaininfo(struct domain *d, struct xen_domctl_getdomaininfo *info)#ifdef CONFIG_MEM_PAGING info->paged_pages =atomic_read (&d->paged_pages);#endif - 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)); +if (IS_ENABLED (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 ; info->cpupool =cpupool_get_id (d);
xen/common/event_channel.c +11 -3
@@ -1323,9 +1323,13 @@ int evtchn_reset(struct domain *d, bool resuming) rc = -EAGAIN ; else if ( d->evtchn_fifo ) { -/* Switching back to 2-level ABI. */ evtchn_fifo_destroy (d); -evtchn_2l_init (d); + +if (IS_ENABLED (CONFIG_HAS_SHARED_INFO ) ) +/* Switching back to 2-level ABI. */ +evtchn_2l_init (d); +else +evtchn_fifo_init_ops (d); }write_unlock (&d->event_lock);
@@ -1624,7 +1628,11 @@ void evtchn_check_pollers(struct domain *d, unsigned int port)int evtchn_init (struct domain *d,unsigned int max_port) { -evtchn_2l_init (d); +if (IS_ENABLED (CONFIG_HAS_SHARED_INFO ) ) +evtchn_2l_init (d); +else +evtchn_fifo_init_ops (d); + d->max_evtchn_port = min_t(unsigned int, max_port, INT_MAX); d->evtchn =alloc_evtchn_bucket (d,0 );
xen/common/event_channel.h +6 -0
@@ -44,7 +44,11 @@ static inline void evtchn_port_print_state(struct domain *d,/* 2-level */ +#ifdef CONFIG_HAS_SHARED_INFO void evtchn_2l_init (struct domain *d); +#else +static inline void evtchn_2l_init (struct domain *d) {} +#endif /* FIFO */
@@ -55,6 +59,7 @@ struct evtchn_expand_array;int evtchn_fifo_init_control (struct evtchn_init_control *init_control);int evtchn_fifo_expand_array (const struct evtchn_expand_array *expand_array);void evtchn_fifo_destroy (struct domain *d); +void evtchn_fifo_init_ops (struct domain *d);#else static inline int evtchn_fifo_init_control (struct evtchn_init_control *init_control) {
@@ -68,6 +73,7 @@ static inline void evtchn_fifo_destroy(struct domain *d) {return ; } +static inline void evtchn_fifo_init_ops (struct domain *d) {}#endif /* CONFIG_EVTCHN_FIFO */ #endif /* EVENT_CHANNEL_H */
xen/common/event_fifo.c +10 -1
@@ -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 ;
@@ -420,6 +423,11 @@ static const struct evtchn_port_ops evtchn_port_ops_fifo = .print_state = evtchn_fifo_print_state, }; +void evtchn_fifo_init_ops (struct domain *d) +{ + d->evtchn_port_ops = &evtchn_port_ops_fifo; +} +static int map_guest_page (struct domain *d,uint64_t gfn,void **virt) {struct page_info *p;
@@ -562,7 +570,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 (IS_ENABLED (CONFIG_HAS_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 +2 -0
@@ -89,6 +89,7 @@ struct tm gmtime(unsigned long t)return tbuf; } +#ifdef CONFIG_HAS_SHARED_INFO void update_domain_wallclock_time (struct domain *d) {uint32_t *wc_version;
@@ -117,6 +118,7 @@ void update_domain_wallclock_time(struct domain *d) spin_unlock(&wc_lock); } +#endif /* CONFIG_HAS_SHARED_INFO */ /* Set clock to <secs,usecs> after 00:00:00 UTC, 1 January, 1970. */ void do_settime (u64 secs,unsigned int nsecs,u64 system_time_base)
xen/include/xen/shared.h +7 -0
@@ -43,7 +43,14 @@ typedef struct vcpu_info vcpu_info_t;extern vcpu_info_t dummy_vcpu_info; +#ifdef CONFIG_HAS_SHARED_INFO #define shared_info (d, field) __shared_info(d, (d)->shared_info, field) +#else +void *shared_info_absent (void ); +#define shared_info (d, field) \ + (*(typeof(__shared_info(d, (d)->shared_info, field)) *)shared_info_absent()) +#endif /* CONFIG_HAS_SHARED_INFO */ +#define vcpu_info (v, field) \ __vcpu_info(v, (vcpu_info_t *)(v)->vcpu_info_area.map, field)
xen/include/xen/time.h +4 -0
@@ -66,7 +66,11 @@ struct tm wallclock_time(uint64_t *ns);#define version_update_begin (v) (((v) + 1) | 1)#define version_update_end (v) ((v) + 1)extern void update_vcpu_system_time (struct vcpu *v); +#ifdef CONFIG_HAS_SHARED_INFO extern void update_domain_wallclock_time (struct domain *d); +#else +static inline void update_domain_wallclock_time (struct domain *d) {} +#endif extern void do_settime (u64 secs,unsigned int nsecs,u64 system_time_base);
Signature · 1 line
2.54.0
Re: [PATCH v3 2/2] xen: introduce CONFIG_HAS_SHARED_INFO for archs without a shared page
xen/common/domain.c void vcpu_info_reset(struct vcpu *v) · line 326
Quoted 13 lines · Oleksii Kurochko
xen/common/domain.c +3
-3@@ -320,9 +320,9 @@ void vcpu_info_reset(struct vcpu *v)struct domain *d = v->domain; v->vcpu_info_area.map = - ((v->vcpu_id <XEN_LEGACY_MAX_VCPUS ) - ? (vcpu_info_t *)&shared_info (d, vcpu_info[v->vcpu_id]) - : &dummy_vcpu_info); +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; }
While the change here is likely okay, it points at possible further omissions. You've dealt with all uses of shared_info(), but you've left alone all uses of vcpu_info() (and __vcpu_info()). Reads are presumably okay, but writes to dummy_vcpu_info open a side channel for possible info leaks. Looking over uses in common code, no code changes may be needed; extending the description may be all that's wanted here.
Quoted 6 lines
xen/common/event_channel.c +0
-0@@ -1323,9 +1323,13 @@ int evtchn_reset(struct domain *d, bool resuming) rc = -EAGAIN ; else if ( d->evtchn_fifo ) {
xen/common/event_channel.c int evtchn_reset(struct domain *d, bool resuming) · line 1333
Quoted 10 lines
xen/common/event_channel.c +6
-2-/* Switching back to 2-level ABI. */ evtchn_fifo_destroy (d); -evtchn_2l_init (d); + +if (IS_ENABLED (CONFIG_HAS_SHARED_INFO ) ) +/* Switching back to 2-level ABI. */ +evtchn_2l_init (d); +else +evtchn_fifo_init_ops (d); }
The "else" part isn't needed, is it? evtchn_fifo_destroy() doesn't undo what ...
xen/common/event_channel.c void evtchn_check_pollers(struct domain *d, unsigned int port) · line 1634
Quoted 9 lines
xen/common/event_channel.c +4
-1@@ -1624,7 +1628,11 @@ void evtchn_check_pollers(struct domain *d, unsigned int port)int evtchn_init (struct domain *d,unsigned int max_port) { -evtchn_2l_init (d); +if (IS_ENABLED (CONFIG_HAS_SHARED_INFO ) ) +evtchn_2l_init (d); +else +evtchn_fifo_init_ops (d);
... was done here.
xen/include/xen/shared.h typedef struct vcpu_info vcpu_info_t; · line 49
Quoted 10 lines
xen/include/xen/shared.h +3
-0@@ -43,7 +43,14 @@ typedef struct vcpu_info vcpu_info_t;extern vcpu_info_t dummy_vcpu_info; +#ifdef CONFIG_HAS_SHARED_INFO #define shared_info (d, field) __shared_info(d, (d)->shared_info, field) +#else +void *shared_info_absent (void );
Nit: Stray blank after '*'.
xen/include/xen/shared.h typedef struct vcpu_info vcpu_info_t; · line 51
Quoted 2 lines
xen/include/xen/shared.h +2
-0+#define shared_info (d, field) \ + (*(typeof(__shared_info(d, (d)->shared_info, field)) *)shared_info_absent())
How about the simpler
extern struct shared_info *shared_info_absent; #define shared_info(d, field) (shared_info_absent->field)
?
Jan
Re: [PATCH v3 2/2] xen: introduce CONFIG_HAS_SHARED_INFO for archs without a shared page
Quoted 6 lines · Jan Beulich
Quoted 5 lines · Oleksii Kurochko
xen/common/domain.c +0
-0@@ -320,9 +320,9 @@ void vcpu_info_reset(struct vcpu *v)struct domain *d = v->domain;
Quoted 15 lines
xen/common/domain.c void vcpu_info_reset(struct vcpu *v) · line 326
Quoted 8 lines
xen/common/domain.c +3
-3v->vcpu_info_area.map = - ((v->vcpu_id <XEN_LEGACY_MAX_VCPUS ) - ? (vcpu_info_t *)&shared_info (d, vcpu_info[v->vcpu_id]) - : &dummy_vcpu_info); +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; }While the change here is likely okay, it points at possible further omissions. You've dealt with all uses of
shared_info(), but you've left alone all uses ofvcpu_info()(and__vcpu_info()). Reads are presumably okay, but writes todummy_vcpu_infoopen a side channel for possible info leaks. Looking over uses in common code, no code changes may be needed; extending the description may be all that's wanted here.
Isn't there already a side channel that could allow leaks, even without this change? The change here just made it worsen because now info leak will happen for all vCPUs when CONFIG_HAS_SHARED_INFO=n.
I will add to the description the following:
``` With CONFIG_HAS_SHARED_INFO=n all vCPUs fall back to the global dummy_vcpu_info, so writes through vcpu_info() could leak data between vCPUs. Reviewing the write paths in common code: the write in map_guest_area() stores the constant ~0 so nothing serious will happen if it will be leaked; the event_2l.c paths are unreachable because the preceding shared_info() call would trap first; the write in vcpu_info_populate() targets the new mapping buffer, not dummy_vcpu_info; all remaining writes are x86 PV-specific for which CONFIG_HAS_SHARED_INFO=y. No code changes are needed. ```
Quoted 12 lines
Quoted 12 lines
xen/common/event_channel.c +2
-2@@ -1323,9 +1323,13 @@ int evtchn_reset(struct domain *d, bool resuming) rc = -EAGAIN ; else if ( d->evtchn_fifo ) { -/* Switching back to 2-level ABI. */ evtchn_fifo_destroy (d); -evtchn_2l_init (d); + +if (IS_ENABLED (CONFIG_HAS_SHARED_INFO ) )
Quoted 8 lines
xen/common/event_channel.c int evtchn_reset(struct domain *d, bool resuming) · line 1333
Quoted 5 lines
xen/common/event_channel.c +4
-0+/* Switching back to 2-level ABI. */ +evtchn_2l_init (d); + else +evtchn_fifo_init_ops (d ); }The "else" part isn't needed, is it?
evtchn_fifo_destroy()doesn't undo what ...
Agree, it isn't really needed. I will drop "else" part.
Quoted 29 lines
xen/common/event_channel.c void evtchn_check_pollers(struct domain *d, unsigned int port) · line 1634
Quoted 10 lines
xen/common/event_channel.c +4
-1@@ -1624,7 +1628,11 @@ void evtchn_check_pollers(struct domain *d, unsigned int port)int evtchn_init (struct domain *d,unsigned int max_port) { -evtchn_2l_init (d); +if (IS_ENABLED (CONFIG_HAS_SHARED_INFO ) ) +evtchn_2l_init (d); +else +evtchn_fifo_init_ops (d);... was done here.
xen/include/xen/shared.h typedef struct vcpu_info vcpu_info_t; · line 49
Quoted 10 lines
xen/include/xen/shared.h +3
-0@@ -43,7 +43,14 @@ typedef struct vcpu_info vcpu_info_t;extern vcpu_info_t dummy_vcpu_info; +#ifdef CONFIG_HAS_SHARED_INFO #define shared_info (d, field) __shared_info(d, (d)->shared_info, field) +#else +void *shared_info_absent (void );Nit: Stray blank after '*'.
xen/include/xen/shared.h typedef struct vcpu_info vcpu_info_t; · line 51
Quoted 2 lines
xen/include/xen/shared.h +2
-0+#define shared_info (d, field) \ + (*(typeof(__shared_info(d, (d)->shared_info, field)) *)shared_info_absent())
Quoted 6 lines
How about the simpler
extern struct
shared_info*shared_info_absent; #defineshared_info(d, field)(shared_info_absent->field)?
It would be better. I will apply that.
Thanks.
~ Oleksii
Re: [PATCH v3 2/2] xen: introduce CONFIG_HAS_SHARED_INFO for archs without a shared page
Quoted 23 lines · Oleksii Kurochko
Quoted 6 lines · Jan Beulich
Quoted 5 lines · Oleksii Kurochko
xen/common/domain.c +0
-0@@ -320,9 +320,9 @@ void vcpu_info_reset(struct vcpu *v)struct domain *d = v->domain;Quoted 15 lines
xen/common/domain.c void vcpu_info_reset(struct vcpu *v) · line 326
Quoted 8 lines
xen/common/domain.c +3
-3v->vcpu_info_area.map = - ((v->vcpu_id <XEN_LEGACY_MAX_VCPUS ) - ? (vcpu_info_t *)&shared_info (d, vcpu_info[v->vcpu_id]) - : &dummy_vcpu_info); +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; }While the change here is likely okay, it points at possible further omissions. You've dealt with all uses of
shared_info(), but you've left alone all uses ofvcpu_info()(and__vcpu_info()). Reads are presumably okay, but writes todummy_vcpu_infoopen a side channel for possible info leaks. Looking over uses in common code, no code changes may be needed; extending the description may be all that's wanted here.
Quoted 2 lines
Isn't there already a side channel that could allow leaks, even without this change?
There are multiple aspects here. First, for PV secondary vCPU-s cannot be launched when their vcpu-info still points at dummy_vcpu_info. HVM vCPU-s make very limited use of vcpu-info fields. Writes look to be limited to the evtchn_upcall_{mask,pending} fields, which isn't really an info leak.
My main point here is: None of this goes without making clear that the necessary auditing was properly done.
Quoted 4 lines
The change here just made it worsen because now info leak will happen for all vCPUs when
CONFIG_HAS_SHARED_INFO=n.I will add to the description the following:
Quoted 11 lines
``` With CONFIG_HAS_SHARED_INFO=n all vCPUs fall back to the global dummy_vcpu_info, so writes through vcpu_info() could leak data between vCPUs. Reviewing the write paths in common code: the write in map_guest_area() stores the constant ~0 so nothing serious will happen if it will be leaked; the event_2l.c paths are unreachable because the preceding shared_info() call would trap first; the write in vcpu_info_populate() targets the new mapping buffer, not dummy_vcpu_info; all remaining writes are x86 PV-specific for which CONFIG_HAS_SHARED_INFO=y. No code changes are needed. ```
As you start with "common code", how come the "x86 PV-specific" part is still there (i.e. relevant)? Isn't all PV stuff in x86-specific code?
Jan
Re: [PATCH v3 2/2] xen: introduce CONFIG_HAS_SHARED_INFO for archs without a shared page
Quoted 51 lines · Jan Beulich
Quoted 23 lines · Oleksii Kurochko
Quoted 6 lines · Jan Beulich
Quoted 5 lines · Oleksii Kurochko
xen/common/domain.c +0
-0@@ -320,9 +320,9 @@ void vcpu_info_reset(struct vcpu *v)struct domain *d = v->domain;Quoted 15 lines
xen/common/domain.c void vcpu_info_reset(struct vcpu *v) · line 326
Quoted 8 lines
xen/common/domain.c +3
-3v->vcpu_info_area.map = - ((v->vcpu_id <XEN_LEGACY_MAX_VCPUS ) - ? (vcpu_info_t *)&shared_info (d, vcpu_info[v->vcpu_id]) - : &dummy_vcpu_info); +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; }While the change here is likely okay, it points at possible further omissions. You've dealt with all uses of
shared_info(), but you've left alone all uses ofvcpu_info()(and__vcpu_info()). Reads are presumably okay, but writes todummy_vcpu_infoopen a side channel for possible info leaks. Looking over uses in common code, no code changes may be needed; extending the description may be all that's wanted here.Quoted 2 lines
Isn't there already a side channel that could allow leaks, even without this change?
There are multiple aspects here. First, for PV secondary vCPU-s cannot be launched when their vcpu-info still points at
dummy_vcpu_info. HVM vCPU-s make very limited use of vcpu-info fields. Writes look to be limited to the evtchn_upcall_{mask,pending} fields, which isn't really an info leak.My main point here is: None of this goes without making clear that the necessary auditing was properly done.
Quoted 4 lines
The change here just made it worsen because now info leak will happen for all vCPUs when
CONFIG_HAS_SHARED_INFO=n.I will add to the description the following:
Quoted 11 lines
``` With CONFIG_HAS_SHARED_INFO=n all vCPUs fall back to the global dummy_vcpu_info, so writes through vcpu_info() could leak data between vCPUs. Reviewing the write paths in common code: the write in map_guest_area() stores the constant ~0 so nothing serious will happen if it will be leaked; the event_2l.c paths are unreachable because the preceding shared_info() call would trap first; the write in vcpu_info_populate() targets the new mapping buffer, not dummy_vcpu_info; all remaining writes are x86 PV-specific for which CONFIG_HAS_SHARED_INFO=y. No code changes are needed. ```
Quoted 2 lines
As you start with "common code", how come the "x86 PV-specific" part is still there (i.e. relevant)? Isn't all PV stuff in x86-specific code?
Good point. The "x86 PV-specific" part is not part of the review of common code. I mentioned it separately to complete the audit of all write paths reachable through vcpu_info(). The intent was:
* the writes discussed before the semicolon are the common-code paths;
* the writes after the semicolon are outside common code and are x86 PV-specific, where CONFIG_HAS_SHARED_INFO=y anyway.
To avoid the ambiguity, I can reword the sentence to make that separation explicit:
``` With CONFIG_HAS_SHARED_INFO=n all vCPUs fall back to the global dummy_vcpu_info, so writes through vcpu_info() could leak data between vCPUs. Reviewing the write paths in common code: the write in map_guest_area() stores the constant ~0 so nothing serious would happen if it were leaked; the event_2l.c paths are unreachable because the preceding shared_info() call would trap first; the write in vcpu_info_populate() targets the new mapping buffer, not dummy_vcpu_info. Outside common code, the remaining writes are x86 PV-specific, for which CONFIG_HAS_SHARED_INFO=y. No code changes are needed. ```
Would that wording work for you?
Thanks.
~ Oleksii
Re: [PATCH v3 2/2] xen: introduce CONFIG_HAS_SHARED_INFO for archs without a shared page
Quoted 81 lines · Oleksii Kurochko
Quoted 51 lines · Jan Beulich
Quoted 23 lines · Oleksii Kurochko
Quoted 6 lines · Jan Beulich
Quoted 5 lines · Oleksii Kurochko
xen/common/domain.c +0
-0@@ -320,9 +320,9 @@ void vcpu_info_reset(struct vcpu *v)struct domain *d = v->domain;Quoted 15 lines
xen/common/domain.c void vcpu_info_reset(struct vcpu *v) · line 326
Quoted 8 lines
xen/common/domain.c +3
-3v->vcpu_info_area.map = - ((v->vcpu_id <XEN_LEGACY_MAX_VCPUS ) - ? (vcpu_info_t *)&shared_info (d, vcpu_info[v->vcpu_id]) - : &dummy_vcpu_info); +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; }While the change here is likely okay, it points at possible further omissions. You've dealt with all uses of
shared_info(), but you've left alone all uses ofvcpu_info()(and__vcpu_info()). Reads are presumably okay, but writes todummy_vcpu_infoopen a side channel for possible info leaks. Looking over uses in common code, no code changes may be needed; extending the description may be all that's wanted here.Quoted 2 lines
Isn't there already a side channel that could allow leaks, even without this change?
There are multiple aspects here. First, for PV secondary vCPU-s cannot be launched when their vcpu-info still points at
dummy_vcpu_info. HVM vCPU-s make very limited use of vcpu-info fields. Writes look to be limited to the evtchn_upcall_{mask,pending} fields, which isn't really an info leak.My main point here is: None of this goes without making clear that the necessary auditing was properly done.
Quoted 4 lines
The change here just made it worsen because now info leak will happen for all vCPUs when
CONFIG_HAS_SHARED_INFO=n.I will add to the description the following:
Quoted 11 lines
``` With CONFIG_HAS_SHARED_INFO=n all vCPUs fall back to the global dummy_vcpu_info, so writes through vcpu_info() could leak data between vCPUs. Reviewing the write paths in common code: the write in map_guest_area() stores the constant ~0 so nothing serious will happen if it will be leaked; the event_2l.c paths are unreachable because the preceding shared_info() call would trap first; the write in vcpu_info_populate() targets the new mapping buffer, not dummy_vcpu_info; all remaining writes are x86 PV-specific for which CONFIG_HAS_SHARED_INFO=y. No code changes are needed. ```Quoted 2 lines
As you start with "common code", how come the "x86 PV-specific" part is still there (i.e. relevant)? Isn't all PV stuff in x86-specific code?
Good point. The "x86 PV-specific" part is not part of the review of common code. I mentioned it separately to complete the audit of all write paths reachable through
vcpu_info(). The intent was:* the writes discussed before the semicolon are the common-code paths;
* the writes after the semicolon are outside common code and are x86 PV-specific, whereCONFIG_HAS_SHARED_INFO=yanyway.To avoid the ambiguity, I can reword the sentence to make that separation explicit:
``` With CONFIG_HAS_SHARED_INFO=n all vCPUs fall back to the global dummy_vcpu_info, so writes through vcpu_info() could leak data between vCPUs. Reviewing the write paths in common code: the write in map_guest_area() stores the constant ~0 so nothing serious would happen if it were leaked; the event_2l.c paths are unreachable because the preceding shared_info() call would trap first; the write in vcpu_info_populate() targets the new mapping buffer, not dummy_vcpu_info. Outside common code, the remaining writes are x86 PV-specific, for which CONFIG_HAS_SHARED_INFO=y. No code changes are needed. ```
Quoted 1 line
Would that wording work for you?
I think so, yes.
Jan
Re: [PATCH v3 2/2] xen: introduce CONFIG_HAS_SHARED_INFO for archs without a shared page
In reply to Jan BeulichQuoted 8 lines · Jan Beulich
Review
Quoted 2 lines
(quoted diff) +2
-0+#define shared_info(d, field) \ + (*(typeof(__shared_info(d, (d)->shared_info, field)) *)shared_info_absent())How about the simpler
extern struct
shared_info*shared_info_absent; #defineshared_info(d, field)(shared_info_absent->field)?
This could lead to compilation error:
common/domain.c: In function 'vcpu_info_reset':common/domain.c:316:20: error: unused variable 'd' [-Werror=unused-variable]
316 | struct domain *d = v->domain;
| ^
cc1: all warnings being treated as errors
One of fixes could be just drop usage of local variable d in vcpu_info_reset():
xen/common/domain.c +0 -2
@@ -313,11 +313,9 @@ static void vcpu_check_shutdown(struct vcpu *v)void vcpu_info_reset (struct vcpu *v) { -struct domain *d = v->domain; - 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])
+ ? (vcpu_info_t *)&shared_info(v->domain, vcpu_info[v->vcpu_id])
: &dummy_vcpu_info;
}
OR shared_info() defintion should be updated to:
#define shared_info(d, field) (*((void)(d), &shared_info_absent->field))
IMO, an update of macros defintion looks a little bit more common.
Which one do you prefer? Any better suggestion?
Thanks.
~ oleksii
Re: [PATCH v3 2/2] xen: introduce CONFIG_HAS_SHARED_INFO for archs without a shared page
Quoted 10 lines · Oleksii Kurochko
Quoted 8 lines · Jan Beulich
Review
Quoted 2 lines
(quoted diff) +2
-0+#define shared_info(d, field) \ + (*(typeof(__shared_info(d, (d)->shared_info, field)) *)shared_info_absent())How about the simpler
extern struct
shared_info*shared_info_absent; #defineshared_info(d, field)(shared_info_absent->field)?
Quoted 7 lines
This could lead to compilation error:
common/domain.c: In function 'vcpu_info_reset':common/domain.c:316:20: error: unused variable 'd' [-Werror=unused-variable]
316 | struct domain *d =v->domain;
| ^
cc1: all warnings being treated as errors
#define shared_info(d, field) ((void)(d), shared_info_absent->field)
Which it should have been anyway, to make sure the first argument is evaluated.
Jan
Re: [PATCH v3 2/2] xen: introduce CONFIG_HAS_SHARED_INFO for archs without a shared page
Quoted 19 lines · Jan Beulich
Quoted 10 lines · Oleksii Kurochko
Quoted 8 lines · Jan Beulich
Review
Quoted 2 lines
(quoted diff) +2
-0+#define shared_info(d, field) \ + (*(typeof(__shared_info(d, (d)->shared_info, field)) *)shared_info_absent())How about the simpler
extern struct
shared_info*shared_info_absent; #defineshared_info(d, field)(shared_info_absent->field)?
Quoted 7 lines
This could lead to compilation error:
common/domain.c: In function 'vcpu_info_reset':common/domain.c:316:20: error: unused variable 'd' [-Werror=unused-variable]316 | struct domain *d = v->domain; | ^cc1: all warnings being treated as errors
Quoted 3 lines
#define
shared_info(d, field)((void)(d),shared_info_absent->field)Which it should have been anyway, to make sure the first argument is evaluated.
I expected that as shared_info() is used as an lvalue whose address is taken (&shared_info(d, vcpu_info[...])). To "consume" d we can't just prepend a comma expression as &(comma_expr) is illegal since a comma result isn't an lvalue.
Trying what you suggested leads to:common/domain.c: In function 'vcpu_info_reset':common/domain.c:320:26: error: lvalue required as unary '&' operand
320 | ? (vcpu_info_t *)&shared_info(d, vcpu_info[v->vcpu_id])
Thereby this option:
#define shared_info(d, field) (*((void)(d), &shared_info_absent->field))
will be better.
~ Oleksii
Re: [PATCH v3 2/2] xen: introduce CONFIG_HAS_SHARED_INFO for archs without a shared page
Quoted 31 lines · Oleksii Kurochko
Quoted 19 lines · Jan Beulich
Quoted 10 lines · Oleksii Kurochko
Quoted 8 lines · Jan Beulich
Review
Quoted 2 lines
(quoted diff) +2
-0+#define shared_info(d, field) \ + (*(typeof(__shared_info(d, (d)->shared_info, field)) *)shared_info_absent())How about the simpler
extern struct
shared_info*shared_info_absent; #defineshared_info(d, field)(shared_info_absent->field)?
Quoted 7 lines
This could lead to compilation error:
common/domain.c: In function 'vcpu_info_reset':common/domain.c:316:20: error: unused variable 'd' [-Werror=unused-variable]316 | struct domain *d = v->domain; | ^cc1: all warnings being treated as errors
Quoted 3 lines
#define
shared_info(d, field)((void)(d),shared_info_absent->field)Which it should have been anyway, to make sure the first argument is evaluated.
I expected that as
shared_info()is used as an lvalue whose address is taken (&shared_info(d, vcpu_info[...])). To "consume" d we can't just prepend a comma expression as &(comma_expr) is illegal since a comma result isn't an lvalue.
Quoted 4 lines
Trying what you suggested leads to:
common/domain.c: In function 'vcpu_info_reset':common/domain.c:320:26: error: lvalue required as unary '&' operand
320 | ? (vcpu_info_t*)&shared_info(d, vcpu_info[v->vcpu_id])
Ah yes.
Quoted 3 lines
Thereby this option:
#define shared_info(d, field) (*((void)(d), &shared_info_absent->field))will be better.
Or maybe
#define shared_info(d, field) (((void)(d), shared_info_absent)->field)
?
Jan
Re: [PATCH v3 2/2] xen: introduce CONFIG_HAS_SHARED_INFO for archs without a shared page
Quoted 43 lines · Jan Beulich
Quoted 31 lines · Oleksii Kurochko
Quoted 19 lines · Jan Beulich
Quoted 10 lines · Oleksii Kurochko
Quoted 8 lines · Jan Beulich
Review
Quoted 2 lines
(quoted diff) +2
-0+#define shared_info(d, field) \ + (*(typeof(__shared_info(d, (d)->shared_info, field)) *)shared_info_absent())How about the simpler
extern struct
shared_info*shared_info_absent; #defineshared_info(d, field)(shared_info_absent->field)?
Quoted 7 lines
This could lead to compilation error:
common/domain.c: In function 'vcpu_info_reset':common/domain.c:316:20: error: unused variable 'd' [-Werror=unused-variable]316 | struct domain *d = v->domain; | ^cc1: all warnings being treated as errors
Quoted 3 lines
#define
shared_info(d, field)((void)(d),shared_info_absent->field)Which it should have been anyway, to make sure the first argument is evaluated.
I expected that as
shared_info()is used as an lvalue whose address is taken (&shared_info(d, vcpu_info[...])). To "consume" d we can't just prepend a comma expression as &(comma_expr) is illegal since a comma result isn't an lvalue.Quoted 4 lines
Trying what you suggested leads to:
common/domain.c: In function 'vcpu_info_reset':common/domain.c:320:26: error: lvalue required as unary '&' operand320 | ? (vcpu_info_t *)&shared_info(d, vcpu_info[v->vcpu_id])Ah yes.
Quoted 3 lines
Thereby this option:
#define shared_info(d, field) (*((void)(d), &shared_info_absent->field))will be better.
Quoted 5 lines
Or maybe
#define
shared_info(d, field)(((void)(d), shared_info_absent)->field)?
This option looks better. Ill apply it.
Thanks.
~ Oleksii