Skip to contentSkip to conversation

drbd: dynamically allocate shash descriptor

PATCH Applied Arnd Bergmann 4 participants 4 messages Jul 22, 2019 – Jul 23, 2019

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 drbd: dynamically allocate shash descriptor

Building with clang and KASAN, we get a warning about an overly large stack frame on 32-bit architectures:

drivers/block/drbd/drbd_receiver.c:921:31: error: stack frame size of 1280 bytes in function 'conn_connect'

      [-Werror,-Wframe-larger-than=]

We already allocate other data dynamically in this function, so just do the same for the shash descriptor, which makes up most of this memory.

---
v2:
- don't try to zero a NULL descriptor pointer,
based on review from Roland Kammerer.

---
 drivers/block/drbd/drbd_receiver.c | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)
drivers/block/drbd/drbd_receiver.c +12 -2
index 90ebfcae0ce6..2b3103c30857 100644
@@ -5417,7 +5417,7 @@ static int drbd_do_auth(struct drbd_connection *connection)
 	unsigned int key_len;
 	char secret[SHARED_SECRET_MAX]; /* 64 byte */
 	unsigned int resp_size;
-	SHASH_DESC_ON_STACK(desc, connection->cram_hmac_tfm);
+	struct shash_desc *desc;
 	struct packet_info pi;
 	struct net_conf *nc;
 	int err, rv;
@@ -5430,6 +5430,13 @@ static int drbd_do_auth(struct drbd_connection *connection)
 	memcpy(secret, nc->shared_secret, key_len);
 	rcu_read_unlock();
 
+	desc = kmalloc(sizeof(struct shash_desc) +
+		       crypto_shash_descsize(connection->cram_hmac_tfm),
+		       GFP_KERNEL);
+	if (!desc) {
+		rv = -1;
+		goto fail;
+	}
 	desc->tfm = connection->cram_hmac_tfm;
 
 	rv = crypto_shash_setkey(connection->cram_hmac_tfm, (u8 *)secret, key_len);
@@ -5571,7 +5578,10 @@ static int drbd_do_auth(struct drbd_connection *connection)
 	kfree(peers_ch);
 	kfree(response);
 	kfree(right_response);
-	shash_desc_zero(desc);
+	if (desc) {
+		shash_desc_zero(desc);
+		kfree(desc);
+	}
 
 	return rv;
 }
Signature · 1 line
2.20.0
Reviewed-by on v2 1/1
#1 lore raw
Quoted 4 lines · Arnd Bergmann

Building with clang and KASAN, we get a warning about an overly large stack frame on 32-bit architectures:

drivers/block/drbd/drbd_receiver.c:921:31: error: stack frame size of 1280 bytes in function 'conn_connect'

Quoted 8 lines
      [-Werror,-Wframe-larger-than=]

We already allocate other data dynamically in this function, so just do the same for the shash descriptor, which makes up most of this memory.

-Kees

Quoted 50 lines

---
v2:
- don't try to zero a NULL descriptor pointer,
based on review from Roland Kammerer.

---
 drivers/block/drbd/drbd_receiver.c | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)
drivers/block/drbd/drbd_receiver.c +12 -2
index 90ebfcae0ce6..2b3103c30857 100644
@@ -5417,7 +5417,7 @@ static int drbd_do_auth(struct drbd_connection *connection)
 	unsigned int key_len;
 	char secret[SHARED_SECRET_MAX]; /* 64 byte */
 	unsigned int resp_size;
-	SHASH_DESC_ON_STACK(desc, connection->cram_hmac_tfm);
+	struct shash_desc *desc;
 	struct packet_info pi;
 	struct net_conf *nc;
 	int err, rv;
@@ -5430,6 +5430,13 @@ static int drbd_do_auth(struct drbd_connection *connection)
 	memcpy(secret, nc->shared_secret, key_len);
 	rcu_read_unlock();
 
+	desc = kmalloc(sizeof(struct shash_desc) +
+		       crypto_shash_descsize(connection->cram_hmac_tfm),
+		       GFP_KERNEL);
+	if (!desc) {
+		rv = -1;
+		goto fail;
+	}
 	desc->tfm = connection->cram_hmac_tfm;
 
 	rv = crypto_shash_setkey(connection->cram_hmac_tfm, (u8 *)secret, key_len);
@@ -5571,7 +5578,10 @@ static int drbd_do_auth(struct drbd_connection *connection)
 	kfree(peers_ch);
 	kfree(response);
 	kfree(right_response);
-	shash_desc_zero(desc);
+	if (desc) {
+		shash_desc_zero(desc);
+		kfree(desc);
+	}
 
 	return rv;
 }
Signature · 1 line
2.20.0
Signature · 1 line
Kees Cook
Reviewed-by on v2 1/1
#2 lore raw
In reply to Arnd Bergmann
Quoted 4 lines · Arnd Bergmann

Building with clang and KASAN, we get a warning about an overly large stack frame on 32-bit architectures:

drivers/block/drbd/drbd_receiver.c:921:31: error: stack frame size of 1280 bytes in function 'conn_connect'

Quoted 8 lines
      [-Werror,-Wframe-larger-than=]

We already allocate other data dynamically in this function, so just do the same for the shash descriptor, which makes up most of this memory.

Quoted 50 lines

---
v2:
- don't try to zero a NULL descriptor pointer,
based on review from Roland Kammerer.

---
 drivers/block/drbd/drbd_receiver.c | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)
drivers/block/drbd/drbd_receiver.c +12 -2
index 90ebfcae0ce6..2b3103c30857 100644
@@ -5417,7 +5417,7 @@ static int drbd_do_auth(struct drbd_connection *connection)
 	unsigned int key_len;
 	char secret[SHARED_SECRET_MAX]; /* 64 byte */
 	unsigned int resp_size;
-	SHASH_DESC_ON_STACK(desc, connection->cram_hmac_tfm);
+	struct shash_desc *desc;
 	struct packet_info pi;
 	struct net_conf *nc;
 	int err, rv;
@@ -5430,6 +5430,13 @@ static int drbd_do_auth(struct drbd_connection *connection)
 	memcpy(secret, nc->shared_secret, key_len);
 	rcu_read_unlock();
 
+	desc = kmalloc(sizeof(struct shash_desc) +
+		       crypto_shash_descsize(connection->cram_hmac_tfm),
+		       GFP_KERNEL);
+	if (!desc) {
+		rv = -1;
+		goto fail;
+	}
 	desc->tfm = connection->cram_hmac_tfm;
 
 	rv = crypto_shash_setkey(connection->cram_hmac_tfm, (u8 *)secret, key_len);
@@ -5571,7 +5578,10 @@ static int drbd_do_auth(struct drbd_connection *connection)
 	kfree(peers_ch);
 	kfree(response);
 	kfree(right_response);
-	shash_desc_zero(desc);
+	if (desc) {
+		shash_desc_zero(desc);
+		kfree(desc);
+	}
 
 	return rv;
 }
Signature · 1 line
2.20.0
maintainer Applied
#3 lore raw
In reply to Arnd Bergmann
Quoted 9 lines · Arnd Bergmann

Building with clang and KASAN, we get a warning about an overly large stack frame on 32-bit architectures:

drivers/block/drbd/drbd_receiver.c:921:31: error: stack frame size of 1280 bytes in function 'conn_connect'

       [-Werror,-Wframe-larger-than=]

We already allocate other data dynamically in this function, so just do the same for the shash descriptor, which makes up most of this memory.

Applied, thanks.

Signature · 1 line
Jens Axboe