From: Simon Horman <horms@kernel.org>
To: zhangzikang1992@126.com
Cc: kexec@lists.infradead.org, zhangzikang <zhangzikang@kylinos.cn>
Subject: Re: [PATCH] kexec: fix stack buffer overflow in update_purgatory()
Date: Tue, 10 Nov 2026 10:58:41 +0000 [thread overview]
Message-ID: <20261110105841.GA1320297@horms.kernel.org> (raw)
In-Reply-To: <20261030030917.103293-1-zhangzikang1992@126.com>
On Fri, Oct 30, 2026 at 11:09:18AM +0800, zhangzikang1992@126.com wrote:
> From: zhangzikang <zhangzikang@kylinos.cn>
>
> Problem:
> A stack buffer overflow exists in "update_purgatory()". The local array "sha256_region region[SHA256_REGIONS]" (size 16) is written during iteration over "info->nr_segments" without boundary checking for the write index "j".
>
> "Add_degment_phys-virt()" only issues a warning when "nr_degments" exceeds "KEXEC_MAX_SGMENTS" (=16) and continues to append segments. Therefore, "update_purgation()" may be called, causing "nr_degments" to be much larger than "SHA256-REGIONS", resulting in "region [j]" being written outside of the 16 item stack array. This type of out of bounds write will damage the Canary stack and adjacent stack frames.
>
> Impact:
> Stack buffer overflow causing process crash (resulting in denial of service through Canary termination); The potential for controllable stack destruction depends on the content of the image.
>
> Fix:
> Before writing to "region[j]" in "update_purgatory()", add a bounds check on "j" to ensure that writing stops immediately when "j" reaches "SHA256_REGIONS", preventing out-of-bounds writes from corrupting adjacent global variables.
>
> Signed-off-by: zhangzikang <zhangzikang@kylinos.cn>
> ---
> kexec/kexec.c | 5 +++++
> 1 file changed, 5 insertions(+)
>
> diff --git a/kexec/kexec.c b/kexec/kexec.c
> index c9e4bcb..732058e 100644
> --- a/kexec/kexec.c
> +++ b/kexec/kexec.c
> @@ -728,6 +728,11 @@ static void update_purgatory(struct kexec_info *info)
> sha256_update(&ctx, null_buf, bytes);
> nullsz -= bytes;
> }
> + if (j >= SHA256_REGIONS) {
> + fprintf(stderr, "Warning: SHA256 region limit reached. "
> + "Skipping remaining segments from checksum region list\n");
> + break;
> + }
> region[j].start = (unsigned long) info->segment[i].mem;
> region[j].len = info->segment[i].memsz;
> j++;
Hi Zikang,
Thanks for your patch. I agree this is a bug.
And that your patch avoids kexec-tools crashing
However, I think by skipping regions as your patch proposes we move the
burden of failing what is an invalid operation to the load call, which will
fail because the Kernel enforces a 16 segment limit. And, moreover, it's
probably not best to create a mismatch between the calculated digest and
the populated regions, which appears to be possible with your patch.
What I would suggest is this:
1. Add a segment limit check to add_segment_phys_virt,
logging an error and returning -1 if the check fails.
2. As a failsafe mechanism, add a check to update_purgatory,
but place it as early in the loop as possible, and
call die() rather than just logging an error and continuing.
next prev parent reply other threads:[~2026-11-10 10:58 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-30 3:09 [PATCH] kexec: fix stack buffer overflow in update_purgatory() zhangzikang1992
2026-11-10 10:58 ` Simon Horman [this message]
2026-11-10 11:21 ` Simon Horman
2026-11-10 12:36 ` 张子康
2026-11-10 12:55 ` Simon Horman
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=20261110105841.GA1320297@horms.kernel.org \
--to=horms@kernel.org \
--cc=kexec@lists.infradead.org \
--cc=zhangzikang1992@126.com \
--cc=zhangzikang@kylinos.cn \
/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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox