[PATCH] vduse: prevent uninitialized memory accesses

Maxime Coquelin posted 1 patch 3 years, 7 months ago
There is a newer version of this series
drivers/vdpa/vdpa_user/vduse_dev.c | 9 +++++++--
1 file changed, 7 insertions(+), 2 deletions(-)
[PATCH] vduse: prevent uninitialized memory accesses
Posted by Maxime Coquelin 3 years, 7 months ago
If the VDUSE application provides a smaller config space
than the driver expects, the driver may use uninitialized
memory from the stack.

This patch prevents it by initializing the buffer passed by
the driver to store the config value.

Signed-off-by: Maxime Coquelin <maxime.coquelin@redhat.com>
---
 drivers/vdpa/vdpa_user/vduse_dev.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

diff --git a/drivers/vdpa/vdpa_user/vduse_dev.c b/drivers/vdpa/vdpa_user/vduse_dev.c
index 41c0b29739f1..35dceee3ed56 100644
--- a/drivers/vdpa/vdpa_user/vduse_dev.c
+++ b/drivers/vdpa/vdpa_user/vduse_dev.c
@@ -673,10 +673,15 @@ static void vduse_vdpa_get_config(struct vdpa_device *vdpa, unsigned int offset,
 {
 	struct vduse_dev *dev = vdpa_to_vduse(vdpa);
 
-	if (offset > dev->config_size ||
-	    len > dev->config_size - offset)
+	/* Initialize the buffer in case of partial copy. */
+	memset(buf, 0, len);
+
+	if (offset > dev->config_size)
 		return;
 
+	if (len > dev->config_size - offset)
+		len = dev->config_size - offset;
+
 	memcpy(buf, dev->config + offset, len);
 }
 
-- 
2.37.1
Re: [PATCH] vduse: prevent uninitialized memory accesses
Posted by Jason Wang 3 years, 7 months ago
On Sat, Aug 27, 2022 at 12:16 AM Maxime Coquelin
<maxime.coquelin@redhat.com> wrote:
>
> If the VDUSE application provides a smaller config space
> than the driver expects, the driver may use uninitialized
> memory from the stack.
>
> This patch prevents it by initializing the buffer passed by
> the driver to store the config value.
>
> Signed-off-by: Maxime Coquelin <maxime.coquelin@redhat.com>

Acked-by: Jason Wang <jasowang@redhat.com>

> ---
>  drivers/vdpa/vdpa_user/vduse_dev.c | 9 +++++++--
>  1 file changed, 7 insertions(+), 2 deletions(-)
>
> diff --git a/drivers/vdpa/vdpa_user/vduse_dev.c b/drivers/vdpa/vdpa_user/vduse_dev.c
> index 41c0b29739f1..35dceee3ed56 100644
> --- a/drivers/vdpa/vdpa_user/vduse_dev.c
> +++ b/drivers/vdpa/vdpa_user/vduse_dev.c
> @@ -673,10 +673,15 @@ static void vduse_vdpa_get_config(struct vdpa_device *vdpa, unsigned int offset,
>  {
>         struct vduse_dev *dev = vdpa_to_vduse(vdpa);
>
> -       if (offset > dev->config_size ||
> -           len > dev->config_size - offset)
> +       /* Initialize the buffer in case of partial copy. */
> +       memset(buf, 0, len);
> +
> +       if (offset > dev->config_size)
>                 return;
>
> +       if (len > dev->config_size - offset)
> +               len = dev->config_size - offset;
> +
>         memcpy(buf, dev->config + offset, len);
>  }
>
> --
> 2.37.1
>
Re: [PATCH] vduse: prevent uninitialized memory accesses
Posted by Dan Carpenter 3 years, 7 months ago
On Fri, Aug 26, 2022 at 06:16:05PM +0200, Maxime Coquelin wrote:
> If the VDUSE application provides a smaller config space
> than the driver expects, the driver may use uninitialized
> memory from the stack.
> 
> This patch prevents it by initializing the buffer passed by
> the driver to store the config value.
> 
> Signed-off-by: Maxime Coquelin <maxime.coquelin@redhat.com>

This sounds like it needs a Fixes tag?

regards,
dan carpenter
Re: [PATCH] vduse: prevent uninitialized memory accesses
Posted by Maxime Coquelin 3 years, 7 months ago

On 8/27/22 08:54, Dan Carpenter wrote:
> On Fri, Aug 26, 2022 at 06:16:05PM +0200, Maxime Coquelin wrote:
>> If the VDUSE application provides a smaller config space
>> than the driver expects, the driver may use uninitialized
>> memory from the stack.
>>
>> This patch prevents it by initializing the buffer passed by
>> the driver to store the config value.
>>
>> Signed-off-by: Maxime Coquelin <maxime.coquelin@redhat.com>
> 
> This sounds like it needs a Fixes tag?

Yes, I actually did it, but somehow forgot to generate the patch bedore
posting.

I'll post a v2 soon.

Thanks,
Maxime

> regards,
> dan carpenter
>