From nobody Sun May 5 16:49:53 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=1610951738; cv=none; d=zohomail.com; s=zohoarc; b=bfsKElWlzc/y+CpCZEc4eVupDVMz8zOsEKpv0upK0LwzRLYuCLtO6KZXV/bds6NZO3BlzexgIw1sgi4L0oqACdqAUzO1piXLe+7YqnnXtJRF8pjOb1jHX1l0jA2EGqbZu3U8SuVhWXvk5xmG92zPcDSeNdX53XWnm1Lx/D+cwEQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1610951738; 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=qL7iuHZzt0H83Hek19HhIBXs9JEYYGdBc/ZVM+dCu0o=; b=lQjBQSrjATQoDiWAvrDQwGs/gdixvkd7Kh++RXgkZsgC6A7bOauaWv5jE08IUdDF4ib0c2Y7zgIjaXmAV1f2LE/PGsfglXYvlO2JRo5n58dCQ/l3LGy/bfFJenVh7+vKiBSDuKyumXp5SqN6YCBJ6QRSiN2tleZpthpBytKQqpM= 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 1610951738442371.85770663216374; Sun, 17 Jan 2021 22:35:38 -0800 (PST) Received: from localhost ([::1]:57930 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1l1O8W-0000q6-LT for importer@patchew.org; Mon, 18 Jan 2021 01:35:36 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:33084) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1l1O7n-0000RA-Oy for qemu-devel@nongnu.org; Mon, 18 Jan 2021 01:34:51 -0500 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:27490) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1l1O7j-0005cL-L9 for qemu-devel@nongnu.org; Mon, 18 Jan 2021 01:34:50 -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-151-j2n1-jhcPn2sSvOba_cqtQ-1; Mon, 18 Jan 2021 01:34:41 -0500 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 9B2C7107ACE3; Mon, 18 Jan 2021 06:34:40 +0000 (UTC) Received: from localhost.localdomain (unknown [10.40.192.71]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 41FD1620D7; Mon, 18 Jan 2021 06:34:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1610951684; 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=qL7iuHZzt0H83Hek19HhIBXs9JEYYGdBc/ZVM+dCu0o=; b=g8WrzDx2AHrrUeZTQ8fPnJ/NIAu92yYvp+vWK1rWTFNVzJlBrJH6PYwOewnz+JPUQG8a6h 6vFhoxkUGIQpKqxloxrM3435SO4tqK6LD5SBcnjPTvEGfGoFiUq8tvZD5OjRpvZ74EHANL dm8Hb7ZcP4KzBKvEx8ucJO/yPTjpMOU= X-MC-Unique: j2n1-jhcPn2sSvOba_cqtQ-1 From: P J P To: Paolo Bonzini Subject: [PATCH v2] ide: atapi: check logical block address and read size (CVE-2020-29443) Date: Mon, 18 Jan 2021 12:02:29 +0530 Message-Id: <20210118063229.442350-1-ppandit@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 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 Fix-suggested-by: Paolo Bonzini Signed-off-by: Prasad J Pandit Reviewed-by: Paolo Bonzini --- hw/ide/atapi.c | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) Update v2: range check lba value early in cmd_read[_cd] routines -> https://lists.gnu.org/archive/html/qemu-devel/2020-12/msg00151.html diff --git a/hw/ide/atapi.c b/hw/ide/atapi.c index e79157863f..35b8494dc8 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