From patchwork Thu Nov 16 10:16:55 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Prasad Pandit X-Patchwork-Id: 10060929 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 4402360230 for ; Thu, 16 Nov 2017 10:18:06 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 4144D2A8DC for ; Thu, 16 Nov 2017 10:18:06 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 34F322A8DE; Thu, 16 Nov 2017 10:18:06 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id B22FD2A8DC for ; Thu, 16 Nov 2017 10:18:04 +0000 (UTC) Received: from localhost ([::1]:39984 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eFHF9-0003XY-SL for patchwork-qemu-devel@patchwork.kernel.org; Thu, 16 Nov 2017 05:17:59 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:46599) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eFHET-0003XO-SJ for qemu-devel@nongnu.org; Thu, 16 Nov 2017 05:17:22 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1eFHEP-0004cQ-Pe for qemu-devel@nongnu.org; Thu, 16 Nov 2017 05:17:17 -0500 Received: from mx1.redhat.com ([209.132.183.28]:46996) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1eFHEP-0004a0-Go for qemu-devel@nongnu.org; Thu, 16 Nov 2017 05:17:13 -0500 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 86EA6C01BCB4; Thu, 16 Nov 2017 10:17:12 +0000 (UTC) Received: from localhost.localdomain (vpn2-54-19.bne.redhat.com [10.64.54.19]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 12D971841B; Thu, 16 Nov 2017 10:17:03 +0000 (UTC) From: P J P To: Qemu Developers Date: Thu, 16 Nov 2017 15:46:55 +0530 Message-Id: <20171116101655.26357-1-ppandit@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.31]); Thu, 16 Nov 2017 10:17:12 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH v2] ps2: check PS2Queue indices in post_load routine X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Prasad J Pandit , Cyrille Chatras , "Dr . David Alan Gilbert" , Gerd Hoffmann , Paolo Bonzini Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP From: Prasad J Pandit During Qemu guest migration, a destination process invokes ps2 post_load function. In that, if 'rptr' and 'count' values were invalid, it could lead to OOB access issue. Add check to avoid it. Reported-by: Cyrille Chatras Signed-off-by: Prasad J Pandit Reviewed-by: Paolo Bonzini --- hw/input/ps2.c | 39 ++++++++++++++++++--------------------- 1 file changed, 18 insertions(+), 21 deletions(-) Update: return -1(bad indices) error, instead of correcting values -> https://lists.gnu.org/archive/html/qemu-devel/2017-11/msg02964.html diff --git a/hw/input/ps2.c b/hw/input/ps2.c index f388a23c8e..35392d55fc 100644 --- a/hw/input/ps2.c +++ b/hw/input/ps2.c @@ -1222,32 +1222,37 @@ static void ps2_common_reset(PS2State *s) s->update_irq(s->update_arg, 0); } -static void ps2_common_post_load(PS2State *s) +static int ps2_common_post_load(PS2State *s) { PS2Queue *q = &s->queue; int size; int i; int tmp_data[PS2_QUEUE_SIZE]; + if (q->count < 0 || q->rptr < 0 || q->rptr >= sizeof(q->data)) { + return -1; + } + /* set the useful data buffer queue size, < PS2_QUEUE_SIZE */ size = q->count > PS2_QUEUE_SIZE ? 0 : q->count; /* move the queue elements to the start of data array */ - if (size > 0) { - for (i = 0; i < size; i++) { - /* move the queue elements to the temporary buffer */ - tmp_data[i] = q->data[q->rptr]; - if (++q->rptr == 256) { - q->rptr = 0; - } + for (i = 0; i < size; i++) { + /* move the queue elements to the temporary buffer */ + tmp_data[i] = q->data[q->rptr]; + if (++q->rptr == 256) { + q->rptr = 0; } - memcpy(q->data, tmp_data, size); } + memcpy(q->data, tmp_data, size); + /* reset rptr/wptr/count */ q->rptr = 0; q->wptr = size; q->count = size; s->update_irq(s->update_arg, q->count != 0); + + return 0; } static void ps2_kbd_reset(void *opaque) @@ -1346,9 +1351,7 @@ static int ps2_kbd_post_load(void* opaque, int version_id) if (version_id == 2) s->scancode_set=2; - ps2_common_post_load(ps2); - - return 0; + return ps2_common_post_load(ps2); } static int ps2_kbd_pre_save(void *opaque) @@ -1356,9 +1359,7 @@ static int ps2_kbd_pre_save(void *opaque) PS2KbdState *s = (PS2KbdState *)opaque; PS2State *ps2 = &s->common; - ps2_common_post_load(ps2); - - return 0; + return ps2_common_post_load(ps2); } static const VMStateDescription vmstate_ps2_keyboard = { @@ -1386,9 +1387,7 @@ static int ps2_mouse_post_load(void *opaque, int version_id) PS2MouseState *s = (PS2MouseState *)opaque; PS2State *ps2 = &s->common; - ps2_common_post_load(ps2); - - return 0; + return ps2_common_post_load(ps2); } static int ps2_mouse_pre_save(void *opaque) @@ -1396,9 +1395,7 @@ static int ps2_mouse_pre_save(void *opaque) PS2MouseState *s = (PS2MouseState *)opaque; PS2State *ps2 = &s->common; - ps2_common_post_load(ps2); - - return 0; + return ps2_common_post_load(ps2); } static const VMStateDescription vmstate_ps2_mouse = {