From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:51995) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bHxhG-0006iC-PN for qemu-devel@nongnu.org; Tue, 28 Jun 2016 14:25:19 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bHxhC-0002NG-Fx for qemu-devel@nongnu.org; Tue, 28 Jun 2016 14:25:17 -0400 Received: from 7.mo3.mail-out.ovh.net ([46.105.57.200]:57023) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bHxhC-0002N3-8h for qemu-devel@nongnu.org; Tue, 28 Jun 2016 14:25:14 -0400 Received: from player772.ha.ovh.net (b7.ovh.net [213.186.33.57]) by mo3.mail-out.ovh.net (Postfix) with ESMTP id 578B3FF8E6C for ; Tue, 28 Jun 2016 20:25:10 +0200 (CEST) From: =?UTF-8?q?C=C3=A9dric=20Le=20Goater?= Date: Tue, 28 Jun 2016 20:24:24 +0200 Message-Id: <1467138270-32481-4-git-send-email-clg@kaod.org> In-Reply-To: <1467138270-32481-1-git-send-email-clg@kaod.org> References: <1467138270-32481-1-git-send-email-clg@kaod.org> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Subject: [Qemu-devel] [PATCH v5 3/9] m25p80: avoid out of bounds accesses List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Peter Maydell , Peter Crosthwaite Cc: qemu-devel@nongnu.org, qemu-arm@nongnu.org, kwolf@redhat.com, armbru@redhat.com, Andrew Jeffery , Paolo Bonzini , =?UTF-8?q?C=C3=A9dric=20Le=20Goater?= From: Paolo Bonzini s->cur_addr can be made to point outside s->storage, either by writing a value >=3D 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 Reviewed-by: C=C3=A9dric Le Goater Reviewed by: Marcin Krzeminski Signed-off-by: C=C3=A9dric Le Goater --- 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 8e4c11573521..4836b4eb9666 100644 --- a/hw/block/m25p80.c +++ b/hw/block/m25p80.c @@ -587,18 +587,16 @@ static inline int get_addr_length(Flash *s) =20 static void complete_collecting_data(Flash *s) { - int i; - - s->cur_addr =3D 0; + int i, n; =20 - for (i =3D 0; i < get_addr_length(s); ++i) { + n =3D get_addr_length(s); + s->cur_addr =3D (n =3D=3D 3 ? s->ear : 0); + for (i =3D 0; i < n; ++i) { s->cur_addr <<=3D 8; s->cur_addr |=3D s->data[i]; } =20 - if (get_addr_length(s) =3D=3D 3) { - s->cur_addr +=3D s->ear * MAX_3BYTES_SIZE; - } + s->cur_addr &=3D s->size - 1; =20 s->state =3D STATE_IDLE; =20 @@ -1100,14 +1098,14 @@ static uint32_t m25p80_transfer8(SSISlave *ss, ui= nt32_t tx) DB_PRINT_L(1, "page program cur_addr=3D%#" PRIx64 " data=3D%" PR= Ix8 "\n", s->cur_addr, (uint8_t)tx); flash_write8(s, s->cur_addr, (uint8_t)tx); - s->cur_addr++; + s->cur_addr =3D (s->cur_addr + 1) & (s->size - 1); break; =20 case STATE_READ: r =3D s->storage[s->cur_addr]; DB_PRINT_L(1, "READ 0x%" PRIx64 "=3D%" PRIx8 "\n", s->cur_addr, (uint8_t)r); - s->cur_addr =3D (s->cur_addr + 1) % s->size; + s->cur_addr =3D (s->cur_addr + 1) & (s->size - 1); break; =20 case STATE_COLLECTING_DATA: --=20 2.1.4