Linux cryptographic layer development
 help / color / mirror / Atom feed
* [PATCH] crypto: mpi: add NULL checks to mpi_normalize().
@ 2024-07-16  8:28 Roman Smirnov
  2024-08-02 12:46 ` Herbert Xu
  0 siblings, 1 reply; 4+ messages in thread
From: Roman Smirnov @ 2024-07-16  8:28 UTC (permalink / raw)
  To: Herbert Xu, David S. Miller
  Cc: Roman Smirnov, Mimi Zohar, Sergey Shtylyov, linux-crypto,
	linux-kernel, lvc-project

If a->d is NULL, the NULL pointer will be dereferenced. It
is necessary to prevent this case. There is at least one call
stack that can lead to it:

    mpi_ec_curve_point()
      ec_pow2()
        ec_mulm()
          ec_mod()
            mpi_mod()
              mpi_fdiv_r()
                mpi_tdiv_r()
                  mpi_tdiv_qr()
                    mpi_resize()
                      kcalloc()

mpi_resize can return -ENOMEM, but this case is not handled in any way.

Next, dereferencing takes place:

    mpi_ec_curve_point()
      mpi_cmp()
        do_mpi_cmp()
          mpi_normalize()

Found by Linux Verification Center (linuxtesting.org) with Svace.

Signed-off-by: Roman Smirnov <r.smirnov@omp.ru>
---
 lib/crypto/mpi/mpi-bit.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/lib/crypto/mpi/mpi-bit.c b/lib/crypto/mpi/mpi-bit.c
index 070ba784c9f1..d7420bdb4ff2 100644
--- a/lib/crypto/mpi/mpi-bit.c
+++ b/lib/crypto/mpi/mpi-bit.c
@@ -29,6 +29,9 @@
  */
 void mpi_normalize(MPI a)
 {
+	if (!a || !a->d)
+		return;
+
 	for (; a->nlimbs && !a->d[a->nlimbs - 1]; a->nlimbs--)
 		;
 }
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2024-08-09  4:29 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-07-16  8:28 [PATCH] crypto: mpi: add NULL checks to mpi_normalize() Roman Smirnov
2024-08-02 12:46 ` Herbert Xu
2024-08-07 14:20   ` Roman Smirnov
2024-08-09  4:28     ` Herbert Xu

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox