lib/mpi: fix off-by-one check on index "no"
Subsystems (2)
PATCH RESEND 1/1 lib/mpi: fix off-by-one check on index "no"
From: Colin Ian King <[email protected]>
There is an off-by-one range check on the upper limit of index "no". Fix this by changing the > comparison to >
Addresses-Coverity: ("Out-of-bounds read")
---
resend to Cc linux-crypto
--- lib/mpi/mpiutil.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
lib/mpi/mpiutil.c +1 -1
@@ -69,7 +69,7 @@ postcore_initcall(mpi_init);*/ MPI mpi_const (enum gcry_mpi_constants no) { -if ((int )no <0 || no >MPI_NUMBER_OF_CONSTANTS ) +if ((int )no <0 || no >=MPI_NUMBER_OF_CONSTANTS )pr_err ("MPI: invalid mpi_const selector %d\n" , no);if (!constants[no])pr_err ("MPI: MPI subsystem not initialized\n" );
Signature · 1 line
2.27.0
st 30. 9. 2020 o 15:04 Colin King <[email protected]> napÃsal(a):
Quoted 5 lines
From: Colin Ian King <[email protected]>
There is an off-by-one range check on the upper limit of index "no". Fix this by changing the > comparison to >=
Note that this doesn't completely fix the bug though... (see below)
Quoted 17 lines
Addresses-Coverity: ("Out-of-bounds read")
Fixes a8ea8bdd9df9 ("lib/mpi: Extend the MPI library")Signed-off-by Colin Ian King <[email protected]>---
resend to Cc linux-crypto
--- lib/mpi/mpiutil.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)lib/mpi/mpiutil.c +0
-0
lib/mpi/mpiutil.c postcore_initcall(mpi_init); · line 73
Quoted 6 lines
lib/mpi/mpiutil.c +1
-1@@ -69,7 +69,7 @@ postcore_initcall(mpi_init);*/ MPI mpi_const (enum gcry_mpi_constants no) { -if ((int )no <0 || no >MPI_NUMBER_OF_CONSTANTS ) +if ((int )no <0 || no >=MPI_NUMBER_OF_CONSTANTS )pr_err ("MPI: invalid mpi_const selector %d\n" , no);
What the code does is it just logs an error if the value is out of range, but then it happily continues dereferencing the array anyway... In the original libgcrypt code [1] (which BTW needs this patch, too), there is log_bug() instead of pr_err(), which doesn't just log the error, but also abort()'s the program. BUG() would be the correct kernel equivalent for log_bug(). It seems the whole kernel's MPI library clone should be re-audited for other instances of pr_*()'s that should in fact be BUG()'s (or even better, WARN_ONCE()'s with proper error handling, but that might diverge the code from libgcrypt too much...).
[1] https://github.com/gpg/libgcrypt/blob/9cd92ebae21900e54cc3d8b607c8ed1afbf2eb9b/mpi/mpiutil.c#L773
Quoted 5 lines
lib/mpi/mpiutil.c +0
-1if (!constants[no])pr_err ("MPI: MPI subsystem not initialized\n" ); --2.27.0
Quoted 10 lines · Ondrej Mosnáček
st 30. 9. 2020 o 15:04 Colin King <[email protected]> napísal(a):
Quoted 5 lines
From: Colin Ian King <[email protected]>
There is an off-by-one range check on the upper limit of index "no". Fix this by changing the > comparison to >=
Note that this doesn't completely fix the bug though... (see below)
And by the same reasoning, it does not address the coverity issue either, since the out of bounds read is still executed.
Quoted 42 lines
Quoted 17 lines
Addresses-Coverity: ("Out-of-bounds read")
Fixes a8ea8bdd9df9 ("lib/mpi: Extend the MPI library")Signed-off-by Colin Ian King <[email protected]>---
resend to Cc linux-crypto
--- lib/mpi/mpiutil.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)lib/mpi/mpiutil.c +0
-0lib/mpi/mpiutil.c postcore_initcall(mpi_init); · line 73
Quoted 6 lines
lib/mpi/mpiutil.c +1
-1@@ -69,7 +69,7 @@ postcore_initcall(mpi_init);*/ MPI mpi_const (enum gcry_mpi_constants no) { -if ((int )no <0 || no >MPI_NUMBER_OF_CONSTANTS ) +if ((int )no <0 || no >=MPI_NUMBER_OF_CONSTANTS )pr_err ("MPI: invalid mpi_const selector %d\n" , no);What the code does is it just logs an error if the value is out of range, but then it happily continues dereferencing the array anyway... In the original libgcrypt code [1] (which BTW needs this patch, too), there is
log_bug()instead ofpr_err(), which doesn't just log the error, but also abort()'s the program.BUG()would be the correct kernel equivalent forlog_bug(). It seems the whole kernel's MPI library clone should be re-audited for other instances of pr_*()'s that should in fact be BUG()'s (or even better, WARN_ONCE()'s with proper error handling, but that might diverge the code from libgcrypt too much...).[1] https://github.com/gpg/libgcrypt/blob/9cd92ebae21900e54cc3d8b607c8ed1afbf2eb9b/mpi/mpiutil.c#L773
Quoted 5 lines
lib/mpi/mpiutil.c +0
-1if (!constants[no])pr_err ("MPI: MPI subsystem not initialized\n" ); --2.27.0