All of lore.kernel.org
 help / color / mirror / Atom feed
* re: mac80111: Add BIP-GMAC-128 and BIP-GMAC-256 ciphers
@ 2015-03-19 12:53 Dan Carpenter
  2015-03-23 13:24 ` Jouni Malinen
  0 siblings, 1 reply; 6+ messages in thread
From: Dan Carpenter @ 2015-03-19 12:53 UTC (permalink / raw)
  To: jouni; +Cc: linux-wireless

Hello Jouni Malinen,

The patch 8ade538bf39b: "mac80111: Add BIP-GMAC-128 and BIP-GMAC-256
ciphers" from Jan 24, 2015, leads to the following static checker
warning:

	net/mac80211/aes_gmac.c:74 ieee80211_aes_gmac_key_setup()
	warn: we tested 'err' before and it was 'true'

net/mac80211/aes_gmac.c
    61  struct crypto_aead *ieee80211_aes_gmac_key_setup(const u8 key[],
    62                                                   size_t key_len)
    63  {
    64          struct crypto_aead *tfm;
    65          int err;
    66  
    67          tfm = crypto_alloc_aead("gcm(aes)", 0, CRYPTO_ALG_ASYNC);
    68          if (IS_ERR(tfm))
    69                  return tfm;
    70  
    71          err = crypto_aead_setkey(tfm, key, key_len);
    72          if (!err)
                    ^^^^
This is success handling.  In the kernel, everyone expects error
hanlding like "if (err) " so this makes the code hard to read if you
have a job and need to read code quickly.  At first I missed the "!"
character, and then I thought,  "What??  Is err a pointer?"

    73                  return tfm;
    74          if (!err)
    75                  err = crypto_aead_setauthsize(tfm, GMAC_MIC_LEN);
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
This is dead code.

    76  
    77          crypto_free_aead(tfm);
    78          return ERR_PTR(err);

This function should be a list of commands in a row with tiny detours
for exceptions and error handling.  It messes everyone up if the success
path is hidden somewhere in the middle and it leads to bugs like this.

    79  }

regards,
dan carpenter

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: mac80111: Add BIP-GMAC-128 and BIP-GMAC-256 ciphers
  2015-03-19 12:53 mac80111: Add BIP-GMAC-128 and BIP-GMAC-256 ciphers Dan Carpenter
@ 2015-03-23 13:24 ` Jouni Malinen
  2015-03-23 14:08   ` [patch] mac80111: aes_ccm: cleanup ieee80211_aes_key_setup_encrypt() Dan Carpenter
  2015-03-23 14:40   ` mac80111: Add BIP-GMAC-128 and BIP-GMAC-256 ciphers Dan Carpenter
  0 siblings, 2 replies; 6+ messages in thread
From: Jouni Malinen @ 2015-03-23 13:24 UTC (permalink / raw)
  To: Dan Carpenter; +Cc: linux-wireless

On Thu, Mar 19, 2015 at 03:53:14PM +0300, Dan Carpenter wrote:
> The patch 8ade538bf39b: "mac80111: Add BIP-GMAC-128 and BIP-GMAC-256
> ciphers" from Jan 24, 2015, leads to the following static checker
> warning:

> net/mac80211/aes_gmac.c
>     61  struct crypto_aead *ieee80211_aes_gmac_key_setup(const u8 key[],

>     71          err = crypto_aead_setkey(tfm, key, key_len);
>     72          if (!err)
>                     ^^^^
> This is success handling.  In the kernel, everyone expects error
> hanlding like "if (err) " so this makes the code hard to read if you
> have a job and need to read code quickly.  At first I missed the "!"
> character, and then I thought,  "What??  Is err a pointer?"

This follows the style used in net/mac80211/aes_ccm.c for an equivalent
key_setup function. I have no issues in someone cleaning that up as long
as it is done consistently through all net/mac80211 key_setup functions.

>     73                  return tfm;
>     74          if (!err)
>     75                  err = crypto_aead_setauthsize(tfm, GMAC_MIC_LEN);
>                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> This is dead code.

That's interesting.. This must be some kind of rebasing issue since this
obviously makes no sense. Lines 72-73 were supposed to be after this
line 75 just like this is done in aes_gcm.c and aes_ccm.c. Thanks for
reporting this, I'll fix it.

>     77          crypto_free_aead(tfm);
>     78          return ERR_PTR(err);
> 
> This function should be a list of commands in a row with tiny detours
> for exceptions and error handling.  It messes everyone up if the success
> path is hidden somewhere in the middle and it leads to bugs like this.

I'm not sure that would be behind this specific bug, but like noted
above, no objection to someone cleaning this up consistently.

-- 
Jouni Malinen                                            PGP id EFC895FA

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [patch] mac80111: aes_ccm: cleanup ieee80211_aes_key_setup_encrypt()
  2015-03-23 13:24 ` Jouni Malinen
@ 2015-03-23 14:08   ` Dan Carpenter
  2015-03-24  9:32     ` Ard Biesheuvel
  2015-03-30  8:40     ` Johannes Berg
  2015-03-23 14:40   ` mac80111: Add BIP-GMAC-128 and BIP-GMAC-256 ciphers Dan Carpenter
  1 sibling, 2 replies; 6+ messages in thread
From: Dan Carpenter @ 2015-03-23 14:08 UTC (permalink / raw)
  To: Johannes Berg, Jouni Malinen; +Cc: linux-wireless, Ard Biesheuvel

This code is written using an anti-pattern called "success handling"
which makes it hard to read, especially if you are used to normal kernel
style.  It should instead be written as a list of directives in a row
with branches for error handling.

Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>

diff --git a/net/mac80211/aes_ccm.c b/net/mac80211/aes_ccm.c
index 7869bb40..208df7c 100644
--- a/net/mac80211/aes_ccm.c
+++ b/net/mac80211/aes_ccm.c
@@ -85,11 +85,15 @@ struct crypto_aead *ieee80211_aes_key_setup_encrypt(const u8 key[],
 		return tfm;
 
 	err = crypto_aead_setkey(tfm, key, key_len);
-	if (!err)
-		err = crypto_aead_setauthsize(tfm, mic_len);
-	if (!err)
-		return tfm;
+	if (err)
+		goto free_aead;
+	err = crypto_aead_setauthsize(tfm, mic_len);
+	if (err)
+		goto free_aead;
+
+	return tfm;
 
+free_aead:
 	crypto_free_aead(tfm);
 	return ERR_PTR(err);
 }

^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: mac80111: Add BIP-GMAC-128 and BIP-GMAC-256 ciphers
  2015-03-23 13:24 ` Jouni Malinen
  2015-03-23 14:08   ` [patch] mac80111: aes_ccm: cleanup ieee80211_aes_key_setup_encrypt() Dan Carpenter
@ 2015-03-23 14:40   ` Dan Carpenter
  1 sibling, 0 replies; 6+ messages in thread
From: Dan Carpenter @ 2015-03-23 14:40 UTC (permalink / raw)
  To: Jouni Malinen; +Cc: linux-wireless

On Mon, Mar 23, 2015 at 03:24:09PM +0200, Jouni Malinen wrote:
> > This function should be a list of commands in a row with tiny detours
> > for exceptions and error handling.  It messes everyone up if the success
> > path is hidden somewhere in the middle and it leads to bugs like this.
> 
> I'm not sure that would be behind this specific bug, 

It is though.

1) The code here:

	err = crypto_aead_setkey(tfm, key, key_len);
	if (!err)
		return tfm;

   That looks like normal error handling code so your mind glosses over
   it.  Reviewers are human and this is a blind spot.

2) If you do it in normal kernel style then the return would have been
   at level 1 indent so the static checkers would have complained about
   the unreachable code.  This was caught with a static checker anyway,
   I suppose, but that static check has a lot more false positives so
   I'm not sure I can release it.

3) This code is just confusing.  The error and the success paths are
   twisted together.  After the first allocation then the exclusive
   parts of the success path move from indent level 1 to indent level 2.
   Confusing code is more likely to have bugs.

regards,
dan carpenter


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [patch] mac80111: aes_ccm: cleanup ieee80211_aes_key_setup_encrypt()
  2015-03-23 14:08   ` [patch] mac80111: aes_ccm: cleanup ieee80211_aes_key_setup_encrypt() Dan Carpenter
@ 2015-03-24  9:32     ` Ard Biesheuvel
  2015-03-30  8:40     ` Johannes Berg
  1 sibling, 0 replies; 6+ messages in thread
From: Ard Biesheuvel @ 2015-03-24  9:32 UTC (permalink / raw)
  To: Dan Carpenter
  Cc: Johannes Berg, Jouni Malinen, <linux-wireless@vger.kernel.org>

On 23 March 2015 at 15:08, Dan Carpenter <dan.carpenter@oracle.com> wrote:
> This code is written using an anti-pattern called "success handling"
> which makes it hard to read, especially if you are used to normal kernel
> style.  It should instead be written as a list of directives in a row
> with branches for error handling.
>
> Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>

Acked-by: Ard Biesheuvel <ard.biesheuvel@linaro.org>

Thanks,
Ard.

>
> diff --git a/net/mac80211/aes_ccm.c b/net/mac80211/aes_ccm.c
> index 7869bb40..208df7c 100644
> --- a/net/mac80211/aes_ccm.c
> +++ b/net/mac80211/aes_ccm.c
> @@ -85,11 +85,15 @@ struct crypto_aead *ieee80211_aes_key_setup_encrypt(const u8 key[],
>                 return tfm;
>
>         err = crypto_aead_setkey(tfm, key, key_len);
> -       if (!err)
> -               err = crypto_aead_setauthsize(tfm, mic_len);
> -       if (!err)
> -               return tfm;
> +       if (err)
> +               goto free_aead;
> +       err = crypto_aead_setauthsize(tfm, mic_len);
> +       if (err)
> +               goto free_aead;
> +
> +       return tfm;
>
> +free_aead:
>         crypto_free_aead(tfm);
>         return ERR_PTR(err);
>  }

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [patch] mac80111: aes_ccm: cleanup ieee80211_aes_key_setup_encrypt()
  2015-03-23 14:08   ` [patch] mac80111: aes_ccm: cleanup ieee80211_aes_key_setup_encrypt() Dan Carpenter
  2015-03-24  9:32     ` Ard Biesheuvel
@ 2015-03-30  8:40     ` Johannes Berg
  1 sibling, 0 replies; 6+ messages in thread
From: Johannes Berg @ 2015-03-30  8:40 UTC (permalink / raw)
  To: Dan Carpenter; +Cc: Jouni Malinen, linux-wireless, Ard Biesheuvel

On Mon, 2015-03-23 at 17:08 +0300, Dan Carpenter wrote:
> This code is written using an anti-pattern called "success handling"
> which makes it hard to read, especially if you are used to normal kernel
> style.  It should instead be written as a list of directives in a row
> with branches for error handling.

Applied.

johannes


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2015-03-30  8:40 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-03-19 12:53 mac80111: Add BIP-GMAC-128 and BIP-GMAC-256 ciphers Dan Carpenter
2015-03-23 13:24 ` Jouni Malinen
2015-03-23 14:08   ` [patch] mac80111: aes_ccm: cleanup ieee80211_aes_key_setup_encrypt() Dan Carpenter
2015-03-24  9:32     ` Ard Biesheuvel
2015-03-30  8:40     ` Johannes Berg
2015-03-23 14:40   ` mac80111: Add BIP-GMAC-128 and BIP-GMAC-256 ciphers Dan Carpenter

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.