xen/common: Avoid undefined behaviour by shifting into a sign bit
PATCH 1/3 xen/common: Avoid undefined behaviour by shifting into a sign bit
---
CC: Jan Beulich <[email protected]>
CC: George Dunlap <[email protected]>
CC: Konrad Rzeszutek Wilk <[email protected]>
CC: Stefano Stabellini <[email protected]>
CC: Tim Deegan <[email protected]>
--- xen/common/domctl.c | 2 +- xen/common/xmalloc_tlsf.c | 4 ++-- xen/include/xen/sched.h | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-)
xen/common/domctl.c +1 -1
@@ -188,7 +188,7 @@ void getdomaininfo(struct domain *d, struct xen_domctl_getdomaininfo *info) (d->controller_pause_count >0 ? XEN_DOMINF_paused :0 ) | (d->debugger_attached ? XEN_DOMINF_debugged :0 ) | (d->is_xenstore ? XEN_DOMINF_xs_domain :0 ) | - d->shutdown_code << XEN_DOMINF_shutdownshift; + (unsigned int )d->shutdown_code << XEN_DOMINF_shutdownshift;switch ( d->guest_type ) {
xen/common/xmalloc_tlsf.c +2 -2
@@ -177,7 +177,7 @@ static inline void MAPPING_INSERT(unsigned long r, int *fl, int *sl)static inline struct bhdr *FIND_SUITABLE_BLOCK (struct xmem_pool *p,int *fl,int *sl) { -u32 tmp = p->sl_bitmap[*fl] & (~0 << *sl); +u32 tmp = p->sl_bitmap[*fl] & (~0u << *sl);struct bhdr *b =NULL ;if ( tmp )
@@ -187,7 +187,7 @@ static inline struct bhdr *FIND_SUITABLE_BLOCK(struct xmem_pool *p, int *fl, }else { - *fl =ffs (p->fl_bitmap & (~0 << (*fl +1 ))) -1 ; + *fl =ffs (p->fl_bitmap & (~0u << (*fl +1 ))) -1 ;if (likely (*fl >0 ) ) { *sl =ffs (p->sl_bitmap[*fl]) -1 ;
xen/include/xen/sched.h +1 -1
@@ -483,7 +483,7 @@ extern struct vcpu *idle_vcpu[NR_CPUS];#define is_idle_domain (d) ((d)->domain_id == DOMID_IDLE)#define is_idle_vcpu (v) (is_idle_domain((v)->domain)) -#define DOMAIN_DESTROYED (1<<31)/* assumes atomic_t is >= 32 bits */ +#define DOMAIN_DESTROYED (1u << 31)/* assumes atomic_t is >= 32 bits */ #define put_domain (_d) \ if ( atomic_dec_and_test(&(_d)->refcnt) ) domain_destroy(_d)
Signature · 7 lines
2.1.4 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xen.org/xen-devel
PATCH 2/3 xen/x86: Avoid undefined behaviour by shifting into a sign bit
---
CC: Jan Beulich <[email protected]>
--- xen/arch/x86/apic.c | 2 +- xen/arch/x86/cpu/common.c | 2 +- xen/arch/x86/x86_64/traps.c | 2 +- xen/include/asm-x86/apicdef.h | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-)
xen/arch/x86/apic.c +1 -1
@@ -571,7 +571,7 @@ void setup_local_APIC(void)for (i =APIC_ISR_NR -1 ; i >=0 ; i--) { value =apic_read (APIC_ISR + i*0x10 );for (j =31 ; j >=0 ; j--) { -if (value & (1 <<j)) +if (value & (1u << j))ack_APIC_irq (); } }
xen/arch/x86/cpu/common.c +1 -1
@@ -476,7 +476,7 @@ void detect_extended_topology(struct cpuinfo_x86 *c) sub_index++; }while (LEAFB_SUBTYPE (ecx) !=INVALID_TYPE ); - core_select_mask = (~(-1 << core_plus_mask_width)) >> ht_mask_width; + core_select_mask = (~(-1u << core_plus_mask_width)) >> ht_mask_width; c->cpu_core_id =phys_pkg_id (initial_apicid, ht_mask_width) & core_select_mask;
xen/arch/x86/x86_64/traps.c +1 -1
@@ -414,7 +414,7 @@ void subarch_percpu_traps_init(void)unmap_domain_page (stub_page);/* Common SYSCALL parameters. */ -wrmsr (MSR_STAR ,0 , (FLAT_RING3_CS32 <<16 ) | __HYPERVISOR_CS); +wrmsr (MSR_STAR ,0 , ((unsigned )FLAT_RING3_CS32 <<16 ) | __HYPERVISOR_CS);wrmsr (MSR_SYSCALL_MASK ,XEN_SYSCALL_MASK ,0U ); }
xen/include/asm-x86/apicdef.h +1 -1
@@ -30,7 +30,7 @@#define APIC_EIO_ACK 0x0/* Write this to the EOI register */ #define APIC_RRR 0xC0#define APIC_LDR 0xD0 -#define APIC_LDR_MASK (0xFF<<24) +#define APIC_LDR_MASK (0xFFu<<24)#define GET_xAPIC_LOGICAL_ID (x) (((x)>>24)&0xFF)#define SET_xAPIC_LOGICAL_ID (x) (((x)<<24))#define APIC_ALL_CPUS 0xFF
Signature · 7 lines
2.1.4 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xen.org/xen-devel
Re: [PATCH 2/3] xen/x86: Avoid undefined behaviour by shifting into a sign bit
xen/arch/x86/cpu/common.c void detect_extended_topology(struct cpuinfo_x86 *c) · line 479
Quoted 9 lines
Quoted 1 line
Quoted 1 line · On 05.08.16 at 15:50, <[email protected]> wrote:
xen/arch/x86/cpu/common.c +1
-1@@ -476,7 +476,7 @@ void detect_extended_topology(struct cpuinfo_x86 *c) sub_index++; }while (LEAFB_SUBTYPE (ecx) !=INVALID_TYPE ); - core_select_mask = (~(-1 << core_plus_mask_width)) >> ht_mask_width; + core_select_mask = (~(-1u << core_plus_mask_width)) >> ht_mask_width;
-1u is kind of bogus; could I talk you into using ~0u instead?
With that
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
PATCH 3/3 x86/microcode: Avoid undefined behaviour from signed integer overflow
In reply to Andrew CooperThe checksum should be calculated using unsigned 32bit integers, as it is intended to overflow and end at 0.
---
CC: Jan Beulich <[email protected]>
CC: Kevin Tian <[email protected]>
CC: Jun Nakajima <[email protected]>
--- xen/arch/x86/microcode_intel.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
xen/arch/x86/microcode_intel.c +3 -2
@@ -143,7 +143,8 @@ static int microcode_sanity_check(void *mc)struct extended_sigtable *ext_header =NULL ;struct extended_signature *ext_sig;unsigned long total_size, data_size, ext_table_size; -int sum, orig_sum, ext_sigcount =0 , i; +uint32_t sum, orig_sum; +int ext_sigcount =0 , i; total_size =get_totalsize (mc_header); data_size =get_datasize (mc_header);
@@ -201,7 +202,7 @@ static int microcode_sanity_check(void *mc) orig_sum =0 ; i = (MC_HEADER_SIZE + data_size) /DWSIZE ;while ( i-- ) - orig_sum += ((int *)mc)[i]; + orig_sum += ((uint32_t *)mc)[i];if ( orig_sum ) {printk (KERN_ERR "microcode: aborting, bad checksum\n" );
Signature · 7 lines
2.1.4 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xen.org/xen-devel
Re: [PATCH 3/3] x86/microcode: Avoid undefined behaviour from signed integer overflow
xen/arch/x86/microcode_intel.c static int microcode_sanity_check(void *mc) · line 147
Quoted 10 lines
Quoted 1 line
Quoted 1 line · On 05.08.16 at 15:50, <[email protected]> wrote:
xen/arch/x86/microcode_intel.c +2
-1@@ -143,7 +143,8 @@ static int microcode_sanity_check(void *mc)struct extended_sigtable *ext_header =NULL ;struct extended_signature *ext_sig;unsigned long total_size, data_size, ext_table_size; -int sum, orig_sum, ext_sigcount =0 , i; +uint32_t sum, orig_sum; +int ext_sigcount =0 , i;
Pretty clearly these other two want to be unsigned int? If you agree,
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Re: [PATCH 3/3] x86/microcode: Avoid undefined behaviour from signed integer overflow
In reply to Andrew CooperQuoted 11 lines
From: Andrew Cooper [mailto:[email protected]]
Sent: Friday, August 05, 2016 9:50 PM
To: Xen-devel
Cc: Andrew Cooper; Jan Beulich; Tian, Kevin; Nakajima, Jun
Subject: [PATCH 3/3] x86/microcode: Avoid undefined behaviour from signed integer overflowThe checksum should be calculated using unsigned 32bit integers, as it is intended to overflow and end at 0.
Signed-off-by Andrew Cooper <[email protected]>
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
xen/common/domctl.c void getdomaininfo(struct domain *d, struct xen_domctl_getdomaininfo *info) · line 191
Quoted 9 lines
Quoted 1 line
Quoted 1 line · On 05.08.16 at 15:50, <[email protected]> wrote:
xen/common/domctl.c +1
-1@@ -188,7 +188,7 @@ void getdomaininfo(struct domain *d, struct xen_domctl_getdomaininfo *info) (d->controller_pause_count >0 ? XEN_DOMINF_paused :0 ) | (d->debugger_attached ? XEN_DOMINF_debugged :0 ) | (d->is_xenstore ? XEN_DOMINF_xs_domain :0 ) | - d->shutdown_code << XEN_DOMINF_shutdownshift; + (unsigned int )d->shutdown_code << XEN_DOMINF_shutdownshift;
Is adding a cast here really the most suitable fix? The only two places shutdown_code gets set (besides the -1 initialization) have their right side a u8. Nothing ever checks for the value being negative (there are just two -1 checks), so converting the field to u8 or unsigned int (and using a sentinel different from -1) should both work, avoiding the need for a cast.
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
George Dunlap
Reviewed-byQuoted 16 lines · Jan Beulich
xen/common/domctl.c void getdomaininfo(struct domain *d, struct xen_domctl_getdomaininfo *info) · line 191
Quoted 9 lines
Quoted 1 line
Quoted 1 line · On 05.08.16 at 15:50, <[email protected]> wrote:
xen/common/domctl.c +1
-1@@ -188,7 +188,7 @@ void getdomaininfo(struct domain *d, struct xen_domctl_getdomaininfo *info) (d->controller_pause_count >0 ? XEN_DOMINF_paused :0 ) | (d->debugger_attached ? XEN_DOMINF_debugged :0 ) | (d->is_xenstore ? XEN_DOMINF_xs_domain :0 ) | - d->shutdown_code << XEN_DOMINF_shutdownshift; + (unsigned int )d->shutdown_code << XEN_DOMINF_shutdownshift;Is adding a cast here really the most suitable fix? The only two places
shutdown_codegets set (besides the -1 initialization) have their right side a u8. Nothing ever checks for the value being negative (there are just two -1 checks), so converting the field to u8 or unsigned int (and using a sentinel different from -1) should both work, avoiding the need for a cast.
This seems sensible if possible.
The other bits:
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
PATCH 1/1 xen/common: Avoid undefined behaviour by shifting into a sign bit
In reply to Andrew CooperFor d->shutdown_code, change the field to being unsigned and using an unsigned sentinel. The sentinal needs to be distinguishable from any value representable in a u8.
---
CC: Jan Beulich <[email protected]>
CC: George Dunlap <[email protected]>
CC: Konrad Rzeszutek Wilk <[email protected]>
CC: Stefano Stabellini <[email protected]>
CC: Tim Deegan <[email protected]>
v2:
* Change d->shutdown_code to being unsigned.
--- xen/common/domain.c | 6 +++--- xen/common/schedule.c | 2 +- xen/common/xmalloc_tlsf.c | 4 ++-- xen/include/xen/sched.h | 5 +++-- 4 files changed, 9 insertions(+), 8 deletions(-)
xen/common/domain.c +3 -3
@@ -293,7 +293,7 @@ struct domain *domain_create(domid_t domid, unsigned int domcr_flags, d->auto_node_affinity =1 ;spin_lock_init (&d->shutdown_lock); - d->shutdown_code =-1 ; + d->shutdown_code =SHUTDOWN_CODE_INVALID ;spin_lock_init (&d->pbuf_lock);
@@ -695,7 +695,7 @@ void domain_shutdown(struct domain *d, u8 reason)spin_lock (&d->shutdown_lock); -if ( d->shutdown_code ==-1 ) +if ( d->shutdown_code ==SHUTDOWN_CODE_INVALID ) d->shutdown_code = reason; reason = d->shutdown_code;
@@ -742,7 +742,7 @@ void domain_resume(struct domain *d)spin_lock (&d->shutdown_lock); d->is_shutting_down = d->is_shut_down =0 ; - d->shutdown_code =-1 ; + d->shutdown_code =SHUTDOWN_CODE_INVALID ; for_each_vcpu ( d, v ) {
xen/common/schedule.c +1 -1
@@ -1120,7 +1120,7 @@ ret_t do_sched_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg) d->domain_id, current->vcpu_id, sched_shutdown.reason);spin_lock (&d->shutdown_lock); -if ( d->shutdown_code ==-1 ) +if ( d->shutdown_code ==SHUTDOWN_CODE_INVALID ) d->shutdown_code = (u8 )sched_shutdown.reason;spin_unlock (&d->shutdown_lock);
xen/common/xmalloc_tlsf.c +2 -2
@@ -177,7 +177,7 @@ static inline void MAPPING_INSERT(unsigned long r, int *fl, int *sl)static inline struct bhdr *FIND_SUITABLE_BLOCK (struct xmem_pool *p,int *fl,int *sl) { -u32 tmp = p->sl_bitmap[*fl] & (~0 << *sl); +u32 tmp = p->sl_bitmap[*fl] & (~0u << *sl);struct bhdr *b =NULL ;if ( tmp )
@@ -187,7 +187,7 @@ static inline struct bhdr *FIND_SUITABLE_BLOCK(struct xmem_pool *p, int *fl, }else { - *fl =ffs (p->fl_bitmap & (~0 << (*fl +1 ))) -1 ; + *fl =ffs (p->fl_bitmap & (~0u << (*fl +1 ))) -1 ;if (likely (*fl >0 ) ) { *sl =ffs (p->sl_bitmap[*fl]) -1 ;
xen/include/xen/sched.h +3 -2
@@ -404,7 +404,8 @@ struct domainspinlock_t shutdown_lock;bool_t is_shutting_down;/* in process of shutting down? */ bool_t is_shut_down;/* fully shut down? */ -int shutdown_code; +#define SHUTDOWN_CODE_INVALID ~0u +unsigned int shutdown_code;/* If this is not 0, send suspend notification here instead of * raising DOM_EXC */
@@ -483,7 +484,7 @@ extern struct vcpu *idle_vcpu[NR_CPUS];#define is_idle_domain (d) ((d)->domain_id == DOMID_IDLE)#define is_idle_vcpu (v) (is_idle_domain((v)->domain)) -#define DOMAIN_DESTROYED (1<<31)/* assumes atomic_t is >= 32 bits */ +#define DOMAIN_DESTROYED (1u << 31)/* assumes atomic_t is >= 32 bits */ #define put_domain (_d) \ if ( atomic_dec_and_test(&(_d)->refcnt) ) domain_destroy(_d)
Signature · 7 lines
2.1.4 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xen.org/xen-devel
PATCH 1/1 xen/x86: Avoid undefined behaviour by shifting into a sign bit
v2:
* Use ~0u as opposed to -1u
--- xen/arch/x86/apic.c | 2 +- xen/arch/x86/cpu/common.c | 2 +- xen/arch/x86/x86_64/traps.c | 2 +- xen/include/asm-x86/apicdef.h | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-)
xen/arch/x86/apic.c +1 -1
@@ -571,7 +571,7 @@ void setup_local_APIC(void)for (i =APIC_ISR_NR -1 ; i >=0 ; i--) { value =apic_read (APIC_ISR + i*0x10 );for (j =31 ; j >=0 ; j--) { -if (value & (1 <<j)) +if (value & (1u << j))ack_APIC_irq (); } }
xen/arch/x86/cpu/common.c +1 -1
@@ -476,7 +476,7 @@ void detect_extended_topology(struct cpuinfo_x86 *c) sub_index++; }while (LEAFB_SUBTYPE (ecx) !=INVALID_TYPE ); - core_select_mask = (~(-1 << core_plus_mask_width)) >> ht_mask_width; + core_select_mask = (~(~0u << core_plus_mask_width)) >> ht_mask_width; c->cpu_core_id =phys_pkg_id (initial_apicid, ht_mask_width) & core_select_mask;
xen/arch/x86/x86_64/traps.c +1 -1
@@ -414,7 +414,7 @@ void subarch_percpu_traps_init(void)unmap_domain_page (stub_page);/* Common SYSCALL parameters. */ -wrmsr (MSR_STAR ,0 , (FLAT_RING3_CS32 <<16 ) | __HYPERVISOR_CS); +wrmsr (MSR_STAR ,0 , ((unsigned int )FLAT_RING3_CS32 <<16 ) | __HYPERVISOR_CS);wrmsr (MSR_SYSCALL_MASK ,XEN_SYSCALL_MASK ,0U ); }
xen/include/asm-x86/apicdef.h +1 -1
@@ -30,7 +30,7 @@#define APIC_EIO_ACK 0x0/* Write this to the EOI register */ #define APIC_RRR 0xC0#define APIC_LDR 0xD0 -#define APIC_LDR_MASK (0xFF<<24) +#define APIC_LDR_MASK (0xFFu<<24)#define GET_xAPIC_LOGICAL_ID (x) (((x)>>24)&0xFF)#define SET_xAPIC_LOGICAL_ID (x) (((x)<<24))#define APIC_ALL_CPUS 0xFF
Signature · 7 lines
2.1.4 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xen.org/xen-devel
PATCH 1/1 x86/microcode: Avoid undefined behaviour from signed integer overflow
In reply to Andrew CooperThe checksums should be calculated using unsigned 32bit integers, as they are intended to overflow and end at 0. Replace some other signed integers with unsigned ones, to avoid mixed-sign comparisons.
---
CC: Jan Beulich <[email protected]>
CC: Kevin Tian <[email protected]>
CC: Jun Nakajima <[email protected]>
v2:
* Include the extended checksum
* Swap more integers to being unsigned
--- xen/arch/x86/microcode_intel.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-)
xen/arch/x86/microcode_intel.c +5 -4
@@ -143,7 +143,8 @@ static int microcode_sanity_check(void *mc)struct extended_sigtable *ext_header =NULL ;struct extended_signature *ext_sig;unsigned long total_size, data_size, ext_table_size; -int sum, orig_sum, ext_sigcount =0 , i; +unsigned int ext_sigcount =0 , i; +uint32_t sum, orig_sum; total_size =get_totalsize (mc_header); data_size =get_datasize (mc_header);
@@ -183,8 +184,8 @@ static int microcode_sanity_check(void *mc)/* check extended table checksum */ if ( ext_table_size ) { -int ext_table_sum =0 ; -int *ext_tablep = (int *)ext_header; +uint32_t ext_table_sum =0 ; +uint32_t *ext_tablep = (uint32_t *)ext_header; i = ext_table_size /DWSIZE ;while ( i-- )
@@ -201,7 +202,7 @@ static int microcode_sanity_check(void *mc) orig_sum =0 ; i = (MC_HEADER_SIZE + data_size) /DWSIZE ;while ( i-- ) - orig_sum += ((int *)mc)[i]; + orig_sum += ((uint32_t *)mc)[i];if ( orig_sum ) {printk (KERN_ERR "microcode: aborting, bad checksum\n" );
Signature · 7 lines
2.1.4 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xen.org/xen-devel
Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 09.08.16 at 14:48, <[email protected]> wrote:
For
d->shutdown_code, change the field to being unsigned and using an unsigned sentinel. The sentinal needs to be distinguishable from any value representable in a u8.Signed-off-by Andrew Cooper <[email protected]>Reviewed-by George Dunlap <[email protected]>
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel