Skip to contentSkip to conversation

ubifs: fix wrong use of crypto_shash_descsize()

PATCH Applied Eric Biggers 4 participants 5 messages May 2, 2020 – May 17, 2020

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

PATCH 1/1 ubifs: fix wrong use of crypto_shash_descsize()

From: Eric Biggers <[email protected]>

crypto_shash_descsize() returns the size of the shash_desc context needed to compute the hash, not the size of the hash itself.

crypto_shash_digestsize() would be correct, or alternatively using c->hash_len and c->hmac_desc_len which already store the correct values. But actually it's simpler to just use stack arrays, so do that instead.

Fixes 49525e5eecca ("ubifs: Add helper functions for authentication support")
Fixes da8ef65f9573 ("ubifs: Authenticate replayed journal")
Cc <[email protected]> # v4.20+
Cc Sascha Hauer <[email protected]>
---
 fs/ubifs/auth.c   | 17 ++++-------------
 fs/ubifs/replay.c | 13 ++-----------
 2 files changed, 6 insertions(+), 24 deletions(-)
fs/ubifs/auth.c +4 -13
index 8cdbd53d780ca7..f985a3fbbb36a1 100644
@@ -79,13 +79,9 @@ int ubifs_prepare_auth_node(struct ubifs_info *c, void *node,
 			     struct shash_desc *inhash)
 {
 	struct ubifs_auth_node *auth = node;
-	u8 *hash;
+	u8 hash[UBIFS_HASH_ARR_SZ];
 	int err;
 
-	hash = kmalloc(crypto_shash_descsize(c->hash_tfm), GFP_NOFS);
-	if (!hash)
-		return -ENOMEM;
-
 	{
 		SHASH_DESC_ON_STACK(hash_desc, c->hash_tfm);
 
@@ -94,21 +90,16 @@ int ubifs_prepare_auth_node(struct ubifs_info *c, void *node,
 
 		err = crypto_shash_final(hash_desc, hash);
 		if (err)
-			goto out;
+			return err;
 	}
 
 	err = ubifs_hash_calc_hmac(c, hash, auth->hmac);
 	if (err)
-		goto out;
+		return err;
 
 	auth->ch.node_type = UBIFS_AUTH_NODE;
 	ubifs_prepare_node(c, auth, ubifs_auth_node_sz(c), 0);
-
-	err = 0;
-out:
-	kfree(hash);
-
-	return err;
+	return 0;
 }
 
 static struct shash_desc *ubifs_get_desc(const struct ubifs_info *c,
fs/ubifs/replay.c +2 -11
index b28ac4dfb4070a..01fcf79750472b 100644
@@ -601,18 +601,12 @@ static int authenticate_sleb(struct ubifs_info *c, struct ubifs_scan_leb *sleb,
 	struct ubifs_scan_node *snod;
 	int n_nodes = 0;
 	int err;
-	u8 *hash, *hmac;
+	u8 hash[UBIFS_HASH_ARR_SZ];
+	u8 hmac[UBIFS_HMAC_ARR_SZ];
 
 	if (!ubifs_authenticated(c))
 		return sleb->nodes_cnt;
 
-	hash = kmalloc(crypto_shash_descsize(c->hash_tfm), GFP_NOFS);
-	hmac = kmalloc(c->hmac_desc_len, GFP_NOFS);
-	if (!hash || !hmac) {
-		err = -ENOMEM;
-		goto out;
-	}
-
 	list_for_each_entry(snod, &sleb->nodes, list) {
 
 		n_nodes++;
@@ -662,9 +656,6 @@ static int authenticate_sleb(struct ubifs_info *c, struct ubifs_scan_leb *sleb,
 		err = 0;
 	}
 out:
-	kfree(hash);
-	kfree(hmac);
-
 	return err ? err : n_nodes - n_not_auth;
 }
 
Signature · 1 line
2.26.2
Acked-by on v1 1/1
#1 lore raw
Quoted 9 lines · Eric Biggers

From: Eric Biggers <[email protected]>

crypto_shash_descsize() returns the size of the shash_desc context needed to compute the hash, not the size of the hash itself.

crypto_shash_digestsize() would be correct, or alternatively using c->hash_len and c->hmac_desc_len which already store the correct values. But actually it's simpler to just use stack arrays, so do that instead.

Quoted 5 lines
Fixes 49525e5eecca ("ubifs: Add helper functions for authentication support")
Fixes da8ef65f9573 ("ubifs: Authenticate replayed journal")
Cc <[email protected]> # v4.20+
Cc Sascha Hauer <[email protected]>

Looks better that way, thanks.

Sascha

Signature · 4 lines
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |
#2 lore raw
Quoted 16 lines · Sascha Hauer
Quoted 9 lines · Eric Biggers

From: Eric Biggers <[email protected]>

crypto_shash_descsize() returns the size of the shash_desc context needed to compute the hash, not the size of the hash itself.

crypto_shash_digestsize() would be correct, or alternatively using c->hash_len and c->hmac_desc_len which already store the correct values. But actually it's simpler to just use stack arrays, so do that instead.

Quoted 5 lines
Fixes 49525e5eecca ("ubifs: Add helper functions for authentication support")
Fixes da8ef65f9573 ("ubifs: Authenticate replayed journal")
Cc <[email protected]> # v4.20+
Cc Sascha Hauer <[email protected]>
Quoted 4 lines

Looks better that way, thanks.

Richard, could you take this through the ubifs tree for 5.8?

- Eric

#3 lore raw

----- Ursprüngliche Mail -----

Quoted 24 lines

Von: "Eric Biggers" <[email protected]>
An: "Sascha Hauer" <[email protected]>, "richard" <[email protected]>
CC: "linux-mtd" <[email protected]>, "Linux Crypto Mailing List" <[email protected]>, "stable" <[email protected]>
Gesendet: Freitag, 15. Mai 2020 21:17:04
Betreff: Re: [PATCH] ubifs: fix wrong use of crypto_shash_descsize()

Quoted 16 lines · Sascha Hauer
Quoted 14 lines · Eric Biggers

From: Eric Biggers <[email protected]>

crypto_shash_descsize() returns the size of the shash_desc context needed to compute the hash, not the size of the hash itself.

crypto_shash_digestsize() would be correct, or alternatively using c->hash_len and c->hmac_desc_len which already store the correct values. But actually it's simpler to just use stack arrays, so do that instead.

Fixes 49525e5eecca ("ubifs: Add helper functions for authentication support")
Fixes da8ef65f9573 ("ubifs: Authenticate replayed journal")
Cc <[email protected]> # v4.20+
Cc Sascha Hauer <[email protected]>
Quoted 6 lines
Quoted 4 lines

Looks better that way, thanks.

Richard, could you take this through the ubifs tree for 5.8?

Sure. I actually will send a PR with various MTD related fixes for 5.7.

Thanks,
//richard

#4 lore raw
Quoted 28 lines · Richard Weinberger

----- Ursprüngliche Mail -----

Quoted 26 lines

Von: "Eric Biggers" <[email protected]>
An: "Sascha Hauer" <[email protected]>, "richard" <[email protected]>
CC: "linux-mtd" <[email protected]>, "Linux Crypto Mailing List" <[email protected]>, "stable" <[email protected]>
Gesendet: Freitag, 15. Mai 2020 21:17:04
Betreff: Re: [PATCH] ubifs: fix wrong use of crypto_shash_descsize()

Quoted 18 lines · Sascha Hauer
Quoted 9 lines · Eric Biggers

From: Eric Biggers <[email protected]>

crypto_shash_descsize() returns the size of the shash_desc context needed to compute the hash, not the size of the hash itself.

crypto_shash_digestsize() would be correct, or alternatively using c->hash_len and c->hmac_desc_len which already store the correct values. But actually it's simpler to just use stack arrays, so do that instead.

Quoted 5 lines
Fixes 49525e5eecca ("ubifs: Add helper functions for authentication support")
Fixes da8ef65f9573 ("ubifs: Authenticate replayed journal")
Cc <[email protected]> # v4.20+
Cc Sascha Hauer <[email protected]>

Looks better that way, thanks.

Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

Richard, could you take this through the ubifs tree for 5.8?

Sure. I actually will send a PR with various MTD related fixes for 5.7.

Applied. Thanks for fixing!

Signature · 2 lines
Thanks,
//richard