Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.128.170 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796434297; b=UyRj7dSzuEjurGdYfVmOHtQ4Vi492clvoClBeubjsKwVXBfnByuyZUrWVqK5P00Jlj8e Qs61Iywqu4JJ2L8QoD25wPKyayKDR1X8J85aQmb3nAdgYlHiVPf/gajbqujl65rDYeW8+ FVyUlJ4HMw0Zm1n72xFoB3vrQY8M3FKQt1xv97pKf1usdarRuaBeTQHitWS2StLDqn/e9 pB7mOWlCbBW3Vzu0yg9hn5DA/AbTNm2sTLvRziSqAAyGSC6c3Kl8af2+jGtI2d0zM2rDA AuPV7mNR2hmH9udsALnHXnTmHkzKePRbEhZgck1E8UljSTRXrDVHyd8e5z4u0i6kosg== 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-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=OOFPYglAbudazdUHta/ZAi+1mcrURWcL7x5nyu0GS+SZ015k4Gd7DV4D5o/chB185+pi zqyTgotmgjrcPlFJ2v+Skzgtc7nUVxTK2FpKr2boB4tJQHOuVmw/EQhUna+15lEmbX3pJ cNGNBZN5Y1kATOGUECEholU3cri1hrziAzCYeUZmpqmpRH4GpfLU1ZKTV7vfnzj+kyHJ1 Twg2zkJ3QQ8zeGdifW2hUWew2bQVQL8cOyie0dWGpHvI5SSEunjnzK8zlqCnwN9aUsOCP mPsh9fdNCaf+fKT2964I+S/T2gFgr+W2kzUZgZNXuUECGihddznDqFxyWtlaDHRd1Kg== 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.170 Received: from mail-yw1-f170.google.com (mail-yw1-f170.google.com [209.85.128.170]) (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 00C4F606A682 for ; Sat, 5 Dec 2026 01:31:36 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 00C4F606A682 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-f170.google.com with SMTP id 00721157ae682-8b4efdc8c20so10447197b3.1 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=NK47M9se3uJjhvHR1CE+7lz+f12GZ2BJYbnUAF0jwxbvBwK4WE/ZMUQYenTwIbzevP wUT50OoeapjSX2e5SxpzD5NRs1loP3Npap8qj4IEscE//7Q8IPGd9amLiBOksBpEMtGO IQhXV96kQ7/TjX6PVKPx/UcgMtbV5Uu7qursN1CUFsbDg2hCRoM1x9IA+j0Cz1ltgJ9F seTh4LBUUt71um2ugQAQlep39PdtLbtVgOInN4Mr17WxYpR7rfkNhkvnpK1qlbUEJiiv ClxyojzP6H3yh5UefKO8Ty+B+RMfRer8tiNowNLNkQx2EcuTJtCYJE9A9bbrWfOfkscz NZxw== X-Gm-Message-State: AB4q+uG/PWpYAf7H+KNqJUgVLA6PXtVXVlmPxt6/L+mr0ZtWgwKJVZRO AQNzaQDR8QbU6ST5pg5bqcwk2nWY5l7ZJe2dZcKeOYWcjokE22WSUJf1eDvYIypDg/w= X-Gm-Gg: AZFdTZf2ikxxZhuTGGilPooAPyjW46hfuKv1P4laDfxUNu/V/yDo4furLPYOFDERQKM kQoe1Oc5TdMMk144inkp83cipdV0dzCDIT/ZjSyoEEAlqPH6fwhNCx3NI/8pmsLdjPnnbE9QBwh FRGx9vJRn0ae4+EB5wHkPPmWN5vZ8hpT1w56mBZY9e3UKheYNEcvWdk9zpqLjz3f7s4nb7B4DCi OWUqUHxS1Q8sktxQSrKrb1HwRftUGxgspgow4iUcKPd+a+LPTj4A8eXEg5VOP3JBr3Q0UkIaoQI pf0dcOmyV53HxCmNlhgKDW/K0dkaWDkYR+6Qu7UspBomAeF4+L4UY50EdUX+pRWlJoOLdamCeOe N6H/GmSWvs3XlAUVSkyuY8uKgACme0wu5aAexXUprhP3g6bXDHFLZctOlcGSHdigICUcO7P1KsT rb7hf7WVaDWkqQefSv+47bhLZNm3Q+/vGXku96KUGJsxyGNzBGfZeT/GCciPxVLVZbAAAJhnhK0 wryhYwoCehdtw67wwRiXAKf/9df49n9XQEl3goTrsH3JbByahV5EKDXvDqpGHIwhGyxdQddiP3k 05uU782DSebCB4m73qkXXOkjvq5eIIAx6hKsR+RZUqBFyLrqKsmnbMC8boNVhmeD/s99I89Csk5 7dtRXbIFxKTjqlvfDJs4EPxZXbQAiU3gthCS0df+2aVumwZ3eJsCz8SguJwpCuddLq/yL+nA= 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-fsdevel@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