From: Pratyush Yadav <pratyush@kernel.org>
To: Tao Liu <ltao@redhat.com>
Cc: Pratyush Yadav <pratyush@kernel.org>,
Baoquan He <bhe@redhat.com>,
kexec@lists.infradead.org, pjw@kernel.org, palmer@dabbelt.com,
aou@eecs.berkeley.edu, alex@ghiti.fr,
Mimi Zohar <zohar@linux.ibm.com>,
Roberto Sassu <roberto.sassu@huawei.com>,
Dmitry Kasatkin <dmitry.kasatkin@gmail.com>,
Eric Snowberg <eric.snowberg@oracle.com>,
linux-integrity@vger.kernel.org,
linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH] riscv: Fix a NULL pointer reference in machine_kexec_prepare
Date: Wed, 01 Jul 2026 14:08:20 +0200 [thread overview]
Message-ID: <2vxzy0fuhpzf.fsf@kernel.org> (raw)
In-Reply-To: <CAO7dBbXzs2pMrVuJ3qUVT2KYNh9RysOOWy_HPTmCY3gYUh9LDQ@mail.gmail.com> (Tao Liu's message of "Wed, 1 Jul 2026 15:29:44 +1200")
On Wed, Jul 01 2026, Tao Liu wrote:
> Hi Pratyush,
>
> On Mon, Jun 29, 2026 at 11:13 PM Pratyush Yadav <pratyush@kernel.org> wrote:
>>
>> +Cc IMA maintainers
>>
>> On Fri, Jun 26 2026, Baoquan He wrote:
>>
>> > Add kexec ML to CC.
>> >
>> > On Fri, Jun 26, 2026 at 7:46 AM Tao Liu <ltao@redhat.com> wrote:
>> >>
>> >> Kindly ping, any comments?
>> >>
>> >> Thanks,
>> >> Tao Liu
>> >>
>> >> On Fri, May 29, 2026 at 3:35 PM Tao Liu <ltao@redhat.com> wrote:
>> >> >
>> >> > 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.
>> >> >
>> >> > Signed-off-by: Tao Liu <ltao@redhat.com>
>> >> > ---
>> >> > arch/riscv/kernel/machine_kexec.c | 3 +++
>> >> > 1 file changed, 3 insertions(+)
>> >> >
>> >> > diff --git a/arch/riscv/kernel/machine_kexec.c b/arch/riscv/kernel/machine_kexec.c
>> >> > index 2306ce3e5f22..d81d576f9cb5 100644
>> >> > --- a/arch/riscv/kernel/machine_kexec.c
>> >> > +++ b/arch/riscv/kernel/machine_kexec.c
>> >> > @@ -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;
>> >> > +
>> >
>> > 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 <bhe@redhat.com>
>>
>> Yeah, the patch LGTM to me too.
>>
>> Acked-by: Pratyush Yadav <pratyush@kernel.org>
>>
>> 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.
>
> 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...
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.
--
Regards,
Pratyush Yadav
_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv
WARNING: multiple messages have this Message-ID (diff)
From: Pratyush Yadav <pratyush@kernel.org>
To: Tao Liu <ltao@redhat.com>
Cc: Pratyush Yadav <pratyush@kernel.org>,
Baoquan He <bhe@redhat.com>,
kexec@lists.infradead.org, pjw@kernel.org, palmer@dabbelt.com,
aou@eecs.berkeley.edu, alex@ghiti.fr,
Mimi Zohar <zohar@linux.ibm.com>,
Roberto Sassu <roberto.sassu@huawei.com>,
Dmitry Kasatkin <dmitry.kasatkin@gmail.com>,
Eric Snowberg <eric.snowberg@oracle.com>,
linux-integrity@vger.kernel.org,
linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH] riscv: Fix a NULL pointer reference in machine_kexec_prepare
Date: Wed, 01 Jul 2026 14:08:20 +0200 [thread overview]
Message-ID: <2vxzy0fuhpzf.fsf@kernel.org> (raw)
In-Reply-To: <CAO7dBbXzs2pMrVuJ3qUVT2KYNh9RysOOWy_HPTmCY3gYUh9LDQ@mail.gmail.com> (Tao Liu's message of "Wed, 1 Jul 2026 15:29:44 +1200")
On Wed, Jul 01 2026, Tao Liu wrote:
> Hi Pratyush,
>
> On Mon, Jun 29, 2026 at 11:13 PM Pratyush Yadav <pratyush@kernel.org> wrote:
>>
>> +Cc IMA maintainers
>>
>> On Fri, Jun 26 2026, Baoquan He wrote:
>>
>> > Add kexec ML to CC.
>> >
>> > On Fri, Jun 26, 2026 at 7:46 AM Tao Liu <ltao@redhat.com> wrote:
>> >>
>> >> Kindly ping, any comments?
>> >>
>> >> Thanks,
>> >> Tao Liu
>> >>
>> >> On Fri, May 29, 2026 at 3:35 PM Tao Liu <ltao@redhat.com> wrote:
>> >> >
>> >> > 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.
>> >> >
>> >> > Signed-off-by: Tao Liu <ltao@redhat.com>
>> >> > ---
>> >> > arch/riscv/kernel/machine_kexec.c | 3 +++
>> >> > 1 file changed, 3 insertions(+)
>> >> >
>> >> > diff --git a/arch/riscv/kernel/machine_kexec.c b/arch/riscv/kernel/machine_kexec.c
>> >> > index 2306ce3e5f22..d81d576f9cb5 100644
>> >> > --- a/arch/riscv/kernel/machine_kexec.c
>> >> > +++ b/arch/riscv/kernel/machine_kexec.c
>> >> > @@ -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;
>> >> > +
>> >
>> > 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 <bhe@redhat.com>
>>
>> Yeah, the patch LGTM to me too.
>>
>> Acked-by: Pratyush Yadav <pratyush@kernel.org>
>>
>> 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.
>
> 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...
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.
--
Regards,
Pratyush Yadav
next prev parent reply other threads:[~2026-07-01 12:08 UTC|newest]
Thread overview: 22+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-05-29 3:27 [PATCH] riscv: Fix a NULL pointer reference in machine_kexec_prepare Tao Liu
2026-05-29 3:27 ` Tao Liu
2026-06-25 23:17 ` Tao Liu
2026-06-25 23:17 ` Tao Liu
2026-06-26 8:04 ` Baoquan He
2026-06-26 8:04 ` Baoquan He
2026-06-26 8:38 ` Tao Liu
2026-06-26 8:38 ` Tao Liu
2026-06-27 22:38 ` Tao Liu
2026-06-27 22:38 ` Tao Liu
2026-06-29 2:44 ` Baoquan He
2026-06-29 2:44 ` Baoquan He
2026-06-29 11:12 ` Pratyush Yadav
2026-06-29 11:12 ` Pratyush Yadav
2026-07-01 3:29 ` Tao Liu
2026-07-01 3:29 ` Tao Liu
2026-07-01 12:08 ` Pratyush Yadav [this message]
2026-07-01 12:08 ` Pratyush Yadav
2026-06-29 12:54 ` Markus Elfring
2026-06-29 12:54 ` Markus Elfring
2026-06-30 0:23 ` Tao Liu
2026-06-30 0:23 ` Tao Liu
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=2vxzy0fuhpzf.fsf@kernel.org \
--to=pratyush@kernel.org \
--cc=alex@ghiti.fr \
--cc=aou@eecs.berkeley.edu \
--cc=bhe@redhat.com \
--cc=dmitry.kasatkin@gmail.com \
--cc=eric.snowberg@oracle.com \
--cc=kexec@lists.infradead.org \
--cc=linux-integrity@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-riscv@lists.infradead.org \
--cc=ltao@redhat.com \
--cc=palmer@dabbelt.com \
--cc=pjw@kernel.org \
--cc=roberto.sassu@huawei.com \
--cc=zohar@linux.ibm.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.