From: Nicolai Stange <nicstange@gmail.com>
To: Herbert Xu <herbert@gondor.apana.org.au>
Cc: Tadeusz Struk <tadeusz.struk@intel.com>,
Michal Marek <mmarek@suse.com>,
Andrzej Zaborowski <andrew.zaborowski@intel.com>,
Stephan Mueller <smueller@chronox.de>,
Arnd Bergmann <arnd@arndb.de>,
linux-kernel@vger.kernel.org,
Nicolai Stange <nicstange@gmail.com>
Subject: [PATCH 8/8] lib/mpi: mpi_read_buffer(): fix buffer overflow
Date: Thu, 17 Mar 2016 19:00:06 +0100 [thread overview]
Message-ID: <1458237606-4954-9-git-send-email-nicstange@gmail.com> (raw)
In-Reply-To: <1458237606-4954-1-git-send-email-nicstange@gmail.com>
Currently, mpi_read_buffer() writes full limbs to the output buffer
and moves memory around to purge leading zero limbs afterwards.
However, with
commit 9cbe21d8f89d ("lib/mpi: only require buffers as big as needed for
the integer")
the caller is only required to provide a buffer large enough to hold the
result without the leading zeros.
This might result in a buffer overflow for small MP numbers with leading
zeros.
Fix this by coping the result to its final destination within the output
buffer and not copying the leading zeros at all.
Fixes: 9cbe21d8f89d ("lib/mpi: only require buffers as big as needed for
the integer")
Signed-off-by: Nicolai Stange <nicstange@gmail.com>
---
lib/mpi/mpicoder.c | 13 +++----------
1 file changed, 3 insertions(+), 10 deletions(-)
diff --git a/lib/mpi/mpicoder.c b/lib/mpi/mpicoder.c
index a999ee1..27582e2 100644
--- a/lib/mpi/mpicoder.c
+++ b/lib/mpi/mpicoder.c
@@ -201,16 +201,9 @@ int mpi_read_buffer(MPI a, uint8_t *buf, unsigned buf_len, unsigned *nbytes,
#else
#error please implement for this limb size.
#endif
- memcpy(p, &alimb, BYTES_PER_MPI_LIMB);
- p += BYTES_PER_MPI_LIMB;
- if (lzeros > 0) {
- mpi_limb_t *limb1 = (void *)p - sizeof(alimb);
- mpi_limb_t *limb2 = (void *)p - sizeof(alimb)
- + lzeros;
- *limb1 = *limb2;
- p -= lzeros;
- lzeros -= sizeof(alimb);
- }
+ memcpy(p, &alimb + lzeros, BYTES_PER_MPI_LIMB - lzeros);
+ p += BYTES_PER_MPI_LIMB - lzeros;
+ lzeros = 0;
}
return 0;
}
--
2.7.2
next prev parent reply other threads:[~2016-03-17 18:01 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-03-17 17:59 [PATCH 0/8] lib/mpi: bug fixes and cleanup Nicolai Stange
2016-03-17 17:59 ` [PATCH 1/8] lib/mpi: mpi_write_sgl(): fix skipping of leading zero limbs Nicolai Stange
2016-03-17 18:00 ` [PATCH 2/8] lib/mpi: mpi_write_sgl(): fix style issue with lzero decrement Nicolai Stange
2016-03-17 18:00 ` [PATCH 3/8] lib/mpi: mpi_write_sgl(): purge redundant pointer arithmetic Nicolai Stange
2016-03-17 18:00 ` [PATCH 4/8] lib/mpi: mpi_write_sgl(): fix out-of-bounds stack access Nicolai Stange
2016-03-17 18:00 ` [PATCH 5/8] lib/mpi: mpi_write_sgl(): replace open coded endian conversion Nicolai Stange
2016-03-17 18:00 ` [PATCH 6/8] lib/mpi: mpi_read_buffer(): optimize skipping of leading zero limbs Nicolai Stange
2016-03-17 18:00 ` [PATCH 7/8] lib/mpi: mpi_read_buffer(): replace open coded endian conversion Nicolai Stange
2016-03-17 18:00 ` Nicolai Stange [this message]
2016-03-20 12:40 ` [PATCH 0/8] lib/mpi: bug fixes and cleanup Nicolai Stange
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=1458237606-4954-9-git-send-email-nicstange@gmail.com \
--to=nicstange@gmail.com \
--cc=andrew.zaborowski@intel.com \
--cc=arnd@arndb.de \
--cc=herbert@gondor.apana.org.au \
--cc=linux-kernel@vger.kernel.org \
--cc=mmarek@suse.com \
--cc=smueller@chronox.de \
--cc=tadeusz.struk@intel.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox