diff mbox series

tipc: stop tipc crypto on failure in tipc_node_create

Message ID 20230725183646.5668-1-pchelkin@ispras.ru (mailing list archive)
State Superseded
Delegated to: Netdev Maintainers
Headers show
Series tipc: stop tipc crypto on failure in tipc_node_create | expand

Checks

Context Check Description
netdev/series_format warning Single patches do not need cover letters; Target tree name not specified in the subject
netdev/tree_selection success Guessed tree name to be net-next
netdev/fixes_present success Fixes tag not required for -next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 1342 this patch: 1342
netdev/cc_maintainers success CCed 9 of 9 maintainers
netdev/build_clang success Errors and warnings before: 1365 this patch: 1365
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success Fixes tag looks correct
netdev/build_allmodconfig_warn success Errors and warnings before: 1365 this patch: 1365
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 35 lines checked
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0

Commit Message

Fedor Pchelkin July 25, 2023, 6:36 p.m. UTC
If tipc_link_bc_create() fails inside tipc_node_create() for a newly
allocated tipc node then we should stop its tipc crypto and free the
resources allocated with a call to tipc_crypto_start().

Call tipc_crypto_stop() in that case. Also extract the similar error exit
paths into a goto statement.

Found by Linux Verification Center (linuxtesting.org).

Fixes: cb8092d70a6f ("tipc: move bc link creation back to tipc_node_create")
Signed-off-by: Fedor Pchelkin <pchelkin@ispras.ru>
---
 net/tipc/node.c | 17 +++++++++++------
 1 file changed, 11 insertions(+), 6 deletions(-)

Comments

Xin Long July 25, 2023, 7:46 p.m. UTC | #1
On Tue, Jul 25, 2023 at 2:37 PM Fedor Pchelkin <pchelkin@ispras.ru> wrote:
>
> If tipc_link_bc_create() fails inside tipc_node_create() for a newly
> allocated tipc node then we should stop its tipc crypto and free the
> resources allocated with a call to tipc_crypto_start().
>
> Call tipc_crypto_stop() in that case. Also extract the similar error exit
> paths into a goto statement.
>
> Found by Linux Verification Center (linuxtesting.org).
>
> Fixes: cb8092d70a6f ("tipc: move bc link creation back to tipc_node_create")
> Signed-off-by: Fedor Pchelkin <pchelkin@ispras.ru>
> ---
>  net/tipc/node.c | 17 +++++++++++------
>  1 file changed, 11 insertions(+), 6 deletions(-)
>
> diff --git a/net/tipc/node.c b/net/tipc/node.c
> index 5e000fde8067..0d64005a803b 100644
> --- a/net/tipc/node.c
> +++ b/net/tipc/node.c
> @@ -546,9 +546,7 @@ struct tipc_node *tipc_node_create(struct net *net, u32 addr, u8 *peer_id,
>  #ifdef CONFIG_TIPC_CRYPTO
>         if (unlikely(tipc_crypto_start(&n->crypto_rx, net, n))) {
>                 pr_warn("Failed to start crypto RX(%s)!\n", n->peer_id_string);
> -               kfree(n);
> -               n = NULL;
> -               goto exit;
> +               goto free_node;
>         }
>  #endif
>         n->addr = addr;
> @@ -583,9 +581,7 @@ struct tipc_node *tipc_node_create(struct net *net, u32 addr, u8 *peer_id,
>                                  n->capabilities, &n->bc_entry.inputq1,
>                                  &n->bc_entry.namedq, snd_l, &n->bc_entry.link)) {
>                 pr_warn("Broadcast rcv link creation failed, no memory\n");
> -               kfree(n);
> -               n = NULL;
> -               goto exit;
> +               goto stop_crypto;
>         }
>         tipc_node_get(n);
Can you please try moving up tipc_node_get(n) ahead tipc_link_bc_create()
and use tipc_node_put(n) to replace kfree(n) to avoid the extra
tipc_crypto_stop() call below?

Thanks.

>         timer_setup(&n->timer, tipc_node_timeout, 0);
> @@ -610,6 +606,15 @@ struct tipc_node *tipc_node_create(struct net *net, u32 addr, u8 *peer_id,
>  exit:
>         spin_unlock_bh(&tn->node_list_lock);
>         return n;
> +stop_crypto:
> +
> +#ifdef CONFIG_TIPC_CRYPTO
> +       tipc_crypto_stop(&n->crypto_rx);
> +free_node:
> +#endif
> +       kfree(n);
> +       spin_unlock_bh(&tn->node_list_lock);
> +       return NULL;
>  }
>
>  static void tipc_node_calculate_timer(struct tipc_node *n, struct tipc_link *l)
> --
> 2.41.0
>
Fedor Pchelkin July 25, 2023, 9:38 p.m. UTC | #2
On 23/07/25 03:46PM, Xin Long wrote:
> On Tue, Jul 25, 2023 at 2:37 PM Fedor Pchelkin <pchelkin@ispras.ru> wrote:
> >
> > If tipc_link_bc_create() fails inside tipc_node_create() for a newly
> > allocated tipc node then we should stop its tipc crypto and free the
> > resources allocated with a call to tipc_crypto_start().
> >
> > Call tipc_crypto_stop() in that case. Also extract the similar error exit
> > paths into a goto statement.
> >
> > Found by Linux Verification Center (linuxtesting.org).
> >
> > Fixes: cb8092d70a6f ("tipc: move bc link creation back to tipc_node_create")
> > Signed-off-by: Fedor Pchelkin <pchelkin@ispras.ru>
> > ---
> >  net/tipc/node.c | 17 +++++++++++------
> >  1 file changed, 11 insertions(+), 6 deletions(-)
> >
> > diff --git a/net/tipc/node.c b/net/tipc/node.c
> > index 5e000fde8067..0d64005a803b 100644
> > --- a/net/tipc/node.c
> > +++ b/net/tipc/node.c
> > @@ -546,9 +546,7 @@ struct tipc_node *tipc_node_create(struct net *net, u32 addr, u8 *peer_id,
> >  #ifdef CONFIG_TIPC_CRYPTO
> >         if (unlikely(tipc_crypto_start(&n->crypto_rx, net, n))) {
> >                 pr_warn("Failed to start crypto RX(%s)!\n", n->peer_id_string);
> > -               kfree(n);
> > -               n = NULL;
> > -               goto exit;
> > +               goto free_node;
> >         }
> >  #endif
> >         n->addr = addr;
> > @@ -583,9 +581,7 @@ struct tipc_node *tipc_node_create(struct net *net, u32 addr, u8 *peer_id,
> >                                  n->capabilities, &n->bc_entry.inputq1,
> >                                  &n->bc_entry.namedq, snd_l, &n->bc_entry.link)) {
> >                 pr_warn("Broadcast rcv link creation failed, no memory\n");
> > -               kfree(n);
> > -               n = NULL;
> > -               goto exit;
> > +               goto stop_crypto;
> >         }
> >         tipc_node_get(n);
> Can you please try moving up tipc_node_get(n) ahead tipc_link_bc_create()
> and use tipc_node_put(n) to replace kfree(n) to avoid the extra
> tipc_crypto_stop() call below?
> 
> Thanks.
> 

Guess moving tipc_node_get() before tipc_link_bc_create() would not solve
the problem as ref is already initialized to 1 at that point. So just
replacing direct kfree() with tipc_node_put() will fix it.

Thank you for advice! I'll resend the v2 shortly.

> >         timer_setup(&n->timer, tipc_node_timeout, 0);
> > @@ -610,6 +606,15 @@ struct tipc_node *tipc_node_create(struct net *net, u32 addr, u8 *peer_id,
> >  exit:
> >         spin_unlock_bh(&tn->node_list_lock);
> >         return n;
> > +stop_crypto:
> > +
> > +#ifdef CONFIG_TIPC_CRYPTO
> > +       tipc_crypto_stop(&n->crypto_rx);
> > +free_node:
> > +#endif
> > +       kfree(n);
> > +       spin_unlock_bh(&tn->node_list_lock);
> > +       return NULL;
> >  }
> >
> >  static void tipc_node_calculate_timer(struct tipc_node *n, struct tipc_link *l)
> > --
> > 2.41.0
> >
diff mbox series

Patch

diff --git a/net/tipc/node.c b/net/tipc/node.c
index 5e000fde8067..0d64005a803b 100644
--- a/net/tipc/node.c
+++ b/net/tipc/node.c
@@ -546,9 +546,7 @@  struct tipc_node *tipc_node_create(struct net *net, u32 addr, u8 *peer_id,
 #ifdef CONFIG_TIPC_CRYPTO
 	if (unlikely(tipc_crypto_start(&n->crypto_rx, net, n))) {
 		pr_warn("Failed to start crypto RX(%s)!\n", n->peer_id_string);
-		kfree(n);
-		n = NULL;
-		goto exit;
+		goto free_node;
 	}
 #endif
 	n->addr = addr;
@@ -583,9 +581,7 @@  struct tipc_node *tipc_node_create(struct net *net, u32 addr, u8 *peer_id,
 				 n->capabilities, &n->bc_entry.inputq1,
 				 &n->bc_entry.namedq, snd_l, &n->bc_entry.link)) {
 		pr_warn("Broadcast rcv link creation failed, no memory\n");
-		kfree(n);
-		n = NULL;
-		goto exit;
+		goto stop_crypto;
 	}
 	tipc_node_get(n);
 	timer_setup(&n->timer, tipc_node_timeout, 0);
@@ -610,6 +606,15 @@  struct tipc_node *tipc_node_create(struct net *net, u32 addr, u8 *peer_id,
 exit:
 	spin_unlock_bh(&tn->node_list_lock);
 	return n;
+stop_crypto:
+
+#ifdef CONFIG_TIPC_CRYPTO
+	tipc_crypto_stop(&n->crypto_rx);
+free_node:
+#endif
+	kfree(n);
+	spin_unlock_bh(&tn->node_list_lock);
+	return NULL;
 }
 
 static void tipc_node_calculate_timer(struct tipc_node *n, struct tipc_link *l)