Skip to contentSkip to conversation

drm/amdgpu: double free error and freeing uninitialized null pointer

PATCH Superseded Sebin Sebastian 3 participants 6 messages Jul 10, 2022 – Jul 18, 2022

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 drm/amdgpu: double free error and freeing uninitialized null pointer

Fix two coverity warning's double free and and an uninitialized pointer read. Both tmp and new are pointing at same address and both are freed which leads to double free. Freeing tmp in the condition after new is assigned with new address fixes the double free issue. new is not initialized to null which also leads to a free on an uninitialized pointer.
Coverity issue: 1518665 (uninitialized pointer read)

		1518679 (double free)
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_debugfs.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_debugfs.c +5 -3
index f3b3c688e4e7..d82fe0e1b06b 100644
@@ -1660,7 +1660,7 @@ static ssize_t amdgpu_reset_dump_register_list_write(struct file *f,
 {
 	struct amdgpu_device *adev = (struct amdgpu_device *)file_inode(f)->i_private;
 	char reg_offset[11];
-	uint32_t *new, *tmp = NULL;
+	uint32_t *new = NULL, *tmp = NULL;
 	int ret, i = 0, len = 0;
 
 	do {
@@ -1692,17 +1692,19 @@ static ssize_t amdgpu_reset_dump_register_list_write(struct file *f,
 		goto error_free;
 	}
 	ret = down_write_killable(&adev->reset_domain->sem);
-	if (ret)
+	if (ret) {
+		kfree(tmp);
 		goto error_free;
+	}
 
 	swap(adev->reset_dump_reg_list, tmp);
 	swap(adev->reset_dump_reg_value, new);
 	adev->num_regs = i;
 	up_write(&adev->reset_domain->sem);
+	kfree(tmp);
 	ret = size;
 
 error_free:
-	kfree(tmp);
 	kfree(new);
 	return ret;
 }
Signature · 1 line
2.34.1
#1 lore raw

Hi Sebin,

Às 10:29 de 10/07/22, Sebin Sebastian escreveu:

Quoted 8 lines

Fix two coverity warning's double free and and an uninitialized pointer read. Both tmp and new are pointing at same address and both are freed which leads to double free. Freeing tmp in the condition after new is assigned with new address fixes the double free issue. new is not initialized to null which also leads to a free on an uninitialized pointer.
Coverity issue: 1518665 (uninitialized pointer read)

		1518679 (double free)

What are those numbers?

Quoted 20 lines
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_debugfs.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_debugfs.c +1 -1
index f3b3c688e4e7..d82fe0e1b06b 100644
@@ -1660,7 +1660,7 @@ static ssize_t amdgpu_reset_dump_register_list_write(struct file *f,
 {
 	struct amdgpu_device *adev = (struct amdgpu_device *)file_inode(f)->i_private;
 	char reg_offset[11];
-	uint32_t *new, *tmp = NULL;
+	uint32_t *new = NULL, *tmp = NULL;
 	int ret, i = 0, len = 0;
 
 	do {

drivers/gpu/drm/amd/amdgpu/amdgpu_debugfs.c static ssize_t amdgpu_reset_dump_register_list_write(struct file *f, · line 1693

Quoted 2 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_debugfs.c +0 -0
@@ -1692,17 +1692,19 @@ static ssize_t amdgpu_reset_dump_register_list_write(struct file *f,
 		goto error_free;
 	}

If the if (!new) { above this line is true, will be tmp freed?

Quoted 19 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_debugfs.c +4 -2
 	ret = down_write_killable(&adev->reset_domain->sem);
-	if (ret)
+	if (ret) {
+		kfree(tmp);
 		goto error_free;
+	}
 
 	swap(adev->reset_dump_reg_list, tmp);
 	swap(adev->reset_dump_reg_value, new);
 	adev->num_regs = i;
 	up_write(&adev->reset_domain->sem);
+	kfree(tmp);
 	ret = size;
 
 error_free:
-	kfree(tmp);
 	kfree(new);
 	return ret;
 }
#2 lore raw
Quoted 12 lines · André Almeida

Hi Sebin,

Às 10:29 de 10/07/22, Sebin Sebastian escreveu:

Quoted 8 lines

Fix two coverity warning's double free and and an uninitialized pointer read. Both tmp and new are pointing at same address and both are freed which leads to double free. Freeing tmp in the condition after new is assigned with new address fixes the double free issue. new is not initialized to null which also leads to a free on an uninitialized pointer.
Coverity issue: 1518665 (uninitialized pointer read)

		1518679 (double free)
Quoted 2 lines

What are those numbers?

These numbers are the issue ID's for the errors that are being reported by the coverity static analyzer tool.

Quoted 18 lines
Quoted 18 lines
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_debugfs.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_debugfs.c +1 -1
index f3b3c688e4e7..d82fe0e1b06b 100644
@@ -1660,7 +1660,7 @@ static ssize_t amdgpu_reset_dump_register_list_write(struct file *f,
 {
 	struct amdgpu_device *adev = (struct amdgpu_device *)file_inode(f)->i_private;
 	char reg_offset[11];
-	uint32_t *new, *tmp = NULL;
+	uint32_t *new = NULL, *tmp = NULL;
 	int ret, i = 0, len = 0;
 
Quoted 7 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_debugfs.c static ssize_t amdgpu_reset_dump_register_list_write(struct file *f, · line 1693

Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_debugfs.c +0 -0
 	do {
@@ -1692,17 +1692,19 @@ static ssize_t amdgpu_reset_dump_register_list_write(struct file *f,
 		goto error_free;
 	}

If the if (!new) { above this line is true, will be tmp freed?

Yes, It doesn't seem to free tmp here. Should I free tmp immediately after the do while loop and remove kfree(tmp) from the if (ret) block? Thanks for pointing out the errors.

Quoted 19 lines
Quoted 19 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_debugfs.c +4 -2
 	ret = down_write_killable(&adev->reset_domain->sem);
-	if (ret)
+	if (ret) {
+		kfree(tmp);
 		goto error_free;
+	}
 
 	swap(adev->reset_dump_reg_list, tmp);
 	swap(adev->reset_dump_reg_value, new);
 	adev->num_regs = i;
 	up_write(&adev->reset_domain->sem);
+	kfree(tmp);
 	ret = size;
 
 error_free:
-	kfree(tmp);
 	kfree(new);
 	return ret;
 }
#3 lore raw

Às 12:06 de 14/07/22, Sebin Sebastian escreveu:

Quoted 13 lines · André Almeida
Quoted 12 lines

Hi Sebin,

Às 10:29 de 10/07/22, Sebin Sebastian escreveu:

Quoted 8 lines

Fix two coverity warning's double free and and an uninitialized pointer read. Both tmp and new are pointing at same address and both are freed which leads to double free. Freeing tmp in the condition after new is assigned with new address fixes the double free issue. new is not initialized to null which also leads to a free on an uninitialized pointer.
Coverity issue: 1518665 (uninitialized pointer read)

		1518679 (double free)
Quoted 5 lines
Quoted 2 lines

What are those numbers?

These numbers are the issue ID's for the errors that are being reported by the coverity static analyzer tool.

I see, but I don't know which tool was used, so those seem like random number to me. I would just remove this part of your commit message, but if you want to keep it, you need to at least mention what's the tool.

Quoted 25 lines
Quoted 18 lines
Quoted 18 lines
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_debugfs.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_debugfs.c +1 -1
index f3b3c688e4e7..d82fe0e1b06b 100644
@@ -1660,7 +1660,7 @@ static ssize_t amdgpu_reset_dump_register_list_write(struct file *f,
 {
 	struct amdgpu_device *adev = (struct amdgpu_device *)file_inode(f)->i_private;
 	char reg_offset[11];
-	uint32_t *new, *tmp = NULL;
+	uint32_t *new = NULL, *tmp = NULL;
 	int ret, i = 0, len = 0;
 
Quoted 7 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_debugfs.c static ssize_t amdgpu_reset_dump_register_list_write(struct file *f, · line 1693

Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_debugfs.c +0 -0
 	do {
@@ -1692,17 +1692,19 @@ static ssize_t amdgpu_reset_dump_register_list_write(struct file *f,
 		goto error_free;
 	}

If the if (!new) { above this line is true, will be tmp freed?

Quoted 3 lines

Yes, It doesn't seem to free tmp here. Should I free tmp immediately after the do while loop and remove kfree(tmp) from the if (ret) block? Thanks for pointing out the errors.

If you free immediately after the while loop, then you would risk a use after free here:

	swap(adev->reset_dump_reg_list, tmp);

So this isn't the solution either.

Quoted 20 lines
Quoted 20 lines
Quoted 20 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_debugfs.c +4 -2
 	ret = down_write_killable(&adev->reset_domain->sem);
-	if (ret)
+	if (ret) {
+		kfree(tmp);
 		goto error_free;
+	}
 
 	swap(adev->reset_dump_reg_list, tmp);
 	swap(adev->reset_dump_reg_value, new);
 	adev->num_regs = i;
 	up_write(&adev->reset_domain->sem);
+	kfree(tmp);
 	ret = size;
 
 error_free:
-	kfree(tmp);
 	kfree(new);
 	return ret;
 }
#4 lore raw
Quoted 13 lines · André Almeida

Às 12:06 de 14/07/22, Sebin Sebastian escreveu:

Quoted 12 lines · André Almeida
Quoted 11 lines

Hi Sebin,

Às 10:29 de 10/07/22, Sebin Sebastian escreveu:

Quoted 8 lines

Fix two coverity warning's double free and and an uninitialized pointer read. Both tmp and new are pointing at same address and both are freed which leads to double free. Freeing tmp in the condition after new is assigned with new address fixes the double free issue. new is not initialized to null which also leads to a free on an uninitialized pointer.
Coverity issue: 1518665 (uninitialized pointer read)

		1518679 (double free)
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

What are those numbers?

These numbers are the issue ID's for the errors that are being reported by the coverity static analyzer tool.

I see, but I don't know which tool was used, so those seem like random number to me. I would just remove this part of your commit message, but if you want to keep it, you need to at least mention what's the tool.

new variable is not needed to initialize.

The only condition double free happens is:

tmp = new;
                if (sscanf(reg_offset, "%X %n", &tmp[i], &ret) != 1) { ret = -EINVAL;
                        goto error_free; *//    if it hits this* }/
/

and can be avoided like:

 error_free:
-       kfree(tmp);
+       if (tmp != new)
+               kfree(tmp);
        kfree(new);
        return ret;
 }

Regards,

S.Amarnath

Quoted 52 lines
Quoted 23 lines
Quoted 17 lines
Quoted 17 lines
---
  drivers/gpu/drm/amd/amdgpu/amdgpu_debugfs.c | 8 +++++---
  1 file changed, 5 insertions(+), 3 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_debugfs.c +1 -1
index f3b3c688e4e7..d82fe0e1b06b 100644
@@ -1660,7 +1660,7 @@ static ssize_t amdgpu_reset_dump_register_list_write(struct file *f,
  {
  	struct amdgpu_device *adev = (struct amdgpu_device *)file_inode(f)->i_private;
  	char reg_offset[11];
-	uint32_t *new, *tmp = NULL;
+	uint32_t *new = NULL, *tmp = NULL;
  	int ret, i = 0, len = 0;
 
Quoted 6 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_debugfs.c static ssize_t amdgpu_reset_dump_register_list_write(struct file *f, · line 1693

Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_debugfs.c +0 -0
  	do {
@@ -1692,17 +1692,19 @@ static ssize_t amdgpu_reset_dump_register_list_write(struct file *f,
  		goto error_free;
  	}

If the if (!new) { above this line is true, will be tmp freed?

Quoted 3 lines

Yes, It doesn't seem to free tmp here. Should I free tmp immediately after the do while loop and remove kfree(tmp) from the if (ret) block? Thanks for pointing out the errors.

If you free immediately after the while loop, then you would risk a use after free here:

	swap(adev->reset_dump_reg_list, tmp);

So this isn't the solution either.

Quoted 19 lines
Quoted 19 lines
Quoted 19 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_debugfs.c +4 -2
  	ret = down_write_killable(&adev->reset_domain->sem);
-	if (ret)
+	if (ret) {
+		kfree(tmp);
  		goto error_free;
+	}
 
  	swap(adev->reset_dump_reg_list, tmp);
  	swap(adev->reset_dump_reg_value, new);
  	adev->num_regs = i;
  	up_write(&adev->reset_domain->sem);
+	kfree(tmp);
  	ret = size;
 
  error_free:
-	kfree(tmp);
  	kfree(new);
  	return ret;
  }
#5 lore raw
Quoted 17 lines · Amaranath
Quoted 15 lines · André Almeida

Às 12:06 de 14/07/22, Sebin Sebastian escreveu:

Quoted 14 lines · André Almeida
Quoted 13 lines

Hi Sebin,

Às 10:29 de 10/07/22, Sebin Sebastian escreveu:

Quoted 8 lines

Fix two coverity warning's double free and and an uninitialized pointer read. Both tmp and new are pointing at same address and both are freed which leads to double free. Freeing tmp in the condition after new is assigned with new address fixes the double free issue. new is not initialized to null which also leads to a free on an uninitialized pointer.
Coverity issue: 1518665 (uninitialized pointer read)

		1518679 (double free)

What are those numbers?

Quoted 9 lines
Quoted 6 lines
Quoted 3 lines

These numbers are the issue ID's for the errors that are being reported by the coverity static analyzer tool.

I see, but I don't know which tool was used, so those seem like random number to me. I would just remove this part of your commit message, but if you want to keep it, you need to at least mention what's the tool.

new variable is not needed to initialize.

But if new is not initialized to null, won't it trigger a free on an uninitialized pointer in the first if block inside the do while loop?

Quoted 20 lines

The only condition double free happens is:

tmp = new;
                if (sscanf(reg_offset, "%X %n", &tmp[i], &ret) != 1) { ret = -EINVAL;
                        goto error_free; *//    if it hits this* }/
/

and can be avoided like:

 error_free:
-       kfree(tmp);
+       if (tmp != new)
+               kfree(tmp);
        kfree(new);
        return ret;
 }

Quoted 4 lines

Regards,

S.Amarnath

This seem's like the best way to avoid the double free. Thanks for the suggestions.

Quoted 49 lines
Quoted 49 lines
Quoted 22 lines
Quoted 5 lines
Quoted 5 lines
---
  drivers/gpu/drm/amd/amdgpu/amdgpu_debugfs.c | 8 +++++---
  1 file changed, 5 insertions(+), 3 deletions(-)
Quoted 17 lines
Quoted 13 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_debugfs.c +1 -1
index f3b3c688e4e7..d82fe0e1b06b 100644
@@ -1660,7 +1660,7 @@ static ssize_t amdgpu_reset_dump_register_list_write(struct file *f,
  {
  	struct amdgpu_device *adev = (struct amdgpu_device *)file_inode(f)->i_private;
  	char reg_offset[11];
-	uint32_t *new, *tmp = NULL;
+	uint32_t *new = NULL, *tmp = NULL;
  	int ret, i = 0, len = 0;
  	do {

drivers/gpu/drm/amd/amdgpu/amdgpu_debugfs.c static ssize_t amdgpu_reset_dump_register_list_write(struct file *f, · line 1693

Quoted 2 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_debugfs.c +0 -0
@@ -1692,17 +1692,19 @@ static ssize_t amdgpu_reset_dump_register_list_write(struct file *f,
  		goto error_free;
  	}

If the if (!new) { above this line is true, will be tmp freed?

Quoted 3 lines

Yes, It doesn't seem to free tmp here. Should I free tmp immediately after the do while loop and remove kfree(tmp) from the if (ret) block? Thanks for pointing out the errors.

If you free immediately after the while loop, then you would risk a use after free here:

	swap(adev->reset_dump_reg_list, tmp);

So this isn't the solution either.

Quoted 17 lines
Quoted 17 lines
Quoted 17 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_debugfs.c +4 -2
  	ret = down_write_killable(&adev->reset_domain->sem);
-	if (ret)
+	if (ret) {
+		kfree(tmp);
  		goto error_free;
+	}
  	swap(adev->reset_dump_reg_list, tmp);
  	swap(adev->reset_dump_reg_value, new);
  	adev->num_regs = i;
  	up_write(&adev->reset_domain->sem);
+	kfree(tmp);
  	ret = size;
  error_free:
-	kfree(tmp);
  	kfree(new);
  	return ret;
  }