Skip to contentSkip to conversation

x86/power/64: Remove VLA usage

PATCH In review Kees Cook 3 participants 4 messages Jul 16, 2018 – Jul 19, 2018

Subsystems (2)
lore
Share & tools
lore

Kees Cook

author
#0 lore raw

PATCH 1/1 x86/power/64: Remove VLA usage

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

---
 arch/x86/power/hibernate_64.c | 35 +++++++++++++++++++----------------
 1 file changed, 19 insertions(+), 16 deletions(-)
arch/x86/power/hibernate_64.c +19 -16
index 67ccf64c8bd8..0ed01bb935a6 100644
@@ -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;
 }
Signature · 6 lines
2.17.1


-- 
Kees Cook
Pixel Security
Acked-by on v1 1/1
#1 lore raw
Quoted 8 lines · Kees Cook

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

, provided it was tested.

Pavel

Signature · 2 lines
(english) http://www.livejournal.com/~pavelmachek
(cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html
#2 lore raw
In reply to Kees Cook
Quoted 39 lines · Kees Cook

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

---
 arch/x86/power/hibernate_64.c | 35 +++++++++++++++++++----------------
 1 file changed, 19 insertions(+), 16 deletions(-)
arch/x86/power/hibernate_64.c +3 -10
index 67ccf64c8bd8..0ed01bb935a6 100644
@@ -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);

arch/x86/power/hibernate_64.c struct restore_data_record { · line 248

Quoted 10 lines
arch/x86/power/hibernate_64.c +4 -6
-
-		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.

Quoted 22 lines
arch/x86/power/hibernate_64.c +12 -0
+
+	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;
 }
Signature · 6 lines
2.17.1


-- 
Kees Cook
Pixel Security
author
#3 lore raw
Quoted 44 lines · Eric Biggers
Quoted 43 lines · Kees Cook

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

---
 arch/x86/power/hibernate_64.c | 35 +++++++++++++++++++----------------
 1 file changed, 19 insertions(+), 16 deletions(-)
arch/x86/power/hibernate_64.c +3 -14
index 67ccf64c8bd8..0ed01bb935a6 100644
@@ -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);
Quoted 8 lines

arch/x86/power/hibernate_64.c struct restore_data_record { · line 248

Quoted 6 lines
arch/x86/power/hibernate_64.c +4 -2
-     }
-     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

Signature · 2 lines
Kees Cook
Pixel Security