From patchwork Thu Dec 14 14:52:54 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Andrey Smirnov X-Patchwork-Id: 10112403 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 8E2216019C for ; Thu, 14 Dec 2017 15:04:26 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 7F40922B39 for ; Thu, 14 Dec 2017 15:04:26 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 73F5F29004; Thu, 14 Dec 2017 15:04:26 +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.8 required=2.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_HI, T_DKIM_INVALID 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 ED09D22B39 for ; Thu, 14 Dec 2017 15:04:25 +0000 (UTC) Received: from localhost ([::1]:41443 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ePV3h-00022I-4E for patchwork-qemu-devel@patchwork.kernel.org; Thu, 14 Dec 2017 10:04:25 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:55521) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ePUtM-0001ej-3m for qemu-devel@nongnu.org; Thu, 14 Dec 2017 09:53:45 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ePUtL-0000sk-3Q for qemu-devel@nongnu.org; Thu, 14 Dec 2017 09:53:44 -0500 Received: from mail-pg0-x241.google.com ([2607:f8b0:400e:c05::241]:33437) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1ePUtH-0000la-F1; Thu, 14 Dec 2017 09:53:39 -0500 Received: by mail-pg0-x241.google.com with SMTP id g7so3565067pgs.0; Thu, 14 Dec 2017 06:53:39 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=Q/p6xPc86L3+l6gZYYE2RbnwLFyAMnE8fAGCcar+YBw=; b=vOjUKsk8MrgUZ2SXsJIwnoaHVDzwOqKYt7/Hx91JLDWHEw/4aBFy4pIS+IUsNWOnE9 /lVrprk0RRkyq225Cwz8BaAX82h7fryMly/U1oDF9cDK4iPOYJXKTid0jMphKZNa0lum GuC9F0v4kTRvZFAlKj7dmwpVJ6Qr3ZXJ/puCUmjNkXJ4UjDq2pafoftJs1iKIoy0pqhJ RBsrmwrqNpVdI0DXfSnyczLLmTzwBY7osmWS1iJwycXSaxQK1nWl8xOlJHY3yc3HbFNG xKgGJvLDrGIZI8OMKk4IwBgyb7pVL9jW0scobLia05Qw7XBNlFoLNZPD2Vx0C8r80qBJ ceWg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=Q/p6xPc86L3+l6gZYYE2RbnwLFyAMnE8fAGCcar+YBw=; b=oppqq3mz+ZUjMml/EjGBd5FTfI9iwD7HwrLHghGZ4gIqvpH/hCounFSwPUCAqTUGav JHmFnzq/cfFO1o22GpP8kfWS2o7YFE+FTgL3HchaXw2KWdIfM19GT92Jc2J2O/XKjoAY sDcqaJQFfUuI4YLssjkkxwZ62pn4lC7mX3BzUjUalEGMp874W/FIK1LMbb0j9QqOABaq xyIlvf4gsO+ctDQZ17rxvaXud5WoSPzONib/IAPe6oTRAhXXQ9MvcWReOmk0eKUTeBvQ CHNvQEZ4Q+JwHUVuy99nca1grHfiH7UDcGuSn6D3Krud9XfeFTmcq8YL+AT3Y7fp+WXF iquA== X-Gm-Message-State: AKGB3mJnWrstTmYX1C+BB4eMpZaGFqfz7VTavZFynoq+GWLiD6Bprt3E 4ask6coXeD8k1GWvVURlzxVHvwty X-Google-Smtp-Source: ACJfBov8VG10v4aBEHPBdODgu9TAZmEL45lT622c53nrLqkHQbtRsuJtgMh4plSmel6enwwXsj6Png== X-Received: by 10.98.166.148 with SMTP id r20mr9776838pfl.80.1513263218183; Thu, 14 Dec 2017 06:53:38 -0800 (PST) Received: from squirtle.lan (c-24-22-235-96.hsd1.wa.comcast.net. [24.22.235.96]) by smtp.gmail.com with ESMTPSA id e7sm8757439pfj.44.2017.12.14.06.53.36 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Thu, 14 Dec 2017 06:53:37 -0800 (PST) From: Andrey Smirnov To: qemu-arm@nongnu.org Date: Thu, 14 Dec 2017 06:52:54 -0800 Message-Id: <20171214145255.31545-15-andrew.smirnov@gmail.com> X-Mailer: git-send-email 2.14.3 In-Reply-To: <20171214145255.31545-1-andrew.smirnov@gmail.com> References: <20171214145255.31545-1-andrew.smirnov@gmail.com> MIME-Version: 1.0 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:400e:c05::241 Subject: [Qemu-devel] [PATCH v2 14/15] sd: Check for READ_MULTIPLE_BLOCK size limit violation first 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: Peter Maydell , Andrey Smirnov , Jason Wang , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , qemu-devel@nongnu.org, yurovsky@gmail.com Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP Check for READ_MULTIPLE_BLOCK size limit violation first as opposed to doing at the end of the command handler. Consider the following scenario: Emulated host driver is trying to read last byte of the last sector via CMD18/ADMA, so what would happen is the following: 1. "ret" is filled with desired byte and "sd->data_offset" is incremented to 512 by ret = sd->data[sd->data_offset ++]; 2. sd->data_offset >= io_len becomes true, so sd->data_start += io_len; moves "sd->data_start" past valid data boundaries. 3. as a result "sd->data_start + io_len > sd->size" check becomes true and sd->card_status is marked with ADDRESS_ERROR, telling emulated host that the last CMD18 read failed, despite nothing bad/illegal happening. To avoid having this false positive, move out-of-bounds check to happen before BLK_READ_BLOCK(), so this way it will only trigger if illegal read is truly about to happen. Cc: Peter Maydell Cc: Jason Wang Cc: Philippe Mathieu-Daudé Cc: qemu-devel@nongnu.org Cc: qemu-arm@nongnu.org Cc: yurovsky@gmail.com Signed-off-by: Andrey Smirnov --- hw/sd/sd.c | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/hw/sd/sd.c b/hw/sd/sd.c index ba47bff4db..ce4ef17be3 100644 --- a/hw/sd/sd.c +++ b/hw/sd/sd.c @@ -1797,8 +1797,17 @@ uint8_t sd_read_data(SDState *sd) break; case 18: /* CMD18: READ_MULTIPLE_BLOCK */ - if (sd->data_offset == 0) + if (sd->data_offset == 0) { + if (sd->data_start + io_len > sd->size) { + qemu_log_mask(LOG_GUEST_ERROR, + "%s: Trying to read past card's capacity\n", + __func__); + sd->card_status |= ADDRESS_ERROR; + return 0x00; + } + BLK_READ_BLOCK(sd->data_start, io_len); + } ret = sd->data[sd->data_offset ++]; if (sd->data_offset >= io_len) { @@ -1812,11 +1821,6 @@ uint8_t sd_read_data(SDState *sd) break; } } - - if (sd->data_start + io_len > sd->size) { - sd->card_status |= ADDRESS_ERROR; - break; - } } break;