Skip to contentSkip to conversation

riscv: Fix a NULL pointer reference in machine_kexec_prepare

PATCH Superseded Tao Liu 5 participants 11 messages May 29 – Jul 1

Subsystems (5)

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Tao Liu

author
#0 lore raw

PATCH 1/1 riscv: Fix a NULL pointer reference in machine_kexec_prepare

A NULL pointer reference issue is noticed in riscv's machine_kexec_prepare, where image->segment[i].buf might be NULL and copied unchecked.

The NULL buf comes from security/integrity/ima/ima_kexec.c:
ima_add_kexec_buffer(), where kbuf is added by kexec_add_buffer(), but kbuf.buffer is NULL.

Fix this by simply adding a check before copy.

---
 arch/riscv/kernel/machine_kexec.c | 3 +++
 1 file changed, 3 insertions(+)
arch/riscv/kernel/machine_kexec.c +3 -0
index 2306ce3e5f22..d81d576f9cb5 100644
@@ -41,6 +41,9 @@ machine_kexec_prepare(struct kimage *image)
 		if (image->segment[i].memsz <= sizeof(fdt))
 			continue;
 
+		if (image->segment[i].buf == NULL)
+			continue;
+
 		if (image->file_mode)
 			memcpy(&fdt, image->segment[i].buf, sizeof(fdt));
 		else if (copy_from_user(&fdt, image->segment[i].buf, sizeof(fdt)))
Signature · 1 line
2.47.0
author
#1 lore raw

Kindly ping, any comments?

Thanks,
Tao Liu

Quoted 32 lines · Tao Liu

A NULL pointer reference issue is noticed in riscv's machine_kexec_prepare, where image->segment[i].buf might be NULL and copied unchecked.

The NULL buf comes from security/integrity/ima/ima_kexec.c:
ima_add_kexec_buffer(), where kbuf is added by kexec_add_buffer(), but kbuf.buffer is NULL.

Fix this by simply adding a check before copy.

---
 arch/riscv/kernel/machine_kexec.c | 3 +++
 1 file changed, 3 insertions(+)
arch/riscv/kernel/machine_kexec.c +3 -1
index 2306ce3e5f22..d81d576f9cb5 100644
@@ -41,6 +41,9 @@ machine_kexec_prepare(struct kimage *image)
                if (image->segment[i].memsz <= sizeof(fdt))
                        continue;
 
+               if (image->segment[i].buf == NULL)
+                       continue;
+
                if (image->file_mode)
                        memcpy(&fdt, image->segment[i].buf, sizeof(fdt));
                else if (copy_from_user(&fdt, image->segment[i].buf, sizeof(fdt)))
--

2.47.0

Acked-by
#2 lore raw

Add kexec ML to CC.

Quoted 30 lines · Tao Liu

Kindly ping, any comments?

Thanks,
Tao Liu

Quoted 23 lines · Tao Liu

A NULL pointer reference issue is noticed in riscv's machine_kexec_prepare, where image->segment[i].buf might be NULL and copied unchecked.

The NULL buf comes from security/integrity/ima/ima_kexec.c:
ima_add_kexec_buffer(), where kbuf is added by kexec_add_buffer(), but kbuf.buffer is NULL.

Fix this by simply adding a check before copy.

---
 arch/riscv/kernel/machine_kexec.c | 3 +++
 1 file changed, 3 insertions(+)
arch/riscv/kernel/machine_kexec.c +0 -0
index 2306ce3e5f22..d81d576f9cb5 100644
@@ -41,6 +41,9 @@ machine_kexec_prepare(struct kimage *image)
                if (image->segment[i].memsz <= sizeof(fdt))
                        continue;
 
Quoted 3 lines

arch/riscv/kernel/machine_kexec.c machine_kexec_prepare(struct kimage *image) · line 46

Quoted 3 lines
arch/riscv/kernel/machine_kexec.c +3 -0
+               if (image->segment[i].buf == NULL)
+                       continue;
+

This is a good fix, maybe we can add code comments to explain it as below, just for reference.

/*
* Some segments (e.g. IMA) reserve space but have no buffer
* loaded yet. Skip them as they cannot contain an FDT. */
And is there any other place where the similar issue exists? e.g on LoongArch?

Other than above concerns, this patch looks good to me:

Quoted 13 lines
Quoted 6 lines
arch/riscv/kernel/machine_kexec.c +0 -1
                if (image->file_mode)
                        memcpy(&fdt, image->segment[i].buf, sizeof(fdt));
                else if (copy_from_user(&fdt, image->segment[i].buf, sizeof(fdt)))
--

2.47.0

_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv

author
#3 lore raw

Hi Baoquan,

Glad to see your message again!

Thanks for the Ack and the suggestion for the comment, I will add it in the next version.

In the meantime, I will check if similar issue exists on LoongArch as well.

Thanks,
Tao Liu

Quoted 65 lines · Baoquan He

Add kexec ML to CC.

Quoted 30 lines · Tao Liu

Kindly ping, any comments?

Thanks,
Tao Liu

Quoted 23 lines · Tao Liu

A NULL pointer reference issue is noticed in riscv's machine_kexec_prepare, where image->segment[i].buf might be NULL and copied unchecked.

The NULL buf comes from security/integrity/ima/ima_kexec.c:
ima_add_kexec_buffer(), where kbuf is added by kexec_add_buffer(), but kbuf.buffer is NULL.

Fix this by simply adding a check before copy.

---
 arch/riscv/kernel/machine_kexec.c | 3 +++
 1 file changed, 3 insertions(+)
arch/riscv/kernel/machine_kexec.c +0 -0
index 2306ce3e5f22..d81d576f9cb5 100644
@@ -41,6 +41,9 @@ machine_kexec_prepare(struct kimage *image)
                if (image->segment[i].memsz <= sizeof(fdt))
                        continue;
 
Quoted 3 lines

arch/riscv/kernel/machine_kexec.c machine_kexec_prepare(struct kimage *image) · line 46

Quoted 3 lines
arch/riscv/kernel/machine_kexec.c +3 -0
+               if (image->segment[i].buf == NULL)
+                       continue;
+

This is a good fix, maybe we can add code comments to explain it as below, just for reference.

/*
* Some segments (e.g. IMA) reserve space but have no buffer
* loaded yet. Skip them as they cannot contain an FDT. */
And is there any other place where the similar issue exists? e.g on LoongArch?

Other than above concerns, this patch looks good to me:

Quoted 13 lines
Quoted 6 lines
arch/riscv/kernel/machine_kexec.c +0 -1
                if (image->file_mode)
                        memcpy(&fdt, image->segment[i].buf, sizeof(fdt));
                else if (copy_from_user(&fdt, image->segment[i].buf, sizeof(fdt)))
--

2.47.0

_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv

author
#4 lore raw

Hi Baoquan,

Quoted 8 lines · Tao Liu

Hi Baoquan,

Glad to see your message again!

Thanks for the Ack and the suggestion for the comment, I will add it in the next version.

Quoted 52 lines

In the meantime, I will check if similar issue exists on LoongArch as well.

Thanks,
Tao Liu

Quoted 46 lines · Baoquan He

Add kexec ML to CC.

Quoted 30 lines · Tao Liu

Kindly ping, any comments?

Thanks,
Tao Liu

Quoted 23 lines · Tao Liu

A NULL pointer reference issue is noticed in riscv's machine_kexec_prepare, where image->segment[i].buf might be NULL and copied unchecked.

The NULL buf comes from security/integrity/ima/ima_kexec.c:
ima_add_kexec_buffer(), where kbuf is added by kexec_add_buffer(), but kbuf.buffer is NULL.

Fix this by simply adding a check before copy.

---
 arch/riscv/kernel/machine_kexec.c | 3 +++
 1 file changed, 3 insertions(+)
arch/riscv/kernel/machine_kexec.c +0 -0
index 2306ce3e5f22..d81d576f9cb5 100644
@@ -41,6 +41,9 @@ machine_kexec_prepare(struct kimage *image)
                if (image->segment[i].memsz <= sizeof(fdt))
                        continue;
 
Quoted 3 lines

arch/riscv/kernel/machine_kexec.c machine_kexec_prepare(struct kimage *image) · line 46

Quoted 3 lines
arch/riscv/kernel/machine_kexec.c +3 -0
+               if (image->segment[i].buf == NULL)
+                       continue;
+

This is a good fix, maybe we can add code comments to explain it as below, just for reference.

/*
* Some segments (e.g. IMA) reserve space but have no buffer
* loaded yet. Skip them as they cannot contain an FDT. */
And is there any other place where the similar issue exists? e.g on LoongArch?

I have tested in LoongArch, it doesn't have the similar issue. The reason is, in arch/loongarch/kernel/machine_kexec.c:machine_kexec_prepare(), the unchecked memcpy() only happens for none kexec file load, however ima_add_kexec_buffer() requires kexec file load. So the condition isn't met.

Quoted 4 lines
Quoted 4 lines

Other than above concerns, this patch looks good to me:

Thanks for your code review and suggestions!

Thanks,
Tao Liu

Quoted 15 lines
Quoted 15 lines
Quoted 14 lines
Quoted 7 lines
arch/riscv/kernel/machine_kexec.c +0 -1
                if (image->file_mode)
                        memcpy(&fdt, image->segment[i].buf, sizeof(fdt));
                else if (copy_from_user(&fdt, image->segment[i].buf, sizeof(fdt)))
--

2.47.0

_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv

#5 lore raw

On 06/28/26 at 10:38am, Tao Liu wrote:
......

Quoted 11 lines
Quoted 5 lines
Quoted 5 lines

/*
* Some segments (e.g. IMA) reserve space but have no buffer
* loaded yet. Skip them as they cannot contain an FDT. */
And is there any other place where the similar issue exists? e.g on LoongArch?

I have tested in LoongArch, it doesn't have the similar issue. The reason is, in arch/loongarch/kernel/machine_kexec.c:machine_kexec_prepare(), the unchecked memcpy() only happens for none kexec file load, however ima_add_kexec_buffer() requires kexec file load. So the condition isn't met.

OK, that's good, thanks for the effort.

Quoted 27 lines
Quoted 5 lines
Quoted 5 lines

Other than above concerns, this patch looks good to me:

Thanks for your code review and suggestions!

Thanks,
Tao Liu

Quoted 15 lines
Quoted 15 lines
Quoted 14 lines
Quoted 7 lines
                if (image->file_mode)
                        memcpy(&fdt, image->segment[i].buf, sizeof(fdt));
                else if (copy_from_user(&fdt, image->segment[i].buf, sizeof(fdt)))

--
2.47.0

_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv

maintainer Acked-by
#6 lore raw
In reply to Baoquan He

+Cc IMA maintainers

Quoted 46 lines · Baoquan He

Add kexec ML to CC.

Quoted 30 lines · Tao Liu

Kindly ping, any comments?

Thanks,
Tao Liu

Quoted 23 lines · Tao Liu

A NULL pointer reference issue is noticed in riscv's machine_kexec_prepare, where image->segment[i].buf might be NULL and copied unchecked.

The NULL buf comes from security/integrity/ima/ima_kexec.c:
ima_add_kexec_buffer(), where kbuf is added by kexec_add_buffer(), but kbuf.buffer is NULL.

Fix this by simply adding a check before copy.

---
 arch/riscv/kernel/machine_kexec.c | 3 +++
 1 file changed, 3 insertions(+)
arch/riscv/kernel/machine_kexec.c +0 -0
index 2306ce3e5f22..d81d576f9cb5 100644
@@ -41,6 +41,9 @@ machine_kexec_prepare(struct kimage *image)
                if (image->segment[i].memsz <= sizeof(fdt))
                        continue;
 
Quoted 3 lines

arch/riscv/kernel/machine_kexec.c machine_kexec_prepare(struct kimage *image) · line 46

Quoted 3 lines
arch/riscv/kernel/machine_kexec.c +3 -0
+               if (image->segment[i].buf == NULL)
+                       continue;
+

This is a good fix, maybe we can add code comments to explain it as below, just for reference.

/*
* Some segments (e.g. IMA) reserve space but have no buffer
* loaded yet. Skip them as they cannot contain an FDT. */
And is there any other place where the similar issue exists? e.g on LoongArch?

Quoted 3 lines

Other than above concerns, this patch looks good to me:

Yeah, the patch LGTM to me too.

Although I think IMA can make this a bit easier to understand. First, in ima_add_kexec_buffer() it should set kbuf.buffer to NULL and kbuf.bufsz to 0 explicitly instead of using kexec_buffer and kexec_buffer_size which are initialized to NULL and 0, but never updated. Using the variables here adds an extra level of indirection.

Also, perhaps we should add a comment in ima_add_kexec_buffer() about how this all works, since where the IMA buffer lives and where it gets updated it fairly complicated and took me some time to piece together.

Signature · 2 lines
Regards,
Pratyush Yadav
#9 lore raw
In reply to Tao Liu
Quoted 1 line

A NULL pointer reference issue is noticed in riscv's machine_kexec_prepare,

…
dereference?

…

Quoted 1 line

Fix this by simply adding a check before copy.

How do you think about to add any tags (like “Fixes” and “Cc”) accordingly?

See also:
* https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/submitting-patches.rst?h=v7.2-rc1#n145
* https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/stable-kernel-rules.rst?h=v7.2-rc1#n34

Regards,
Markus

author
#10 lore raw

Hi Markus,

Quoted 10 lines · Markus Elfring
Quoted 2 lines

A NULL pointer reference issue is noticed in riscv's machine_kexec_prepare,

…
dereference?

…

Quoted 1 line

Fix this by simply adding a check before copy.

How do you think about to add any tags (like “Fixes” and “Cc”) accordingly?

Right, thanks for the suggestion. I will improve the commit message wording in v3.

Thanks,
Tao Liu

Quoted 9 lines

See also:
* https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/submitting-patches.rst?h=v7.2-rc1#n145
* https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/stable-kernel-rules.rst?h=v7.2-rc1#n34

Regards,
Markus

author
#7 lore raw
In reply to Pratyush Yadav

Hi Pratyush,

Quoted 65 lines · Pratyush Yadav

+Cc IMA maintainers

Quoted 46 lines · Baoquan He

Add kexec ML to CC.

Quoted 30 lines · Tao Liu

Kindly ping, any comments?

Thanks,
Tao Liu

Quoted 23 lines · Tao Liu

A NULL pointer reference issue is noticed in riscv's machine_kexec_prepare, where image->segment[i].buf might be NULL and copied unchecked.

The NULL buf comes from security/integrity/ima/ima_kexec.c:
ima_add_kexec_buffer(), where kbuf is added by kexec_add_buffer(), but kbuf.buffer is NULL.

Fix this by simply adding a check before copy.

---
 arch/riscv/kernel/machine_kexec.c | 3 +++
 1 file changed, 3 insertions(+)
arch/riscv/kernel/machine_kexec.c +0 -0
index 2306ce3e5f22..d81d576f9cb5 100644
@@ -41,6 +41,9 @@ machine_kexec_prepare(struct kimage *image)
                if (image->segment[i].memsz <= sizeof(fdt))
                        continue;
 
Quoted 3 lines

arch/riscv/kernel/machine_kexec.c machine_kexec_prepare(struct kimage *image) · line 46

Quoted 3 lines
arch/riscv/kernel/machine_kexec.c +3 -0
+               if (image->segment[i].buf == NULL)
+                       continue;
+

This is a good fix, maybe we can add code comments to explain it as below, just for reference.

/*
* Some segments (e.g. IMA) reserve space but have no buffer
* loaded yet. Skip them as they cannot contain an FDT. */
And is there any other place where the similar issue exists? e.g on LoongArch?

Quoted 3 lines

Other than above concerns, this patch looks good to me:

Yeah, the patch LGTM to me too.

Although I think IMA can make this a bit easier to understand. First, in ima_add_kexec_buffer() it should set kbuf.buffer to NULL and kbuf.bufsz to 0 explicitly instead of using kexec_buffer and kexec_buffer_size which are initialized to NULL and 0, but never updated. Using the variables here adds an extra level of indirection.

Quoted 3 lines

Also, perhaps we should add a comment in ima_add_kexec_buffer() about how this all works, since where the IMA buffer lives and where it gets updated it fairly complicated and took me some time to piece together.

Thanks for your patch review and suggestions! I agree with your point on the IMA part, I was confused by the code too, e.g in ima_add_kexec_buffer():

void *kexec_buffer = NULL;
kbuf.buffer = kexec_buffer;
ret = kexec_add_buffer(&kbuf);

if (ret) {
    pr_err("Error passing over kexec measurement buffer.\n");
    vfree(kexec_buffer);
    return;
}

Do we need to vfree(kexec_buffer)? When kexec_buffer is NULL and seems never get updated.

I'm not familiar with IMA code, maybe there is a reason which I'm unaware of...

Thanks,
Tao Liu

Quoted 5 lines

--
Regards,
Pratyush Yadav

maintainer
#8 lore raw
Quoted 85 lines · Tao Liu

Hi Pratyush,

Quoted 65 lines · Pratyush Yadav

+Cc IMA maintainers

Quoted 46 lines · Baoquan He

Add kexec ML to CC.

Quoted 30 lines · Tao Liu

Kindly ping, any comments?

Thanks,
Tao Liu

Quoted 23 lines · Tao Liu

A NULL pointer reference issue is noticed in riscv's machine_kexec_prepare, where image->segment[i].buf might be NULL and copied unchecked.

The NULL buf comes from security/integrity/ima/ima_kexec.c:
ima_add_kexec_buffer(), where kbuf is added by kexec_add_buffer(), but kbuf.buffer is NULL.

Fix this by simply adding a check before copy.

---
 arch/riscv/kernel/machine_kexec.c | 3 +++
 1 file changed, 3 insertions(+)
arch/riscv/kernel/machine_kexec.c +0 -0
index 2306ce3e5f22..d81d576f9cb5 100644
@@ -41,6 +41,9 @@ machine_kexec_prepare(struct kimage *image)
                if (image->segment[i].memsz <= sizeof(fdt))
                        continue;
 
Quoted 3 lines

arch/riscv/kernel/machine_kexec.c machine_kexec_prepare(struct kimage *image) · line 46

Quoted 3 lines
arch/riscv/kernel/machine_kexec.c +3 -0
+               if (image->segment[i].buf == NULL)
+                       continue;
+

This is a good fix, maybe we can add code comments to explain it as below, just for reference.

/*
* Some segments (e.g. IMA) reserve space but have no buffer
* loaded yet. Skip them as they cannot contain an FDT. */
And is there any other place where the similar issue exists? e.g on LoongArch?

Quoted 3 lines

Other than above concerns, this patch looks good to me:

Yeah, the patch LGTM to me too.

Although I think IMA can make this a bit easier to understand. First, in ima_add_kexec_buffer() it should set kbuf.buffer to NULL and kbuf.bufsz to 0 explicitly instead of using kexec_buffer and kexec_buffer_size which are initialized to NULL and 0, but never updated. Using the variables here adds an extra level of indirection.

Quoted 3 lines

Also, perhaps we should add a comment in ima_add_kexec_buffer() about how this all works, since where the IMA buffer lives and where it gets updated it fairly complicated and took me some time to piece together.

Thanks for your patch review and suggestions! I agree with your point on the IMA part, I was confused by the code too, e.g in ima_add_kexec_buffer():

void *kexec_buffer = NULL;
kbuf.buffer = kexec_buffer;
ret = kexec_add_buffer(&kbuf);

if (ret) {
    pr_err("Error passing over kexec measurement buffer.\n");
    vfree(kexec_buffer);
    return;
}
Quoted 4 lines

Do we need to vfree(kexec_buffer)? When kexec_buffer is NULL and seems never get updated.

I'm not familiar with IMA code, maybe there is a reason which I'm unaware of...

I think it is just leftover from refactors. kexec_buffer used to point to an actual buffer at some point and refactors to the function left them always NULL but never removed them.

Signature · 2 lines
Regards,
Pratyush Yadav