From nobody Tue Nov 18 14:10:27 2025 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=1611134018; cv=none; d=zohomail.com; s=zohoarc; b=DN15d3tlPghLn6t2iQvwbf5lJx6x5GNTobxJ9RfywlPsLgygvWiJar2IzLmBer1T+fcBmeHuf8EncrPfAzzATtmybXHgE6yHmPDBFkIqG0DvI9dMy5rXF+nsAYJdZWozxzYgaDLlBW9LXgX8L2M0EGqOuCTR9d2cYLzybbxrp3w= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1611134018; 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=IT0N/adDtZethcpXugOwi+ignahrpU/pPnxEwGs/cLU=; b=lpdJd8JC6WRyf4pWdgznLbn9MoePuZ6CotF1pUQKMnElg8GueNItU/B+wRtAFikU4CvkGz42tJQMlO+3eJDI91g8lXLgGI6adKMNdmI7Zyn9/s9HbPWZ8JQ+0OUzYRGh5fgUiV4ooTj4EkYAF9QeLjoT/aOC/k9l96RqKrbyIrA= 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 1611134018503476.36723706276564; Wed, 20 Jan 2021 01:13:38 -0800 (PST) Received: from localhost ([::1]:47026 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1l29YX-0000t4-C1 for importer@patchew.org; Wed, 20 Jan 2021 04:13:37 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:46278) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1l29XN-0008NW-Ko for qemu-devel@nongnu.org; Wed, 20 Jan 2021 04:12:25 -0500 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:43305) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1l29XE-0007uv-4w for qemu-devel@nongnu.org; Wed, 20 Jan 2021 04:12:24 -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-367-KkMEuRQpPSCUCJ2aG9rLSQ-1; Wed, 20 Jan 2021 04:12:11 -0500 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id D502F10052FE; Wed, 20 Jan 2021 09:12:10 +0000 (UTC) Received: from localhost.localdomain (unknown [10.74.9.143]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 45CFF5C8AB; Wed, 20 Jan 2021 09:12:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1611133934; 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=IT0N/adDtZethcpXugOwi+ignahrpU/pPnxEwGs/cLU=; b=bT2UX5b06eFEhTVoGAxfIJ35z8U6RzqU0vLMM7ZBRlsTn7gzYf/TQTqRw6y8Skfnoa/66T 4+jlbcf+nP+UxjT4cPSvSXiM50MZ80iII4GrKn+duE6Mu/qKz8r1MGaSZzYZn2v3edRtJ0 0aif83qS28OXMsEF/FAJRarq1WGKLuY= X-MC-Unique: KkMEuRQpPSCUCJ2aG9rLSQ-1 From: P J P To: =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= Subject: [PATCH v2] ide: set an upper limit to nb_sectors Date: Wed, 20 Jan 2021 14:40:02 +0530 Message-Id: <20210120091002.521931-1-ppandit@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 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.195, 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.001, RCVD_IN_MSPIKE_WL=0.001, 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: Paolo Bonzini , Prasad J Pandit , John Snow , QEMU Developers , Markus Armbruster 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 Set an upper limit to number of sectors on an IDE disk media. This is to ensure that logical block addresses (LBA) and nb_sector arguments remain within INT_MAX range. Suggested-by: Paolo Bonzini Signed-off-by: Prasad J Pandit --- hw/ide/core.c | 27 ++++++++++++++++----------- include/hw/ide/internal.h | 1 + 2 files changed, 17 insertions(+), 11 deletions(-) Update v2: add explanatory comment, define macro -> https://lists.gnu.org/archive/html/qemu-devel/2021-01/msg04610.html Update v1: limit s->nb_sectors count -> https://lists.gnu.org/archive/html/qemu-devel/2021-01/msg04270.html -> https://lists.gnu.org/archive/html/qemu-devel/2021-01/msg04173.html diff --git a/hw/ide/core.c b/hw/ide/core.c index b49e4cfbc6..902f51423d 100644 --- a/hw/ide/core.c +++ b/hw/ide/core.c @@ -1161,15 +1161,25 @@ static void ide_cfata_metadata_write(IDEState *s) s->nsector << 9), 0x200 - 2)); } +static void ide_set_nb_sectors(IDEState *s) +{ + uint64_t nb_sectors; + blk_get_geometry(s->blk, &nb_sectors); + + /* + * Restrict total number of addressable sectors to (INT_MAX << 2), + * so that logical addresses (LBA) stay within INT_MAX limit. + */ + s->nb_sectors =3D MIN(nb_sectors, IDE_MAX_NB_SECTORS); +} + /* called when the inserted state of the media has changed */ static void ide_cd_change_cb(void *opaque, bool load, Error **errp) { IDEState *s =3D opaque; - uint64_t nb_sectors; s->tray_open =3D !load; - blk_get_geometry(s->blk, &nb_sectors); - s->nb_sectors =3D nb_sectors; + ide_set_nb_sectors(s); /* * First indicate to the guest that a CD has been removed. That's @@ -2475,14 +2485,12 @@ static bool ide_cd_is_medium_locked(void *opaque) static void ide_resize_cb(void *opaque) { IDEState *s =3D opaque; - uint64_t nb_sectors; if (!s->identify_set) { return; } - blk_get_geometry(s->blk, &nb_sectors); - s->nb_sectors =3D nb_sectors; + ide_set_nb_sectors(s); /* Update the identify data buffer. */ if (s->drive_kind =3D=3D IDE_CFATA) { @@ -2511,17 +2519,14 @@ int ide_init_drive(IDEState *s, BlockBackend *blk, = IDEDriveKind kind, uint32_t cylinders, uint32_t heads, uint32_t secs, int chs_trans, Error **errp) { - uint64_t nb_sectors; - s->blk =3D blk; s->drive_kind =3D kind; - - blk_get_geometry(blk, &nb_sectors); s->cylinders =3D cylinders; s->heads =3D heads; s->sectors =3D secs; s->chs_trans =3D chs_trans; - s->nb_sectors =3D nb_sectors; + ide_set_nb_sectors(s); + s->wwn =3D wwn; /* The SMART values should be preserved across power cycles but they aren't. */ diff --git a/include/hw/ide/internal.h b/include/hw/ide/internal.h index 2d09162eeb..7c436def5b 100644 --- a/include/hw/ide/internal.h +++ b/include/hw/ide/internal.h @@ -219,6 +219,7 @@ OBJECT_DECLARE_SIMPLE_TYPE(IDEBus, IDE_BUS) /* set to 1 set disable mult support */ #define MAX_MULT_SECTORS 16 +#define IDE_MAX_NB_SECTORS ((uint64_t)INT_MAX << 2) #define IDE_DMA_BUF_SECTORS 256 /* feature values for Data Set Management */ -- 2.29.2