Misc Xen fixes identified by Coverity
cover Misc Xen fixes identified by Coverity
Hello,
Here are 4 largely unrelated patches fixing issues identified by Coverity in Xen.
I hope they are all uncontentious.
PATCH 1/4 xen/irq: irq_to_desc() can't return NULL
So NULL checks are logically dead code.
Coverity IDs: 1055247, 1087147
--- xen/arch/x86/hvm/vmsi.c | 2 -- xen/arch/x86/irq.c | 2 -- 2 files changed, 4 deletions(-)
xen/arch/x86/hvm/vmsi.c +0 -2
@@ -304,8 +304,6 @@ static int msixtbl_write(struct vcpu *v, unsigned long address, goto out; desc =irq_to_desc (msi_desc->irq); -if ( !desc ) - goto out;spin_lock_irqsave (&desc->lock, flags);
xen/arch/x86/irq.c +0 -2
@@ -646,8 +646,6 @@ void irq_move_cleanup_interrupt(struct cpu_user_regs *regs)continue ; desc =irq_to_desc (irq); -if (!desc) -continue ;spin_lock (&desc->lock);if (!desc->arch.move_cleanup_count)
Signature · 1 line
1.7.10.4
Re: [PATCH 1/4] xen/irq: irq_to_desc() can't return NULL
Quoted 2 lines
Quoted 1 line
Quoted 1 line · Andrew Cooper
So NULL checks are logically dead code.
That's the case currently, but will need to change sooner or later. Hence I'd prefer to keep these...
Jan
Quoted 40 lines
Coverity IDs: 1055247, 1087147
Signed-off-by Andrew Cooper <[email protected]>CC Keir Fraser <[email protected]>CC Jan Beulich <[email protected]>--- xen/arch/x86/hvm/vmsi.c | 2 -- xen/arch/x86/irq.c | 2 -- 2 files changed, 4 deletions(-)xen/arch/x86/hvm/vmsi.c +0
-2@@ -304,8 +304,6 @@ static int msixtbl_write(struct vcpu *v, unsigned long address, goto out; desc =irq_to_desc (msi_desc->irq); -if ( !desc ) - goto out;spin_lock_irqsave (&desc->lock, flags);xen/arch/x86/irq.c +0
-2@@ -646,8 +646,6 @@ void irq_move_cleanup_interrupt(struct cpu_user_regs *regs)continue ; desc =irq_to_desc (irq); -if (!desc) -continue ;spin_lock (&desc->lock);if (!desc->arch.move_cleanup_count)Signature · 1 line
1.7.10.4
PATCH 2/4 x86/hap: Remove bogus assertion in hap_free_p2m_page()
In reply to Andrew CooperCoverity ID: 1055622
Coverity correctly points out that this ASSERT() is unconditionally true as an unsigned integer is always >= 0.
Judging from the shadow counterpart and p2m callsites, there is nothing invalid about freeing the final p2m page.
--- xen/arch/x86/mm/hap/hap.c | 1 - 1 file changed, 1 deletion(-)
xen/arch/x86/mm/hap/hap.c +0 -1
@@ -289,7 +289,6 @@ static void hap_free_p2m_page(struct domain *d, struct page_info *pg) d->arch.paging.hap.p2m_pages--; d->arch.paging.hap.total_pages++;hap_free (d,page_to_mfn (pg)); -ASSERT (d->arch.paging.hap.p2m_pages >=0 );paging_unlock (d); }
Signature · 1 line
1.7.10.4
Re: [PATCH 2/4] x86/hap: Remove bogus assertion in hap_free_p2m_page()
xen/arch/x86/mm/hap/hap.c static void hap_free_p2m_page(struct domain *d, struct page_info *pg) · line 292
Quoted 7 lines · Andrew Cooper
xen/arch/x86/mm/hap/hap.c +0
-1@@ -289,7 +289,6 @@ static void hap_free_p2m_page(struct domain *d, struct page_info *pg) d->arch.paging.hap.p2m_pages--; d->arch.paging.hap.total_pages++;hap_free (d,page_to_mfn (pg)); -ASSERT (d->arch.paging.hap.p2m_pages >=0 );
Huh, I'm surprised clang didn't complain about that one.
PATCH 3/4 common/page_alloc: Remove node id ASSERT()s
In reply to Andrew CooperCoverity ID 1055622, 1055630
node is an unsigned integer, so neccesserily >= 0. There does not appear to be any purpose to these assertions in the first place.
--- xen/common/page_alloc.c | 2 -- 1 file changed, 2 deletions(-)
xen/common/page_alloc.c +0 -2
@@ -585,7 +585,6 @@ static struct page_info *alloc_heap_pages( } first_node = node; -ASSERT (node >=0 );ASSERT (zone_lo <= zone_hi);ASSERT (zone_hi <NR_ZONES );
@@ -812,7 +811,6 @@ static void free_heap_pages(unsigned int zone =page_to_zone (pg);ASSERT (order <=MAX_ORDER ); -ASSERT (node >=0 );spin_lock (&heap_lock);
Signature · 1 line
1.7.10.4
Re: [PATCH 3/4] common/page_alloc: Remove node id ASSERT()s
Quoted 5 lines
Quoted 1 line
Quoted 1 line · Andrew Cooper
Coverity ID 1055622, 1055630
node is an unsigned integer, so neccesserily >= 0. There does not appear to be any purpose to these assertions in the first place.
I think assertions should be treated more forgivingly than "normal"
code: The check is very valid is the type of "node" got changed back to a signed int, as "node" is being used as array index. It is quite likely that on such type changes people would not remember to add back respective assertions.
That said - I'm not really opposed to this change, I just think that assertions like the one here have documentation aspects going beyond mere correctness checking intentions.
Jan
Quoted 30 lines
Signed-off-by Andrew Cooper <[email protected]>CC Keir Fraser <[email protected]>CC Jan Beulich <[email protected]>CC Tim Deegan <[email protected]>--- xen/common/page_alloc.c | 2 -- 1 file changed, 2 deletions(-)xen/common/page_alloc.c +0
-2@@ -585,7 +585,6 @@ static struct page_info *alloc_heap_pages( } first_node = node; -ASSERT (node >=0 );ASSERT (zone_lo <= zone_hi);ASSERT (zone_hi <NR_ZONES );@@ -812,7 +811,6 @@ static void free_heap_pages(unsigned int zone =page_to_zone (pg);ASSERT (order <=MAX_ORDER ); -ASSERT (node >=0 );spin_lock (&heap_lock);Signature · 1 line
1.7.10.4
PATCH 4/4 x86/microcode_amd: Fail attempts to load a 0-length microcode blob.
In reply to Andrew CooperCoverity ID: 1055319
Coverity identified that when passed a microcode header with a length field of 0, get_ucode_from_buffer_amd() would end up calling memcpy(NULL, data, 0) which is undefined behaviour.
While Xen's implementation of memcpy will do the correct thing in this case, any user trying to load a 0 length microcode blob deserves an -EINVAL.
--- xen/arch/x86/microcode_amd.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
xen/arch/x86/microcode_amd.c +1 -1
@@ -202,7 +202,7 @@ static int get_ucode_from_buffer_amd(return -EINVAL ; } -if ( (off + mpbuf->len) > bufsize ) +if ( mpbuf->len ==0 || ((off + mpbuf->len) > bufsize) ) {printk (KERN_ERR "microcode: Bad data in microcode data file\n" );return -EINVAL ;
Signature · 1 line
1.7.10.4
Re: [PATCH 4/4] x86/microcode_amd: Fail attempts to load a 0-length microcode blob.
Quoted 6 lines
Quoted 1 line
Quoted 1 line · Andrew Cooper
Coverity ID: 1055319
Coverity identified that when passed a microcode header with a length field of 0,
get_ucode_from_buffer_amd()would end up callingmemcpy(NULL, data, 0)which is undefined behaviour.
I think that's at least questionable: memcpy(..., 0) can hardly be anything but a no-op, no matter whether either of the two pointers in fact is a NULL one.
That's not to say that I disagree that strict reading of the C standard may indeed yield this undefined, but I don't think we are to hunt down all such undefined-nesses. The tool should really stay away from hair-splitting, and concentrate on pointing out real issues.
Jan
Quoted 27 lines
While Xen's implementation of memcpy will do the correct thing in this case, any user trying to load a 0 length microcode blob deserves an
-EINVAL.Signed-off-by Andrew Cooper <[email protected]>CC Keir Fraser <[email protected]>CC Jan Beulich <[email protected]>CC Suravee Suthikulpanit <[email protected]>CC Boris Ostrovsky <[email protected]>--- xen/arch/x86/microcode_amd.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)xen/arch/x86/microcode_amd.c +1
-1@@ -202,7 +202,7 @@ static int get_ucode_from_buffer_amd(return -EINVAL ; } -if ( (off + mpbuf->len) > bufsize ) +if ( mpbuf->len ==0 || ((off + mpbuf->len) > bufsize) ) {printk (KERN_ERR "microcode: Bad data in microcode data file\n" );return -EINVAL ;Signature · 1 line
1.7.10.4
Re: [PATCH 4/4] x86/microcode_amd: Fail attempts to load a 0-length microcode blob.
Quoted 9 lines · Jan Beulich
Quoted 6 lines
Quoted 1 line
Quoted 1 line · Andrew Cooper
Coverity ID: 1055319
Coverity identified that when passed a microcode header with a length field of 0,
get_ucode_from_buffer_amd()would end up callingmemcpy(NULL, data, 0)which is undefined behaviour.I think that's at least questionable:
memcpy(..., 0)can hardly be anything but a no-op, no matter whether either of the two pointers in fact is a NULL one.
I think what this patch is trying to prevent is passing NULL pointer to memcpy, not length being zero (if you follow the logic in get_ucode_from_buffer_amd() you will see that destination buffer may not be allocated when length is zero).
AMD APM says
REP. The REP prefix repeats its associated string instruction the number of times specified in the counter register (rCX). It terminates the repetition when the value in rCX reaches 0.
So presumably rCX is checked before rDI and memcpy would indeed be a nop.
Still, I think in this particular case (destination can be NULL) the patch is a good thing since we usually (often) check before passing NULL pointer to routines. Alternatively, we can check 'if (mc_amd->mpb != NULL)'.
-boris
Quoted 37 lines
That's not to say that I disagree that strict reading of the C standard may indeed yield this undefined, but I don't think we are to hunt down all such undefined-nesses. The tool should really stay away from hair-splitting, and concentrate on pointing out real issues.
Jan
Quoted 27 lines
While Xen's implementation of memcpy will do the correct thing in this case, any user trying to load a 0 length microcode blob deserves an
-EINVAL.Signed-off-by Andrew Cooper <[email protected]>CC Keir Fraser <[email protected]>CC Jan Beulich <[email protected]>CC Suravee Suthikulpanit <[email protected]>CC Boris Ostrovsky <[email protected]>--- xen/arch/x86/microcode_amd.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)xen/arch/x86/microcode_amd.c +1
-1@@ -202,7 +202,7 @@ static int get_ucode_from_buffer_amd(return -EINVAL ; } -if ( (off + mpbuf->len) > bufsize ) +if ( mpbuf->len ==0 || ((off + mpbuf->len) > bufsize) ) {printk (KERN_ERR "microcode: Bad data in microcode data file\n" );return -EINVAL ;Signature · 1 line
1.7.10.4
Re: [PATCH 4/4] x86/microcode_amd: Fail attempts to load a 0-length microcode blob.
Quoted 13 lines
Quoted 1 line
Quoted 1 line · Boris Ostrovsky
Quoted 4 lines · Jan Beulich
Quoted 4 lines
Quoted 1 line
Quoted 1 line · Andrew Cooper
Coverity ID: 1055319
Coverity identified that when passed a microcode header with a length field
of
Quoted 5 lines
Quoted 2 lines
0,
get_ucode_from_buffer_amd()would end up callingmemcpy(NULL, data, 0)which is undefined behaviour.I think that's at least questionable:
memcpy(..., 0)can hardly be anything but a no-op, no matter whether either of the two pointers in fact is a NULL one.
Quoted 5 lines
I think what this patch is trying to prevent is passing NULL pointer to memcpy,
not length being zero (if you follow the logic inget_ucode_from_buffer_amd()you
will see that destination buffer may not be allocated when length is zero).
I understand that; what I was trying to say is that it can't be other than benign if you pass NULL for at least one of the two pointers as long as 0 is being passed for the size.
Jan
Re: [PATCH 4/4] x86/microcode_amd: Fail attempts to load a 0-length microcode blob.
In reply to Boris OstrovskyQuoted 30 lines · Boris Ostrovsky
Quoted 4 lines · Jan Beulich
Quoted 4 lines
Quoted 2 lines
Quoted 2 lines
On 24.09.13 at 14:10, Andrew Cooper <[email protected]> wrote:
Coverity ID: 1055319
Quoted 8 lines
Quoted 5 lines
Coverity identified that when passed a microcode header with a length field of
0,get_ucode_from_buffer_amd()would end up callingmemcpy(NULL, data, 0)
which is undefined behaviour.I think that's at least questionable:
memcpy(..., 0)can hardly be anything but a no-op, no matter whether either of the two pointers in fact is a NULL one.I think what this patch is trying to prevent is passing NULL pointer to memcpy,
not length being zero (if you follow the logic inget_ucode_from_buffer_amd()you
will see that destination buffer may not be allocated when length is zero).AMD APM says
REP. The REP prefix repeats its associated string instruction the
number
of times specified in the counter register (rCX). It terminates the repetition when the value in rCX reaches 0.So presumably rCX is checked before rDI and memcpy would indeed be a nop.
Quoted 7 lines
Still, I think in this particular case (destination can be NULL) the patch is
a good thing since we usually (often) check before passing NULL pointer to
routines. Alternatively, we can check 'if (mc_amd->mpb!= NULL)'.-boris
That may not work as expected.
The subtalty here is that we only allocate memory for mc_amd->mpb if mc_amd->mpb_size is less than mpbuf->len.
mc_amd->mpb is unconditionally NULL before the first allocation, and mc_amd->mpb_size is 0 (from the caller)
Therefore, a user passing 0 for mpbuf->len causes the routine to choose not to allocate any memory, then copy 0 bytes into it.
~Andrew