[PATCH] net: xfrm: assign the per_cpu_ptr pointer before return

Subsystems: networking [general], networking [ipsec], the rest

STALE1839d

4 messages, 2 authors, 2021-08-18 · open the first message on its own page

[PATCH] net: xfrm: assign the per_cpu_ptr pointer before return

From: Dongliang Mu <hidden>
Date: 2021-08-18 03:26:23

In ipcomp_alloc_scratches, if the vmalloc fails, there leaves an oudated
pointer in *per_cpu_ptr(scratches, i). However, in the error handling of
ipcomp_init_state, if ipcomp_alloc_scratches fails, it will invoke
ipcomp_free_scratches directly. This functio will vfree the per_cpu_ptr
pointer. If the pointer points to an invalid memory, the kernel crashes.

Fix this by moving the assignment of per_cpu_ptr point before return.

Call Trace:
 ipcomp_free_scratches+0xbc/0x160 net/xfrm/xfrm_ipcomp.c:203
 ipcomp_free_data net/xfrm/xfrm_ipcomp.c:312 [inline]
 ipcomp_init_state+0x77c/0xa40 net/xfrm/xfrm_ipcomp.c:364
 ipcomp6_init_state+0xc2/0x700 net/ipv6/ipcomp6.c:154
 __xfrm_init_state+0x995/0x15c0 net/xfrm/xfrm_state.c:2648
 xfrm_init_state+0x1a/0x70 net/xfrm/xfrm_state.c:2675
 pfkey_msg2xfrm_state net/key/af_key.c:1287 [inline]
 pfkey_add+0x1a64/0x2cd0 net/key/af_key.c:1504
 pfkey_process+0x685/0x7e0 net/key/af_key.c:2837
 pfkey_sendmsg+0x43a/0x820 net/key/af_key.c:3676
 sock_sendmsg_nosec net/socket.c:703 [inline]
 sock_sendmsg+0xcf/0x120 net/socket.c:723

Reported-by: syzbot+b9cfd1cc5d57ee0a09ab@syzkaller.appspotmail.com
Cc: stable@vger.kernel.org
Fixes: 6fccab671f2f ("ipsec: ipcomp - Merge IPComp impl")
Signed-off-by: Dongliang Mu <redacted>
---
 net/xfrm/xfrm_ipcomp.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/net/xfrm/xfrm_ipcomp.c b/net/xfrm/xfrm_ipcomp.c
index cb40ff0ff28d..01dbec70dfba 100644
--- a/net/xfrm/xfrm_ipcomp.c
+++ b/net/xfrm/xfrm_ipcomp.c
@@ -223,9 +223,9 @@ static void * __percpu *ipcomp_alloc_scratches(void)
 		void *scratch;
 
 		scratch = vmalloc_node(IPCOMP_SCRATCH_SIZE, cpu_to_node(i));
+		*per_cpu_ptr(scratches, i) = scratch;
 		if (!scratch)
 			return NULL;
-		*per_cpu_ptr(scratches, i) = scratch;
 	}
 
 	return scratches;
-- 
2.25.1

Re: [PATCH] net: xfrm: assign the per_cpu_ptr pointer before return

From: Herbert Xu <herbert@gondor.apana.org.au>
Date: 2021-08-18 03:32:31

On Wed, Aug 18, 2021 at 11:25:53AM +0800, Dongliang Mu wrote:
quoted hunk
diff --git a/net/xfrm/xfrm_ipcomp.c b/net/xfrm/xfrm_ipcomp.c
index cb40ff0ff28d..01dbec70dfba 100644
--- a/net/xfrm/xfrm_ipcomp.c
+++ b/net/xfrm/xfrm_ipcomp.c
@@ -223,9 +223,9 @@ static void * __percpu *ipcomp_alloc_scratches(void)
 		void *scratch;
 
 		scratch = vmalloc_node(IPCOMP_SCRATCH_SIZE, cpu_to_node(i));
+		*per_cpu_ptr(scratches, i) = scratch;
 		if (!scratch)
 			return NULL;
-		*per_cpu_ptr(scratches, i) = scratch;
scratches comes from alloc_percpu so it should already be zeroed.

Cheers,
-- 
Email: Herbert Xu [off-list ref]
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt

Re: [PATCH] net: xfrm: assign the per_cpu_ptr pointer before return

From: Dongliang Mu <hidden>
Date: 2021-08-18 10:40:06

On Wed, Aug 18, 2021 at 11:32 AM Herbert Xu [off-list ref] wrote:
On Wed, Aug 18, 2021 at 11:25:53AM +0800, Dongliang Mu wrote:
quoted
diff --git a/net/xfrm/xfrm_ipcomp.c b/net/xfrm/xfrm_ipcomp.c
index cb40ff0ff28d..01dbec70dfba 100644
--- a/net/xfrm/xfrm_ipcomp.c
+++ b/net/xfrm/xfrm_ipcomp.c
@@ -223,9 +223,9 @@ static void * __percpu *ipcomp_alloc_scratches(void)
              void *scratch;

              scratch = vmalloc_node(IPCOMP_SCRATCH_SIZE, cpu_to_node(i));
+             *per_cpu_ptr(scratches, i) = scratch;
              if (!scratch)
                      return NULL;
-             *per_cpu_ptr(scratches, i) = scratch;
scratches comes from alloc_percpu so it should already be zeroed.
Correct.

:\ Then I have no idea how this crash occurs. This crash report does
not have any reproducer. It seems like a random crash, but I am not
sure.

If you have any patch for this crash, please let me know.
Cheers,
--
Email: Herbert Xu [off-list ref]
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt

Re: [PATCH] net: xfrm: assign the per_cpu_ptr pointer before return

From: Herbert Xu <herbert@gondor.apana.org.au>
Date: 2021-08-18 12:49:11

On Wed, Aug 18, 2021 at 06:39:25PM +0800, Dongliang Mu wrote:
:\ Then I have no idea how this crash occurs. This crash report does
not have any reproducer. It seems like a random crash, but I am not
sure.

If you have any patch for this crash, please let me know.
I don't have any ideas either, sorry.  But my guess would be that
there is corruption somewhere else that's showing up in ipcomp.

Cheers,
-- 
Email: Herbert Xu [off-list ref]
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help