From nobody Wed May 8 10:08:44 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1610970907; cv=none; d=zohomail.com; s=zohoarc; b=ByGfxdOq0JL91uF+IydUYK86vY3Hm0HzK1Tz8Om6yCVyBh2jNRjIhhZK2N0Pa9QOtJWTcevmoKGguGlVbnIq5hMuU+l9A405jcyc1H2K9ekiFL1l+MjI8IVjcpMDeBF8UFrpToSOKWUBodnGhHPNoaOnXjDC7oSVcjRtMVl5H6Q= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1610970907; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:To; bh=t02qZHqElruEje9H2HUQ//DZ2O8BhzliustNfGhNgpA=; b=iUloyOp4YUUoDMBgmSN9iGvKkNl5wC5o6WQbB3Cc2ctbl2AfJEcbvkV2Z47cbA8nQ8iBRAeYB/iWoWU0ef2pGisMdg8ZVVH9gPaj7D5F4NGrQYlvSCFr048pJAw6VsTqzh0ZtrpjgYI8JSmhqGNYn+ZamDvCwjsNqGWEIZ4ju6Y= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1610970907873274.1262429880326; Mon, 18 Jan 2021 03:55:07 -0800 (PST) Received: from localhost ([::1]:37092 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1l1T7i-00067R-Qb for importer@patchew.org; Mon, 18 Jan 2021 06:55:06 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:35588) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1l1T6l-0005gd-GV for qemu-devel@nongnu.org; Mon, 18 Jan 2021 06:54:07 -0500 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:26401) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1l1T6g-0001YQ-SN for qemu-devel@nongnu.org; Mon, 18 Jan 2021 06:54:07 -0500 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-517-O2LGETIFN8yZicihVcQLZQ-1; Mon, 18 Jan 2021 06:53:45 -0500 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id ADF7C10054FF; Mon, 18 Jan 2021 11:53:42 +0000 (UTC) Received: from localhost.localdomain (unknown [10.40.192.71]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 081B95D9D3; Mon, 18 Jan 2021 11:53:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1610970841; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=t02qZHqElruEje9H2HUQ//DZ2O8BhzliustNfGhNgpA=; b=FJ/abliwzixxXwYLVVy2OJc4ctkGGzZXuweYO5GUb7oF9o8B28Hi30+/xhwy98H5/nFoZo zESk+UF+BOP9VHD1ZW4HJ8DulBX4GypgkEJkIVi+kxf+lrlY3JN8BGtWKxcYEydWg2gEta a2QHlVoNRM/mO38+PqPgf62osfjShTc= X-MC-Unique: O2LGETIFN8yZicihVcQLZQ-1 From: P J P To: Paolo Bonzini Subject: [PATCH v3] ide: atapi: check logical block address and read size (CVE-2020-29443) Date: Mon, 18 Jan 2021 17:21:30 +0530 Message-Id: <20210118115130.457044-1-ppandit@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=ppandit@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=216.205.24.124; envelope-from=ppandit@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -29 X-Spam_score: -3.0 X-Spam_bar: --- X-Spam_report: (-3.0 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.189, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H3=-0.01, RCVD_IN_MSPIKE_WL=-0.01, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Peter Maydell , Prasad J Pandit , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , QEMU Developers , Markus Armbruster , Wenxiang Qian , John Snow Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" From: Prasad J Pandit While processing ATAPI cmd_read/cmd_read_cd commands, Logical Block Address (LBA) maybe invalid OR closer to the last block, leading to an OOB access issues. Add range check to avoid it. Fixes: CVE-2020-29443 Reported-by: Wenxiang Qian Suggested-by: Paolo Bonzini Reviewed-by: Paolo Bonzini Signed-off-by: Prasad J Pandit --- hw/ide/atapi.c | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) Update v3: Fix space typo/error, add Reviewed-by tag -> https://lists.gnu.org/archive/html/qemu-devel/2021-01/msg04159.html -> https://lists.gnu.org/archive/html/qemu-devel/2021-01/msg04173.html diff --git a/hw/ide/atapi.c b/hw/ide/atapi.c index e79157863f..b626199e3d 100644 --- a/hw/ide/atapi.c +++ b/hw/ide/atapi.c @@ -322,6 +322,8 @@ static void ide_atapi_cmd_reply(IDEState *s, int size, = int max_size) static void ide_atapi_cmd_read_pio(IDEState *s, int lba, int nb_sectors, int sector_size) { + assert(0 <=3D lba && lba < (s->nb_sectors >> 2)); + s->lba =3D lba; s->packet_transfer_size =3D nb_sectors * sector_size; s->elementary_transfer_size =3D 0; @@ -420,6 +422,8 @@ eot: static void ide_atapi_cmd_read_dma(IDEState *s, int lba, int nb_sectors, int sector_size) { + assert(0 <=3D lba && lba < (s->nb_sectors >> 2)); + s->lba =3D lba; s->packet_transfer_size =3D nb_sectors * sector_size; s->io_buffer_size =3D 0; @@ -973,35 +977,49 @@ static void cmd_prevent_allow_medium_removal(IDEState= *s, uint8_t* buf) static void cmd_read(IDEState *s, uint8_t* buf) { - int nb_sectors, lba; + unsigned int nb_sectors, lba; + + /* Total logical sectors of ATAPI_SECTOR_SIZE(=3D2048) bytes */ + uint64_t total_sectors =3D s->nb_sectors >> 2; if (buf[0] =3D=3D GPCMD_READ_10) { nb_sectors =3D lduw_be_p(buf + 7); } else { nb_sectors =3D ldl_be_p(buf + 6); } - - lba =3D ldl_be_p(buf + 2); if (nb_sectors =3D=3D 0) { ide_atapi_cmd_ok(s); return; } + lba =3D ldl_be_p(buf + 2); + if (lba >=3D total_sectors || lba + nb_sectors - 1 >=3D total_sectors)= { + ide_atapi_cmd_error(s, ILLEGAL_REQUEST, ASC_LOGICAL_BLOCK_OOR); + return; + } + ide_atapi_cmd_read(s, lba, nb_sectors, 2048); } static void cmd_read_cd(IDEState *s, uint8_t* buf) { - int nb_sectors, lba, transfer_request; + unsigned int nb_sectors, lba, transfer_request; + + /* Total logical sectors of ATAPI_SECTOR_SIZE(=3D2048) bytes */ + uint64_t total_sectors =3D s->nb_sectors >> 2; nb_sectors =3D (buf[6] << 16) | (buf[7] << 8) | buf[8]; - lba =3D ldl_be_p(buf + 2); - if (nb_sectors =3D=3D 0) { ide_atapi_cmd_ok(s); return; } + lba =3D ldl_be_p(buf + 2); + if (lba >=3D total_sectors || lba + nb_sectors - 1 >=3D total_sectors)= { + ide_atapi_cmd_error(s, ILLEGAL_REQUEST, ASC_LOGICAL_BLOCK_OOR); + return; + } + transfer_request =3D buf[9] & 0xf8; if (transfer_request =3D=3D 0x00) { /* nothing */ -- 2.29.2