Skip to contentSkip to conversation

fanotify: add error handling for kmem_cache_create

PATCH In review Zhouyang Jia 2 participants 2 messages Jun 12, 2018

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 fanotify: add error handling for kmem_cache_create

When kmem_cache_create fails, the lack of error-handling code may cause unexpected results.

This patch adds error-handling code after calling kmem_cache_create.

---
 fs/notify/fanotify/fanotify_user.c | 5 +++++
 1 file changed, 5 insertions(+)
fs/notify/fanotify/fanotify_user.c +5 -0
index ec4d8c5..e3fa861 100644
@@ -959,9 +959,14 @@ static int __init fanotify_user_setup(void)
 {
 	fanotify_mark_cache = KMEM_CACHE(fsnotify_mark, SLAB_PANIC);
 	fanotify_event_cachep = KMEM_CACHE(fanotify_event_info, SLAB_PANIC);
+	if (!fanotify_mark_cache || !fanotify_event_cachep)
+		return -ENOMEM;
+
 	if (IS_ENABLED(CONFIG_FANOTIFY_ACCESS_PERMISSIONS)) {
 		fanotify_perm_event_cachep =
 			KMEM_CACHE(fanotify_perm_event_info, SLAB_PANIC);
+		if (!fanotify_perm_event_cachep)
+			return -ENOMEM;
 	}
 
 	return 0;
Signature · 1 line
2.7.4
#1 lore raw
Quoted 15 lines · Zhouyang Jia

When kmem_cache_create fails, the lack of error-handling code may cause unexpected results.

This patch adds error-handling code after calling kmem_cache_create.

---
 fs/notify/fanotify/fanotify_user.c | 5 +++++
 1 file changed, 5 insertions(+)
fs/notify/fanotify/fanotify_user.c +0 -0
index ec4d8c5..e3fa861 100644

fs/notify/fanotify/fanotify_user.c static int __init fanotify_user_setup(void) · line 963

Quoted 5 lines
fs/notify/fanotify/fanotify_user.c +2 -0
@@ -959,9 +959,14 @@ static int __init fanotify_user_setup(void)
 {
        fanotify_mark_cache = KMEM_CACHE(fsnotify_mark, SLAB_PANIC);
        fanotify_event_cachep = KMEM_CACHE(fanotify_event_info, SLAB_PANIC);
+       if (!fanotify_mark_cache || !fanotify_event_cachep)
+               return -ENOMEM;

If only one failed need to free the other.

fs/notify/fanotify/fanotify_user.c static int __init fanotify_user_setup(void) · line 969

Quoted 6 lines
fs/notify/fanotify/fanotify_user.c +3 -0
+
        if (IS_ENABLED(CONFIG_FANOTIFY_ACCESS_PERMISSIONS)) {
                fanotify_perm_event_cachep =
                        KMEM_CACHE(fanotify_perm_event_info, SLAB_PANIC);
+               if (!fanotify_perm_event_cachep)
+                       return -ENOMEM;

here as well.
best implement as goto fail

fs/notify/fanotify/fanotify_user.c static int __init fanotify_user_setup(void) · line 972

Quoted 3 lines
fs/notify/fanotify/fanotify_user.c +0 -0
        }
 
        return 0;

fail:

           if (fanotify_mark_cache)
                            kmem_cache_destroy(fanotify_mark_cache);
           ...
           return -ENOMEM;

Thanks,
Amir.