All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Horia Geantă" <horia.geanta@freescale.com>
To: Marek Vasut <marex@denx.de>
Cc: Herbert Xu <herbert@gondor.apana.org.au>,
	"David S. Miller" <davem@davemloft.net>,
	<linux-crypto@vger.kernel.org>, <stable@vger.kernel.org>,
	Kim Phillips <kim.phillips@freescale.com>
Subject: Re: [PATCH crypto 2/2] crypto: caam - add allocation failure handling in SPRINTFCAT macro
Date: Wed, 23 Apr 2014 19:35:45 +0300	[thread overview]
Message-ID: <5357EBE1.9000402@freescale.com> (raw)
In-Reply-To: <201404230156.41234.marex@denx.de>

On 4/23/2014 2:56 AM, Marek Vasut wrote:
> On Friday, April 18, 2014 at 12:01:42 PM, Horia Geanta wrote:
>> GFP_ATOMIC memory allocation could fail.
>> In this case, avoid NULL pointer dereference and notify user.
>>
>> Cc: <stable@vger.kernel.org> # 3.2+
>
> If I recall correctly, you need to get the patch accepted into mainline before
> sending it for -stable .
>

 From Documentation/stable_kernel_rules.txt
  - To have the patch automatically included in the stable tree, add the tag
      Cc: stable@vger.kernel.org
    in the sign-off area. Once the patch is merged it will be applied to
    the stable tree without anything else needing to be done by the author
    or subsystem maintainer.

>> Cc: Kim Phillips <kim.phillips@freescale.com>
>> Signed-off-by: Horia Geanta <horia.geanta@freescale.com>
>> ---
>>   drivers/crypto/caam/error.c | 10 +++++++---
>>   1 file changed, 7 insertions(+), 3 deletions(-)
>>
>> diff --git a/drivers/crypto/caam/error.c b/drivers/crypto/caam/error.c
>> index 9f25f5296029..0eabd81e1a90 100644
>> --- a/drivers/crypto/caam/error.c
>> +++ b/drivers/crypto/caam/error.c
>> @@ -16,9 +16,13 @@
>>   	char *tmp;						\
>>   								\
>>   	tmp = kmalloc(sizeof(format) + max_alloc, GFP_ATOMIC);	\
>> -	sprintf(tmp, format, param);				\
>> -	strcat(str, tmp);					\
>> -	kfree(tmp);						\
>> +	if (likely(tmp)) {					\
>> +		sprintf(tmp, format, param);			\
>> +		strcat(str, tmp);				\
>> +		kfree(tmp);					\
>> +	} else {						\
>> +		strcat(str, "kmalloc failure in SPRINTFCAT");	\
>
> This entire macro looks somewhat strange.

I am trying to fix it with minimal changes, so the patch qualifies for 
-stable.

> 1) Can't you just snprintf() into $str + some offset ? Something like:
>     snprintf(str + strlen(str), str_total_sz - strlen(str), format, param);
>

I think this would work. It also gets rid of memory allocation.

Note that strlen(str) is undefined if str is not initialized / 
null-terminated.
However, all code paths seem to touch this line in caam_jr_strstatus():
sprintf(outstr, "%s: ", status_src[ssrc].error);
before reaching SPRINTFCAT macros, so str is null-terminated.

I'll send v2.

> 2) Why is noone checking if the $str has enough space for contents of $tmp ?

All call sites reach this macro via caam_jr_strstatus(tmp, ...), which 
is always called having:
char tmp[CAAM_ERROR_STR_MAX];

CAAM_ERROR_STR_MAX is 302, probably enough according to commit
de2954d66408da3ae34effda777bb564fd17781b (crypto: caam - fix printk 
recursion for long error texts).

Horia

  reply	other threads:[~2014-04-23 16:50 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-04-18 10:01 [PATCH crypto 1/2] crypto: caam - fix mem leak in ahash_setkey Horia Geanta
2014-04-18 10:01 ` [PATCH crypto 2/2] crypto: caam - add allocation failure handling in SPRINTFCAT macro Horia Geanta
2014-04-22 23:56   ` Marek Vasut
2014-04-23 16:35     ` Horia Geantă [this message]
2014-04-23 17:12       ` Marek Vasut
2014-04-23 17:41         ` Marek Vasut
2014-04-28 10:24   ` Herbert Xu
2014-04-28 19:28     ` Marek Vasut
2014-04-28 21:53       ` Herbert Xu
2014-04-28 22:29         ` Marek Vasut
2014-04-22 23:47 ` [PATCH crypto 1/2] crypto: caam - fix mem leak in ahash_setkey Marek Vasut
2014-04-28 10:25   ` Herbert Xu

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=5357EBE1.9000402@freescale.com \
    --to=horia.geanta@freescale.com \
    --cc=davem@davemloft.net \
    --cc=herbert@gondor.apana.org.au \
    --cc=kim.phillips@freescale.com \
    --cc=linux-crypto@vger.kernel.org \
    --cc=marex@denx.de \
    --cc=stable@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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.