From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.128.177 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796434297; b=MNqcBlooPH2MI7AvdjClrhvXUCuzsuAZMIaTTFC059Q3KwMAgPbbC4AlFr8DTcwm8BVD aQCjHBCuMl+EJvMRu9zAZvXD+Cmtzoz570L1WCFRxVuGk8ILXS9V6n1YtQWoOTrqjgzQk rPGG6IUuss5QJZNCELKTK8bEaphvxSVFylj/7nNOWxPpNGoBbybpO5Oe9IzdLpoyFhrEM rK0Y3Nhtk+pQZE1lB/kk4KSSTzQrYJ7tnpoavJVpmbH4IzKhuPKTm9pTu0HCdP8Ql+gx3 Mgimp7+uACjS+Uka96G1p15IumnWtLrOQWRY87+eEPnJnWx9NFluRLVmabohMsET29A== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796434297; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: From:To:Cc:Subject:Date:Message-ID:X-Mailer:In-Reply-To:References: MIME-Version:Content-Transfer-Encoding; bh=vcvJe7FGV+n1CigkPtA79+sMUI6y3AsIHYDUwl5siKA=; b=2iN29RZsNbOVJLo36RnPCG364fI5vZAYd6PFgWvJZKmqcsbX6uyroyY3gnIa6bBOErAr UGSfJa5v6f/supeGZSm90x0tsdv58eliy6jLQaUo6POALh+52neDrEPoqWViOr4V6xodE jmxBy7koVDSPnK+64aGuDoky16PA7OFkXysgOWi51Yp8pCxCG63m3zIVrS+VEQSbrdHEA xTaWC+pNwnue1rk6Cdac0/nr/7rcvyGWTyTLgEXeFBuUeypDqFnC/o6Sd83YE7NNwO63W 1gGOA0ojSGx9vq7AL2nYhQsowT57St4PqBzQoWq4DVP5qfSKVoiNhpK1lstM6vUbF5w== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none header.from=dubeyko.com; spf=pass smtp.mailfrom=dubeyko.com; dkim=pass header.d=dubeyko-com.20260707.gappssmtp.com header.i=@dubeyko-com.20260707.gappssmtp.com header.a=rsa-sha256 header.s=20260707 header.b=tCJEiUrD; arc=none smtp.remote-ip=209.85.128.177 Received: from mail-yw1-f177.google.com (mail-yw1-f177.google.com [209.85.128.177]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id F2995606A322 for ; Sat, 5 Dec 2026 01:31:36 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org F2995606A322 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=dubeyko.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=dubeyko.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=dubeyko-com.20260707.gappssmtp.com header.i=@dubeyko-com.20260707.gappssmtp.com header.a=rsa-sha256 header.s=20260707 header.b=tCJEiUrD Received: by mail-yw1-f177.google.com with SMTP id 00721157ae682-8b0d126f631so14000667b3.2 for ; Fri, 04 Dec 2026 17:31:36 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=dubeyko-com.20260707.gappssmtp.com; s=20260707; t=1796434296; x=1797039096; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=vcvJe7FGV+n1CigkPtA79+sMUI6y3AsIHYDUwl5siKA=; b=tCJEiUrDfSMOf5w3Lqc208WSmJW93iTY6njCK94V/UQvRlW6QMTmMlMQT1l90FV1Mf uteHWnuGGblUqoba9WStZrqWP3YnvVVbaNudJy0GwWAjqqPQpVlQQlcb+cgUIPbVSpPO Neiitb9a43wyuxzrhiaqrTu3ri79W2NoMbHBJDez51DFm51SFd1cbaTtiovZSh+pdsdV CPcTUu2erkN0eOV+UxOPWxYdGWiYFiZ6HDliZpXjDKK06uKYRTG5RrnCi0syp7wuKssq yNeisbPTxf0iL1LmWOEZ6WD9ZZZ4C06rp1TEc7N9EaltVObSplf4IYdjm/ALhXtlmgZL YbrQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796434296; x=1797039096; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=vcvJe7FGV+n1CigkPtA79+sMUI6y3AsIHYDUwl5siKA=; b=Y1za9VPA87U96GHBrM1uffIrtvjQmSz5O4tYkWJzbGf3yzH1/GUPzMJ3/Dj4WhyAn2 VRyWh1KpCOzdwXzNJKqDJqvWxmiLkIpvfK1Xap/opKzN6NBZo59Ig90fCwSC69DxtbSg yJCksqNaObk/9KL+9d8Lz+NjkzlhFan07QKVvJnwI9E9E5WE4Ubao4Y/wcpZyKVS004v UJOwfYzZ5biY1PdxxIitr+kW5D4hgwc+kzVMhZJCoZWwNTOTtdDB0lcu/pPMNHys97lh WX7tGfKiENg1DEiBPDKmzsGuR4sLU3Nqur+wAY/DD6elwbZ1z+PRP38YPw/kvR/hWl5i skew== X-Forwarded-Encrypted: i=1; AL+hErH20mEBuRZf8MEXG6NaiPkG1OGXo3mUdnpMeyS831tOguB9jOUWNV10haUHJtEnhWSEhMPCOdBU6towb6c=@vger.kernel.org X-Gm-Message-State: AB4q+uFCA3UJRD/EfX3JqMDoZ+wwSUk3MmDKZRh718ifoeQV9ha2u1YZ QPxJJfkvxNwq/XU0LusHj7uuVfYo62GBW+9EBO7Z3Mz6OOrfypVRjrgJq3+P1ThnRgY= X-Gm-Gg: AZFdTZc18ZCs6oOvf4fzIMoKt1BZ8skByKuF0BJVz+/Or1pO0rT5s0EAefMJNAB1mBH L99BG395a//EkZSdMzw/PuBMS0Bi/0mgb44HkRud2xx/6NI4NIoupncgI8VFsZBDOzIce1wf1LZ MNjNa2Jbg68/NGBgaCJ1lHwV/0Y5828o26058lpqQmtBKntMxzMmpX+HIM8FOigKv5+1ZihzH0S CeQZaq8LkfPyz1cWuxYDq/gLtY41z0cih6ADZXZ7agVmKwFcNs+tqpvqcVVH8Or9AYE3BxFH14J on6K7HxP676X1E34pazJEiC44rknOQU9xyAk6kr9IGFAeuijq94oFz+zFoAOaxG1vcJUJhT8d+6 YMIS04e5tQ87uKkWNrCWA3pFL3IgxbQB3Yj4i4qKysUHItupa4WK/yoh9Syx9JfPNVJ2SejzZ7c 3RXRX5zlOUXneFXp9ZcGQNatmVdZJCg7PJIc7rmho7lYlefLyheRUmEPcWkQNfkMY1OYQYmSGqA j73Ubf2AnjWJfBUSFDogdGgwajw7la9A35W+OZulvuSGR5GTQFAtiI0jYKSjmoqlcUklBnSE1w0 6rOx4pcewXKaWmvnaWKq19wEqS5acqFN3MAyRKHGidvlalkBNSd93gLdKoG5eZBAY+O4w4eZdaX 5VsqQzHF1ei/5rl8b/76wNAHy/T0jYUbSCjR3Vs01uQxtr3Wy8p9i/VaHZ+b/vRG3+qWu94U= X-Received: by 2002:a05:690c:f8f:b0:8b4:c2c4:bda1 with SMTP id 00721157ae682-8b660e070eamr21677937b3.67.1796434296077; Fri, 04 Dec 2026 17:31:36 -0800 (PST) Received: from system76-laptop.attlocal.net ([2600:1700:6476:1430:d9f4:eaa9:479c:5d66]) by smtp.gmail.com with ESMTPSA id 00721157ae682-8b660e183acsm17228137b3.45.2026.12.04.17.31.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Dec 2026 17:31:35 -0800 (PST) From: Viacheslav Dubeyko To: glaubitz@physik.fu-berlin.de, frank.li@vivo.com Cc: linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, vdubeyko@coreweave.com, Viacheslav Dubeyko , Aymen T , stable@vger.kernel.org Subject: [PATCH 2/6] hfsplus: validate the HFS wrapper before following it Date: Fri, 4 Dec 2026 17:31:23 -0800 Message-ID: <20261205013127.1533652-3-slava@dubeyko.com> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20261205013127.1533652-1-slava@dubeyko.com> References: <20261205013127.1533652-1-slava@dubeyko.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit For an HFS wrapper, hfsplus_read_wrapper() moves part_start to the embedded HFS+ volume by drAlBlSt + the start of the embedded extent. hfsplus_read_mdb() only checks the embedded signature, two attribute bits and the allocation block size, so the location of the embedded volume is used as it is on disk, even if it points back to the wrapper MDB itself or beyond the wrapper. The volume bitmap of the wrapper follows its MDB, the allocation blocks follow the volume bitmap, and the embedded volume occupies some of the allocation blocks. Check this in hfsplus_read_mdb(): the volume bitmap must start after the MDB, the first allocation block must be after the volume bitmap, and the embedded extent must be non-empty, lie within the allocation blocks and end within the wrapper. Then the embedded volume always starts beyond the wrapper MDB. Also check that the embedded volume is the one the wrapper declares: its volume header must have the signature of drEmbedSigWord, and the volume must fit in the embedded extent. Reported-by: Aymen T Link: https://lore.kernel.org/linux-fsdevel/45bfa4d7-e9e2-45f2-9e12-a794defae64a@gmail.com/ Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2") Signed-off-by: Viacheslav Dubeyko Cc: stable@vger.kernel.org cc: John Paul Adrian Glaubitz cc: Yangtao Li cc: linux-fsdevel@vger.kernel.org --- fs/hfsplus/wrapper.c | 96 ++++++++++++++++++++++++++++++++++++++------ 1 file changed, 84 insertions(+), 12 deletions(-) diff --git a/fs/hfsplus/wrapper.c b/fs/hfsplus/wrapper.c index 33dcdcbe5761..71122cb2265e 100644 --- a/fs/hfsplus/wrapper.c +++ b/fs/hfsplus/wrapper.c @@ -17,7 +17,28 @@ #include "hfsplus_fs.h" #include "hfsplus_raw.h" +/** + * struct hfsplus_wd - location of an HFS+ volume embedded in an HFS wrapper + * @embed_sig: signature of the embedded volume (drEmbedSigWord), in on-disk + * byte order. It must be HFSPLUS_VOLHEAD_SIG or HFSPLUS_VOLHEAD_SIGX, + * and the embedded volume header must carry the same signature. + * @ablk_size: size of an allocation block of the wrapper (drAlBlkSiz). + * hfsplus_read_mdb() stores it in bytes, and hfsplus_read_wrapper() + * converts it in place into 512-byte sectors before using it. + * @ablk_start: first sector of allocation block 0 of the wrapper (drAlBlSt), + * in 512-byte sectors from the start of the wrapper. + * @embed_start: first allocation block of the embedded volume + * (drEmbedExtent.xdrStABN). + * @embed_count: number of allocation blocks of the embedded volume + * (drEmbedExtent.xdrNumABlks). + * + * hfsplus_read_mdb() fills it in from the MDB of the wrapper. With + * @ablk_size in sectors, the embedded volume starts at sector + * @ablk_start + @embed_start * @ablk_size of the wrapper and is + * @embed_count * @ablk_size sectors long. + */ struct hfsplus_wd { + __be16 embed_sig; u32 ablk_size; u16 ablk_start; u16 embed_start; @@ -63,35 +84,69 @@ int hfsplus_submit_bio(struct super_block *sb, sector_t sector, return bdev_rw_virt(sb->s_bdev, sector, buf, io_size, opf); } -static int hfsplus_read_mdb(void *bufptr, struct hfsplus_wd *wd) +/* + * Check the HFS wrapper MDB in @bufptr and get the location of the + * embedded HFS+ volume from it. + * + * The volume bitmap of the wrapper follows its MDB, the allocation blocks + * follow the volume bitmap, and the embedded volume occupies some of the + * allocation blocks. So, the embedded volume starts beyond the MDB and + * must end within the wrapper, which is @part_size sectors long. + */ +static int hfsplus_read_mdb(void *bufptr, sector_t part_size, + struct hfsplus_wd *wd) { + struct hfs_mdb *mdb = bufptr; + u32 nr_ablks, vbm_start, vbm_sects; + u64 embed_end; u32 extent; u16 attrib; - __be16 sig; - sig = *(__be16 *)(bufptr + HFSP_WRAPOFF_EMBEDSIG); - if (sig != cpu_to_be16(HFSPLUS_VOLHEAD_SIG) && - sig != cpu_to_be16(HFSPLUS_VOLHEAD_SIGX)) + wd->embed_sig = mdb->drEmbedSigWord; + if (wd->embed_sig != cpu_to_be16(HFSPLUS_VOLHEAD_SIG) && + wd->embed_sig != cpu_to_be16(HFSPLUS_VOLHEAD_SIGX)) return 0; - attrib = be16_to_cpu(*(__be16 *)(bufptr + HFSP_WRAPOFF_ATTRIB)); + attrib = be16_to_cpu(mdb->drAtrb); if (!(attrib & HFSP_WRAP_ATTRIB_SLOCK) || !(attrib & HFSP_WRAP_ATTRIB_SPARED)) return 0; - wd->ablk_size = - be32_to_cpu(*(__be32 *)(bufptr + HFSP_WRAPOFF_ABLKSIZE)); + wd->ablk_size = be32_to_cpu(mdb->drAlBlkSiz); if (wd->ablk_size < HFSPLUS_SECTOR_SIZE) return 0; if (wd->ablk_size % HFSPLUS_SECTOR_SIZE) return 0; - wd->ablk_start = - be16_to_cpu(*(__be16 *)(bufptr + HFSP_WRAPOFF_ABLKSTART)); + wd->ablk_start = be16_to_cpu(mdb->drAlBlSt); - extent = get_unaligned_be32(bufptr + HFSP_WRAPOFF_EMBEDEXT); + extent = get_unaligned_be32(&mdb->drEmbedExtent); wd->embed_start = (extent >> 16) & 0xFFFF; wd->embed_count = extent & 0xFFFF; + nr_ablks = be16_to_cpu(mdb->drNmAlBlks); + vbm_start = be16_to_cpu(mdb->drVBMSt); + vbm_sects = DIV_ROUND_UP(nr_ablks, HFSPLUS_SECTOR_SIZE * BITS_PER_BYTE); + + /* The MDB is in the same sector as the HFS+ volume header. */ + if (vbm_start <= HFSPLUS_VOLHEAD_SECTOR || + wd->ablk_start < vbm_start + vbm_sects) { + pr_err("invalid HFS wrapper layout\n"); + return 0; + } + + embed_end = (u64)wd->embed_start + wd->embed_count; + if (!wd->embed_count || embed_end > nr_ablks) { + pr_err("invalid HFS wrapper embedded extent\n"); + return 0; + } + + embed_end *= wd->ablk_size >> HFSPLUS_SECTOR_SHIFT; + embed_end += wd->ablk_start; + if (embed_end > part_size) { + pr_err("HFS wrapper embedded volume is beyond the wrapper\n"); + return 0; + } + return 1; } @@ -208,7 +263,7 @@ int hfsplus_read_wrapper(struct super_block *sb) } if (sbi->s_vhdr->signature == cpu_to_be16(HFSP_WRAP_MAGIC)) { - if (!hfsplus_read_mdb(sbi->s_vhdr, &wd)) + if (!hfsplus_read_mdb(sbi->s_vhdr, part_size, &wd)) goto out_free_backup_vhdr; wd.ablk_size >>= HFSPLUS_SECTOR_SHIFT; part_start += (sector_t)wd.ablk_start + @@ -219,6 +274,23 @@ int hfsplus_read_wrapper(struct super_block *sb) if (error) goto out_free_backup_vhdr; error = -EINVAL; + + /* + * The embedded volume must be the volume that the wrapper + * declares, and it must fit in the embedded extent. + */ + if (sbi->s_vhdr->signature != wd.embed_sig) { + pr_err("HFS wrapper embedded volume has signature 0x%04x, not 0x%04x\n", + be16_to_cpu(sbi->s_vhdr->signature), + be16_to_cpu(wd.embed_sig)); + goto out_free_backup_vhdr; + } + if ((u64)be32_to_cpu(sbi->s_vhdr->total_blocks) * + be32_to_cpu(sbi->s_vhdr->blocksize) > + (u64)part_size << HFSPLUS_SECTOR_SHIFT) { + pr_err("HFS wrapper embedded volume is larger than its extent\n"); + goto out_free_backup_vhdr; + } } /* -- 2.53.0