Skip to contentSkip to conversation

net: Fix potential memory leak in proto_register()

PATCH linmiaohe 2 participants 2 messages Aug 12, 2020

Subsystems (2)
lore
Share & tools
lore

linmiaohe

author
#0 lore raw

Hi all:

Quoted 18 lines · David Miller

From: Cong Wang <[email protected]>
Date: Tue, 11 Aug 2020 16:02:51 -0700

Quoted 15 lines

Review

Quoted 9 lines
(quoted diff) +0 -0
@@ -3406,6 +3406,16 @@ static void sock_inuse_add(struct net *net, 

int val) } #endif

(quoted diff) +6 -0
+static void tw_prot_cleanup(struct timewait_sock_ops *twsk_prot) {
+       if (!twsk_prot)
+               return;
+       kfree(twsk_prot->twsk_slab_name);
+       twsk_prot->twsk_slab_name = NULL;
+       kmem_cache_destroy(twsk_prot->twsk_slab);

Hmm, are you sure you can free the kmem cache name before kmem_cache_destroy()? To me, it seems kmem_cache_destroy() frees the name via slab_kmem_cache_release() via kfree_const(). With your patch, we have a double-free on the name?

Quoted 5 lines
Quoted 1 line

Or am I missing anything?

Yep, there is a double free here.

Please fix this.

Many thanks for both of you to point this issue out. But I'am not really understand, could you please explain it more? As far as I can see, the double free path is:
1. kfree(twsk_prot->twsk_slab_name)
2. kmem_cache_destroy

	--> shutdown_memcg_caches
		--> shutdown_cache
			--> slab_kmem_cache_release
				--> kfree_const(s->name)

But twsk_prot->twsk_slab_name is allocated from kasprintf via kmalloc_track_caller while twsk_prot->twsk_slab->name is allocated via kstrdup_const. So I think twsk_prot->twsk_slab_name and twsk_prot->twsk_slab->name point to different memory, and there is no double free.

Or am I missing anything?

By the way, req_prot_cleanup() do the same things, i.e. free the slab_name before involve kmem_cache_destroy(). If there is a double free, so as here?

Thanks.

#1 lore raw
Quoted 31 lines · linmiaohe

Hi all:

Quoted 18 lines · David Miller

From: Cong Wang <[email protected]>
Date: Tue, 11 Aug 2020 16:02:51 -0700

Quoted 15 lines

Review

Quoted 9 lines
(quoted diff) +0 -0
@@ -3406,6 +3406,16 @@ static void sock_inuse_add(struct net *net,

int val) } #endif

(quoted diff) +6 -0
+static void tw_prot_cleanup(struct timewait_sock_ops *twsk_prot) {
+       if (!twsk_prot)
+               return;
+       kfree(twsk_prot->twsk_slab_name);
+       twsk_prot->twsk_slab_name = NULL;
+       kmem_cache_destroy(twsk_prot->twsk_slab);

Hmm, are you sure you can free the kmem cache name before kmem_cache_destroy()? To me, it seems kmem_cache_destroy() frees the name via slab_kmem_cache_release() via kfree_const(). With your patch, we have a double-free on the name?

Quoted 5 lines
Quoted 1 line

Or am I missing anything?

Yep, there is a double free here.

Please fix this.

Many thanks for both of you to point this issue out. But I'am not really understand, could you please explain it more? As far as I can see, the double free path is:
1. kfree(twsk_prot->twsk_slab_name)
2. kmem_cache_destroy

Quoted 7 lines
        --> shutdown_memcg_caches
                --> shutdown_cache
                        --> slab_kmem_cache_release
                                --> kfree_const(s->name)

But twsk_prot->twsk_slab_name is allocated from kasprintf via kmalloc_track_caller while twsk_prot->twsk_slab->name is allocated via kstrdup_const. So I think twsk_prot->twsk_slab_name and twsk_prot->twsk_slab->name point to different memory, and there is no double free.

You are right. Since it is duplicated, then there is no need to keep ->twsk_slab_name, we can just use twsk_slab->name. I will send a patch to get rid of it.

Quoted 4 lines

Or am I missing anything?

By the way, req_prot_cleanup() do the same things, i.e. free the slab_name before involve kmem_cache_destroy(). If there is a double free, so as here?

Ditto. Can be just removed.

Thanks.