riscv: Fix a NULL pointer reference in machine_kexec_prepare
Subsystems (5)
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
@@ -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
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 bykexec_add_buffer(), but kbuf.buffer is NULL.Fix this by simply adding a check before copy.
Signed-off-by Tao Liu <[email protected]>--- arch/riscv/kernel/machine_kexec.c | 3 +++ 1 file changed, 3 insertions(+)arch/riscv/kernel/machine_kexec.c +3
-1@@ -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
Baoquan He
Acked-byAdd kexec ML to CC.
Quoted 30 lines · Tao Liu
Kindly ping, any comments?
Thanks,
Tao LiuQuoted 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 bykexec_add_buffer(), but kbuf.buffer is NULL.Fix this by simply adding a check before copy.
Signed-off-by Tao Liu <[email protected]>--- arch/riscv/kernel/machine_kexec.c | 3 +++ 1 file changed, 3 insertions(+)arch/riscv/kernel/machine_kexec.c +0
-0@@ -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
-1if (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
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 LiuQuoted 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 bykexec_add_buffer(), but kbuf.buffer is NULL.Fix this by simply adding a check before copy.
Signed-off-by Tao Liu <[email protected]>--- arch/riscv/kernel/machine_kexec.c | 3 +++ 1 file changed, 3 insertions(+)arch/riscv/kernel/machine_kexec.c +0
-0@@ -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:
Acked-by Baoquan He <[email protected]>Quoted 13 lines
Quoted 6 lines
arch/riscv/kernel/machine_kexec.c +0
-1if (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
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 LiuQuoted 46 lines · Baoquan He
Add kexec ML to CC.
Quoted 30 lines · Tao Liu
Kindly ping, any comments?
Thanks,
Tao LiuQuoted 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 bykexec_add_buffer(), but kbuf.buffer is NULL.Fix this by simply adding a check before copy.
Signed-off-by Tao Liu <[email protected]>--- arch/riscv/kernel/machine_kexec.c | 3 +++ 1 file changed, 3 insertions(+)arch/riscv/kernel/machine_kexec.c +0
-0@@ -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:
Acked-by Baoquan He <[email protected]>
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
-1if (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
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, howeverima_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:
Acked-by Baoquan He <[email protected]>Thanks for your code review and suggestions!
Thanks,
Tao LiuQuoted 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
+Cc IMA maintainers
Quoted 46 lines · Baoquan He
Add kexec ML to CC.
Quoted 30 lines · Tao Liu
Kindly ping, any comments?
Thanks,
Tao LiuQuoted 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 bykexec_add_buffer(), but kbuf.buffer is NULL.Fix this by simply adding a check before copy.
Signed-off-by Tao Liu <[email protected]>--- arch/riscv/kernel/machine_kexec.c | 3 +++ 1 file changed, 3 insertions(+)arch/riscv/kernel/machine_kexec.c +0
-0@@ -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:
Acked-by Baoquan He <[email protected]>
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
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 LiuQuoted 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 bykexec_add_buffer(), but kbuf.buffer is NULL.Fix this by simply adding a check before copy.
Signed-off-by Tao Liu <[email protected]>--- arch/riscv/kernel/machine_kexec.c | 3 +++ 1 file changed, 3 insertions(+)arch/riscv/kernel/machine_kexec.c +0
-0@@ -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:
Acked-by Baoquan He <[email protected]>Yeah, the patch LGTM to me too.
Acked-by Pratyush Yadav <[email protected]>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 usingkexec_bufferandkexec_buffer_sizewhich 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
Pratyush Yadav
maintainerQuoted 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 LiuQuoted 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 bykexec_add_buffer(), but kbuf.buffer is NULL.Fix this by simply adding a check before copy.
Signed-off-by Tao Liu <[email protected]>--- arch/riscv/kernel/machine_kexec.c | 3 +++ 1 file changed, 3 insertions(+)arch/riscv/kernel/machine_kexec.c +0
-0@@ -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:
Acked-by Baoquan He <[email protected]>Yeah, the patch LGTM to me too.
Acked-by Pratyush Yadav <[email protected]>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 usingkexec_bufferandkexec_buffer_sizewhich 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)? Whenkexec_bufferis 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
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
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#n34Regards,
Markus