Skip to contentSkip to conversation

KEYS: trusted: fix a couple error pointer dereferences

PATCH In review Dan Carpenter 3 participants 5 messages Apr 14, 2021 – Apr 16, 2021

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

PATCH 1/1 KEYS: trusted: fix a couple error pointer dereferences

If registering "reg_shm_out" fails, then it is an error pointer and the error handling will call tee_shm_free(reg_shm_out) which leads to an error pointer dereference and an Oops.

I've re-arranged it so we only free things that have been allocated successfully.

Fixes 6dd95e650c8a ("KEYS: trusted: Introduce TEE based Trusted Keys")
---
 security/keys/trusted-keys/trusted_tee.c | 24 ++++++++++--------------
 1 file changed, 10 insertions(+), 14 deletions(-)
security/keys/trusted-keys/trusted_tee.c +10 -14
index 2ce66c199e1d..45f96f6ed673 100644
@@ -65,7 +65,7 @@ static int trusted_tee_seal(struct trusted_key_payload *p, char *datablob)
 	int ret;
 	struct tee_ioctl_invoke_arg inv_arg;
 	struct tee_param param[4];
-	struct tee_shm *reg_shm_in = NULL, *reg_shm_out = NULL;
+	struct tee_shm *reg_shm_in, *reg_shm_out;
 
 	memset(&inv_arg, 0, sizeof(inv_arg));
 	memset(&param, 0, sizeof(param));
@@ -84,7 +84,7 @@ static int trusted_tee_seal(struct trusted_key_payload *p, char *datablob)
 	if (IS_ERR(reg_shm_out)) {
 		dev_err(pvt_data.dev, "blob shm register failed\n");
 		ret = PTR_ERR(reg_shm_out);
-		goto out;
+		goto free_shm_in;
 	}
 
 	inv_arg.func = TA_CMD_SEAL;
@@ -109,11 +109,9 @@ static int trusted_tee_seal(struct trusted_key_payload *p, char *datablob)
 		p->blob_len = param[1].u.memref.size;
 	}
 
-out:
-	if (reg_shm_out)
-		tee_shm_free(reg_shm_out);
-	if (reg_shm_in)
-		tee_shm_free(reg_shm_in);
+	tee_shm_free(reg_shm_out);
+free_shm_in:
+	tee_shm_free(reg_shm_in);
 
 	return ret;
 }
@@ -126,7 +124,7 @@ static int trusted_tee_unseal(struct trusted_key_payload *p, char *datablob)
 	int ret;
 	struct tee_ioctl_invoke_arg inv_arg;
 	struct tee_param param[4];
-	struct tee_shm *reg_shm_in = NULL, *reg_shm_out = NULL;
+	struct tee_shm *reg_shm_in, *reg_shm_out;
 
 	memset(&inv_arg, 0, sizeof(inv_arg));
 	memset(&param, 0, sizeof(param));
@@ -145,7 +143,7 @@ static int trusted_tee_unseal(struct trusted_key_payload *p, char *datablob)
 	if (IS_ERR(reg_shm_out)) {
 		dev_err(pvt_data.dev, "key shm register failed\n");
 		ret = PTR_ERR(reg_shm_out);
-		goto out;
+		goto free_shm_in;
 	}
 
 	inv_arg.func = TA_CMD_UNSEAL;
@@ -170,11 +168,9 @@ static int trusted_tee_unseal(struct trusted_key_payload *p, char *datablob)
 		p->key_len = param[1].u.memref.size;
 	}
 
-out:
-	if (reg_shm_out)
-		tee_shm_free(reg_shm_out);
-	if (reg_shm_in)
-		tee_shm_free(reg_shm_in);
+	tee_shm_free(reg_shm_out);
+free_shm_in:
+	tee_shm_free(reg_shm_in);
 
 	return ret;
 }
Signature · 1 line
2.30.2
maintainer
#1 lore raw
Quoted 18 lines · Dan Carpenter

If registering "reg_shm_out" fails, then it is an error pointer and the error handling will call tee_shm_free(reg_shm_out) which leads to an error pointer dereference and an Oops.

I've re-arranged it so we only free things that have been allocated successfully.

Fixes 6dd95e650c8a ("KEYS: trusted: Introduce TEE based Trusted Keys")
---
 security/keys/trusted-keys/trusted_tee.c | 24 ++++++++++--------------
 1 file changed, 10 insertions(+), 14 deletions(-)
security/keys/trusted-keys/trusted_tee.c +0 -0
index 2ce66c199e1d..45f96f6ed673 100644

security/keys/trusted-keys/trusted_tee.c static int trusted_tee_seal(struct trusted_key_payload *p, char *datablob) · line 68

Quoted 5 lines
security/keys/trusted-keys/trusted_tee.c +1 -1
@@ -65,7 +65,7 @@ static int trusted_tee_seal(struct trusted_key_payload *p, char *datablob)
 	int ret;
 	struct tee_ioctl_invoke_arg inv_arg;
 	struct tee_param param[4];
-	struct tee_shm *reg_shm_in = NULL, *reg_shm_out = NULL;
+	struct tee_shm *reg_shm_in, *reg_shm_out;

I don't have this file (trusted_tee.c) in my tree and there's no lore link here to previous what threads this depends on. Based on the context I can't verify that reg_shm_in will always be initialized before you get to the free_shm_in label.

Quoted 62 lines
security/keys/trusted-keys/trusted_tee.c +9 -13
 	memset(&inv_arg, 0, sizeof(inv_arg));
 	memset(&param, 0, sizeof(param));
@@ -84,7 +84,7 @@ static int trusted_tee_seal(struct trusted_key_payload *p, char *datablob)
 	if (IS_ERR(reg_shm_out)) {
 		dev_err(pvt_data.dev, "blob shm register failed\n");
 		ret = PTR_ERR(reg_shm_out);
-		goto out;
+		goto free_shm_in;
 	}
 
 	inv_arg.func = TA_CMD_SEAL;
@@ -109,11 +109,9 @@ static int trusted_tee_seal(struct trusted_key_payload *p, char *datablob)
 		p->blob_len = param[1].u.memref.size;
 	}
 
-out:
-	if (reg_shm_out)
-		tee_shm_free(reg_shm_out);
-	if (reg_shm_in)
-		tee_shm_free(reg_shm_in);
+	tee_shm_free(reg_shm_out);
+free_shm_in:
+	tee_shm_free(reg_shm_in);
 
 	return ret;
 }
@@ -126,7 +124,7 @@ static int trusted_tee_unseal(struct trusted_key_payload *p, char *datablob)
 	int ret;
 	struct tee_ioctl_invoke_arg inv_arg;
 	struct tee_param param[4];
-	struct tee_shm *reg_shm_in = NULL, *reg_shm_out = NULL;
+	struct tee_shm *reg_shm_in, *reg_shm_out;
 
 	memset(&inv_arg, 0, sizeof(inv_arg));
 	memset(&param, 0, sizeof(param));
@@ -145,7 +143,7 @@ static int trusted_tee_unseal(struct trusted_key_payload *p, char *datablob)
 	if (IS_ERR(reg_shm_out)) {
 		dev_err(pvt_data.dev, "key shm register failed\n");
 		ret = PTR_ERR(reg_shm_out);
-		goto out;
+		goto free_shm_in;
 	}
 
 	inv_arg.func = TA_CMD_UNSEAL;
@@ -170,11 +168,9 @@ static int trusted_tee_unseal(struct trusted_key_payload *p, char *datablob)
 		p->key_len = param[1].u.memref.size;
 	}
 
-out:
-	if (reg_shm_out)
-		tee_shm_free(reg_shm_out);
-	if (reg_shm_in)
-		tee_shm_free(reg_shm_in);
+	tee_shm_free(reg_shm_out);
+free_shm_in:
+	tee_shm_free(reg_shm_in);
 
 	return ret;
 }
Signature · 1 line
2.30.2
Reviewed-by on v1 1/1
#4 lore raw
In reply to Dan Carpenter

Hi Dan,

Quoted 5 lines · Dan Carpenter

If registering "reg_shm_out" fails, then it is an error pointer and the error handling will call tee_shm_free(reg_shm_out) which leads to an error pointer dereference and an Oops.

Quoted 9 lines

I've re-arranged it so we only free things that have been allocated successfully.

Fixes 6dd95e650c8a ("KEYS: trusted: Introduce TEE based Trusted Keys")
---
 security/keys/trusted-keys/trusted_tee.c | 24 ++++++++++--------------
 1 file changed, 10 insertions(+), 14 deletions(-)

Thanks for fixing the error path. Although an alternative simple fix could be [1] but I liked your approach to get rid of redundant "if" checks. FWIW:

[1]

(quoted diff) +0 -0

b/security/keys/trusted-keys/trusted_tee.c
index 2ce66c199e1d..c4a54b0fa3eb 100644

security/keys/trusted-keys/trusted_tee.c +0 -0
@@ -84,6 +84,7 @@ static int trusted_tee_seal(struct

trusted_key_payload *p, char *datablob)

        if (IS_ERR(reg_shm_out)) {
                dev_err(pvt_data.dev, "blob shm register failed\n");
                ret = PTR_ERR(reg_shm_out);
+               reg_shm_out = NULL;
                goto out;
        }

@@ -145,6 +146,7 @@ static int trusted_tee_unseal(struct trusted_key_payload *p, char *datablob)

        if (IS_ERR(reg_shm_out)) {
                dev_err(pvt_data.dev, "key shm register failed\n");
                ret = PTR_ERR(reg_shm_out);
+               reg_shm_out = NULL;
                goto out;
        }

-Sumit

Quoted 73 lines
security/keys/trusted-keys/trusted_tee.c +10 -15
index 2ce66c199e1d..45f96f6ed673 100644
@@ -65,7 +65,7 @@ static int trusted_tee_seal(struct trusted_key_payload *p, char *datablob)
        int ret;
        struct tee_ioctl_invoke_arg inv_arg;
        struct tee_param param[4];
-       struct tee_shm *reg_shm_in = NULL, *reg_shm_out = NULL;
+       struct tee_shm *reg_shm_in, *reg_shm_out;
 
        memset(&inv_arg, 0, sizeof(inv_arg));
        memset(&param, 0, sizeof(param));
@@ -84,7 +84,7 @@ static int trusted_tee_seal(struct trusted_key_payload *p, char *datablob)
        if (IS_ERR(reg_shm_out)) {
                dev_err(pvt_data.dev, "blob shm register failed\n");
                ret = PTR_ERR(reg_shm_out);
-               goto out;
+               goto free_shm_in;
        }
 
        inv_arg.func = TA_CMD_SEAL;
@@ -109,11 +109,9 @@ static int trusted_tee_seal(struct trusted_key_payload *p, char *datablob)
                p->blob_len = param[1].u.memref.size;
        }
 
-out:
-       if (reg_shm_out)
-               tee_shm_free(reg_shm_out);
-       if (reg_shm_in)
-               tee_shm_free(reg_shm_in);
+       tee_shm_free(reg_shm_out);
+free_shm_in:
+       tee_shm_free(reg_shm_in);
 
        return ret;
 }
@@ -126,7 +124,7 @@ static int trusted_tee_unseal(struct trusted_key_payload *p, char *datablob)
        int ret;
        struct tee_ioctl_invoke_arg inv_arg;
        struct tee_param param[4];
-       struct tee_shm *reg_shm_in = NULL, *reg_shm_out = NULL;
+       struct tee_shm *reg_shm_in, *reg_shm_out;
 
        memset(&inv_arg, 0, sizeof(inv_arg));
        memset(&param, 0, sizeof(param));
@@ -145,7 +143,7 @@ static int trusted_tee_unseal(struct trusted_key_payload *p, char *datablob)
        if (IS_ERR(reg_shm_out)) {
                dev_err(pvt_data.dev, "key shm register failed\n");
                ret = PTR_ERR(reg_shm_out);
-               goto out;
+               goto free_shm_in;
        }
 
        inv_arg.func = TA_CMD_UNSEAL;
@@ -170,11 +168,9 @@ static int trusted_tee_unseal(struct trusted_key_payload *p, char *datablob)
                p->key_len = param[1].u.memref.size;
        }
 
-out:
-       if (reg_shm_out)
-               tee_shm_free(reg_shm_out);
-       if (reg_shm_in)
-               tee_shm_free(reg_shm_in);
+       tee_shm_free(reg_shm_out);
+free_shm_in:
+       tee_shm_free(reg_shm_in);
 
        return ret;
 }
--

2.30.2

#2 lore raw
In reply to Serge E. Hallyn

Hi Serge,

Quoted 15 lines · Serge E. Hallyn
Quoted 13 lines · Dan Carpenter

If registering "reg_shm_out" fails, then it is an error pointer and the error handling will call tee_shm_free(reg_shm_out) which leads to an error pointer dereference and an Oops.

I've re-arranged it so we only free things that have been allocated successfully.

Fixes 6dd95e650c8a ("KEYS: trusted: Introduce TEE based Trusted Keys")
---
 security/keys/trusted-keys/trusted_tee.c | 24 ++++++++++--------------
 1 file changed, 10 insertions(+), 14 deletions(-)
Quoted 16 lines

security/keys/trusted-keys/trusted_tee.c static int trusted_tee_seal(struct trusted_key_payload *p, char *datablob) · line 68

Quoted 10 lines
security/keys/trusted-keys/trusted_tee.c +1 -1
index 2ce66c199e1d..45f96f6ed673 100644
@@ -65,7 +65,7 @@ static int trusted_tee_seal(struct trusted_key_payload *p, char *datablob)
      int ret;
      struct tee_ioctl_invoke_arg inv_arg;
      struct tee_param param[4];
-     struct tee_shm *reg_shm_in = NULL, *reg_shm_out = NULL;
+     struct tee_shm *reg_shm_in, *reg_shm_out;

I don't have this file (trusted_tee.c) in my tree and there's no lore link here to previous what threads this depends on. Based on the context I can't verify that reg_shm_in will always be initialized before you get to the free_shm_in label.

You can find trusted_tee.c here [1].

[1] https://git.kernel.org/pub/scm/linux/kernel/git/jarkko/linux-tpmdd.git/tree/security/keys/trusted-keys/trusted_tee.c

-Sumit

Quoted 62 lines
Quoted 62 lines
security/keys/trusted-keys/trusted_tee.c +9 -14
      memset(&inv_arg, 0, sizeof(inv_arg));
      memset(&param, 0, sizeof(param));
@@ -84,7 +84,7 @@ static int trusted_tee_seal(struct trusted_key_payload *p, char *datablob)
      if (IS_ERR(reg_shm_out)) {
              dev_err(pvt_data.dev, "blob shm register failed\n");
              ret = PTR_ERR(reg_shm_out);
-             goto out;
+             goto free_shm_in;
      }
 
      inv_arg.func = TA_CMD_SEAL;
@@ -109,11 +109,9 @@ static int trusted_tee_seal(struct trusted_key_payload *p, char *datablob)
              p->blob_len = param[1].u.memref.size;
      }
 
-out:
-     if (reg_shm_out)
-             tee_shm_free(reg_shm_out);
-     if (reg_shm_in)
-             tee_shm_free(reg_shm_in);
+     tee_shm_free(reg_shm_out);
+free_shm_in:
+     tee_shm_free(reg_shm_in);
 
      return ret;
 }
@@ -126,7 +124,7 @@ static int trusted_tee_unseal(struct trusted_key_payload *p, char *datablob)
      int ret;
      struct tee_ioctl_invoke_arg inv_arg;
      struct tee_param param[4];
-     struct tee_shm *reg_shm_in = NULL, *reg_shm_out = NULL;
+     struct tee_shm *reg_shm_in, *reg_shm_out;
 
      memset(&inv_arg, 0, sizeof(inv_arg));
      memset(&param, 0, sizeof(param));
@@ -145,7 +143,7 @@ static int trusted_tee_unseal(struct trusted_key_payload *p, char *datablob)
      if (IS_ERR(reg_shm_out)) {
              dev_err(pvt_data.dev, "key shm register failed\n");
              ret = PTR_ERR(reg_shm_out);
-             goto out;
+             goto free_shm_in;
      }
 
      inv_arg.func = TA_CMD_UNSEAL;
@@ -170,11 +168,9 @@ static int trusted_tee_unseal(struct trusted_key_payload *p, char *datablob)
              p->key_len = param[1].u.memref.size;
      }
 
-out:
-     if (reg_shm_out)
-             tee_shm_free(reg_shm_out);
-     if (reg_shm_in)
-             tee_shm_free(reg_shm_in);
+     tee_shm_free(reg_shm_out);
+free_shm_in:
+     tee_shm_free(reg_shm_in);
 
      return ret;
 }
--

2.30.2

maintainer
#3 lore raw
Quoted 35 lines · Sumit Garg

Hi Serge,

Quoted 15 lines · Serge E. Hallyn
Quoted 13 lines · Dan Carpenter

If registering "reg_shm_out" fails, then it is an error pointer and the error handling will call tee_shm_free(reg_shm_out) which leads to an error pointer dereference and an Oops.

I've re-arranged it so we only free things that have been allocated successfully.

Fixes 6dd95e650c8a ("KEYS: trusted: Introduce TEE based Trusted Keys")
---
 security/keys/trusted-keys/trusted_tee.c | 24 ++++++++++--------------
 1 file changed, 10 insertions(+), 14 deletions(-)
Quoted 16 lines

security/keys/trusted-keys/trusted_tee.c static int trusted_tee_seal(struct trusted_key_payload *p, char *datablob) · line 68

Quoted 10 lines
security/keys/trusted-keys/trusted_tee.c +1 -1
index 2ce66c199e1d..45f96f6ed673 100644
@@ -65,7 +65,7 @@ static int trusted_tee_seal(struct trusted_key_payload *p, char *datablob)
      int ret;
      struct tee_ioctl_invoke_arg inv_arg;
      struct tee_param param[4];
-     struct tee_shm *reg_shm_in = NULL, *reg_shm_out = NULL;
+     struct tee_shm *reg_shm_in, *reg_shm_out;

I don't have this file (trusted_tee.c) in my tree and there's no lore link here to previous what threads this depends on. Based on the context I can't verify that reg_shm_in will always be initialized before you get to the free_shm_in label.

Quoted 3 lines

You can find trusted_tee.c here [1].

[1] https://git.kernel.org/pub/scm/linux/kernel/git/jarkko/linux-tpmdd.git/tree/security/keys/trusted-keys/trusted_tee.c

Thanks. Looks good then :)