[PATCH 2/2] dm-crypt: Use common error handling code in crypt_set_keyring_key()

Markus Elfring posted 2 patches 2 months, 1 week ago
[PATCH 2/2] dm-crypt: Use common error handling code in crypt_set_keyring_key()
Posted by Markus Elfring 2 months, 1 week ago
From: Markus Elfring <elfring@users.sourceforge.net>
Date: Wed, 18 Sep 2024 15:34:45 +0200

Add a jump target so that a bit of exception handling can be better reused
at the end of this function implementation.

Signed-off-by: Markus Elfring <elfring@users.sourceforge.net>
---
 drivers/md/dm-crypt.c | 24 ++++++++++++------------
 1 file changed, 12 insertions(+), 12 deletions(-)

diff --git a/drivers/md/dm-crypt.c b/drivers/md/dm-crypt.c
index dae2fe3cb182..3d2247cfd42b 100644
--- a/drivers/md/dm-crypt.c
+++ b/drivers/md/dm-crypt.c
@@ -2614,8 +2614,8 @@ static int crypt_set_keyring_key(struct crypt_config *cc, const char *key_string

 	key = request_key(type, key_desc + 1, NULL);
 	if (IS_ERR(key)) {
-		kfree_sensitive(new_key_string);
-		return PTR_ERR(key);
+		ret = PTR_ERR(key);
+		goto free_new_key_string;
 	}

 	down_read(&key->sem);
@@ -2623,23 +2623,23 @@ static int crypt_set_keyring_key(struct crypt_config *cc, const char *key_string
 	ret = set_key(cc, key);
 	up_read(&key->sem);
 	key_put(key);
-	if (ret < 0) {
-		kfree_sensitive(new_key_string);
-		return ret;
-	}
+	if (ret < 0)
+		goto free_new_key_string;

 	/* clear the flag since following operations may invalidate previously valid key */
 	clear_bit(DM_CRYPT_KEY_VALID, &cc->flags);

 	ret = crypt_setkey(cc);
+	if (ret)
+		goto free_new_key_string;

-	if (!ret) {
-		set_bit(DM_CRYPT_KEY_VALID, &cc->flags);
-		kfree_sensitive(cc->key_string);
-		cc->key_string = new_key_string;
-	} else
-		kfree_sensitive(new_key_string);
+	set_bit(DM_CRYPT_KEY_VALID, &cc->flags);
+	kfree_sensitive(cc->key_string);
+	cc->key_string = new_key_string;
+	return 0;

+free_new_key_string:
+	kfree_sensitive(new_key_string);
 	return ret;
 }

--
2.46.0
Re: [PATCH 2/2] dm-crypt: Use common error handling code in crypt_set_keyring_key()
Posted by Mikulas Patocka 2 months, 1 week ago
Both patches applied, thanks.

Mikulas


On Wed, 18 Sep 2024, Markus Elfring wrote:

> From: Markus Elfring <elfring@users.sourceforge.net>
> Date: Wed, 18 Sep 2024 15:34:45 +0200
> 
> Add a jump target so that a bit of exception handling can be better reused
> at the end of this function implementation.
> 
> Signed-off-by: Markus Elfring <elfring@users.sourceforge.net>
> ---
>  drivers/md/dm-crypt.c | 24 ++++++++++++------------
>  1 file changed, 12 insertions(+), 12 deletions(-)
> 
> diff --git a/drivers/md/dm-crypt.c b/drivers/md/dm-crypt.c
> index dae2fe3cb182..3d2247cfd42b 100644
> --- a/drivers/md/dm-crypt.c
> +++ b/drivers/md/dm-crypt.c
> @@ -2614,8 +2614,8 @@ static int crypt_set_keyring_key(struct crypt_config *cc, const char *key_string
> 
>  	key = request_key(type, key_desc + 1, NULL);
>  	if (IS_ERR(key)) {
> -		kfree_sensitive(new_key_string);
> -		return PTR_ERR(key);
> +		ret = PTR_ERR(key);
> +		goto free_new_key_string;
>  	}
> 
>  	down_read(&key->sem);
> @@ -2623,23 +2623,23 @@ static int crypt_set_keyring_key(struct crypt_config *cc, const char *key_string
>  	ret = set_key(cc, key);
>  	up_read(&key->sem);
>  	key_put(key);
> -	if (ret < 0) {
> -		kfree_sensitive(new_key_string);
> -		return ret;
> -	}
> +	if (ret < 0)
> +		goto free_new_key_string;
> 
>  	/* clear the flag since following operations may invalidate previously valid key */
>  	clear_bit(DM_CRYPT_KEY_VALID, &cc->flags);
> 
>  	ret = crypt_setkey(cc);
> +	if (ret)
> +		goto free_new_key_string;
> 
> -	if (!ret) {
> -		set_bit(DM_CRYPT_KEY_VALID, &cc->flags);
> -		kfree_sensitive(cc->key_string);
> -		cc->key_string = new_key_string;
> -	} else
> -		kfree_sensitive(new_key_string);
> +	set_bit(DM_CRYPT_KEY_VALID, &cc->flags);
> +	kfree_sensitive(cc->key_string);
> +	cc->key_string = new_key_string;
> +	return 0;
> 
> +free_new_key_string:
> +	kfree_sensitive(new_key_string);
>  	return ret;
>  }
> 
> --
> 2.46.0
>