Skip to contentSkip to conversation

evm: Check also if *tfm is an error pointer in init_desc()

bug report Superseded Dan Carpenter 4 participants 12 messages May 12, 2020 – May 14, 2020

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

Hello Roberto Sassu,

The patch 53de3b080d5e: "evm: Check also if *tfm is an error pointer in init_desc()" from Apr 27, 2020, leads to the following static checker warning:

security/integrity/evm/evm_crypto.c:119 init_desc()
error: '*tfm' dereferencing possible ERR_PTR()

security/integrity/evm/evm_crypto.c

    89  
    90                  tfm = &evm_tfm[hash_algo];
    91                  algo = hash_algo_name[hash_algo];
    92          }
    93  
    94          if (IS_ERR_OR_NULL(*tfm)) {

This used to be a "if (!*tfm)" check.

    95                  mutex_lock(&mutex);
    96                  if (*tfm)
    97                          goto out;

Then we test again with the lock held. But in the new code if "*tfm" is an error pointer then we jump directly to the unlock and crash on the next line. I can't see how the commit would fix anything.

    98                  *tfm = crypto_alloc_shash(algo, 0, CRYPTO_NOLOAD);
    99                  if (IS_ERR(*tfm)) {
   100                          rc = PTR_ERR(*tfm);
   101                          pr_err("Can not allocate %s (reason: %ld)\n", algo, rc);
   102                          *tfm = NULL;
   103                          mutex_unlock(&mutex);
   104                          return ERR_PTR(rc);
   105                  }
   106                  if (type == EVM_XATTR_HMAC) {
   107                          rc = crypto_shash_setkey(*tfm, evmkey, evmkey_len);
   108                          if (rc) {
   109                                  crypto_free_shash(*tfm);
   110                                  *tfm = NULL;
   111                                  mutex_unlock(&mutex);
   112                                  return ERR_PTR(rc);
   113                          }
   114                  }
   115  out:
   116                  mutex_unlock(&mutex);
   117          }
   118  
   119          desc = kmalloc(sizeof(*desc) + crypto_shash_descsize(*tfm),
                                                                     ^^^^

I don't understand how using *tfm outside of a lock is safe at all anyway.

   120                          GFP_KERNEL);
   121          if (!desc)
   122                  return ERR_PTR(-ENOMEM);
   123  

regards,
dan carpenter

maintainer
#1 lore raw
Quoted 22 lines

From: Dan Carpenter [mailto:[email protected]]
Sent: Tuesday, May 12, 2020 12:48 PM

Hello Roberto Sassu,

The patch 53de3b080d5e: "evm: Check also if *tfm is an error pointer in init_desc()" from Apr 27, 2020, leads to the following static checker warning:

security/integrity/evm/evm_crypto.c:119 init_desc()
error: '*tfm' dereferencing possible ERR_PTR()

security/integrity/evm/evm_crypto.c

    89
    90                  tfm = &evm_tfm[hash_algo];
    91                  algo = hash_algo_name[hash_algo];
    92          }
    93
    94          if (IS_ERR_OR_NULL(*tfm)) {

This used to be a "if (!*tfm)" check.

Quoted 7 lines
    95                  mutex_lock(&mutex);
    96                  if (*tfm)
    97                          goto out;

Then we test again with the lock held. But in the new code if "*tfm" is an error pointer then we jump directly to the unlock and crash on the next line. I can't see how the commit would fix anything.

Hello Dan

you are right. The fix should be applied in both places.

if (!IS_ERR_OR_NULL(*tfm))

	goto out;
Quoted 23 lines
    98                  *tfm = crypto_alloc_shash(algo, 0, CRYPTO_NOLOAD);
    99                  if (IS_ERR(*tfm)) {
   100                          rc = PTR_ERR(*tfm);
   101                          pr_err("Can not allocate %s (reason: %ld)\n", algo, rc);
   102                          *tfm = NULL;
   103                          mutex_unlock(&mutex);
   104                          return ERR_PTR(rc);
   105                  }
   106                  if (type == EVM_XATTR_HMAC) {
   107                          rc = crypto_shash_setkey(*tfm, evmkey, evmkey_len);
   108                          if (rc) {
   109                                  crypto_free_shash(*tfm);
   110                                  *tfm = NULL;
   111                                  mutex_unlock(&mutex);
   112                                  return ERR_PTR(rc);
   113                          }
   114                  }
   115  out:
   116                  mutex_unlock(&mutex);
   117          }
   118
   119          desc = kmalloc(sizeof(*desc) + crypto_shash_descsize(*tfm),
                                                                     ^^^^
Quoted 2 lines

I don't understand how using *tfm outside of a lock is safe at all anyway.

I think the purpose of the mutex is just to prevent two concurrent allocations. Later, it should not be a problem, as *tfm is never freed.

Thanks

Roberto

HUAWEI TECHNOLOGIES Duesseldorf GmbH, HRB 56063
Managing Director: Li Peng, Li Jian, Shi Yanli

Quoted 7 lines
   120                          GFP_KERNEL);
   121          if (!desc)
   122                  return ERR_PTR(-ENOMEM);
   123

regards,
dan carpenter

#2 lore raw
Quoted 30 lines · Roberto Sassu
Quoted 22 lines

From: Dan Carpenter [mailto:[email protected]]
Sent: Tuesday, May 12, 2020 12:48 PM

Hello Roberto Sassu,

The patch 53de3b080d5e: "evm: Check also if *tfm is an error pointer in init_desc()" from Apr 27, 2020, leads to the following static checker warning:

security/integrity/evm/evm_crypto.c:119 init_desc()
error: '*tfm' dereferencing possible ERR_PTR()

security/integrity/evm/evm_crypto.c

    89
    90                  tfm = &evm_tfm[hash_algo];
    91                  algo = hash_algo_name[hash_algo];
    92          }
    93
    94          if (IS_ERR_OR_NULL(*tfm)) {

This used to be a "if (!*tfm)" check.

Quoted 7 lines
    95                  mutex_lock(&mutex);
    96                  if (*tfm)
    97                          goto out;

Then we test again with the lock held. But in the new code if "*tfm" is an error pointer then we jump directly to the unlock and crash on the next line. I can't see how the commit would fix anything.

Quoted 6 lines

Hello Dan

you are right. The fix should be applied in both places.

if (!IS_ERR_OR_NULL(*tfm))

	goto out;

No. I was wrong.

Quoted 27 lines
Quoted 24 lines
    98                  *tfm = crypto_alloc_shash(algo, 0, CRYPTO_NOLOAD);
    99                  if (IS_ERR(*tfm)) {
   100                          rc = PTR_ERR(*tfm);
   101                          pr_err("Can not allocate %s (reason: %ld)\n", algo, rc);
   102                          *tfm = NULL;
   103                          mutex_unlock(&mutex);
   104                          return ERR_PTR(rc);
   105                  }
   106                  if (type == EVM_XATTR_HMAC) {
   107                          rc = crypto_shash_setkey(*tfm, evmkey, evmkey_len);
   108                          if (rc) {
   109                                  crypto_free_shash(*tfm);
   110                                  *tfm = NULL;
   111                                  mutex_unlock(&mutex);
   112                                  return ERR_PTR(rc);
   113                          }
   114                  }
   115  out:
   116                  mutex_unlock(&mutex);
   117          }
   118
   119          desc = kmalloc(sizeof(*desc) + crypto_shash_descsize(*tfm),
                                                                     ^^^^
Quoted 2 lines

I don't understand how using *tfm outside of a lock is safe at all anyway.

Quoted 3 lines

I think the purpose of the mutex is just to prevent two concurrent allocations. Later, it should not be a problem, as *tfm is never freed.

Actually by the time we take the lock then *tfm is either valid or NULL so this code works. It's confusing though.

regards,
dan carpenter

maintainer
#3 lore raw
Quoted 71 lines

From: [email protected] [mailto:owner-linux- [email protected]] On Behalf Of Dan Carpenter
Sent: Tuesday, May 12, 2020 2:34 PM

Quoted 30 lines · Roberto Sassu
Quoted 22 lines

From: Dan Carpenter [mailto:[email protected]]
Sent: Tuesday, May 12, 2020 12:48 PM

Hello Roberto Sassu,

The patch 53de3b080d5e: "evm: Check also if *tfm is an error pointer in init_desc()" from Apr 27, 2020, leads to the following static checker warning:

security/integrity/evm/evm_crypto.c:119 init_desc()
error: '*tfm' dereferencing possible ERR_PTR()

security/integrity/evm/evm_crypto.c

    89
    90                  tfm = &evm_tfm[hash_algo];
    91                  algo = hash_algo_name[hash_algo];
    92          }
    93
    94          if (IS_ERR_OR_NULL(*tfm)) {

This used to be a "if (!*tfm)" check.

Quoted 7 lines
    95                  mutex_lock(&mutex);
    96                  if (*tfm)
    97                          goto out;

Then we test again with the lock held. But in the new code if "*tfm" is an error pointer then we jump directly to the unlock and crash on the next line. I can't see how the commit would fix anything.

Quoted 6 lines

Hello Dan

you are right. The fix should be applied in both places.

if (!IS_ERR_OR_NULL(*tfm))

	goto out;

No. I was wrong.

Quoted 11 lines
Quoted 11 lines
    98                  *tfm = crypto_alloc_shash(algo, 0, CRYPTO_NOLOAD);
    99                  if (IS_ERR(*tfm)) {
   100                          rc = PTR_ERR(*tfm);
   101                          pr_err("Can not allocate %s (reason: %ld)\n", algo, rc);
   102                          *tfm = NULL;
   103                          mutex_unlock(&mutex);
   104                          return ERR_PTR(rc);

105 }
106 if (type == EVM_XATTR_HMAC) {
107 rc = crypto_shash_setkey(*tfm, evmkey,

evmkey_len);

Quoted 16 lines
Quoted 15 lines
   108                          if (rc) {
   109                                  crypto_free_shash(*tfm);
   110                                  *tfm = NULL;
   111                                  mutex_unlock(&mutex);
   112                                  return ERR_PTR(rc);
   113                          }
   114                  }
   115  out:
   116                  mutex_unlock(&mutex);
   117          }
   118
   119          desc = kmalloc(sizeof(*desc) + crypto_shash_descsize(*tfm),
                                                                     ^^^^

I don't understand how using *tfm outside of a lock is safe at all anyway.

Quoted 6 lines
Quoted 3 lines

I think the purpose of the mutex is just to prevent two concurrent allocations. Later, it should not be a problem, as *tfm is never freed.

Actually by the time we take the lock then *tfm is either valid or NULL so this code works. It's confusing though.

static inline bool __must_check IS_ERR_OR_NULL(__force const void *ptr)

{
        return unlikely(!ptr) || IS_ERR_VALUE((unsigned long)ptr);
}

CPU#1 CPU#2

			*tfm = crypto_alloc_shash(algo, 0, CRYPTO_NOLOAD);
unlikely(!ptr)
			*tfm = NULL;

IS_ERR_VALUE((unsigned long)ptr);

desc = kmalloc(sizeof(*desc) + crypto_shash_descsize(*tfm),

Could this happen?

Roberto

HUAWEI TECHNOLOGIES Duesseldorf GmbH, HRB 56063
Managing Director: Li Peng, Li Jian, Shi Yanli

#4 lore raw
Quoted 84 lines · Roberto Sassu
Quoted 71 lines

From: [email protected] [mailto:owner-linux- [email protected]] On Behalf Of Dan Carpenter
Sent: Tuesday, May 12, 2020 2:34 PM

Quoted 30 lines · Roberto Sassu
Quoted 22 lines

From: Dan Carpenter [mailto:[email protected]]
Sent: Tuesday, May 12, 2020 12:48 PM

Hello Roberto Sassu,

The patch 53de3b080d5e: "evm: Check also if *tfm is an error pointer in init_desc()" from Apr 27, 2020, leads to the following static checker warning:

security/integrity/evm/evm_crypto.c:119 init_desc()
error: '*tfm' dereferencing possible ERR_PTR()

security/integrity/evm/evm_crypto.c

    89
    90                  tfm = &evm_tfm[hash_algo];
    91                  algo = hash_algo_name[hash_algo];
    92          }
    93
    94          if (IS_ERR_OR_NULL(*tfm)) {

This used to be a "if (!*tfm)" check.

Quoted 7 lines
    95                  mutex_lock(&mutex);
    96                  if (*tfm)
    97                          goto out;

Then we test again with the lock held. But in the new code if "*tfm" is an error pointer then we jump directly to the unlock and crash on the next line. I can't see how the commit would fix anything.

Quoted 6 lines

Hello Dan

you are right. The fix should be applied in both places.

if (!IS_ERR_OR_NULL(*tfm))

	goto out;

No. I was wrong.

Quoted 11 lines
Quoted 11 lines
    98                  *tfm = crypto_alloc_shash(algo, 0, CRYPTO_NOLOAD);
    99                  if (IS_ERR(*tfm)) {
   100                          rc = PTR_ERR(*tfm);
   101                          pr_err("Can not allocate %s (reason: %ld)\n", algo, rc);
   102                          *tfm = NULL;
   103                          mutex_unlock(&mutex);
   104                          return ERR_PTR(rc);

105 }
106 if (type == EVM_XATTR_HMAC) {
107 rc = crypto_shash_setkey(*tfm, evmkey,

evmkey_len);

Quoted 16 lines
Quoted 15 lines
   108                          if (rc) {
   109                                  crypto_free_shash(*tfm);
   110                                  *tfm = NULL;
   111                                  mutex_unlock(&mutex);
   112                                  return ERR_PTR(rc);
   113                          }
   114                  }
   115  out:
   116                  mutex_unlock(&mutex);
   117          }
   118
   119          desc = kmalloc(sizeof(*desc) + crypto_shash_descsize(*tfm),
                                                                     ^^^^

I don't understand how using *tfm outside of a lock is safe at all anyway.

Quoted 6 lines
Quoted 3 lines

I think the purpose of the mutex is just to prevent two concurrent allocations. Later, it should not be a problem, as *tfm is never freed.

Actually by the time we take the lock then *tfm is either valid or NULL so this code works. It's confusing though.

static inline bool __must_check IS_ERR_OR_NULL(__force const void *ptr)

{
        return unlikely(!ptr) || IS_ERR_VALUE((unsigned long)ptr);
}

CPU#1 CPU#2

Quoted 8 lines
			*tfm = crypto_alloc_shash(algo, 0, CRYPTO_NOLOAD);
unlikely(!ptr)
			*tfm = NULL;

IS_ERR_VALUE((unsigned long)ptr);

desc = kmalloc(sizeof(*desc) + crypto_shash_descsize(*tfm),

Could this happen?

Yeah. Huh. That's true. Good eyes.

I feel like this would be more clear as well if we used a temporary variable instead of working directly on "*tfm".

regards,
dan carpenter

maintainer
#5 lore raw
Quoted 98 lines

From: [email protected] [mailto:owner-linux- [email protected]] On Behalf Of Dan Carpenter
Sent: Tuesday, May 12, 2020 3:04 PM

Quoted 1 line · Roberto Sassu
Quoted 1 line

From: [email protected] [mailto:owner-

linux-

Quoted 23 lines
Quoted 23 lines

[email protected]] On Behalf Of Dan Carpenter
Sent: Tuesday, May 12, 2020 2:34 PM

Quoted 20 lines · Roberto Sassu
Quoted 20 lines

From: Dan Carpenter [mailto:[email protected]]
Sent: Tuesday, May 12, 2020 12:48 PM

Hello Roberto Sassu,

The patch 53de3b080d5e: "evm: Check also if *tfm is an error pointer in init_desc()" from Apr 27, 2020, leads to the following static checker warning:

security/integrity/evm/evm_crypto.c:119 init_desc()
error: '*tfm' dereferencing possible ERR_PTR()

security/integrity/evm/evm_crypto.c

    89
    90                  tfm = &evm_tfm[hash_algo];
    91                  algo = hash_algo_name[hash_algo];
    92          }
    93
    94          if (IS_ERR_OR_NULL(*tfm)) {
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

This used to be a "if (!*tfm)" check.

    95                  mutex_lock(&mutex);
    96                  if (*tfm)
    97                          goto out;

Then we test again with the lock held. But in the new code if "*tfm" is an error pointer then we jump directly to the unlock and crash on

the

Quoted 9 lines
Quoted 9 lines
Quoted 8 lines
Quoted 1 line

next line. I can't see how the commit would fix anything.

Hello Dan

you are right. The fix should be applied in both places.

if (!IS_ERR_OR_NULL(*tfm))

	goto out;
Quoted 7 lines
Quoted 7 lines

No. I was wrong.

Quoted 5 lines
Quoted 5 lines
    98                  *tfm = crypto_alloc_shash(algo, 0, CRYPTO_NOLOAD);
    99                  if (IS_ERR(*tfm)) {

100 rc = PTR_ERR(*tfm);
101 pr_err("Can not allocate %s (reason: %ld)\n", algo,

rc);

Quoted 7 lines
Quoted 7 lines
Quoted 6 lines
Quoted 6 lines
   102                          *tfm = NULL;
   103                          mutex_unlock(&mutex);
   104                          return ERR_PTR(rc);

105 }
106 if (type == EVM_XATTR_HMAC) {
107 rc = crypto_shash_setkey(*tfm, evmkey,

evmkey_len);

Quoted 12 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
   108                          if (rc) {
   109                                  crypto_free_shash(*tfm);
   110                                  *tfm = NULL;
   111                                  mutex_unlock(&mutex);
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
   112                                  return ERR_PTR(rc);
   113                          }
   114                  }
   115  out:
   116                  mutex_unlock(&mutex);
   117          }
   118
   119          desc = kmalloc(sizeof(*desc) +

crypto_shash_descsize(*tfm),

Quoted 11 lines
Quoted 10 lines
Quoted 7 lines
Quoted 3 lines
                                                                     ^^^^

I don't understand how using *tfm outside of a lock is safe at all anyway.

I think the purpose of the mutex is just to prevent two concurrent allocations. Later, it should not be a problem, as *tfm is never freed.

Actually by the time we take the lock then *tfm is either valid or NULL so this code works. It's confusing though.

Quoted 7 lines

static inline bool __must_check IS_ERR_OR_NULL(__force const void *ptr)

{
        return unlikely(!ptr) || IS_ERR_VALUE((unsigned long)ptr);
}

CPU#1 CPU#2
*tfm = crypto_alloc_shash(algo, 0,

CRYPTO_NOLOAD);

Quoted 4 lines

unlikely(!ptr)

			*tfm = NULL;

IS_ERR_VALUE((unsigned long)ptr);

Quoted 8 lines
Quoted 3 lines

desc = kmalloc(sizeof(*desc) + crypto_shash_descsize(*tfm),

Could this happen?

Yeah. Huh. That's true. Good eyes.

I feel like this would be more clear as well if we used a temporary variable instead of working directly on "*tfm".

Yes, seems better. I will send a new version of the patch.

Thanks

Roberto

HUAWEI TECHNOLOGIES Duesseldorf GmbH, HRB 56063
Managing Director: Li Peng, Li Jian, Shi Yanli

#6 lore raw

PATCH 1/1 evm: Fix a small race in init_desc()

In reply to Dan Carpenter

The IS_ERR_OR_NULL() function has two conditions and if we got really unlucky we could hit a race where "ptr" started as an error pointer and then was set to NULL. Both conditions would be false even though the pointer at the end was NULL.

This patch fixes the problem by ensuring that "*tfm" can only be NULL or valid. I have introduced a "tmp_tfm" variable to make that work. I also reversed a condition and pulled the code in one tab.

Fixes 53de3b080d5e: "evm: Check also if *tfm is an error pointer in init_desc()"

---
I just had a go at the fix. I'm not super wedded to this solution, but I hoped it was nice.

 security/integrity/evm/evm_crypto.c | 44 ++++++++++++++---------------
 1 file changed, 22 insertions(+), 22 deletions(-)
security/integrity/evm/evm_crypto.c +22 -22
index 5d3789edab71f..168c3b78ac47b 100644
@@ -73,7 +73,7 @@ static struct shash_desc *init_desc(char type, uint8_t hash_algo)
 {
 	long rc;
 	const char *algo;
-	struct crypto_shash **tfm;
+	struct crypto_shash **tfm, *tmp_tfm;
 	struct shash_desc *desc;
 
 	if (type = EVM_XATTR_HMAC) {
@@ -91,31 +91,31 @@ static struct shash_desc *init_desc(char type, uint8_t hash_algo)
 		algo = hash_algo_name[hash_algo];
 	}
 
-	if (IS_ERR_OR_NULL(*tfm)) {
-		mutex_lock(&mutex);
-		if (*tfm)
-			goto out;
-		*tfm = crypto_alloc_shash(algo, 0, CRYPTO_NOLOAD);
-		if (IS_ERR(*tfm)) {
-			rc = PTR_ERR(*tfm);
-			pr_err("Can not allocate %s (reason: %ld)\n", algo, rc);
-			*tfm = NULL;
+	if (*tfm)
+		goto alloc;
+	mutex_lock(&mutex);
+	if (*tfm)
+		goto unlock;
+
+	tmp_tfm = crypto_alloc_shash(algo, 0, CRYPTO_NOLOAD);
+	if (IS_ERR(tmp_tfm)) {
+		pr_err("Can not allocate %s (reason: %ld)\n", algo,
+		       PTR_ERR(tmp_tfm));
+		mutex_unlock(&mutex);
+		return ERR_CAST(tmp_tfm);
+	}
+	if (type = EVM_XATTR_HMAC) {
+		rc = crypto_shash_setkey(tmp_tfm, evmkey, evmkey_len);
+		if (rc) {
+			crypto_free_shash(tmp_tfm);
 			mutex_unlock(&mutex);
 			return ERR_PTR(rc);
 		}
-		if (type = EVM_XATTR_HMAC) {
-			rc = crypto_shash_setkey(*tfm, evmkey, evmkey_len);
-			if (rc) {
-				crypto_free_shash(*tfm);
-				*tfm = NULL;
-				mutex_unlock(&mutex);
-				return ERR_PTR(rc);
-			}
-		}
-out:
-		mutex_unlock(&mutex);
 	}
-
+	*tfm = tmp_tfm;
+unlock:
+	mutex_unlock(&mutex);
+alloc:
 	desc = kmalloc(sizeof(*desc) + crypto_shash_descsize(*tfm),
 			GFP_KERNEL);
 	if (!desc)
Signature · 1 line
2.26.2
maintainer
#7 lore raw

RE: [PATCH] evm: Fix a small race in init_desc()

Quoted 14 lines

From: [email protected] [mailto:linux-integrity- [email protected]] On Behalf Of Dan Carpenter
Sent: Tuesday, May 12, 2020 3:19 PM
Subject: [PATCH] evm: Fix a small race in init_desc()

The IS_ERR_OR_NULL() function has two conditions and if we got really unlucky we could hit a race where "ptr" started as an error pointer and then was set to NULL. Both conditions would be false even though the pointer at the end was NULL.

This patch fixes the problem by ensuring that "*tfm" can only be NULL or valid. I have introduced a "tmp_tfm" variable to make that work. I also reversed a condition and pulled the code in one tab.

Quoted 7 lines
Fixes 53de3b080d5e: "evm: Check also if *tfm is an error pointer in

init_desc()"

---
I just had a go at the fix. I'm not super wedded to this solution, but I hoped it was nice.

Thanks. I think you can merge both patches in one, as the first one is not yet pulled.

Roberto

HUAWEI TECHNOLOGIES Duesseldorf GmbH, HRB 56063
Managing Director: Li Peng, Li Jian, Shi Yanli

Quoted 76 lines
 security/integrity/evm/evm_crypto.c | 44 ++++++++++++++---------------
 1 file changed, 22 insertions(+), 22 deletions(-)
security/integrity/evm/evm_crypto.c +1 -1
index 5d3789edab71f..168c3b78ac47b 100644
@@ -73,7 +73,7 @@ static struct shash_desc *init_desc(char type, uint8_t
 hash_algo)
 {
 	long rc;
 	const char *algo;
-	struct crypto_shash **tfm;
+	struct crypto_shash **tfm, *tmp_tfm;
 	struct shash_desc *desc;
 
security/integrity/evm/evm_crypto.c +21 -21
 	if (type = EVM_XATTR_HMAC) {
@@ -91,31 +91,31 @@ static struct shash_desc *init_desc(char type, uint8_t
 hash_algo)
 		algo = hash_algo_name[hash_algo];
 	}
 
-	if (IS_ERR_OR_NULL(*tfm)) {
-		mutex_lock(&mutex);
-		if (*tfm)
-			goto out;
-		*tfm = crypto_alloc_shash(algo, 0, CRYPTO_NOLOAD);
-		if (IS_ERR(*tfm)) {
-			rc = PTR_ERR(*tfm);
-			pr_err("Can not allocate %s (reason: %ld)\n", algo,
 rc);
-			*tfm = NULL;
+	if (*tfm)
+		goto alloc;
+	mutex_lock(&mutex);
+	if (*tfm)
+		goto unlock;
+
+	tmp_tfm = crypto_alloc_shash(algo, 0, CRYPTO_NOLOAD);
+	if (IS_ERR(tmp_tfm)) {
+		pr_err("Can not allocate %s (reason: %ld)\n", algo,
+		       PTR_ERR(tmp_tfm));
+		mutex_unlock(&mutex);
+		return ERR_CAST(tmp_tfm);
+	}
+	if (type = EVM_XATTR_HMAC) {
+		rc = crypto_shash_setkey(tmp_tfm, evmkey, evmkey_len);
+		if (rc) {
+			crypto_free_shash(tmp_tfm);
 			mutex_unlock(&mutex);
 			return ERR_PTR(rc);
 		}
-		if (type = EVM_XATTR_HMAC) {
-			rc = crypto_shash_setkey(*tfm, evmkey,
 evmkey_len);
-			if (rc) {
-				crypto_free_shash(*tfm);
-				*tfm = NULL;
-				mutex_unlock(&mutex);
-				return ERR_PTR(rc);
-			}
-		}
-out:
-		mutex_unlock(&mutex);
 	}
-
+	*tfm = tmp_tfm;
+unlock:
+	mutex_unlock(&mutex);
+alloc:
security/integrity/evm/evm_crypto.c +0 -1
 	desc = kmalloc(sizeof(*desc) + crypto_shash_descsize(*tfm),
 			GFP_KERNEL);
 	if (!desc)
--

2.26.2

#8 lore raw

PATCH v2 1/1 evm: Fix a small race in init_desc()

This patch avoids a kernel panic due to accessing an error pointer set by crypto_alloc_shash(). It occurs especially when there are many files that require an unsupported algorithm, as it would increase the likelihood of the following race condition.

Imagine we have two threads and in the first thread crypto_alloc_shash() fails and returns an error pointer.

		*tfm = crypto_alloc_shash(algo, 0, CRYPTO_NOLOAD);
		if (IS_ERR(*tfm)) {
			rc = PTR_ERR(*tfm); <--- FIRST THREAD HERE!
			pr_err("Can not allocate %s (reason: %ld)\n", algo, rc);
			*tfm = NULL;

And the second thread is here:

	if (*tfm = NULL) {  <--- SECOND THREAD HERE!
		mutex_lock(&mutex);
		if (*tfm)
			goto out;

Since "*tfm" is non-NULL, we assume that it is valid and that leads to a crash when it dereferences "*tfm".

	desc = kmalloc(sizeof(*desc) + crypto_shash_descsize(*tfm),
                                                             ^^^^

This patch fixes the problem by introducing a temporary "tmp_tfm" and only setting "*tfm" at the very end after everything has succeeded. The other change is that I reversed the initial "if (!*tfm) {" condition and pull the code in one indent level.

Fixes d46eb3699502b ("evm: crypto hash replaced by shash")

---
v2: I folded mine patch together with Roberto's

 security/integrity/evm/evm_crypto.c | 44 ++++++++++++++---------------
 1 file changed, 22 insertions(+), 22 deletions(-)
security/integrity/evm/evm_crypto.c +22 -22
index 35682852ddea9..c9f7206591b30 100644
@@ -73,7 +73,7 @@ static struct shash_desc *init_desc(char type, uint8_t hash_algo)
 {
 	long rc;
 	const char *algo;
-	struct crypto_shash **tfm;
+	struct crypto_shash **tfm, *tmp_tfm;
 	struct shash_desc *desc;
 
 	if (type = EVM_XATTR_HMAC) {
@@ -91,31 +91,31 @@ static struct shash_desc *init_desc(char type, uint8_t hash_algo)
 		algo = hash_algo_name[hash_algo];
 	}
 
-	if (*tfm = NULL) {
-		mutex_lock(&mutex);
-		if (*tfm)
-			goto out;
-		*tfm = crypto_alloc_shash(algo, 0, CRYPTO_NOLOAD);
-		if (IS_ERR(*tfm)) {
-			rc = PTR_ERR(*tfm);
-			pr_err("Can not allocate %s (reason: %ld)\n", algo, rc);
-			*tfm = NULL;
+	if (*tfm)
+		goto alloc;
+	mutex_lock(&mutex);
+	if (*tfm)
+		goto unlock;
+
+	tmp_tfm = crypto_alloc_shash(algo, 0, CRYPTO_NOLOAD);
+	if (IS_ERR(tmp_tfm)) {
+		pr_err("Can not allocate %s (reason: %ld)\n", algo,
+		       PTR_ERR(tmp_tfm));
+		mutex_unlock(&mutex);
+		return ERR_CAST(tmp_tfm);
+	}
+	if (type = EVM_XATTR_HMAC) {
+		rc = crypto_shash_setkey(tmp_tfm, evmkey, evmkey_len);
+		if (rc) {
+			crypto_free_shash(tmp_tfm);
 			mutex_unlock(&mutex);
 			return ERR_PTR(rc);
 		}
-		if (type = EVM_XATTR_HMAC) {
-			rc = crypto_shash_setkey(*tfm, evmkey, evmkey_len);
-			if (rc) {
-				crypto_free_shash(*tfm);
-				*tfm = NULL;
-				mutex_unlock(&mutex);
-				return ERR_PTR(rc);
-			}
-		}
-out:
-		mutex_unlock(&mutex);
 	}
-
+	*tfm = tmp_tfm;
+unlock:
+	mutex_unlock(&mutex);
+alloc:
 	desc = kmalloc(sizeof(*desc) + crypto_shash_descsize(*tfm),
 			GFP_KERNEL);
 	if (!desc)
Signature · 1 line
2.26.2
maintainer Acked-by on v2 1/1
#9 lore raw

RE: [PATCH v2] evm: Fix a small race in init_desc()

Quoted 35 lines

From: Dan Carpenter [mailto:[email protected]]
Sent: Tuesday, May 12, 2020 7:47 PM
This patch avoids a kernel panic due to accessing an error pointer set by crypto_alloc_shash(). It occurs especially when there are many files that require an unsupported algorithm, as it would increase the likelihood of the following race condition.

Imagine we have two threads and in the first thread crypto_alloc_shash() fails and returns an error pointer.

		*tfm = crypto_alloc_shash(algo, 0, CRYPTO_NOLOAD);
		if (IS_ERR(*tfm)) {
			rc = PTR_ERR(*tfm); <--- FIRST THREAD HERE!
			pr_err("Can not allocate %s (reason: %ld)\n", algo,
rc);
			*tfm = NULL;

And the second thread is here:

	if (*tfm = NULL) {  <--- SECOND THREAD HERE!
		mutex_lock(&mutex);
		if (*tfm)
			goto out;

Since "*tfm" is non-NULL, we assume that it is valid and that leads to a crash when it dereferences "*tfm".

	desc = kmalloc(sizeof(*desc) + crypto_shash_descsize(*tfm),
                                                             ^^^^

This patch fixes the problem by introducing a temporary "tmp_tfm" and only setting "*tfm" at the very end after everything has succeeded. The other change is that I reversed the initial "if (!*tfm) {" condition and pull the code in one indent level.

Quoted 5 lines
Fixes d46eb3699502b ("evm: crypto hash replaced by shash")

Roberto

HUAWEI TECHNOLOGIES Duesseldorf GmbH, HRB 56063
Managing Director: Li Peng, Li Jian, Shi Yanli

Quoted 79 lines

---
v2: I folded mine patch together with Roberto's

 security/integrity/evm/evm_crypto.c | 44 ++++++++++++++---------------
 1 file changed, 22 insertions(+), 22 deletions(-)
security/integrity/evm/evm_crypto.c +1 -1
index 35682852ddea9..c9f7206591b30 100644
@@ -73,7 +73,7 @@ static struct shash_desc *init_desc(char type, uint8_t
 hash_algo)
 {
 	long rc;
 	const char *algo;
-	struct crypto_shash **tfm;
+	struct crypto_shash **tfm, *tmp_tfm;
 	struct shash_desc *desc;
 
security/integrity/evm/evm_crypto.c +21 -21
 	if (type = EVM_XATTR_HMAC) {
@@ -91,31 +91,31 @@ static struct shash_desc *init_desc(char type, uint8_t
 hash_algo)
 		algo = hash_algo_name[hash_algo];
 	}
 
-	if (*tfm = NULL) {
-		mutex_lock(&mutex);
-		if (*tfm)
-			goto out;
-		*tfm = crypto_alloc_shash(algo, 0, CRYPTO_NOLOAD);
-		if (IS_ERR(*tfm)) {
-			rc = PTR_ERR(*tfm);
-			pr_err("Can not allocate %s (reason: %ld)\n", algo,
 rc);
-			*tfm = NULL;
+	if (*tfm)
+		goto alloc;
+	mutex_lock(&mutex);
+	if (*tfm)
+		goto unlock;
+
+	tmp_tfm = crypto_alloc_shash(algo, 0, CRYPTO_NOLOAD);
+	if (IS_ERR(tmp_tfm)) {
+		pr_err("Can not allocate %s (reason: %ld)\n", algo,
+		       PTR_ERR(tmp_tfm));
+		mutex_unlock(&mutex);
+		return ERR_CAST(tmp_tfm);
+	}
+	if (type = EVM_XATTR_HMAC) {
+		rc = crypto_shash_setkey(tmp_tfm, evmkey, evmkey_len);
+		if (rc) {
+			crypto_free_shash(tmp_tfm);
 			mutex_unlock(&mutex);
 			return ERR_PTR(rc);
 		}
-		if (type = EVM_XATTR_HMAC) {
-			rc = crypto_shash_setkey(*tfm, evmkey,
 evmkey_len);
-			if (rc) {
-				crypto_free_shash(*tfm);
-				*tfm = NULL;
-				mutex_unlock(&mutex);
-				return ERR_PTR(rc);
-			}
-		}
-out:
-		mutex_unlock(&mutex);
 	}
-
+	*tfm = tmp_tfm;
+unlock:
+	mutex_unlock(&mutex);
+alloc:
security/integrity/evm/evm_crypto.c +0 -1
 	desc = kmalloc(sizeof(*desc) + crypto_shash_descsize(*tfm),
 			GFP_KERNEL);
 	if (!desc)
--

2.26.2

#10 lore raw

RE: [PATCH v2] evm: Fix a small race in init_desc()

Quoted 47 lines

-----Original Message-----
From: Roberto Sassu
Sent: Thursday, May 14, 2020 8:47 AM
To: Dan Carpenter <[email protected]>; Mimi Zohar <[email protected]>; Krzysztof Struczynski <[email protected]>
Cc: James Morris <[email protected]>; Serge E. Hallyn <[email protected]>; Dmitry Kasatkin <[email protected]>; linux- [email protected]; [email protected]; kernel- [email protected]
Subject: RE: [PATCH v2] evm: Fix a small race in init_desc()

Quoted 34 lines

From: Dan Carpenter [mailto:[email protected]]
Sent: Tuesday, May 12, 2020 7:47 PM
This patch avoids a kernel panic due to accessing an error pointer set by crypto_alloc_shash(). It occurs especially when there are many files that require an unsupported algorithm, as it would increase the likelihood of the following race condition.

Imagine we have two threads and in the first thread crypto_alloc_shash() fails and returns an error pointer.

		*tfm = crypto_alloc_shash(algo, 0, CRYPTO_NOLOAD);
		if (IS_ERR(*tfm)) {
			rc = PTR_ERR(*tfm); <--- FIRST THREAD HERE!
			pr_err("Can not allocate %s (reason: %ld)\n", algo, rc);
			*tfm = NULL;

And the second thread is here:

	if (*tfm = NULL) {  <--- SECOND THREAD HERE!
		mutex_lock(&mutex);
		if (*tfm)
			goto out;

Since "*tfm" is non-NULL, we assume that it is valid and that leads to a crash when it dereferences "*tfm".

	desc = kmalloc(sizeof(*desc) + crypto_shash_descsize(*tfm),
                                                             ^^^^

This patch fixes the problem by introducing a temporary "tmp_tfm" and only setting "*tfm" at the very end after everything has succeeded. The other change is that I reversed the initial "if (!*tfm) {" condition and pull the code in one indent level.

Quoted 7 lines
Quoted 5 lines
Fixes d46eb3699502b ("evm: crypto hash replaced by shash")

Krzysztof

Quoted 89 lines

Roberto

HUAWEI TECHNOLOGIES Duesseldorf GmbH, HRB 56063 Managing Director: Li Peng, Li Jian, Shi Yanli

Quoted 82 lines

---
v2: I folded mine patch together with Roberto's

 security/integrity/evm/evm_crypto.c | 44
++++++++++++++---------------
 1 file changed, 22 insertions(+), 22 deletions(-)
security/integrity/evm/evm_crypto.c +0 -0
index 35682852ddea9..c9f7206591b30 100644
@@ -73,7 +73,7 @@ static struct shash_desc *init_desc(char type,

uint8_t
hash_algo)
{

security/integrity/evm/evm_crypto.c +1 -1
 	long rc;
 	const char *algo;
-	struct crypto_shash **tfm;
+	struct crypto_shash **tfm, *tmp_tfm;
 	struct shash_desc *desc;
 
 	if (type = EVM_XATTR_HMAC) {
@@ -91,31 +91,31 @@ static struct shash_desc *init_desc(char type,

uint8_t
hash_algo)

security/integrity/evm/evm_crypto.c +0 -8
 		algo = hash_algo_name[hash_algo];
 	}
 
-	if (*tfm = NULL) {
-		mutex_lock(&mutex);
-		if (*tfm)
-			goto out;
-		*tfm = crypto_alloc_shash(algo, 0, CRYPTO_NOLOAD);
-		if (IS_ERR(*tfm)) {
-			rc = PTR_ERR(*tfm);
-			pr_err("Can not allocate %s (reason: %ld)\n", algo,

rc);

security/integrity/evm/evm_crypto.c +17 -3
-			*tfm = NULL;
+	if (*tfm)
+		goto alloc;
+	mutex_lock(&mutex);
+	if (*tfm)
+		goto unlock;
+
+	tmp_tfm = crypto_alloc_shash(algo, 0, CRYPTO_NOLOAD);
+	if (IS_ERR(tmp_tfm)) {
+		pr_err("Can not allocate %s (reason: %ld)\n", algo,
+		       PTR_ERR(tmp_tfm));
+		mutex_unlock(&mutex);
+		return ERR_CAST(tmp_tfm);
+	}
+	if (type = EVM_XATTR_HMAC) {
+		rc = crypto_shash_setkey(tmp_tfm, evmkey, evmkey_len);
+		if (rc) {
+			crypto_free_shash(tmp_tfm);
 			mutex_unlock(&mutex);
 			return ERR_PTR(rc);
 		}
-		if (type = EVM_XATTR_HMAC) {
-			rc = crypto_shash_setkey(*tfm, evmkey,

evmkey_len);

security/integrity/evm/evm_crypto.c +4 -11
-			if (rc) {
-				crypto_free_shash(*tfm);
-				*tfm = NULL;
-				mutex_unlock(&mutex);
-				return ERR_PTR(rc);
-			}
-		}
-out:
-		mutex_unlock(&mutex);
 	}
-
+	*tfm = tmp_tfm;
+unlock:
+	mutex_unlock(&mutex);
+alloc:
 	desc = kmalloc(sizeof(*desc) + crypto_shash_descsize(*tfm),
 			GFP_KERNEL);
 	if (!desc)
--

2.26.2

maintainer
#11 lore raw

Re: [PATCH v2] evm: Fix a small race in init_desc()

Quoted 39 lines · Krzysztof Struczynski
Quoted 39 lines
Quoted 38 lines

From: Dan Carpenter [mailto:[email protected]]
This patch avoids a kernel panic due to accessing an error pointer set by crypto_alloc_shash(). It occurs especially when there are many files that require an unsupported algorithm, as it would increase the likelihood of the following race condition.

Imagine we have two threads and in the first thread crypto_alloc_shash() fails and returns an error pointer.

		*tfm = crypto_alloc_shash(algo, 0, CRYPTO_NOLOAD);
		if (IS_ERR(*tfm)) {
			rc = PTR_ERR(*tfm); <--- FIRST THREAD HERE!
			pr_err("Can not allocate %s (reason: %ld)\n", algo, rc);
			*tfm = NULL;

And the second thread is here:

	if (*tfm = NULL) {  <--- SECOND THREAD HERE!
		mutex_lock(&mutex);
		if (*tfm)
			goto out;

Since "*tfm" is non-NULL, we assume that it is valid and that leads to a crash when it dereferences "*tfm".

	desc = kmalloc(sizeof(*desc) + crypto_shash_descsize(*tfm),
                                                             ^^^^

This patch fixes the problem by introducing a temporary "tmp_tfm" and only setting "*tfm" at the very end after everything has succeeded. The other change is that I reversed the initial "if (!*tfm) {" condition and pull the code in one indent level.

Fixes d46eb3699502b ("evm: crypto hash replaced by shash")
Quoted 3 lines
Quoted 1 line

Thanks, Roberto and Krzysztof.

This patch is now queued in the "fixes" branch.

Mimi