Skip to contentSkip to conversation

powerpc: Free fdt on error in elf64_load()

PATCH Superseded Lakshmi Ramasubramanian 3 participants 8 messages Apr 20, 2021 – Apr 21, 2021 PowerPC

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 powerpc: Free fdt on error in elf64_load()

There are a few "goto out;" statements before the local variable "fdt" is initialized through the call to of_kexec_alloc_and_setup_fdt() in elf64_load(). This will result in an uninitialized "fdt" being passed to kvfree() in this function if there is an error before the call to of_kexec_alloc_and_setup_fdt().

If there is any error after fdt is allocated, but before it is saved in the arch specific kimage struct, free the fdt.

---
 arch/powerpc/kexec/elf_64.c | 16 ++++++----------
 1 file changed, 6 insertions(+), 10 deletions(-)
arch/powerpc/kexec/elf_64.c +6 -10
index 5a569bb51349..02662e72c53d 100644
@@ -114,7 +114,7 @@ static void *elf64_load(struct kimage *image, char *kernel_buf,
 	ret = setup_new_fdt_ppc64(image, fdt, initrd_load_addr,
 				  initrd_len, cmdline);
 	if (ret)
-		goto out;
+		goto out_free_fdt;
 
 	fdt_pack(fdt);
 
@@ -125,7 +125,7 @@ static void *elf64_load(struct kimage *image, char *kernel_buf,
 	kbuf.mem = KEXEC_BUF_MEM_UNKNOWN;
 	ret = kexec_add_buffer(&kbuf);
 	if (ret)
-		goto out;
+		goto out_free_fdt;
 
 	/* FDT will be freed in arch_kimage_file_post_load_cleanup */
 	image->arch.fdt = fdt;
@@ -140,18 +140,14 @@ static void *elf64_load(struct kimage *image, char *kernel_buf,
 	if (ret)
 		pr_err("Error setting up the purgatory.\n");
 
+	goto out;
+
+out_free_fdt:
+	kvfree(fdt);
 out:
 	kfree(modified_cmdline);
 	kexec_free_elf_info(&elf_info);
 
-	/*
-	 * Once FDT buffer has been successfully passed to kexec_add_buffer(),
-	 * the FDT buffer address is saved in image->arch.fdt. In that case,
-	 * the memory cannot be freed here in case of any other error.
-	 */
-	if (ret && !image->arch.fdt)
-		kvfree(fdt);
-
 	return ret ? ERR_PTR(ret) : NULL;
 }
 
Signature · 1 line
2.31.0
#1 lore raw

PATCH 2/2 powerpc: If kexec_build_elf_info() fails return immediately from elf64_load()

Uninitialized local variable "elf_info" would be passed to kexec_free_elf_info() if kexec_build_elf_info() returns an error in elf64_load().

If kexec_build_elf_info() returns an error, return the error immediately.

---
 arch/powerpc/kexec/elf_64.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/powerpc/kexec/elf_64.c +1 -1
index 02662e72c53d..eeb258002d1e 100644
@@ -45,7 +45,7 @@ static void *elf64_load(struct kimage *image, char *kernel_buf,
 
 	ret = kexec_build_elf_info(kernel_buf, kernel_len, &ehdr, &elf_info);
 	if (ret)
-		goto out;
+		return ERR_PTR(ret);
 
 	if (image->type == KEXEC_TYPE_CRASH) {
 		/* min & max buffer values for kdump case */
Signature · 1 line
2.31.0
#3 lore raw
In reply to Lakshmi Ramasubramanian

Hi Lakshmi,

Quoted 22 lines · Lakshmi Ramasubramanian

There are a few "goto out;" statements before the local variable "fdt" is initialized through the call to of_kexec_alloc_and_setup_fdt() in elf64_load(). This will result in an uninitialized "fdt" being passed to kvfree() in this function if there is an error before the call to of_kexec_alloc_and_setup_fdt().

If there is any error after fdt is allocated, but before it is saved in the arch specific kimage struct, free the fdt.

---
 arch/powerpc/kexec/elf_64.c | 16 ++++++----------
 1 file changed, 6 insertions(+), 10 deletions(-)
arch/powerpc/kexec/elf_64.c +0 -0
index 5a569bb51349..02662e72c53d 100644

arch/powerpc/kexec/elf_64.c static void *elf64_load(struct kimage *image, char *kernel_buf, · line 117

Quoted 5 lines
arch/powerpc/kexec/elf_64.c +1 -1
@@ -114,7 +114,7 @@ static void *elf64_load(struct kimage *image, char *kernel_buf,
 	ret = setup_new_fdt_ppc64(image, fdt, initrd_load_addr,
 				  initrd_len, cmdline);
 	if (ret)
-		goto out;
+		goto out_free_fdt;

Shouldn't there be a goto out_free_fdt if fdt_open_into fails?

Quoted 13 lines
arch/powerpc/kexec/elf_64.c +1 -1
 	fdt_pack(fdt);
 
@@ -125,7 +125,7 @@ static void *elf64_load(struct kimage *image, char *kernel_buf,
 	kbuf.mem = KEXEC_BUF_MEM_UNKNOWN;
 	ret = kexec_add_buffer(&kbuf);
 	if (ret)
-		goto out;
+		goto out_free_fdt;
 
 	/* FDT will be freed in arch_kimage_file_post_load_cleanup */
 	image->arch.fdt = fdt;

arch/powerpc/kexec/elf_64.c static void *elf64_load(struct kimage *image, char *kernel_buf, · line 146

Quoted 7 lines
arch/powerpc/kexec/elf_64.c +4 -0
@@ -140,18 +140,14 @@ static void *elf64_load(struct kimage *image, char *kernel_buf,
 	if (ret)
 		pr_err("Error setting up the purgatory.\n");
 
+	goto out;
+
+out_free_fdt:
+	kvfree(fdt);

Can just use kfree here?

Thanks,
Santosh

Quoted 17 lines
arch/powerpc/kexec/elf_64.c +0 -8
 out:
 	kfree(modified_cmdline);
 	kexec_free_elf_info(&elf_info);
 
-	/*
-	 * Once FDT buffer has been successfully passed to kexec_add_buffer(),
-	 * the FDT buffer address is saved in image->arch.fdt. In that case,
-	 * the memory cannot be freed here in case of any other error.
-	 */
-	if (ret && !image->arch.fdt)
-		kvfree(fdt);
-
 	return ret ? ERR_PTR(ret) : NULL;
 }
 
Signature · 1 line
2.31.0
#6 lore raw
In reply to Lakshmi Ramasubramanian
Quoted 6 lines · Lakshmi Ramasubramanian

There are a few "goto out;" statements before the local variable "fdt" is initialized through the call to of_kexec_alloc_and_setup_fdt() in elf64_load(). This will result in an uninitialized "fdt" being passed to kvfree() in this function if there is an error before the call to of_kexec_alloc_and_setup_fdt().

Quoted 7 lines

If there is any error after fdt is allocated, but before it is saved in the arch specific kimage struct, free the fdt.

I basically sent you the diff, so this should probably be:

Otherwise looks good to me, thanks for turning it into a proper patch and submitting it.

cheers

Quoted 47 lines
arch/powerpc/kexec/elf_64.c +6 -10
index 5a569bb51349..02662e72c53d 100644
@@ -114,7 +114,7 @@ static void *elf64_load(struct kimage *image, char *kernel_buf,
 	ret = setup_new_fdt_ppc64(image, fdt, initrd_load_addr,
 				  initrd_len, cmdline);
 	if (ret)
-		goto out;
+		goto out_free_fdt;
 
 	fdt_pack(fdt);
 
@@ -125,7 +125,7 @@ static void *elf64_load(struct kimage *image, char *kernel_buf,
 	kbuf.mem = KEXEC_BUF_MEM_UNKNOWN;
 	ret = kexec_add_buffer(&kbuf);
 	if (ret)
-		goto out;
+		goto out_free_fdt;
 
 	/* FDT will be freed in arch_kimage_file_post_load_cleanup */
 	image->arch.fdt = fdt;
@@ -140,18 +140,14 @@ static void *elf64_load(struct kimage *image, char *kernel_buf,
 	if (ret)
 		pr_err("Error setting up the purgatory.\n");
 
+	goto out;
+
+out_free_fdt:
+	kvfree(fdt);
 out:
 	kfree(modified_cmdline);
 	kexec_free_elf_info(&elf_info);
 
-	/*
-	 * Once FDT buffer has been successfully passed to kexec_add_buffer(),
-	 * the FDT buffer address is saved in image->arch.fdt. In that case,
-	 * the memory cannot be freed here in case of any other error.
-	 */
-	if (ret && !image->arch.fdt)
-		kvfree(fdt);
-
 	return ret ? ERR_PTR(ret) : NULL;
 }
 
Signature · 1 line
2.31.0
reviewer Reviewed-by on v1 2/2
#2 lore raw

Re: [PATCH 2/2] powerpc: If kexec_build_elf_info() fails return immediately from elf64_load()

In reply to Lakshmi Ramasubramanian
Quoted 9 lines · Lakshmi Ramasubramanian

Uninitialized local variable "elf_info" would be passed to kexec_free_elf_info() if kexec_build_elf_info() returns an error in elf64_load().

If kexec_build_elf_info() returns an error, return the error immediately.

cheers

Quoted 15 lines
arch/powerpc/kexec/elf_64.c +1 -1
index 02662e72c53d..eeb258002d1e 100644
@@ -45,7 +45,7 @@ static void *elf64_load(struct kimage *image, char *kernel_buf,
 
 	ret = kexec_build_elf_info(kernel_buf, kernel_len, &ehdr, &elf_info);
 	if (ret)
-		goto out;
+		return ERR_PTR(ret);
 
 	if (image->type == KEXEC_TYPE_CRASH) {
 		/* min & max buffer values for kdump case */
Signature · 1 line
2.31.0
#4 lore raw
In reply to Santosh Sivaraj

On 4/20/21 10:35 PM, Santosh Sivaraj wrote:
Hi Santosh,

Quoted 18 lines
Quoted 18 lines

There are a few "goto out;" statements before the local variable "fdt" is initialized through the call to of_kexec_alloc_and_setup_fdt() in elf64_load(). This will result in an uninitialized "fdt" being passed to kvfree() in this function if there is an error before the call to of_kexec_alloc_and_setup_fdt().

If there is any error after fdt is allocated, but before it is saved in the arch specific kimage struct, free the fdt.

---
  arch/powerpc/kexec/elf_64.c | 16 ++++++----------
  1 file changed, 6 insertions(+), 10 deletions(-)
Quoted 12 lines

arch/powerpc/kexec/elf_64.c static void *elf64_load(struct kimage *image, char *kernel_buf, · line 117

Quoted 10 lines
arch/powerpc/kexec/elf_64.c +1 -1
index 5a569bb51349..02662e72c53d 100644
@@ -114,7 +114,7 @@ static void *elf64_load(struct kimage *image, char *kernel_buf,
  	ret = setup_new_fdt_ppc64(image, fdt, initrd_load_addr,
  				  initrd_len, cmdline);
  	if (ret)
-		goto out;
+		goto out_free_fdt;

Shouldn't there be a goto out_free_fdt if fdt_open_into fails?

You are likely looking at elf_64.c in the mainline branch. The patch I have submitted is based on Rob's device-tree for-next branch. Please see the link below:

https://git.kernel.org/pub/scm/linux/kernel/git/robh/linux.git/tree/arch/powerpc/kexec/elf_64.c?h=for-next

Quoted 17 lines
Quoted 17 lines
arch/powerpc/kexec/elf_64.c +1 -1
  	fdt_pack(fdt);
 
@@ -125,7 +125,7 @@ static void *elf64_load(struct kimage *image, char *kernel_buf,
  	kbuf.mem = KEXEC_BUF_MEM_UNKNOWN;
  	ret = kexec_add_buffer(&kbuf);
  	if (ret)
-		goto out;
+		goto out_free_fdt;
 
  	/* FDT will be freed in arch_kimage_file_post_load_cleanup */
  	image->arch.fdt = fdt;
@@ -140,18 +140,14 @@ static void *elf64_load(struct kimage *image, char *kernel_buf,
  	if (ret)
  		pr_err("Error setting up the purgatory.\n");
 
Quoted 6 lines

arch/powerpc/kexec/elf_64.c static void *elf64_load(struct kimage *image, char *kernel_buf, · line 146

Quoted 4 lines
arch/powerpc/kexec/elf_64.c +4 -0
+	goto out;
+
+out_free_fdt:
+	kvfree(fdt);

Can just use kfree here?

"fdt" is allocated through kvmalloc(). So it is freed using kvfree.

thanks,
-lakshmi

Quoted 17 lines
Quoted 17 lines
arch/powerpc/kexec/elf_64.c +0 -8
  out:
  	kfree(modified_cmdline);
  	kexec_free_elf_info(&elf_info);
 
-	/*
-	 * Once FDT buffer has been successfully passed to kexec_add_buffer(),
-	 * the FDT buffer address is saved in image->arch.fdt. In that case,
-	 * the memory cannot be freed here in case of any other error.
-	 */
-	if (ret && !image->arch.fdt)
-		kvfree(fdt);
-
  	return ret ? ERR_PTR(ret) : NULL;
  }
 
Signature · 1 line
2.31.0
#7 lore raw
In reply to Michael Ellerman
Quoted 17 lines · Michael Ellerman
Quoted 6 lines · Lakshmi Ramasubramanian

There are a few "goto out;" statements before the local variable "fdt" is initialized through the call to of_kexec_alloc_and_setup_fdt() in elf64_load(). This will result in an uninitialized "fdt" being passed to kvfree() in this function if there is an error before the call to of_kexec_alloc_and_setup_fdt().

Quoted 7 lines

If there is any error after fdt is allocated, but before it is saved in the arch specific kimage struct, free the fdt.

I basically sent you the diff, so this should probably be:

Quoted 7 lines

Otherwise looks good to me, thanks for turning it into a proper patch and submitting it.

I will submit the patch again with the above change. Thanks for reviewing the patch.

Could you please review [PATCH 2/2] as well?

thanks,
-lakshmi

Quoted 51 lines

cheers

Quoted 47 lines
arch/powerpc/kexec/elf_64.c +6 -10
index 5a569bb51349..02662e72c53d 100644
@@ -114,7 +114,7 @@ static void *elf64_load(struct kimage *image, char *kernel_buf,
  	ret = setup_new_fdt_ppc64(image, fdt, initrd_load_addr,
  				  initrd_len, cmdline);
  	if (ret)
-		goto out;
+		goto out_free_fdt;
 
  	fdt_pack(fdt);
 
@@ -125,7 +125,7 @@ static void *elf64_load(struct kimage *image, char *kernel_buf,
  	kbuf.mem = KEXEC_BUF_MEM_UNKNOWN;
  	ret = kexec_add_buffer(&kbuf);
  	if (ret)
-		goto out;
+		goto out_free_fdt;
 
  	/* FDT will be freed in arch_kimage_file_post_load_cleanup */
  	image->arch.fdt = fdt;
@@ -140,18 +140,14 @@ static void *elf64_load(struct kimage *image, char *kernel_buf,
  	if (ret)
  		pr_err("Error setting up the purgatory.\n");
 
+	goto out;
+
+out_free_fdt:
+	kvfree(fdt);
  out:
  	kfree(modified_cmdline);
  	kexec_free_elf_info(&elf_info);
 
-	/*
-	 * Once FDT buffer has been successfully passed to kexec_add_buffer(),
-	 * the FDT buffer address is saved in image->arch.fdt. In that case,
-	 * the memory cannot be freed here in case of any other error.
-	 */
-	if (ret && !image->arch.fdt)
-		kvfree(fdt);
-
  	return ret ? ERR_PTR(ret) : NULL;
  }
 
Signature · 1 line
2.31.0
#5 lore raw
In reply to Lakshmi Ramasubramanian
Quoted 34 lines · Lakshmi Ramasubramanian

On 4/20/21 10:35 PM, Santosh Sivaraj wrote:
Hi Santosh,

Quoted 18 lines
Quoted 18 lines

There are a few "goto out;" statements before the local variable "fdt" is initialized through the call to of_kexec_alloc_and_setup_fdt() in elf64_load(). This will result in an uninitialized "fdt" being passed to kvfree() in this function if there is an error before the call to of_kexec_alloc_and_setup_fdt().

If there is any error after fdt is allocated, but before it is saved in the arch specific kimage struct, free the fdt.

---
  arch/powerpc/kexec/elf_64.c | 16 ++++++----------
  1 file changed, 6 insertions(+), 10 deletions(-)
Quoted 12 lines

arch/powerpc/kexec/elf_64.c static void *elf64_load(struct kimage *image, char *kernel_buf, · line 117

Quoted 10 lines
arch/powerpc/kexec/elf_64.c +1 -1
index 5a569bb51349..02662e72c53d 100644
@@ -114,7 +114,7 @@ static void *elf64_load(struct kimage *image, char *kernel_buf,
  	ret = setup_new_fdt_ppc64(image, fdt, initrd_load_addr,
  				  initrd_len, cmdline);
  	if (ret)
-		goto out;
+		goto out_free_fdt;

Shouldn't there be a goto out_free_fdt if fdt_open_into fails?

Quoted 5 lines

You are likely looking at elf_64.c in the mainline branch. The patch I have submitted is based on Rob's device-tree for-next branch. Please see the link below:

https://git.kernel.org/pub/scm/linux/kernel/git/robh/linux.git/tree/arch/powerpc/kexec/elf_64.c?h=for-next

That's right, I was indeed looking at the mainline. Sorry for the noise.

Thanks,
Santosh

Quoted 46 lines
Quoted 18 lines
Quoted 18 lines
arch/powerpc/kexec/elf_64.c +1 -1
  	fdt_pack(fdt);
 
@@ -125,7 +125,7 @@ static void *elf64_load(struct kimage *image, char *kernel_buf,
  	kbuf.mem = KEXEC_BUF_MEM_UNKNOWN;
  	ret = kexec_add_buffer(&kbuf);
  	if (ret)
-		goto out;
+		goto out_free_fdt;
 
  	/* FDT will be freed in arch_kimage_file_post_load_cleanup */
  	image->arch.fdt = fdt;
@@ -140,18 +140,14 @@ static void *elf64_load(struct kimage *image, char *kernel_buf,
  	if (ret)
  		pr_err("Error setting up the purgatory.\n");
 
Quoted 6 lines

arch/powerpc/kexec/elf_64.c static void *elf64_load(struct kimage *image, char *kernel_buf, · line 146

Quoted 4 lines
arch/powerpc/kexec/elf_64.c +4 -0
+	goto out;
+
+out_free_fdt:
+	kvfree(fdt);

Can just use kfree here?

"fdt" is allocated through kvmalloc(). So it is freed using kvfree.

thanks,
-lakshmi

Quoted 17 lines
Quoted 17 lines
arch/powerpc/kexec/elf_64.c +0 -8
  out:
  	kfree(modified_cmdline);
  	kexec_free_elf_info(&elf_info);
 
-	/*
-	 * Once FDT buffer has been successfully passed to kexec_add_buffer(),
-	 * the FDT buffer address is saved in image->arch.fdt. In that case,
-	 * the memory cannot be freed here in case of any other error.
-	 */
-	if (ret && !image->arch.fdt)
-		kvfree(fdt);
-
  	return ret ? ERR_PTR(ret) : NULL;
  }
 
Signature · 1 line
2.31.0