mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH]  virtio/virtio_mem: handle a possible NULL as a memcpy parameter
@ 2021-12-22  1:12 Peng Hao
  2021-12-22  8:27 ` David Hildenbrand
  0 siblings, 1 reply; 2+ messages in thread
From: Peng Hao @ 2021-12-22  1:12 UTC (permalink / raw)
  To: david, mst; +Cc: linux-block, linux-kernel

There is a check for vm->sbm.sb_states before, and it should check
it here as well.

Signed-off-by: Peng Hao <flyingpeng@tencent.com>
---
 drivers/virtio/virtio_mem.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/virtio/virtio_mem.c b/drivers/virtio/virtio_mem.c
index 96e5a8782769..b6b7c489c8b6 100644
--- a/drivers/virtio/virtio_mem.c
+++ b/drivers/virtio/virtio_mem.c
@@ -592,7 +592,7 @@ static int virtio_mem_sbm_sb_states_prepare_next_mb(struct virtio_mem *vm)
 		return -ENOMEM;
 
 	mutex_lock(&vm->hotplug_mutex);
-	if (new_bitmap)
+	if (vm->sbm.sb_states)
 		memcpy(new_bitmap, vm->sbm.sb_states, old_pages * PAGE_SIZE);
 
 	old_bitmap = vm->sbm.sb_states;
-- 
2.27.0


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] virtio/virtio_mem: handle a possible NULL as a memcpy parameter
  2021-12-22  1:12 [PATCH] virtio/virtio_mem: handle a possible NULL as a memcpy parameter Peng Hao
@ 2021-12-22  8:27 ` David Hildenbrand
  0 siblings, 0 replies; 2+ messages in thread
From: David Hildenbrand @ 2021-12-22  8:27 UTC (permalink / raw)
  To: Peng Hao, mst; +Cc: linux-block, linux-kernel

On 22.12.21 02:12, Peng Hao wrote:
> There is a check for vm->sbm.sb_states before, and it should check
> it here as well.
> 
> Signed-off-by: Peng Hao <flyingpeng@tencent.com>
> ---
>  drivers/virtio/virtio_mem.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/drivers/virtio/virtio_mem.c b/drivers/virtio/virtio_mem.c
> index 96e5a8782769..b6b7c489c8b6 100644
> --- a/drivers/virtio/virtio_mem.c
> +++ b/drivers/virtio/virtio_mem.c
> @@ -592,7 +592,7 @@ static int virtio_mem_sbm_sb_states_prepare_next_mb(struct virtio_mem *vm)
>  		return -ENOMEM;
>  
>  	mutex_lock(&vm->hotplug_mutex);
> -	if (new_bitmap)
> +	if (vm->sbm.sb_states)
>  		memcpy(new_bitmap, vm->sbm.sb_states, old_pages * PAGE_SIZE);
>  
>  	old_bitmap = vm->sbm.sb_states;

Right, on the first iteration (vm->sbm.sb_states == NULL) we would copy
from NULL.

I wonder why this never failed so far. I guess that's because "the
behavior is undefined" if a NULL pointer is passed.

I assume the memcpy implementation that we've been using so far simply
skips the operation if they detect a NULL pointer, although according to
the standard the behavior is undefined:

"
AFAIK, most implementations allow null pointers for no-op calls to
memcpy() but gcc issues a warning when it detects at compile time
that a null pointer is passed as the first or second argument to
memcpy().
" [1]

Fixes: 5f1f79bbc9e2 ("virtio-mem: Paravirtualized memory hotplug")
Cc: stable@vger.kernel.org # v5.8+


Thanks!

[1]
https://mail-archives.apache.org/mod_mbox/stdcxx-dev/200804.mbox/%3CCFFDD219128FD94FB4F92B99F52D0A49010A36F4@exchmail01.Blue.Roguewave.Com%3E

-- 
Thanks,

David / dhildenb


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2021-12-22  8:27 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-12-22  1:12 [PATCH] virtio/virtio_mem: handle a possible NULL as a memcpy parameter Peng Hao
2021-12-22  8:27 ` David Hildenbrand

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®