All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Maholi Tumanggor" <maholitumanggor@gmail.com>,
	linux-crypto@vger.kernel.org
Cc: herbert@gondor.apana.org.au, davem@davemloft.net
Subject: Re: [PATCH v2] lib/crypto/mpi: Fix heap OOB write in mpi_powm() when res aliases input
Date: Mon, 02 Nov 2026 13:03:14 +0000	[thread overview]
Message-ID: <sashiko-outbox-193454@kernel.org> (raw)
In-Reply-To: <20261102092550.2633-1-maholitumanggor@gmail.com>

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Critical] lib/crypto/mpi: NULL dereference in mpi_powm() due to unassigned rp_alloc
--

commit eb877144fed936eb853d022502854e67c6b7e317
Author: Maholi Tumanggor <maholitumanggor@gmail.com>

lib/crypto/mpi: Fix heap OOB write in mpi_powm() when res aliases input

This commit attempts to fix an out-of-bounds write in mpi_powm() when res
aliases an input operand and requires reallocation by introducing rp_alloc
as the write-back destination buffer when assign_rp is set.

> diff --git a/lib/crypto/mpi/mpi-pow.c b/lib/crypto/mpi/mpi-pow.c
> index 9e695a3bda3a0..f4df9fbcbc2c4 100644
> --- a/lib/crypto/mpi/mpi-pow.c
> +++ b/lib/crypto/mpi/mpi-pow.c
> @@ -28,6 +28,7 @@ int mpi_powm(MPI res, MPI base, MPI exp, MPI mod)
>  	mpi_ptr_t mp_marker = NULL, bp_marker = NULL, ep_marker = NULL;
>  	struct karatsuba_ctx karactx = {};
>  	mpi_ptr_t xp_marker = NULL;
> +	mpi_ptr_t rp_alloc = NULL;
>  	mpi_ptr_t tspace = NULL;
>  	mpi_ptr_t rp, ep, mp, bp;
>  	mpi_size_t esize, msize, bsize, rsize;
> @@ -256,16 +257,23 @@ int mpi_powm(MPI res, MPI base, MPI exp, MPI mod)
>  		 * might be, see above).
>  		 */
>  		if (mod_shift_cnt) {
> -			carry_limb =
> -			    mpihelp_lshift(res->d, rp, rsize, mod_shift_cnt);
> -			rp = res->d;
> +			mpi_ptr_t dst = assign_rp ? rp_alloc : res->d;
> +
> +			carry_limb = mpihelp_lshift(dst, rp, rsize, mod_shift_cnt);
> +			rp = dst;
>  			if (carry_limb) {
>  				rp[rsize] = carry_limb;
>  				rsize++;
>  			}
>  		} else {
> -			MPN_COPY(res->d, rp, rsize);
> -			rp = res->d;
> +			if (assign_rp) {
> +				if (rp != rp_alloc)
> +					MPN_COPY(rp_alloc, rp, rsize);
> +				rp = rp_alloc;
> +			} else {
> +				MPN_COPY(res->d, rp, rsize);
> +				rp = res->d;
> +			}
>  		}

[Severity: Critical]
As reported by Sashiko on v1 [1], rp_alloc is initialized to NULL in
mpi_powm(), but is it ever assigned when the new limb buffer is allocated?

In lib/crypto/mpi/mpi-pow.c:mpi_powm(), when res->alloced < size and rp
aliases an input operand:

	if (res->alloced < size) {
		if (rp == ep || rp == mp || rp == bp) {
			rp = mpi_alloc_limb_space(size);
			if (!rp)
				goto enomem;
			assign_rp = 1;
		}

rp_alloc is not updated here and remains NULL.

When execution reaches the write-back block with assign_rp set to 1, won't
mpihelp_lshift() (when mod_shift_cnt is non-zero) or MPN_COPY() (when
mod_shift_cnt is zero) dereference a NULL rp_alloc pointer, and overwrite
rp with NULL so the limb buffer allocated via mpi_alloc_limb_space() is
also leaked?

[1] https://lore.kernel.org/r/20261102064225.2271-1-maholitumanggor@gmail.com

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261102092550.2633-1-maholitumanggor@gmail.com?part=1

  reply	other threads:[~2026-11-02 13:03 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-02  6:42 [PATCH] lib/crypto/mpi: Fix heap OOB write in mpi_powm() when res aliases input Maholi Tumanggor
2026-11-02  6:49 ` sashiko-bot
2026-11-02  9:25 ` [PATCH v2] " Maholi Tumanggor
2026-11-02 13:03   ` sashiko-bot [this message]
2026-11-02 18:12   ` Eric Biggers

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=sashiko-outbox-193454@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=davem@davemloft.net \
    --cc=herbert@gondor.apana.org.au \
    --cc=linux-crypto@vger.kernel.org \
    --cc=maholitumanggor@gmail.com \
    --cc=sashiko-reviews@lists.linux.dev \
    /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.