From nobody Wed Apr 16 07:39:13 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1499448050181529.1654770796379; Fri, 7 Jul 2017 10:20:50 -0700 (PDT) Received: from localhost ([::1]:57749 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dTWvw-0006ag-TA for importer@patchew.org; Fri, 07 Jul 2017 13:20:48 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:41517) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dTWl5-0004sX-A5 for qemu-devel@nongnu.org; Fri, 07 Jul 2017 13:09:36 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dTWl0-0007j3-Ty for qemu-devel@nongnu.org; Fri, 07 Jul 2017 13:09:35 -0400 Received: from mx1.redhat.com ([209.132.183.28]:56510) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1dTWky-0007gg-6z; Fri, 07 Jul 2017 13:09:28 -0400 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 3BCD7C0587CD; Fri, 7 Jul 2017 17:09:27 +0000 (UTC) Received: from noname.redhat.com (ovpn-117-34.ams2.redhat.com [10.36.117.34]) by smtp.corp.redhat.com (Postfix) with ESMTP id 47EE9627DB; Fri, 7 Jul 2017 17:09:26 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 3BCD7C0587CD Authentication-Results: ext-mx08.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx08.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=kwolf@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 3BCD7C0587CD From: Kevin Wolf To: qemu-block@nongnu.org Date: Fri, 7 Jul 2017 19:07:30 +0200 Message-Id: <1499447335-6125-16-git-send-email-kwolf@redhat.com> In-Reply-To: <1499447335-6125-1-git-send-email-kwolf@redhat.com> References: <1499447335-6125-1-git-send-email-kwolf@redhat.com> MIME-Version: 1.0 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.32]); Fri, 07 Jul 2017 17:09:27 +0000 (UTC) Content-Transfer-Encoding: quoted-printable 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] [PULL 015/100] vvfat: limit number of entries in root directory in FAT12/FAT16 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: kwolf@redhat.com, qemu-devel@nongnu.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" From: Herv=C3=A9 Poussineau FAT12/FAT16 root directory is two sectors in size, which allows only 512 di= rectory entries. Prevent QEMU startup if too much files exist, instead of overflowing root d= irectory. Also introduce variable root_entries, which will be required for FAT32. Fixes: https://bugs.launchpad.net/qemu/+bug/1599539/comments/4 Signed-off-by: Herv=C3=A9 Poussineau Signed-off-by: Kevin Wolf --- block/vvfat.c | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/block/vvfat.c b/block/vvfat.c index 62f411b..dc9af01 100644 --- a/block/vvfat.c +++ b/block/vvfat.c @@ -337,8 +337,9 @@ typedef struct BDRVVVFATState { unsigned int cluster_size; unsigned int sectors_per_cluster; unsigned int sectors_per_fat; - unsigned int sectors_of_root_directory; uint32_t last_cluster_of_root_directory; + /* how many entries are available in root directory (0 for FAT32) */ + uint16_t root_entries; uint32_t sector_count; /* total number of sectors of the partition */ uint32_t cluster_count; /* total number of clusters of this partition = */ uint32_t max_fat_value; @@ -785,6 +786,12 @@ static int read_directory(BDRVVVFATState* s, int mappi= ng_index) int is_dot=3D!strcmp(entry->d_name,"."); int is_dotdot=3D!strcmp(entry->d_name,".."); =20 + if (first_cluster =3D=3D 0 && s->directory.next >=3D s->root_entri= es - 1) { + fprintf(stderr, "Too many entries in root directory\n"); + closedir(dir); + return -2; + } + if(first_cluster =3D=3D 0 && (is_dotdot || is_dot)) continue; =20 @@ -858,15 +865,15 @@ static int read_directory(BDRVVVFATState* s, int mapp= ing_index) memset(direntry,0,sizeof(direntry_t)); } =20 -/* TODO: if there are more entries, bootsector has to be adjusted! */ -#define ROOT_ENTRIES (0x02 * 0x10 * s->sectors_per_cluster) - if (mapping_index =3D=3D 0 && s->directory.next < ROOT_ENTRIES) { + if (s->fat_type !=3D 32 && + mapping_index =3D=3D 0 && + s->directory.next < s->root_entries) { /* root directory */ int cur =3D s->directory.next; - array_ensure_allocated(&(s->directory), ROOT_ENTRIES - 1); - s->directory.next =3D ROOT_ENTRIES; + array_ensure_allocated(&(s->directory), s->root_entries - 1); + s->directory.next =3D s->root_entries; memset(array_get(&(s->directory), cur), 0, - (ROOT_ENTRIES - cur) * sizeof(direntry_t)); + (s->root_entries - cur) * sizeof(direntry_t)); } =20 /* re-get the mapping, since s->mapping was possibly realloc()ed */ @@ -931,6 +938,8 @@ static int init_directories(BDRVVVFATState* s, /* Now build FAT, and write back information into directory */ init_fat(s); =20 + /* TODO: if there are more entries, bootsector has to be adjusted! */ + s->root_entries =3D 0x02 * 0x10 * s->sectors_per_cluster; s->cluster_count=3Dsector2cluster(s, s->sector_count); =20 mapping =3D array_get_next(&(s->mapping)); @@ -999,7 +1008,6 @@ static int init_directories(BDRVVVFATState* s, } =20 mapping =3D array_get(&(s->mapping), 0); - s->sectors_of_root_directory =3D mapping->end * s->sectors_per_cluster; s->last_cluster_of_root_directory =3D mapping->end; =20 /* the FAT signature */ @@ -1018,7 +1026,7 @@ static int init_directories(BDRVVVFATState* s, bootsector->sectors_per_cluster=3Ds->sectors_per_cluster; bootsector->reserved_sectors=3Dcpu_to_le16(1); bootsector->number_of_fats=3D0x2; /* number of FATs */ - bootsector->root_entries=3Dcpu_to_le16(s->sectors_of_root_directory*0x= 10); + bootsector->root_entries =3D cpu_to_le16(s->root_entries); bootsector->total_sectors16=3Ds->sector_count>0xffff?0:cpu_to_le16(s->= sector_count); /* media descriptor: hard disk=3D0xf8, floppy=3D0xf0 */ bootsector->media_type =3D (s->offset_to_bootsector > 0 ? 0xf8 : 0xf0); --=20 1.8.3.1