linux-nfs.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 1/4] NFS4: Avoid NULL reference or double free in nfsd4_fslocs_free()
@ 2014-05-23 11:57 Kinglong Mee
  2014-05-23 13:58 ` J. Bruce Fields
  0 siblings, 1 reply; 2+ messages in thread
From: Kinglong Mee @ 2014-05-23 11:57 UTC (permalink / raw)
  To: J. Bruce Fields; +Cc: Linux NFS Mailing List, kinglongmee

If fsloc_parse() failed at kzalloc(), fs/nfsd/export.c
 411
 412         fsloc->locations = kzalloc(fsloc->locations_count
 413                         * sizeof(struct nfsd4_fs_location), GFP_KERNEL);
 414         if (!fsloc->locations)
 415                 return -ENOMEM;

svc_export_parse() will call nfsd4_fslocs_free() with fsloc->locations = NULL,
so that, "kfree(fsloc->locations[i].path);" will cause a crash.

If fsloc_parse() failed after that, fsloc_parse() will call nfsd4_fslocs_free(),
and svc_export_parse() will call it again, so that, a double free is caused.

This patch checks the fsloc->locations, and set to NULL after it be freed.

Signed-off-by: Kinglong Mee <kinglongmee@gmail.com>
---
 fs/nfsd/export.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)

diff --git a/fs/nfsd/export.c b/fs/nfsd/export.c
index 8513c59..9a41d3d 100644
--- a/fs/nfsd/export.c
+++ b/fs/nfsd/export.c
@@ -295,13 +295,19 @@ svc_expkey_update(struct cache_detail *cd, struct svc_expkey *new,
 
 static void nfsd4_fslocs_free(struct nfsd4_fs_locations *fsloc)
 {
+	struct nfsd4_fs_location *locations = fsloc->locations;
 	int i;
 
+	if (!locations)
+		return;
+
 	for (i = 0; i < fsloc->locations_count; i++) {
-		kfree(fsloc->locations[i].path);
-		kfree(fsloc->locations[i].hosts);
+		kfree(locations[i].path);
+		kfree(locations[i].hosts);
 	}
-	kfree(fsloc->locations);
+
+	kfree(locations);
+	fsloc->locations = NULL;
 }
 
 static void svc_export_put(struct kref *ref)
-- 
1.9.0


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

* Re: [PATCH 1/4] NFS4: Avoid NULL reference or double free in nfsd4_fslocs_free()
  2014-05-23 11:57 [PATCH 1/4] NFS4: Avoid NULL reference or double free in nfsd4_fslocs_free() Kinglong Mee
@ 2014-05-23 13:58 ` J. Bruce Fields
  0 siblings, 0 replies; 2+ messages in thread
From: J. Bruce Fields @ 2014-05-23 13:58 UTC (permalink / raw)
  To: Kinglong Mee; +Cc: Linux NFS Mailing List

On Fri, May 23, 2014 at 07:57:49PM +0800, Kinglong Mee wrote:
> If fsloc_parse() failed at kzalloc(), fs/nfsd/export.c
>  411
>  412         fsloc->locations = kzalloc(fsloc->locations_count
>  413                         * sizeof(struct nfsd4_fs_location), GFP_KERNEL);
>  414         if (!fsloc->locations)
>  415                 return -ENOMEM;
> 
> svc_export_parse() will call nfsd4_fslocs_free() with fsloc->locations = NULL,
> so that, "kfree(fsloc->locations[i].path);" will cause a crash.
> 
> If fsloc_parse() failed after that, fsloc_parse() will call nfsd4_fslocs_free(),
> and svc_export_parse() will call it again, so that, a double free is caused.
> 
> This patch checks the fsloc->locations, and set to NULL after it be freed.

Applying this and 2/4.

--b.

> 
> Signed-off-by: Kinglong Mee <kinglongmee@gmail.com>
> ---
>  fs/nfsd/export.c | 12 +++++++++---
>  1 file changed, 9 insertions(+), 3 deletions(-)
> 
> diff --git a/fs/nfsd/export.c b/fs/nfsd/export.c
> index 8513c59..9a41d3d 100644
> --- a/fs/nfsd/export.c
> +++ b/fs/nfsd/export.c
> @@ -295,13 +295,19 @@ svc_expkey_update(struct cache_detail *cd, struct svc_expkey *new,
>  
>  static void nfsd4_fslocs_free(struct nfsd4_fs_locations *fsloc)
>  {
> +	struct nfsd4_fs_location *locations = fsloc->locations;
>  	int i;
>  
> +	if (!locations)
> +		return;
> +
>  	for (i = 0; i < fsloc->locations_count; i++) {
> -		kfree(fsloc->locations[i].path);
> -		kfree(fsloc->locations[i].hosts);
> +		kfree(locations[i].path);
> +		kfree(locations[i].hosts);
>  	}
> -	kfree(fsloc->locations);
> +
> +	kfree(locations);
> +	fsloc->locations = NULL;
>  }
>  
>  static void svc_export_put(struct kref *ref)
> -- 
> 1.9.0
> 

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

end of thread, other threads:[~2014-05-23 13:58 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-05-23 11:57 [PATCH 1/4] NFS4: Avoid NULL reference or double free in nfsd4_fslocs_free() Kinglong Mee
2014-05-23 13:58 ` J. Bruce Fields

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).