qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Paolo Bonzini <pbonzini@redhat.com>
To: qemu-devel@nongnu.org
Cc: clg@kaod.org
Subject: [Qemu-devel] [PATCH 2/3] m25p80: avoid out of bounds accesses
Date: Tue, 28 Jun 2016 10:39:29 +0200	[thread overview]
Message-ID: <1467103170-5784-3-git-send-email-pbonzini@redhat.com> (raw)
In-Reply-To: <1467103170-5784-1-git-send-email-pbonzini@redhat.com>

s->cur_addr can be made to point outside s->storage, either by
writing a value >= 128 to s->ear (because s->ear * MAX_3BYTES_SIZE
is a signed integer and sign-extends into the 64-bit cur_addr),
or just by writing an address beyond the size of the flash being
emulated.  Avoid the sign extension to make the code cleaner, and
on top of that mask s->cur_addr to s->size.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 hw/block/m25p80.c | 16 +++++++---------
 1 file changed, 7 insertions(+), 9 deletions(-)

diff --git a/hw/block/m25p80.c b/hw/block/m25p80.c
index dd6714d..76a9bcf 100644
--- a/hw/block/m25p80.c
+++ b/hw/block/m25p80.c
@@ -586,18 +586,16 @@ static inline int get_addr_length(Flash *s)
 
 static void complete_collecting_data(Flash *s)
 {
-    int i;
-
-    s->cur_addr = 0;
+    int i, n;
 
-    for (i = 0; i < get_addr_length(s); ++i) {
+    n = get_addr_length(s);
+    s->cur_addr = (n == 3 ? s->ear : 0);
+    for (i = 0; i < n; ++i) {
         s->cur_addr <<= 8;
         s->cur_addr |= s->data[i];
     }
 
-    if (get_addr_length(s) == 3) {
-        s->cur_addr += s->ear * MAX_3BYTES_SIZE;
-    }
+    s->cur_addr &= s->size - 1;
 
     s->state = STATE_IDLE;
 
@@ -1099,14 +1097,14 @@ static uint32_t m25p80_transfer8(SSISlave *ss, uint32_t tx)
         DB_PRINT_L(1, "page program cur_addr=%#" PRIx64 " data=%" PRIx8 "\n",
                    s->cur_addr, (uint8_t)tx);
         flash_write8(s, s->cur_addr, (uint8_t)tx);
-        s->cur_addr++;
+        s->cur_addr = (s->cur_addr + 1) & (s->size - 1);
         break;
 
     case STATE_READ:
         r = s->storage[s->cur_addr];
         DB_PRINT_L(1, "READ 0x%" PRIx64 "=%" PRIx8 "\n", s->cur_addr,
                    (uint8_t)r);
-        s->cur_addr = (s->cur_addr + 1) % s->size;
+        s->cur_addr = (s->cur_addr + 1) & (s->size - 1);
         break;
 
     case STATE_COLLECTING_DATA:
-- 
2.7.4

  parent reply	other threads:[~2016-06-28  8:39 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-06-28  8:39 [Qemu-devel] [PATCH 0/3] m25p80: various fixes Paolo Bonzini
2016-06-28  8:39 ` [Qemu-devel] [PATCH 1/3] m25p80: do not put iovec on the stack Paolo Bonzini
2016-06-28  8:53   ` Cédric Le Goater
2016-06-28 10:02     ` Cédric Le Goater
2016-06-28 14:33   ` Eric Blake
2016-06-28  8:39 ` Paolo Bonzini [this message]
2016-06-28  9:05   ` [Qemu-devel] [PATCH 2/3] m25p80: avoid out of bounds accesses Cédric Le Goater
2016-06-28 11:42     ` Paolo Bonzini
2016-06-28 12:00       ` [Qemu-devel] " Krzeminski, Marcin (Nokia - PL/Wroclaw)
2016-06-28  8:39 ` [Qemu-devel] [PATCH 3/3] m25p80: change cur_addr to 32 bit integer Paolo Bonzini
2016-06-28  9:18   ` Cédric Le Goater
2016-06-28 11:41     ` Paolo Bonzini

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=1467103170-5784-3-git-send-email-pbonzini@redhat.com \
    --to=pbonzini@redhat.com \
    --cc=clg@kaod.org \
    --cc=qemu-devel@nongnu.org \
    /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;
as well as URLs for NNTP newsgroup(s).