virt: vbox: fix memory leak in hgcm_call_preprocess_linaddr
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.
--- drivers/virt/vboxguest/vboxguest_utils.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/virt/vboxguest/vboxguest_utils.c +3 -1
@@ -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
Hi,
Quoted 6 lines · Navid Emamdoost
In
hgcm_call_preprocess_linaddrmemory is allocated forbounce_bufbut is not released ifcopy_form_userfails. The release is added.Fixes 579db9d45cb4 ("virt: Add vboxguest VMMDEV communication code")Signed-off-by Navid Emamdoost <[email protected]>
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@@ -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
-1if (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
It is a neat fix now, thank you.
Quoted 104 lines · Hans de Goede
Hi,
Quoted 6 lines · Navid Emamdoost
In
hgcm_call_preprocess_linaddrmemory is allocated forbounce_bufbut is not released ifcopy_form_userfails. The release is added.Fixes 579db9d45cb4 ("virt: Add vboxguest VMMDEV communication code")Signed-off-by Navid Emamdoost <[email protected]>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@@ -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
-1if (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 ofhgcm_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_bufin thebounce_bufsarray before doing thecopy_from_user, then ifcopy_from_userfails it will be cleaned up by the code at thefree_bounce_bufslabel.IOW I believe it is better to fix this by changing the part of
hgcm_call_preprocess_linaddrI 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.
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_linaddrmemory is allocated forbounce_bufbut is not released ifcopy_form_userfails. The release is added.Fixes 579db9d45cb4 ("virt: Add vboxguest VMMDEV communication code")Signed-off-by Navid Emamdoost <[email protected]>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@@ -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
-1if (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 ofhgcm_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_bufin thebounce_bufsarray before doing thecopy_from_user, then ifcopy_from_userfails it will be cleaned up by the code at thefree_bounce_bufslabel.IOW I believe it is better to fix this by changing the part of
hgcm_call_preprocess_linaddrI 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
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.
--- drivers/virt/vboxguest/vboxguest_utils.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/virt/vboxguest/vboxguest_utils.c +2 -1
@@ -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
Hi,
Quoted 8 lines · Navid Emamdoost
In
hgcm_call_preprocess_linaddrmemory is allocated forbounce_bufbut is not released ifcopy_form_userfails. In order to prevent memory leak in case of failure, the assignment tobounce_buf_retis moved before the error check. This way the allocatedbounce_bufwill be released by the caller.Fixes 579db9d45cb4 ("virt: Add vboxguest VMMDEV communication code")Signed-off-by Navid Emamdoost <[email protected]>
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@@ -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 ; }
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_linaddrmemory is allocated forbounce_bufbut is not released ifcopy_form_userfails. The release is added.Fixes 579db9d45cb4 ("virt: Add vboxguest VMMDEV communication code")Signed-off-by Navid Emamdoost <[email protected]>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@@ -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
-1if (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 ofhgcm_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_bufin thebounce_bufsarray before doing thecopy_from_user, then ifcopy_from_userfails it will be cleaned up by the code at thefree_bounce_bufslabel.IOW I believe it is better to fix this by changing the part of
hgcm_call_preprocess_linaddrI 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.