Received: from eggs.gnu.org ([2001:4830:134:3::10]:57722) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bHoYV-0002LI-1B for qemu-devel@nongnu.org; Tue, 28 Jun 2016 04:39:40 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bHoYS-0006ou-SW for qemu-devel@nongnu.org; Tue, 28 Jun 2016 04:39:37 -0400 Received: from mail-wm0-x244.google.com ([2a00:1450:400c:c09::244]:33258) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bHoYS-0006oe-LG for qemu-devel@nongnu.org; Tue, 28 Jun 2016 04:39:36 -0400 Received: by mail-wm0-x244.google.com with SMTP id r201so3848099wme.0 for ; Tue, 28 Jun 2016 01:39:36 -0700 (PDT) Sender: Paolo Bonzini From: Paolo Bonzini Date: Tue, 28 Jun 2016 10:39:29 +0200 Message-Id: <1467103170-5784-3-git-send-email-pbonzini@redhat.com> In-Reply-To: <1467103170-5784-1-git-send-email-pbonzini@redhat.com> References: <1467103170-5784-1-git-send-email-pbonzini@redhat.com> Subject: [Qemu-devel] [PATCH 2/3] m25p80: avoid out of bounds accesses List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: clg@kaod.org 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 --- 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