[PATCH] tipc: check for null after calling kmemdup

Subsystems: networking [general], the rest, tipc network layer

STALE1726d

10 messages, 4 authors, 2021-11-16 · open the first message on its own page

[PATCH] tipc: check for null after calling kmemdup

From: Tadeusz Struk <hidden>
Date: 2021-11-11 20:59:46

kmemdup can return a null pointer so need to check for it, otherwise
the null key will be dereferenced later in tipc_crypto_key_xmit as
can be seen in the trace [1].

Cc: Jon Maloy <jmaloy@redhat.com>
Cc: Ying Xue <redacted>
Cc: "David S. Miller" <davem@davemloft.net>
Cc: Jakub Kicinski <kuba@kernel.org>
Cc: netdev@vger.kernel.org
Cc: tipc-discussion@lists.sourceforge.net
Cc: linux-kernel@vger.kernel.org
Cc: stable@vger.kernel.org # 5.15, 5.14, 5.10

[1] https://syzkaller.appspot.com/bug?id=bca180abb29567b189efdbdb34cbf7ba851c2a58

Reported-by: Dmitry Vyukov <dvyukov@google.com>
Signed-off-by: Tadeusz Struk <redacted>
---
 net/tipc/crypto.c | 5 +++++
 1 file changed, 5 insertions(+)
diff --git a/net/tipc/crypto.c b/net/tipc/crypto.c
index dc60c32bb70d..988a343f9fd5 100644
--- a/net/tipc/crypto.c
+++ b/net/tipc/crypto.c
@@ -597,6 +597,11 @@ static int tipc_aead_init(struct tipc_aead **aead, struct tipc_aead_key *ukey,
 	tmp->cloned = NULL;
 	tmp->authsize = TIPC_AES_GCM_TAG_SIZE;
 	tmp->key = kmemdup(ukey, tipc_aead_key_size(ukey), GFP_KERNEL);
+	if (!tmp->key) {
+		free_percpu(tmp->tfm_entry);
+		kfree_sensitive(tmp);
+		return -ENOMEM;
+	}
 	memcpy(&tmp->salt, ukey->key + keylen, TIPC_AES_GCM_SALT_SIZE);
 	atomic_set(&tmp->users, 0);
 	atomic64_set(&tmp->seqno, 0);
-- 
2.33.1

[PATCH] tipc: use consistent GFP flags

From: Tadeusz Struk <hidden>
Date: 2021-11-11 21:00:03

Some functions, like tipc_crypto_start use inconsisten GFP flags
when allocating memory. The mentioned function use GFP_ATOMIC to
to alloc a crypto instance, and then calls alloc_ordered_workqueue()
which allocates memory with GFP_KERNEL. tipc_aead_init() function
even uses GFP_KERNEL and GFP_ATOMIC interchangeably.
No doc comment specifies what context a function is designed to
work in, but the flags should at least be consistent within a function.

Cc: Jon Maloy <jmaloy@redhat.com>
Cc: Ying Xue <redacted>
Cc: "David S. Miller" <davem@davemloft.net>
Cc: Jakub Kicinski <kuba@kernel.org>
Cc: netdev@vger.kernel.org
Cc: tipc-discussion@lists.sourceforge.net
Cc: linux-kernel@vger.kernel.org

Signed-off-by: Tadeusz Struk <redacted>
---
 net/tipc/crypto.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
diff --git a/net/tipc/crypto.c b/net/tipc/crypto.c
index 988a343f9fd5..a59c4eece5db 100644
--- a/net/tipc/crypto.c
+++ b/net/tipc/crypto.c
@@ -524,7 +524,7 @@ static int tipc_aead_init(struct tipc_aead **aead, struct tipc_aead_key *ukey,
 		return -EEXIST;
 
 	/* Allocate a new AEAD */
-	tmp = kzalloc(sizeof(*tmp), GFP_ATOMIC);
+	tmp = kzalloc(sizeof(*tmp), GFP_KERNEL);
 	if (unlikely(!tmp))
 		return -ENOMEM;
 
@@ -1475,7 +1475,7 @@ int tipc_crypto_start(struct tipc_crypto **crypto, struct net *net,
 		return -EEXIST;
 
 	/* Allocate crypto */
-	c = kzalloc(sizeof(*c), GFP_ATOMIC);
+	c = kzalloc(sizeof(*c), GFP_KERNEL);
 	if (!c)
 		return -ENOMEM;
 
@@ -1489,7 +1489,7 @@ int tipc_crypto_start(struct tipc_crypto **crypto, struct net *net,
 	}
 
 	/* Allocate statistic structure */
-	c->stats = alloc_percpu_gfp(struct tipc_crypto_stats, GFP_ATOMIC);
+	c->stats = alloc_percpu(struct tipc_crypto_stats);
 	if (!c->stats) {
 		if (c->wq)
 			destroy_workqueue(c->wq);
@@ -2462,7 +2462,7 @@ static void tipc_crypto_work_tx(struct work_struct *work)
 	}
 
 	/* Lets duplicate it first */
-	skey = kmemdup(aead->key, tipc_aead_key_size(aead->key), GFP_ATOMIC);
+	skey = kmemdup(aead->key, tipc_aead_key_size(aead->key), GFP_KERNEL);
 	rcu_read_unlock();
 
 	/* Now, generate new key, initiate & distribute it */
-- 
2.33.1

Re: [PATCH] tipc: check for null after calling kmemdup

From: Jon Maloy <jmaloy@redhat.com>
Date: 2021-11-12 00:04:11


On 11/11/21 15:59, Tadeusz Struk wrote:
quoted hunk
kmemdup can return a null pointer so need to check for it, otherwise
the null key will be dereferenced later in tipc_crypto_key_xmit as
can be seen in the trace [1].

Cc: Jon Maloy <jmaloy@redhat.com>
Cc: Ying Xue <redacted>
Cc: "David S. Miller" <davem@davemloft.net>
Cc: Jakub Kicinski <kuba@kernel.org>
Cc: netdev@vger.kernel.org
Cc: tipc-discussion@lists.sourceforge.net
Cc: linux-kernel@vger.kernel.org
Cc: stable@vger.kernel.org # 5.15, 5.14, 5.10

[1] https://syzkaller.appspot.com/bug?id=bca180abb29567b189efdbdb34cbf7ba851c2a58

Reported-by: Dmitry Vyukov <dvyukov@google.com>
Signed-off-by: Tadeusz Struk <redacted>
---
  net/tipc/crypto.c | 5 +++++
  1 file changed, 5 insertions(+)
diff --git a/net/tipc/crypto.c b/net/tipc/crypto.c
index dc60c32bb70d..988a343f9fd5 100644
--- a/net/tipc/crypto.c
+++ b/net/tipc/crypto.c
@@ -597,6 +597,11 @@ static int tipc_aead_init(struct tipc_aead **aead, struct tipc_aead_key *ukey,
  	tmp->cloned = NULL;
  	tmp->authsize = TIPC_AES_GCM_TAG_SIZE;
  	tmp->key = kmemdup(ukey, tipc_aead_key_size(ukey), GFP_KERNEL);
+	if (!tmp->key) {
+		free_percpu(tmp->tfm_entry);
+		kfree_sensitive(tmp);
+		return -ENOMEM;
+	}
  	memcpy(&tmp->salt, ukey->key + keylen, TIPC_AES_GCM_SALT_SIZE);
  	atomic_set(&tmp->users, 0);
  	atomic64_set(&tmp->seqno, 0);
Acked-by: Jon Maloy <jmaloy@redhat.com>

Re: [PATCH] tipc: check for null after calling kmemdup

From: Jakub Kicinski <kuba@kernel.org>
Date: 2021-11-13 04:08:33

On Thu, 11 Nov 2021 12:59:15 -0800 Tadeusz Struk wrote:
kmemdup can return a null pointer so need to check for it, otherwise
the null key will be dereferenced later in tipc_crypto_key_xmit as
can be seen in the trace [1].
Reported-by: Dmitry Vyukov <dvyukov@google.com>
Signed-off-by: Tadeusz Struk <redacted>
Fixes: 1ef6f7c9390f ("tipc: add automatic session key exchange")

Re: [PATCH] tipc: check for null after calling kmemdup

From: Jakub Kicinski <kuba@kernel.org>
Date: 2021-11-13 04:13:39

On Thu, 11 Nov 2021 19:06:18 -0500 Jon Maloy wrote:
On 11/11/21 15:59, Tadeusz Struk wrote:
quoted
kmemdup can return a null pointer so need to check for it, otherwise
the null key will be dereferenced later in tipc_crypto_key_xmit as
can be seen in the trace [1].
quoted
[1] https://syzkaller.appspot.com/bug?id=bca180abb29567b189efdbdb34cbf7ba851c2a58

Reported-by: Dmitry Vyukov <dvyukov@google.com>
Signed-off-by: Tadeusz Struk <redacted>
---
  net/tipc/crypto.c | 5 +++++
  1 file changed, 5 insertions(+)
diff --git a/net/tipc/crypto.c b/net/tipc/crypto.c
index dc60c32bb70d..988a343f9fd5 100644
--- a/net/tipc/crypto.c
+++ b/net/tipc/crypto.c
@@ -597,6 +597,11 @@ static int tipc_aead_init(struct tipc_aead **aead, struct tipc_aead_key *ukey,
  	tmp->cloned = NULL;
  	tmp->authsize = TIPC_AES_GCM_TAG_SIZE;
  	tmp->key = kmemdup(ukey, tipc_aead_key_size(ukey), GFP_KERNEL);
+	if (!tmp->key) {
+		free_percpu(tmp->tfm_entry);
+		kfree_sensitive(tmp);
+		return -ENOMEM;
+	}
Acked-by: Jon Maloy <jmaloy@redhat.com>
Hm, shouldn't we free all the tfm entries here?

Re: [PATCH] tipc: check for null after calling kmemdup

From: Tadeusz Struk <hidden>
Date: 2021-11-13 05:42:18

On 11/12/21 20:13, Jakub Kicinski wrote:
quoted
quoted
@@ -597,6 +597,11 @@ static int tipc_aead_init(struct tipc_aead **aead, struct tipc_aead_key *ukey,
   	tmp->cloned = NULL;
   	tmp->authsize = TIPC_AES_GCM_TAG_SIZE;
   	tmp->key = kmemdup(ukey, tipc_aead_key_size(ukey), GFP_KERNEL);
+	if (!tmp->key) {
+		free_percpu(tmp->tfm_entry);
+		kfree_sensitive(tmp);
+		return -ENOMEM;
+	}
Acked-by: Jon Maloy<jmaloy@redhat.com>
Hm, shouldn't we free all the tfm entries here?
Right, I think we just need to call tipc_aead_free(&tmp->rcu);
here and return an error.

-- 
Thanks,
Tadeusz

Re: [PATCH] tipc: use consistent GFP flags

From: patchwork-bot+netdevbpf@kernel.org
Date: 2021-11-15 13:00:46

Hello:

This patch was applied to netdev/net.git (master)
by David S. Miller [off-list ref]:

On Thu, 11 Nov 2021 12:59:16 -0800 you wrote:
Some functions, like tipc_crypto_start use inconsisten GFP flags
when allocating memory. The mentioned function use GFP_ATOMIC to
to alloc a crypto instance, and then calls alloc_ordered_workqueue()
which allocates memory with GFP_KERNEL. tipc_aead_init() function
even uses GFP_KERNEL and GFP_ATOMIC interchangeably.
No doc comment specifies what context a function is designed to
work in, but the flags should at least be consistent within a function.

[...]
Here is the summary with links:
  - tipc: use consistent GFP flags
    https://git.kernel.org/netdev/net/c/86c3a3e964d9

You are awesome, thank you!
-- 
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html

Re: [PATCH] tipc: use consistent GFP flags

From: Tadeusz Struk <hidden>
Date: 2021-11-16 16:40:57

On 11/15/21 05:00, patchwork-bot+netdevbpf@kernel.org wrote:
This patch was applied to netdev/net.git (master)
by David S. Miller[off-list ref]:

On Thu, 11 Nov 2021 12:59:16 -0800 you wrote:
quoted
Some functions, like tipc_crypto_start use inconsisten GFP flags
when allocating memory. The mentioned function use GFP_ATOMIC to
to alloc a crypto instance, and then calls alloc_ordered_workqueue()
which allocates memory with GFP_KERNEL. tipc_aead_init() function
even uses GFP_KERNEL and GFP_ATOMIC interchangeably.
No doc comment specifies what context a function is designed to
work in, but the flags should at least be consistent within a function.

[...]
Here is the summary with links:
   - tipc: use consistent GFP flags
     https://git.kernel.org/netdev/net/c/86c3a3e964d9

You are awesome, thank you!
Thanks, you are awesome too! ;)
Any feedback about the patch:
[PATCH v2] tipc: check for null after calling kmemdup

-- 
Thanks,
Tadeusz

Re: [PATCH] tipc: check for null after calling kmemdup

From: Jakub Kicinski <kuba@kernel.org>
Date: 2021-11-16 19:32:42

On Fri, 12 Nov 2021 21:42:11 -0800 Tadeusz Struk wrote:
On 11/12/21 20:13, Jakub Kicinski wrote:
 [...]  
quoted
quoted
Acked-by: Jon Maloy<jmaloy@redhat.com>  
Hm, shouldn't we free all the tfm entries here?  
Right, I think we just need to call tipc_aead_free(&tmp->rcu);
here and return an error.
Would be good to get an ack From Jon or Ying on that one.

Re: [PATCH] tipc: check for null after calling kmemdup

From: Jakub Kicinski <kuba@kernel.org>
Date: 2021-11-16 19:33:31

On Tue, 16 Nov 2021 11:32:32 -0800 Jakub Kicinski wrote:
On Fri, 12 Nov 2021 21:42:11 -0800 Tadeusz Struk wrote:
quoted
On 11/12/21 20:13, Jakub Kicinski wrote:
 [...]    
 [...]  
quoted
quoted
Hm, shouldn't we free all the tfm entries here?    
Right, I think we just need to call tipc_aead_free(&tmp->rcu);
here and return an error.  
Would be good to get an ack From Jon or Ying on that one.
Ah, I hit reply on the wrong sub-thread, this was meant for the "any
feedback question".
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help