All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] x86/power/64: Remove VLA usage
@ 2018-07-16  3:56 Kees Cook
  2018-07-16  8:52 ` Pavel Machek
  2018-07-16 17:22 ` Eric Biggers
  0 siblings, 2 replies; 4+ messages in thread
From: Kees Cook @ 2018-07-16  3:56 UTC (permalink / raw)
  To: Rafael J. Wysocki
  Cc: Pavel Machek, Herbert Xu, Arnd Bergmann, Eric Biggers,
	Gustavo A. R. Silva, linux-pm, linux-kernel

In the quest to remove all stack VLA usage from the kernel[1], this
removes the discouraged use of AHASH_REQUEST_ON_STACK by switching to
shash directly and allocating the descriptor in heap memory (which should
be fine: the tfm has already been allocated there too).

[1] https://lkml.kernel.org/r/CA+55aFzCG-zNmZwX4A2FQpadafLfEzK6CC=qPXydAacU1RqZWA@mail.gmail.com

Signed-off-by: Kees Cook <keescook@chromium.org>
---
 arch/x86/power/hibernate_64.c | 35 +++++++++++++++++++----------------
 1 file changed, 19 insertions(+), 16 deletions(-)

diff --git a/arch/x86/power/hibernate_64.c b/arch/x86/power/hibernate_64.c
index 67ccf64c8bd8..0ed01bb935a6 100644
--- a/arch/x86/power/hibernate_64.c
+++ b/arch/x86/power/hibernate_64.c
@@ -233,28 +233,31 @@ struct restore_data_record {
  */
 static int get_e820_md5(struct e820_table *table, void *buf)
 {
-	struct scatterlist sg;
-	struct crypto_ahash *tfm;
+	struct crypto_shash *tfm;
+	struct shash_desc *desc;
 	int size;
 	int ret = 0;
 
-	tfm = crypto_alloc_ahash("md5", 0, CRYPTO_ALG_ASYNC);
+	tfm = crypto_alloc_shash("md5", 0, 0);
 	if (IS_ERR(tfm))
 		return -ENOMEM;
 
-	{
-		AHASH_REQUEST_ON_STACK(req, tfm);
-		size = offsetof(struct e820_table, entries) + sizeof(struct e820_entry) * table->nr_entries;
-		ahash_request_set_tfm(req, tfm);
-		sg_init_one(&sg, (u8 *)table, size);
-		ahash_request_set_callback(req, 0, NULL, NULL);
-		ahash_request_set_crypt(req, &sg, buf, size);
-
-		if (crypto_ahash_digest(req))
-			ret = -EINVAL;
-		ahash_request_zero(req);
-	}
-	crypto_free_ahash(tfm);
+	desc = kmalloc(sizeof(struct shash_desc) + crypto_shash_descsize(tfm),
+		       GFP_KERNEL);
+	if (!desc)
+		return -ENOMEM;
+
+	desc->tfm = tfm;
+	desc->flags = 0;
+
+	size = offsetof(struct e820_table, entries) +
+		sizeof(struct e820_entry) * table->nr_entries;
+
+	if (crypto_shash_digest(desc, (u8 *)table, size, buf))
+		ret = -EINVAL;
+
+	kzfree(desc);
+	crypto_free_shash(tfm);
 
 	return ret;
 }
-- 
2.17.1


-- 
Kees Cook
Pixel Security

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

* Re: [PATCH] x86/power/64: Remove VLA usage
  2018-07-16  3:56 [PATCH] x86/power/64: Remove VLA usage Kees Cook
@ 2018-07-16  8:52 ` Pavel Machek
  2018-07-16 17:22 ` Eric Biggers
  1 sibling, 0 replies; 4+ messages in thread
From: Pavel Machek @ 2018-07-16  8:52 UTC (permalink / raw)
  To: Kees Cook
  Cc: Rafael J. Wysocki, Herbert Xu, Arnd Bergmann, Eric Biggers,
	Gustavo A. R. Silva, linux-pm, linux-kernel

[-- Attachment #1: Type: text/plain, Size: 724 bytes --]

On Sun 2018-07-15 20:56:57, Kees Cook wrote:
> In the quest to remove all stack VLA usage from the kernel[1], this
> removes the discouraged use of AHASH_REQUEST_ON_STACK by switching to
> shash directly and allocating the descriptor in heap memory (which should
> be fine: the tfm has already been allocated there too).
> 
> [1] https://lkml.kernel.org/r/CA+55aFzCG-zNmZwX4A2FQpadafLfEzK6CC=qPXydAacU1RqZWA@mail.gmail.com
> 
> Signed-off-by: Kees Cook <keescook@chromium.org>

Acked-by: Pavel Machek <pavel@ucw.cz>

, provided it was tested.

									Pavel
									
-- 
(english) http://www.livejournal.com/~pavelmachek
(cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html

[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 181 bytes --]

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

* Re: [PATCH] x86/power/64: Remove VLA usage
  2018-07-16  3:56 [PATCH] x86/power/64: Remove VLA usage Kees Cook
  2018-07-16  8:52 ` Pavel Machek
@ 2018-07-16 17:22 ` Eric Biggers
  2018-07-19  4:39   ` Kees Cook
  1 sibling, 1 reply; 4+ messages in thread
From: Eric Biggers @ 2018-07-16 17:22 UTC (permalink / raw)
  To: Kees Cook
  Cc: Rafael J. Wysocki, Pavel Machek, Herbert Xu, Arnd Bergmann,
	Gustavo A. R. Silva, linux-pm, linux-kernel

On Sun, Jul 15, 2018 at 08:56:57PM -0700, Kees Cook wrote:
> In the quest to remove all stack VLA usage from the kernel[1], this
> removes the discouraged use of AHASH_REQUEST_ON_STACK by switching to
> shash directly and allocating the descriptor in heap memory (which should
> be fine: the tfm has already been allocated there too).
> 
> [1] https://lkml.kernel.org/r/CA+55aFzCG-zNmZwX4A2FQpadafLfEzK6CC=qPXydAacU1RqZWA@mail.gmail.com
> 
> Signed-off-by: Kees Cook <keescook@chromium.org>
> ---
>  arch/x86/power/hibernate_64.c | 35 +++++++++++++++++++----------------
>  1 file changed, 19 insertions(+), 16 deletions(-)
> 
> diff --git a/arch/x86/power/hibernate_64.c b/arch/x86/power/hibernate_64.c
> index 67ccf64c8bd8..0ed01bb935a6 100644
> --- a/arch/x86/power/hibernate_64.c
> +++ b/arch/x86/power/hibernate_64.c
> @@ -233,28 +233,31 @@ struct restore_data_record {
>   */
>  static int get_e820_md5(struct e820_table *table, void *buf)
>  {
> -	struct scatterlist sg;
> -	struct crypto_ahash *tfm;
> +	struct crypto_shash *tfm;
> +	struct shash_desc *desc;
>  	int size;
>  	int ret = 0;
>  
> -	tfm = crypto_alloc_ahash("md5", 0, CRYPTO_ALG_ASYNC);
> +	tfm = crypto_alloc_shash("md5", 0, 0);
>  	if (IS_ERR(tfm))
>  		return -ENOMEM;
>  
> -	{
> -		AHASH_REQUEST_ON_STACK(req, tfm);
> -		size = offsetof(struct e820_table, entries) + sizeof(struct e820_entry) * table->nr_entries;
> -		ahash_request_set_tfm(req, tfm);
> -		sg_init_one(&sg, (u8 *)table, size);
> -		ahash_request_set_callback(req, 0, NULL, NULL);
> -		ahash_request_set_crypt(req, &sg, buf, size);
> -
> -		if (crypto_ahash_digest(req))
> -			ret = -EINVAL;
> -		ahash_request_zero(req);
> -	}
> -	crypto_free_ahash(tfm);
> +	desc = kmalloc(sizeof(struct shash_desc) + crypto_shash_descsize(tfm),
> +		       GFP_KERNEL);
> +	if (!desc)
> +		return -ENOMEM;

Need crypto_free_shash(tfm) if the kmalloc() here fails.

> +
> +	desc->tfm = tfm;
> +	desc->flags = 0;
> +
> +	size = offsetof(struct e820_table, entries) +
> +		sizeof(struct e820_entry) * table->nr_entries;
> +
> +	if (crypto_shash_digest(desc, (u8 *)table, size, buf))
> +		ret = -EINVAL;
> +
> +	kzfree(desc);
> +	crypto_free_shash(tfm);
>  
>  	return ret;
>  }
> -- 
> 2.17.1
> 
> 
> -- 
> Kees Cook
> Pixel Security

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

* Re: [PATCH] x86/power/64: Remove VLA usage
  2018-07-16 17:22 ` Eric Biggers
@ 2018-07-19  4:39   ` Kees Cook
  0 siblings, 0 replies; 4+ messages in thread
From: Kees Cook @ 2018-07-19  4:39 UTC (permalink / raw)
  To: Eric Biggers
  Cc: Rafael J. Wysocki, Pavel Machek, Herbert Xu, Arnd Bergmann,
	Gustavo A. R. Silva, Linux PM list, LKML

On Mon, Jul 16, 2018 at 10:22 AM, Eric Biggers <ebiggers@google.com> wrote:
> On Sun, Jul 15, 2018 at 08:56:57PM -0700, Kees Cook wrote:
>> In the quest to remove all stack VLA usage from the kernel[1], this
>> removes the discouraged use of AHASH_REQUEST_ON_STACK by switching to
>> shash directly and allocating the descriptor in heap memory (which should
>> be fine: the tfm has already been allocated there too).
>>
>> [1] https://lkml.kernel.org/r/CA+55aFzCG-zNmZwX4A2FQpadafLfEzK6CC=qPXydAacU1RqZWA@mail.gmail.com
>>
>> Signed-off-by: Kees Cook <keescook@chromium.org>
>> ---
>>  arch/x86/power/hibernate_64.c | 35 +++++++++++++++++++----------------
>>  1 file changed, 19 insertions(+), 16 deletions(-)
>>
>> diff --git a/arch/x86/power/hibernate_64.c b/arch/x86/power/hibernate_64.c
>> index 67ccf64c8bd8..0ed01bb935a6 100644
>> --- a/arch/x86/power/hibernate_64.c
>> +++ b/arch/x86/power/hibernate_64.c
>> @@ -233,28 +233,31 @@ struct restore_data_record {
>>   */
>>  static int get_e820_md5(struct e820_table *table, void *buf)
>>  {
>> -     struct scatterlist sg;
>> -     struct crypto_ahash *tfm;
>> +     struct crypto_shash *tfm;
>> +     struct shash_desc *desc;
>>       int size;
>>       int ret = 0;
>>
>> -     tfm = crypto_alloc_ahash("md5", 0, CRYPTO_ALG_ASYNC);
>> +     tfm = crypto_alloc_shash("md5", 0, 0);
>>       if (IS_ERR(tfm))
>>               return -ENOMEM;
>>
>> -     {
>> -             AHASH_REQUEST_ON_STACK(req, tfm);
>> -             size = offsetof(struct e820_table, entries) + sizeof(struct e820_entry) * table->nr_entries;
>> -             ahash_request_set_tfm(req, tfm);
>> -             sg_init_one(&sg, (u8 *)table, size);
>> -             ahash_request_set_callback(req, 0, NULL, NULL);
>> -             ahash_request_set_crypt(req, &sg, buf, size);
>> -
>> -             if (crypto_ahash_digest(req))
>> -                     ret = -EINVAL;
>> -             ahash_request_zero(req);
>> -     }
>> -     crypto_free_ahash(tfm);
>> +     desc = kmalloc(sizeof(struct shash_desc) + crypto_shash_descsize(tfm),
>> +                    GFP_KERNEL);
>> +     if (!desc)
>> +             return -ENOMEM;
>
> Need crypto_free_shash(tfm) if the kmalloc() here fails.

Ah thanks! Fixed now for the next version.

-Kees

-- 
Kees Cook
Pixel Security

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

end of thread, other threads:[~2018-07-19  4:39 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-07-16  3:56 [PATCH] x86/power/64: Remove VLA usage Kees Cook
2018-07-16  8:52 ` Pavel Machek
2018-07-16 17:22 ` Eric Biggers
2018-07-19  4:39   ` Kees Cook

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.