Skip to contentSkip to conversation

virt: vbox: fix memory leak in hgcm_call_preprocess_linaddr

PATCH Superseded Navid Emamdoost 2 participants 7 messages Sep 27, 2019 – Oct 1, 2019 Virtual box guest device driver

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 virt: vbox: fix memory leak in hgcm_call_preprocess_linaddr

In hgcm_call_preprocess_linaddr memory is allocated for bounce_buf but is not released if copy_form_user fails. The release is added.

Fixes 579db9d45cb4 ("virt: Add vboxguest VMMDEV communication code")
---
 drivers/virt/vboxguest/vboxguest_utils.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/virt/vboxguest/vboxguest_utils.c +3 -1
index 75fd140b02ff..7965885a50fa 100644
@@ -222,8 +222,10 @@ static int hgcm_call_preprocess_linaddr(
 
 	if (copy_in) {
 		ret = copy_from_user(bounce_buf, (void __user *)buf, len);
-		if (ret)
+		if (ret) {
+			kvfree(bounce_buf);
 			return -EFAULT;
+		}
 	} else {
 		memset(bounce_buf, 0, len);
 	}
Signature · 1 line
2.17.1
#1 lore raw

Hi,

Quoted 6 lines · Navid Emamdoost

In hgcm_call_preprocess_linaddr memory is allocated for bounce_buf but is not released if copy_form_user fails. The release is added.

Fixes 579db9d45cb4 ("virt: Add vboxguest VMMDEV communication code")

Thank you for catching this, I agree this is a bug, but I think we can fix it in a cleaner way (see below).

Quoted 11 lines
---
  drivers/virt/vboxguest/vboxguest_utils.c | 4 +++-
  1 file changed, 3 insertions(+), 1 deletion(-)
drivers/virt/vboxguest/vboxguest_utils.c +0 -0
index 75fd140b02ff..7965885a50fa 100644
@@ -222,8 +222,10 @@ static int hgcm_call_preprocess_linaddr(
 

drivers/virt/vboxguest/vboxguest_utils.c static int hgcm_call_preprocess_linaddr( · line 231

Quoted 10 lines
drivers/virt/vboxguest/vboxguest_utils.c +3 -1
  	if (copy_in) {
  		ret = copy_from_user(bounce_buf, (void __user *)buf, len);
-		if (ret)
+		if (ret) {
+			kvfree(bounce_buf);
  			return -EFAULT;
+		}
  	} else {
  		memset(bounce_buf, 0, len);
  	}

First let me quote some more of the function, pre leak fix, for context:

	bounce_buf = kvmalloc(len, GFP_KERNEL);
	if (!bounce_buf)
		return -ENOMEM;

	if (copy_in) {
		ret = copy_from_user(bounce_buf, (void __user *)buf, len);
		if (ret)
			return -EFAULT;
	} else {
		memset(bounce_buf, 0, len);
	}

	*bounce_buf_ret = bounce_buf;

This function gets called repeatedly by hgcm_call_preprocess(), and the caller of hgcm_call_preprocess() already takes care of freeing the bounce bufs both on a (later) error and on success:

	ret = hgcm_call_preprocess(parms, parm_count, &bounce_bufs, &size);
	if (ret) {
		/* Even on error bounce bufs may still have been allocated */
		goto free_bounce_bufs;
	}

	...

free_bounce_bufs:

	if (bounce_bufs) {
		for (i = 0; i < parm_count; i++)
			kvfree(bounce_bufs[i]);
		kfree(bounce_bufs);
	}

So we are already taking care of freeing bounce-bufs allocated for previous parameters to the call (which me must do anyways), so a cleaner fix would be to store the allocated bounce_buf in the bounce_bufs array before doing the copy_from_user, then if copy_from_user fails it will be cleaned up by the code at the free_bounce_bufs label.

IOW I believe it is better to fix this by changing the part of hgcm_call_preprocess_linaddr I quoted to:

	bounce_buf = kvmalloc(len, GFP_KERNEL);
	if (!bounce_buf)
		return -ENOMEM;

	*bounce_buf_ret = bounce_buf;

	if (copy_in) {
		ret = copy_from_user(bounce_buf, (void __user *)buf, len);
		if (ret)
			return -EFAULT;
	} else {
		memset(bounce_buf, 0, len);
	}

This should also fix the leak in IMHO is a clear way of doing so.

Regards,

Hans

#2 lore raw

It is a neat fix now, thank you.

Quoted 104 lines · Hans de Goede

Hi,

Quoted 6 lines · Navid Emamdoost

In hgcm_call_preprocess_linaddr memory is allocated for bounce_buf but is not released if copy_form_user fails. The release is added.

Fixes 579db9d45cb4 ("virt: Add vboxguest VMMDEV communication code")

Thank you for catching this, I agree this is a bug, but I think we can fix it in a cleaner way (see below).

Quoted 11 lines
---
  drivers/virt/vboxguest/vboxguest_utils.c | 4 +++-
  1 file changed, 3 insertions(+), 1 deletion(-)
drivers/virt/vboxguest/vboxguest_utils.c +0 -0
index 75fd140b02ff..7965885a50fa 100644
@@ -222,8 +222,10 @@ static int hgcm_call_preprocess_linaddr(
 

drivers/virt/vboxguest/vboxguest_utils.c static int hgcm_call_preprocess_linaddr( · line 231

Quoted 10 lines
drivers/virt/vboxguest/vboxguest_utils.c +3 -1
      if (copy_in) {
              ret = copy_from_user(bounce_buf, (void __user *)buf, len);
-             if (ret)
+             if (ret) {
+                     kvfree(bounce_buf);
                      return -EFAULT;
+             }
      } else {
              memset(bounce_buf, 0, len);
      }

First let me quote some more of the function, pre leak fix, for context:

        bounce_buf = kvmalloc(len, GFP_KERNEL);
        if (!bounce_buf)
                return -ENOMEM;

        if (copy_in) {
                ret = copy_from_user(bounce_buf, (void __user *)buf, len);
                if (ret)
                        return -EFAULT;
        } else {
                memset(bounce_buf, 0, len);
        }

        *bounce_buf_ret = bounce_buf;

This function gets called repeatedly by hgcm_call_preprocess(), and the caller of hgcm_call_preprocess() already takes care of freeing the bounce bufs both on a (later) error and on success:

        ret = hgcm_call_preprocess(parms, parm_count, &bounce_bufs, &size);
        if (ret) {
                /* Even on error bounce bufs may still have been allocated */
                goto free_bounce_bufs;
        }

        ...

free_bounce_bufs:

        if (bounce_bufs) {
                for (i = 0; i < parm_count; i++)
                        kvfree(bounce_bufs[i]);
                kfree(bounce_bufs);
        }

So we are already taking care of freeing bounce-bufs allocated for previous parameters to the call (which me must do anyways), so a cleaner fix would be to store the allocated bounce_buf in the bounce_bufs array before doing the copy_from_user, then if copy_from_user fails it will be cleaned up by the code at the free_bounce_bufs label.

IOW I believe it is better to fix this by changing the part of hgcm_call_preprocess_linaddr I quoted to:

        bounce_buf = kvmalloc(len, GFP_KERNEL);
        if (!bounce_buf)
                return -ENOMEM;

        *bounce_buf_ret = bounce_buf;

        if (copy_in) {
                ret = copy_from_user(bounce_buf, (void __user *)buf, len);
                if (ret)
                        return -EFAULT;
        } else {
                memset(bounce_buf, 0, len);
        }

This should also fix the leak in IMHO is a clear way of doing so.

Regards,

Hans

Signature · 1 line
Navid.
#3 lore raw

Hi,

Quoted 1 line · Navid Emamdoost

It is a neat fix now, thank you.

Can you submit a new version of your patch with the fix I proposed please ?

Regards,

Hans

Quoted 109 lines · Hans de Goede
Quoted 104 lines

Hi,

Quoted 6 lines · Navid Emamdoost

In hgcm_call_preprocess_linaddr memory is allocated for bounce_buf but is not released if copy_form_user fails. The release is added.

Fixes 579db9d45cb4 ("virt: Add vboxguest VMMDEV communication code")

Thank you for catching this, I agree this is a bug, but I think we can fix it in a cleaner way (see below).

Quoted 11 lines
---
   drivers/virt/vboxguest/vboxguest_utils.c | 4 +++-
   1 file changed, 3 insertions(+), 1 deletion(-)
drivers/virt/vboxguest/vboxguest_utils.c +0 -0
index 75fd140b02ff..7965885a50fa 100644
@@ -222,8 +222,10 @@ static int hgcm_call_preprocess_linaddr(
 

drivers/virt/vboxguest/vboxguest_utils.c static int hgcm_call_preprocess_linaddr( · line 231

Quoted 10 lines
drivers/virt/vboxguest/vboxguest_utils.c +3 -1
       if (copy_in) {
               ret = copy_from_user(bounce_buf, (void __user *)buf, len);
-             if (ret)
+             if (ret) {
+                     kvfree(bounce_buf);
                       return -EFAULT;
+             }
       } else {
               memset(bounce_buf, 0, len);
       }

First let me quote some more of the function, pre leak fix, for context:

         bounce_buf = kvmalloc(len, GFP_KERNEL);
         if (!bounce_buf)
                 return -ENOMEM;

         if (copy_in) {
                 ret = copy_from_user(bounce_buf, (void __user *)buf, len);
                 if (ret)
                         return -EFAULT;
         } else {
                 memset(bounce_buf, 0, len);
         }

         *bounce_buf_ret = bounce_buf;

This function gets called repeatedly by hgcm_call_preprocess(), and the caller of hgcm_call_preprocess() already takes care of freeing the bounce bufs both on a (later) error and on success:

         ret = hgcm_call_preprocess(parms, parm_count, &bounce_bufs, &size);
         if (ret) {
                 /* Even on error bounce bufs may still have been allocated */
                 goto free_bounce_bufs;
         }

         ...

free_bounce_bufs:

         if (bounce_bufs) {
                 for (i = 0; i < parm_count; i++)
                         kvfree(bounce_bufs[i]);
                 kfree(bounce_bufs);
         }

So we are already taking care of freeing bounce-bufs allocated for previous parameters to the call (which me must do anyways), so a cleaner fix would be to store the allocated bounce_buf in the bounce_bufs array before doing the copy_from_user, then if copy_from_user fails it will be cleaned up by the code at the free_bounce_bufs label.

IOW I believe it is better to fix this by changing the part of hgcm_call_preprocess_linaddr I quoted to:

         bounce_buf = kvmalloc(len, GFP_KERNEL);
         if (!bounce_buf)
                 return -ENOMEM;

         *bounce_buf_ret = bounce_buf;

         if (copy_in) {
                 ret = copy_from_user(bounce_buf, (void __user *)buf, len);
                 if (ret)
                         return -EFAULT;
         } else {
                 memset(bounce_buf, 0, len);
         }

This should also fix the leak in IMHO is a clear way of doing so.

Regards,

Hans

#4 lore raw

PATCH v2 1/1 virt: vbox: fix memory leak in hgcm_call_preprocess_linaddr

In hgcm_call_preprocess_linaddr memory is allocated for bounce_buf but is not released if copy_form_user fails. In order to prevent memory leak in case of failure, the assignment to bounce_buf_ret is moved before the error check. This way the allocated bounce_buf will be released by the caller.

Fixes 579db9d45cb4 ("virt: Add vboxguest VMMDEV communication code")
---
 drivers/virt/vboxguest/vboxguest_utils.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/virt/vboxguest/vboxguest_utils.c +2 -1
index 75fd140b02ff..43c391626a00 100644
@@ -220,6 +220,8 @@ static int hgcm_call_preprocess_linaddr(
 	if (!bounce_buf)
 		return -ENOMEM;
 
+	*bounce_buf_ret = bounce_buf;
+
 	if (copy_in) {
 		ret = copy_from_user(bounce_buf, (void __user *)buf, len);
 		if (ret)
@@ -228,7 +230,6 @@ static int hgcm_call_preprocess_linaddr(
 		memset(bounce_buf, 0, len);
 	}
 
-	*bounce_buf_ret = bounce_buf;
 	hgcm_call_add_pagelist_size(bounce_buf, len, extra);
 	return 0;
 }
Signature · 1 line
2.17.1
Reviewed-by on v2 1/1
#5 lore raw

Hi,

Quoted 8 lines · Navid Emamdoost

In hgcm_call_preprocess_linaddr memory is allocated for bounce_buf but is not released if copy_form_user fails. In order to prevent memory leak in case of failure, the assignment to bounce_buf_ret is moved before the error check. This way the allocated bounce_buf will be released by the caller.

Fixes 579db9d45cb4 ("virt: Add vboxguest VMMDEV communication code")

Thank you.

Patch looks good to me:

Regards,

Hans

Quoted 26 lines
---
  drivers/virt/vboxguest/vboxguest_utils.c | 3 ++-
  1 file changed, 2 insertions(+), 1 deletion(-)
drivers/virt/vboxguest/vboxguest_utils.c +2 -1
index 75fd140b02ff..43c391626a00 100644
@@ -220,6 +220,8 @@ static int hgcm_call_preprocess_linaddr(
  	if (!bounce_buf)
  		return -ENOMEM;
 
+	*bounce_buf_ret = bounce_buf;
+
  	if (copy_in) {
  		ret = copy_from_user(bounce_buf, (void __user *)buf, len);
  		if (ret)
@@ -228,7 +230,6 @@ static int hgcm_call_preprocess_linaddr(
  		memset(bounce_buf, 0, len);
  	}
 
-	*bounce_buf_ret = bounce_buf;
  	hgcm_call_add_pagelist_size(bounce_buf, len, extra);
  	return 0;
  }
#6 lore raw
In reply to Hans de Goede
Quoted 12 lines · Hans de Goede

Hi,

Quoted 1 line · Navid Emamdoost

It is a neat fix now, thank you.

Can you submit a new version of your patch with the fix I proposed please ?

Regards,

Hans

Sure, v2 was sent.

Quoted 111 lines
Quoted 110 lines · Hans de Goede
Quoted 104 lines

Hi,

Quoted 6 lines · Navid Emamdoost

In hgcm_call_preprocess_linaddr memory is allocated for bounce_buf but is not released if copy_form_user fails. The release is added.

Fixes 579db9d45cb4 ("virt: Add vboxguest VMMDEV communication code")

Thank you for catching this, I agree this is a bug, but I think we can fix it in a cleaner way (see below).

Quoted 11 lines
---
   drivers/virt/vboxguest/vboxguest_utils.c | 4 +++-
   1 file changed, 3 insertions(+), 1 deletion(-)
drivers/virt/vboxguest/vboxguest_utils.c +0 -0
index 75fd140b02ff..7965885a50fa 100644
@@ -222,8 +222,10 @@ static int hgcm_call_preprocess_linaddr(
 

drivers/virt/vboxguest/vboxguest_utils.c static int hgcm_call_preprocess_linaddr( · line 231

Quoted 10 lines
drivers/virt/vboxguest/vboxguest_utils.c +3 -1
       if (copy_in) {
               ret = copy_from_user(bounce_buf, (void __user *)buf, len);
-             if (ret)
+             if (ret) {
+                     kvfree(bounce_buf);
                       return -EFAULT;
+             }
       } else {
               memset(bounce_buf, 0, len);
       }

First let me quote some more of the function, pre leak fix, for context:

         bounce_buf = kvmalloc(len, GFP_KERNEL);
         if (!bounce_buf)
                 return -ENOMEM;

         if (copy_in) {
                 ret = copy_from_user(bounce_buf, (void __user *)buf, len);
                 if (ret)
                         return -EFAULT;
         } else {
                 memset(bounce_buf, 0, len);
         }

         *bounce_buf_ret = bounce_buf;

This function gets called repeatedly by hgcm_call_preprocess(), and the caller of hgcm_call_preprocess() already takes care of freeing the bounce bufs both on a (later) error and on success:

         ret = hgcm_call_preprocess(parms, parm_count, &bounce_bufs, &size);
         if (ret) {
                 /* Even on error bounce bufs may still have been allocated */
                 goto free_bounce_bufs;
         }

         ...

free_bounce_bufs:

         if (bounce_bufs) {
                 for (i = 0; i < parm_count; i++)
                         kvfree(bounce_bufs[i]);
                 kfree(bounce_bufs);
         }

So we are already taking care of freeing bounce-bufs allocated for previous parameters to the call (which me must do anyways), so a cleaner fix would be to store the allocated bounce_buf in the bounce_bufs array before doing the copy_from_user, then if copy_from_user fails it will be cleaned up by the code at the free_bounce_bufs label.

IOW I believe it is better to fix this by changing the part of hgcm_call_preprocess_linaddr I quoted to:

         bounce_buf = kvmalloc(len, GFP_KERNEL);
         if (!bounce_buf)
                 return -ENOMEM;

         *bounce_buf_ret = bounce_buf;

         if (copy_in) {
                 ret = copy_from_user(bounce_buf, (void __user *)buf, len);
                 if (ret)
                         return -EFAULT;
         } else {
                 memset(bounce_buf, 0, len);
         }

This should also fix the leak in IMHO is a clear way of doing so.

Regards,

Hans

Signature · 1 line
Navid.