linux-efi.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [patch] efi: fix error handling in add_sysfs_runtime_map_entry()
@ 2015-04-21 13:46 Dan Carpenter
  2015-04-27 20:31 ` Matt Fleming
  0 siblings, 1 reply; 2+ messages in thread
From: Dan Carpenter @ 2015-04-21 13:46 UTC (permalink / raw)
  To: Matt Fleming, Guangyu Sun; +Cc: linux-efi-u79uwXL29TY76Z2rM5mHXA

I spotted two (difficult to hit) bugs while reviewing this.

1)  There is a double free bug because we unregister "map_kset" in
    add_sysfs_runtime_map_entry() and also efi_runtime_map_init().
2)  If we fail to allocate "entry" then we should return
    ERR_PTR(-ENOMEM) instead of NULL.

Signed-off-by: Dan Carpenter <dan.carpenter-QHcLZuEGTsvQT0dZR+AlfA@public.gmane.org>

diff --git a/drivers/firmware/efi/runtime-map.c b/drivers/firmware/efi/runtime-map.c
index 87b8e3b..5c55227 100644
--- a/drivers/firmware/efi/runtime-map.c
+++ b/drivers/firmware/efi/runtime-map.c
@@ -120,7 +120,8 @@ add_sysfs_runtime_map_entry(struct kobject *kobj, int nr)
 	entry = kzalloc(sizeof(*entry), GFP_KERNEL);
 	if (!entry) {
 		kset_unregister(map_kset);
-		return entry;
+		map_kset = NULL;
+		return ERR_PTR(-ENOMEM);
 	}
 
 	memcpy(&entry->md, efi_runtime_map + nr * efi_memdesc_size,
@@ -132,6 +133,7 @@ add_sysfs_runtime_map_entry(struct kobject *kobj, int nr)
 	if (ret) {
 		kobject_put(&entry->kobj);
 		kset_unregister(map_kset);
+		map_kset = NULL;
 		return ERR_PTR(ret);
 	}
 
@@ -195,8 +197,6 @@ out_add_entry:
 		entry = *(map_entries + j);
 		kobject_put(&entry->kobj);
 	}
-	if (map_kset)
-		kset_unregister(map_kset);
 out:
 	return ret;
 }

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

* Re: [patch] efi: fix error handling in add_sysfs_runtime_map_entry()
  2015-04-21 13:46 [patch] efi: fix error handling in add_sysfs_runtime_map_entry() Dan Carpenter
@ 2015-04-27 20:31 ` Matt Fleming
  0 siblings, 0 replies; 2+ messages in thread
From: Matt Fleming @ 2015-04-27 20:31 UTC (permalink / raw)
  To: Dan Carpenter
  Cc: Matt Fleming, Guangyu Sun, linux-efi-u79uwXL29TY76Z2rM5mHXA,
	Dave Young

On Tue, 21 Apr, at 04:46:28PM, Dan Carpenter wrote:
> I spotted two (difficult to hit) bugs while reviewing this.
> 
> 1)  There is a double free bug because we unregister "map_kset" in
>     add_sysfs_runtime_map_entry() and also efi_runtime_map_init().
> 2)  If we fail to allocate "entry" then we should return
>     ERR_PTR(-ENOMEM) instead of NULL.
> 
> Signed-off-by: Dan Carpenter <dan.carpenter-QHcLZuEGTsvQT0dZR+AlfA@public.gmane.org>
 
Thanks applied.

(Leaving the patch intact for Dave's benefit)

> diff --git a/drivers/firmware/efi/runtime-map.c b/drivers/firmware/efi/runtime-map.c
> index 87b8e3b..5c55227 100644
> --- a/drivers/firmware/efi/runtime-map.c
> +++ b/drivers/firmware/efi/runtime-map.c
> @@ -120,7 +120,8 @@ add_sysfs_runtime_map_entry(struct kobject *kobj, int nr)
>  	entry = kzalloc(sizeof(*entry), GFP_KERNEL);
>  	if (!entry) {
>  		kset_unregister(map_kset);
> -		return entry;
> +		map_kset = NULL;
> +		return ERR_PTR(-ENOMEM);
>  	}
>  
>  	memcpy(&entry->md, efi_runtime_map + nr * efi_memdesc_size,
> @@ -132,6 +133,7 @@ add_sysfs_runtime_map_entry(struct kobject *kobj, int nr)
>  	if (ret) {
>  		kobject_put(&entry->kobj);
>  		kset_unregister(map_kset);
> +		map_kset = NULL;
>  		return ERR_PTR(ret);
>  	}
>  
> @@ -195,8 +197,6 @@ out_add_entry:
>  		entry = *(map_entries + j);
>  		kobject_put(&entry->kobj);
>  	}
> -	if (map_kset)
> -		kset_unregister(map_kset);
>  out:
>  	return ret;
>  }
> --
> To unsubscribe from this list: send the line "unsubscribe linux-efi" in
> the body of a message to majordomo-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html

-- 
Matt Fleming, Intel Open Source Technology Center

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

end of thread, other threads:[~2015-04-27 20:31 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-04-21 13:46 [patch] efi: fix error handling in add_sysfs_runtime_map_entry() Dan Carpenter
2015-04-27 20:31 ` Matt Fleming

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).