From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-yx2-f3.google.com (mail-yx2-f3.google.com [74.125.224.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B15EA272816 for ; Sat, 8 Aug 2026 00:49:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.224.131 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786150165; cv=none; b=tMX6F0jm8pzsYPuli7snW3W0R6W+gUJu5dYOB74x9oWsf/kJDA8E2gTDiNrA4i/d3kLnkTwd7RUJG3adib7/nvyZkQijB93+mBNxVBop06g6tXl9VaYgePxD2csvUVLvDzbGvbtyWsPuhUNzQbq+AOjRVQP/PUrBg/HrlCr+vWQ= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786150165; c=relaxed/simple; bh=paSIXozKbh0u8P5dFbXucPMJb9a1pZpArQ5hmYyz/PE=; h=Message-ID:Subject:From:To:Cc:Date:In-Reply-To:References: Content-Type:MIME-Version; b=FAjMr2cCsCGzGTamRzQ1lbTu7rubCtgeUem2OgS0Q54ZVg2Wx/weJrVX7USsbdLCrZ5ruQAxzrI9CzNOPZxCi8vZ09pA6lS2fc/z7/LMyXPMJSXssxxN7SGsgorzqwfdtnYcgdV2LQroZJWEUcXCHKPDCnWH0JSoBsUBaYCNbfM= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=dubeyko.com; spf=pass smtp.mailfrom=dubeyko.com; dkim=pass (2048-bit key) header.d=dubeyko-com.20251104.gappssmtp.com header.i=@dubeyko-com.20251104.gappssmtp.com header.b=BZq+3yCx; arc=none smtp.client-ip=74.125.224.131 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.20251104.gappssmtp.com header.i=@dubeyko-com.20251104.gappssmtp.com header.b="BZq+3yCx" Received: by mail-yx2-f3.google.com with SMTP id 956f58d0204a3-668311ade52so32690d50.0 for ; Fri, 07 Aug 2026 17:49:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=dubeyko-com.20251104.gappssmtp.com; s=20251104; t=1786150161; x=1786754961; darn=vger.kernel.org; h=mime-version:user-agent:content-transfer-encoding:content-type :autocrypt:references:in-reply-to:date:cc:to:from:subject:message-id :from:to:cc:subject:date:message-id:reply-to:content-type; bh=IrZ1q+PGrQbp9QmuhGulcP1tFMUDfM/GhPm45HZu1oM=; b=BZq+3yCxm/3ttrGNy0Wm1Ro8lqFb6mSLoEo5QgmakLXXXHn/fphRlCN8O8u9j4Iic6 KqqLow10bQu6R5t4pYQT15KJVuHCTk1QCYak5c6AY8aetO1UQ3DlrIAjTLOqVXMTuUqG DlPF2IV6UMlxyONk+YV+PsN+xlPkOj3muasdT2ne37uJJDA6vyu0a0CcqbQ2nzNz+kzP wkNFjqILbEZbhBVCaBX6j2ql847rNj/7f7gidCf+gr+b5+NsMaKXX30ontxUurX1wxS8 Qokf5UczwcPgaaGXzk5SzZMeSZUGmPwFwUERf3du5Iw7HBbNWdB/FHbBAZUyE4ayti1o sDGg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1786150161; x=1786754961; h=mime-version:user-agent:content-transfer-encoding:content-type :autocrypt:references:in-reply-to:date:cc:to:from:subject:message-id :x-gm-gg:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to:content-type; bh=IrZ1q+PGrQbp9QmuhGulcP1tFMUDfM/GhPm45HZu1oM=; b=Yg8+MFQT0DY92BlQDuVwYISTW/aBPe8xSzLSgD3w5bvO+jyvW5E3ZoAps4Tr1Ej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uvzqan8u29/wdQPN+LvO4Yw7QPQM5Wz8EDpbvxb5jW4Df0V++3s3TnsbMKegb2J GJc0rR+oZBm8D6ctyRfI/OU0vjW15vQtbUUmhXmZBemzhsmytT4h1g== X-Received: by 2002:a05:690e:4814:b0:668:9f36:5496 with SMTP id 956f58d0204a3-66acd3cbf42mr3332273d50.51.1786150160502; Fri, 07 Aug 2026 17:49:20 -0700 (PDT) Received: from ?IPv6:2600:1700:6476:1430:891e:7b31:87a7:701f? ([2600:1700:6476:1430:891e:7b31:87a7:701f]) by smtp.gmail.com with ESMTPSA id 956f58d0204a3-66acafe723csm2183917d50.20.2026.08.07.17.49.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 07 Aug 2026 17:49:19 -0700 (PDT) Message-ID: Subject: Re: [PATCH v6] hfsplus: validate B-tree record offset table From: Viacheslav Dubeyko To: Jiaming Zhang Cc: frank.li@vivo.com, glaubitz@physik.fu-berlin.de, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, syzkaller@googlegroups.com, vdubeyko@coreweave.com Date: Fri, 07 Aug 2026 17:49:18 -0700 In-Reply-To: <20260806073358.1184938-1-r772577952@gmail.com> References: <20260806073358.1184938-1-r772577952@gmail.com> Autocrypt: addr=slava@dubeyko.com; prefer-encrypt=mutual; keydata=mQINBGgaTLYBEADaJc/WqWTeunGetXyyGJ5Za7b23M/ozuDCWCp+yWUa2GqQKH40dxRIR zshgOmAue7t9RQJU9lxZ4ZHWbi1Hzz85+0omefEdAKFmxTO6+CYV0g/sapU0wPJws3sC2Pbda9/eJ ZcvScAX2n/PlhpTnzJKf3JkHh3nM1ACO3jzSe2/muSQJvqMLG2D71ccekr1RyUh8V+OZdrPtfkDam V6GOT6IvyE+d+55fzmo20nJKecvbyvdikWwZvjjCENsG9qOf3TcCJ9DDYwjyYe1To8b+mQM9nHcxp jUsUuH074BhISFwt99/htZdSgp4csiGeXr8f9BEotRB6+kjMBHaiJ6B7BIlDmlffy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FXDC2tnzsoLQtrbBDlc2cLfhEB1BQJoVemuAhsBBQkDw mcABQsJCAcCBhUKCQgLAgQWAgMBAh4BAheAAAoJEDlc2cLfhEB1GRwP/1scX5HO9Sk7dRicLD/fxo ipwEs+UbeA0/TM8OQfdRI4C/tFBYbQCR7lD05dfq8VsYLEyrgeLqP/iRhabLky8LTaEdwoAqPDc/O 9HRffx/faJZqkKc1dZryjqS6b8NExhKOVWmDqN357+Cl/H4hT9wnvjCj1YEqXIxSd/2Pc8+yw/KRC AP7jtRzXHcc/49Lpz/NU5irScusxy2GLKa5o/13jFK3F1fWX1wsOJF8NlTx3rLtBy4GWHITwkBmu8 zI4qcJGp7eudI0l4xmIKKQWanEhVdzBm5UnfyLIa7gQ2T48UbxJlWnMhLxMPrxgtC4Kos1G3zovEy Ep+fJN7D1pwN9aR36jVKvRsX7V4leIDWGzCdfw1FGWkMUfrRwgIl6i3wgqcCP6r9YSWVQYXdmwdMu 1RFLC44iF9340S0hw9+30yGP8TWwd1mm8V/+zsdDAFAoAwisi5QLLkQnEsJSgLzJ9daAsE8KjMthv hUWHdpiUSjyCpigT+KPl9YunZhyrC1jZXERCDPCQVYgaPt+Xbhdjcem/ykv8UVIDAGVXjuk4OW8la nf8SP+uxkTTDKcPHOa5rYRaeNj7T/NClRSd4z6aV3F6pKEJnEGvv/DFMXtSHlbylhyiGKN2Amd0b4 9jg+DW85oNN7q2UYzYuPwkHsFFq5iyF1QggiwYYTpoVXsw Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable User-Agent: Evolution 3.60.1 (by Flathub.org) Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 On Thu, 2026-08-06 at 15:33 +0800, Jiaming Zhang wrote: > A crafted HFS+ image can contain a corrupted B-tree node. The node > descriptor may contain a record count that does not fit in the node, > and > record offsets may be unordered, unaligned, outside the node, or > point into > the offset table itself. >=20 > Several B-tree helpers consume these on-disk fields before validating > them: > hfs_bnode_dump() can walk past the offset table when num_recs is > corrupted, > hfs_brec_lenoff() can produce an underflowed length or a record range > that > overlaps the offset table. This can make the unlink/writeback path > repeatedly call hfs_bnode_read_u16() with invalid offsets while > holding the > HFS+ B-tree lock, producing a flood of "requested invalid offset" > messages. > Other writeback workers then block on tree->tree_lock and the system > reports tasks hung in hfsplus_write_inode(). >=20 > Validate num_recs against the node size before walking the record > offset > table. Reject record ranges that are unordered, unaligned, outside > the > node, or overlapping the offset table. Reject invalid record indexes > before > reading their offset entries, and avoid decrementing an already-zero > leaf_count. >=20 > Closes: > https://lore.kernel.org/lkml/CANypQFb_2TqKGrztAXj5m0_v+QChxXDnQVeifzV8J25= Vuju10Q@mail.gmail.com/ > Assisted-by: Codex:gpt-5.5-xhigh > Signed-off-by: Jiaming Zhang > --- > Changes in v6: > - hfs_bmap_free(): Drop offset checks and print error message when B- > tree > =C2=A0 map record length is invalid. >=20 > Changes in v5: > - Switch helpers to invalid checker and invert callers. > - hfs_brec_offsets_invalid(): Drop the redundant offset-table > argument > =C2=A0 since it is already covered by the offset-table overlap check and > take > =C2=A0 just the two neighboring record offsets (off and next_off). > - hfs_brec_len_invalid(): reject a length against node_size. > - Use hfs_brec_len_invalid() for both length checks in > hfs_bmap_get_map_page(). > - Validate the record offset in hfs_bmap_free(). > - Check the __hfs_brec_find() return code in [2] and [3]. [1] and [4] > call > =C2=A0 it to find the insertion slot for a new index key after a split, - > ENOENT is > =C2=A0 one of expected, even -EINVAL the following hfs_brec_insert() just > inserts > =C2=A0 at slot 0 of a node already validated by hfs_bnode_find(), so it > stays > =C2=A0 in-bounds and cannot trigger the invalid-offset flood. Hence no > check was > =C2=A0 added. >=20 > [1] > https://elixir.bootlin.com/linux/v7.2-rc3/source/fs/hfsplus/brec.c#L160 > [2] > https://elixir.bootlin.com/linux/v7.2-rc3/source/fs/hfsplus/brec.c#L208 > [3] > https://elixir.bootlin.com/linux/v7.2-rc3/source/fs/hfsplus/brec.c#L382 > [4] > https://elixir.bootlin.com/linux/v7.2-rc3/source/fs/hfsplus/brec.c#L449 >=20 > Changes in v4: > - Rename hfs_find_reset() to hfs_find_result_init(). > - Reset find result fields in __hfs_brec_find(). > - Move num_recs validation next to descriptor field initialization. > - Rename hfs_brec_range_valid() to hfs_brec_offpair_valid(). > - Use U16_MAX for invalid offset/len/keylen sentinels and update > callers. > - Add hfs_brec_len_valid() to check validity of len/keylen. > - Handle invalid B-tree map record lengths in hfs_bmap_get_map_page() > =C2=A0 and hfs_bmap_free(). > - Return -EINVAL instead of -EIO for invalid remove cursor/leaf_count > state. >=20 > Changes in v3: > - Drop the keylen =3D=3D len check. > - Drop the explicit zero-record check in __hfs_brec_find(). > - Move find cursor reset into hfs_find_reset() and call it from > hfs_find_init() and hfs_brec_find(). > - Rename helper-local variables as suggested. >=20 > =C2=A0fs/hfsplus/bfind.c=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 | 23 ++++++------ > =C2=A0fs/hfsplus/bnode.c=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 | 16 ++++++--- > =C2=A0fs/hfsplus/brec.c=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 | 53 ++++++++= ++++++++++--------- > =C2=A0fs/hfsplus/btree.c=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 | 16 +++++++-- > =C2=A0fs/hfsplus/hfsplus_fs.h | 79 > +++++++++++++++++++++++++++++++++++++++++ > =C2=A05 files changed, 155 insertions(+), 32 deletions(-) >=20 > diff --git a/fs/hfsplus/bfind.c b/fs/hfsplus/bfind.c > index 9a55fa6d5294..ca9813f58a6d 100644 > --- a/fs/hfsplus/bfind.c > +++ b/fs/hfsplus/bfind.c > @@ -18,6 +18,7 @@ int hfs_find_init(struct hfs_btree *tree, struct > hfs_find_data *fd) > =C2=A0 > =C2=A0 fd->tree =3D tree; > =C2=A0 fd->bnode =3D NULL; > + hfs_find_result_init(fd); > =C2=A0 ptr =3D kzalloc(tree->max_key_len * 2 + 4, GFP_KERNEL); > =C2=A0 if (!ptr) > =C2=A0 return -ENOMEM; > @@ -106,17 +107,21 @@ int __hfs_brec_find(struct hfs_bnode *bnode, > struct hfs_find_data *fd, > =C2=A0 u16 off, len, keylen; > =C2=A0 int rec; > =C2=A0 int b, e; > - int res; > + int res =3D -ENOENT; > =C2=A0 > =C2=A0 BUG_ON(!rec_found); > + hfs_find_result_init(fd); > + if (hfs_bnode_num_recs_invalid(bnode)) > + goto fail; > + > =C2=A0 b =3D 0; > =C2=A0 e =3D bnode->num_recs - 1; > - res =3D -ENOENT; > =C2=A0 do { > =C2=A0 rec =3D (e + b) / 2; > =C2=A0 len =3D hfs_brec_lenoff(bnode, rec, &off); > =C2=A0 keylen =3D hfs_brec_keylen(bnode, rec); > - if (keylen =3D=3D 0) { > + if (hfs_brec_len_invalid(bnode, len) || > + =C2=A0=C2=A0=C2=A0 hfs_brec_len_invalid(bnode, keylen)) { > =C2=A0 res =3D -EINVAL; > =C2=A0 goto fail; > =C2=A0 } I've realized that we have additional issue in HFS+ logic. Potentially, keylen and len could be not invalid but we could have negative value later in the logic: done: fd->record =3D e; fd->keyoffset =3D off; fd->keylength =3D keylen; fd->entryoffset =3D off + keylen; fd->entrylength =3D len - keylen; <-- negative value here. The negative value of fd->entrylength is checked in multiple places. However, there is extents tree logic that has no such check [1]: static int __hfsplus_ext_write_extent(struct inode *inode, struct hfs_find_data *fd) { } else { if (res) return res; hfs_bnode_write(fd->bnode, hip->cached_extents, fd->entryoffset, fd->entrylength); hip->extent_state &=3D ~HFSPLUS_EXT_DIRTY; } } Could you please double check my conclusion? We can fix the issue in another patch. Could you please fix the issue? > @@ -130,7 +135,8 @@ int __hfs_brec_find(struct hfs_bnode *bnode, > struct hfs_find_data *fd, > =C2=A0 if (rec !=3D e && e >=3D 0) { > =C2=A0 len =3D hfs_brec_lenoff(bnode, e, &off); > =C2=A0 keylen =3D hfs_brec_keylen(bnode, e); > - if (keylen =3D=3D 0) { > + if (hfs_brec_len_invalid(bnode, keylen) || > + =C2=A0=C2=A0=C2=A0 hfs_brec_len_invalid(bnode, len)) { > =C2=A0 res =3D -EINVAL; > =C2=A0 goto fail; > =C2=A0 } > @@ -158,11 +164,7 @@ int hfs_brec_find(struct hfs_find_data *fd, > search_strategy_t do_key_compare) > =C2=A0 __be32 data; > =C2=A0 int height, res; > =C2=A0 > - fd->record =3D -1; > - fd->keyoffset =3D -1; > - fd->keylength =3D -1; > - fd->entryoffset =3D -1; > - fd->entrylength =3D -1; > + hfs_find_result_init(fd); > =C2=A0 > =C2=A0 tree =3D fd->tree; > =C2=A0 if (fd->bnode) > @@ -274,7 +276,8 @@ int hfs_brec_goto(struct hfs_find_data *fd, int > cnt) > =C2=A0 > =C2=A0 len =3D hfs_brec_lenoff(bnode, fd->record, &off); > =C2=A0 keylen =3D hfs_brec_keylen(bnode, fd->record); > - if (keylen =3D=3D 0) { > + if (hfs_brec_len_invalid(bnode, len) || > + =C2=A0=C2=A0=C2=A0 hfs_brec_len_invalid(bnode, keylen)) { > =C2=A0 res =3D -EINVAL; > =C2=A0 goto out; > =C2=A0 } > diff --git a/fs/hfsplus/bnode.c b/fs/hfsplus/bnode.c > index d088fb7eb0df..3a1a1fa6f2e2 100644 > --- a/fs/hfsplus/bnode.c > +++ b/fs/hfsplus/bnode.c > @@ -352,15 +352,22 @@ void hfs_bnode_dump(struct hfs_bnode *node) > =C2=A0 struct hfs_bnode_desc desc; > =C2=A0 __be32 cnid; > =C2=A0 int i, off, key_off; > + u16 num_recs; > =C2=A0 > =C2=A0 hfs_dbg("node %d\n", node->this); > =C2=A0 hfs_bnode_read(node, &desc, 0, sizeof(desc)); > + num_recs =3D node->num_recs; > =C2=A0 hfs_dbg("next %d, prev %d, type %d, height %d, num_recs > %d\n", > =C2=A0 be32_to_cpu(desc.next), be32_to_cpu(desc.prev), > =C2=A0 desc.type, desc.height, be16_to_cpu(desc.num_recs)); > =C2=A0 > + if (hfs_bnode_num_recs_invalid(node)) { > + hfs_dbg("invalid num_recs %u\n", num_recs); > + return; > + } > + > =C2=A0 off =3D node->tree->node_size - 2; > - for (i =3D be16_to_cpu(desc.num_recs); i >=3D 0; off -=3D 2, i--) > { > + for (i =3D num_recs; i >=3D 0; off -=3D 2, i--) { > =C2=A0 key_off =3D hfs_bnode_read_u16(node, off); > =C2=A0 hfs_dbg(" key_off %d", key_off); > =C2=A0 if (i && node->type =3D=3D HFS_NODE_INDEX) { > @@ -561,6 +568,9 @@ struct hfs_bnode *hfs_bnode_find(struct hfs_btree > *tree, u32 num) > =C2=A0 node->height =3D desc->height; > =C2=A0 kunmap_local(desc); > =C2=A0 > + if (hfs_bnode_num_recs_invalid(node)) > + goto node_error; > + > =C2=A0 switch (node->type) { > =C2=A0 case HFS_NODE_HEADER: > =C2=A0 case HFS_NODE_MAP: > @@ -586,9 +596,7 @@ struct hfs_bnode *hfs_bnode_find(struct hfs_btree > *tree, u32 num) > =C2=A0 for (i =3D 1; i <=3D node->num_recs; off =3D next_off, i++) { > =C2=A0 rec_off -=3D 2; > =C2=A0 next_off =3D hfs_bnode_read_u16(node, rec_off); > - if (next_off <=3D off || > - =C2=A0=C2=A0=C2=A0 next_off > tree->node_size || > - =C2=A0=C2=A0=C2=A0 next_off & 1) > + if (hfs_brec_offsets_invalid(node, off, next_off)) > =C2=A0 goto node_error; > =C2=A0 entry_size =3D next_off - off; > =C2=A0 if (node->type !=3D HFS_NODE_INDEX && > diff --git a/fs/hfsplus/brec.c b/fs/hfsplus/brec.c > index e3df89284079..f416e562d439 100644 > --- a/fs/hfsplus/brec.c > +++ b/fs/hfsplus/brec.c > @@ -9,6 +9,8 @@ > =C2=A0 * Handle individual btree records > =C2=A0 */ > =C2=A0 > +#include > + > =C2=A0#include "hfsplus_fs.h" > =C2=A0#include "hfsplus_raw.h" > =C2=A0 > @@ -20,41 +22,49 @@ static int hfs_btree_inc_height(struct hfs_btree > *); > =C2=A0u16 hfs_brec_lenoff(struct hfs_bnode *node, u16 rec, u16 *off) > =C2=A0{ > =C2=A0 __be16 retval[2]; > - u16 dataoff; > + u16 data_off; > + u16 next_off; > + > + if (hfs_brec_record_invalid(node, rec)) { > + *off =3D U16_MAX; > + return U16_MAX; > + } > =C2=A0 > - dataoff =3D node->tree->node_size - (rec + 2) * 2; > - hfs_bnode_read(node, retval, dataoff, 4); > + data_off =3D node->tree->node_size - (rec + 2) * 2; > + hfs_bnode_read(node, retval, data_off, 4); > =C2=A0 *off =3D be16_to_cpu(retval[1]); > - return be16_to_cpu(retval[0]) - *off; > + next_off =3D be16_to_cpu(retval[0]); > + if (hfs_brec_offsets_invalid(node, *off, next_off)) { > + *off =3D U16_MAX; > + return U16_MAX; > + } > + return next_off - *off; > =C2=A0} > =C2=A0 > =C2=A0/* Get the length of the key from a keyed record */ > =C2=A0u16 hfs_brec_keylen(struct hfs_bnode *node, u16 rec) > =C2=A0{ > - u16 retval, recoff; > + u16 retval, recoff, len; > =C2=A0 > =C2=A0 if (node->type !=3D HFS_NODE_INDEX && node->type !=3D > HFS_NODE_LEAF) > =C2=A0 return 0; > + if (hfs_brec_record_invalid(node, rec)) > + return U16_MAX; > =C2=A0 > =C2=A0 if ((node->type =3D=3D HFS_NODE_INDEX) && > =C2=A0 =C2=A0=C2=A0 !(node->tree->attributes & HFS_TREE_VARIDXKEYS) && > =C2=A0 =C2=A0=C2=A0 (node->tree->cnid !=3D HFSPLUS_ATTR_CNID)) { > =C2=A0 retval =3D node->tree->max_key_len + 2; > =C2=A0 } else { > - recoff =3D hfs_bnode_read_u16(node, > - node->tree->node_size - (rec + 1) * 2); > - if (!recoff) > - return 0; > - if (recoff > node->tree->node_size - 2) { > - pr_err("recoff %d too large\n", recoff); > - return 0; > - } > + len =3D hfs_brec_lenoff(node, rec, &recoff); > + if (hfs_brec_len_invalid(node, len)) > + return len; > =C2=A0 > =C2=A0 retval =3D hfs_bnode_read_u16(node, recoff) + 2; > =C2=A0 if (retval > node->tree->max_key_len + 2) { > =C2=A0 pr_err("keylen %d too large\n", > =C2=A0 retval); > - retval =3D 0; > + retval =3D U16_MAX; > =C2=A0 } > =C2=A0 } > =C2=A0 return retval; > @@ -181,14 +191,20 @@ int hfs_brec_remove(struct hfs_find_data *fd) > =C2=A0 struct hfs_btree *tree; > =C2=A0 struct hfs_bnode *node, *parent; > =C2=A0 int end_off, rec_off, data_off, size; > + int res; > =C2=A0 > =C2=A0 tree =3D fd->tree; > =C2=A0 node =3D fd->bnode; > =C2=A0again: > + if (hfs_brec_record_invalid(node, fd->record)) > + return -EINVAL; > + > =C2=A0 rec_off =3D tree->node_size - (fd->record + 2) * 2; > =C2=A0 end_off =3D tree->node_size - (node->num_recs + 1) * 2; > =C2=A0 > =C2=A0 if (node->type =3D=3D HFS_NODE_LEAF) { > + if (tree->leaf_count =3D=3D 0) > + return -EINVAL; > =C2=A0 tree->leaf_count--; > =C2=A0 mark_inode_dirty(tree->inode); > =C2=A0 } > @@ -205,7 +221,9 @@ int hfs_brec_remove(struct hfs_find_data *fd) > =C2=A0 hfs_bnode_put(node); > =C2=A0 node =3D fd->bnode =3D parent; > =C2=A0 > - __hfs_brec_find(node, fd, hfs_find_rec_by_key); > + res =3D __hfs_brec_find(node, fd, > hfs_find_rec_by_key); > + if (res && res !=3D -ENOENT) > + return res; > =C2=A0 goto again; > =C2=A0 } > =C2=A0 hfs_bnode_write_u16(node, > @@ -368,6 +386,7 @@ static int hfs_brec_update_parent(struct > hfs_find_data *fd) > =C2=A0 int newkeylen, diff; > =C2=A0 int rec, rec_off, end_rec_off; > =C2=A0 int start_off, end_off; > + int res; > =C2=A0 > =C2=A0 tree =3D fd->tree; > =C2=A0 node =3D fd->bnode; > @@ -379,7 +398,9 @@ static int hfs_brec_update_parent(struct > hfs_find_data *fd) > =C2=A0 parent =3D hfs_bnode_find(tree, node->parent); > =C2=A0 if (IS_ERR(parent)) > =C2=A0 return PTR_ERR(parent); > - __hfs_brec_find(parent, fd, hfs_find_rec_by_key); > + res =3D __hfs_brec_find(parent, fd, hfs_find_rec_by_key); > + if (res && res !=3D -ENOENT) > + return res; > =C2=A0 if (fd->record < 0) > =C2=A0 return -ENOENT; > =C2=A0 hfs_bnode_dump(parent); > diff --git a/fs/hfsplus/btree.c b/fs/hfsplus/btree.c > index 394542a47e60..2ea8cd5658e1 100644 > --- a/fs/hfsplus/btree.c > +++ b/fs/hfsplus/btree.c > @@ -168,8 +168,8 @@ static struct page *hfs_bmap_get_map_page(struct > hfs_bnode *node, > =C2=A0 } > =C2=A0 > =C2=A0 ctx->len =3D hfs_brec_lenoff(node, rec_idx, &off16); > - if (!ctx->len) > - return ERR_PTR(-ENOENT); > + if (hfs_brec_len_invalid(node, ctx->len)) > + return ERR_PTR(-EINVAL); > =C2=A0 > =C2=A0 if (!is_bnode_offset_valid(node, off16)) > =C2=A0 return ERR_PTR(-EIO); > @@ -622,6 +622,12 @@ void hfs_bmap_free(struct hfs_bnode *node) > =C2=A0 if (IS_ERR(node)) > =C2=A0 return; > =C2=A0 len =3D hfs_brec_lenoff(node, 2, &off); > + if (hfs_brec_len_invalid(node, len)) { > + pr_err("invalid bmap record length: node %u, len > %u\n", > + =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 node->this, len); > + hfs_bnode_put(node); > + return; > + } > =C2=A0 while (nidx >=3D len * 8) { > =C2=A0 u32 i; > =C2=A0 > @@ -648,6 +654,12 @@ void hfs_bmap_free(struct hfs_bnode *node) > =C2=A0 return; > =C2=A0 } > =C2=A0 len =3D hfs_brec_lenoff(node, 0, &off); > + if (hfs_brec_len_invalid(node, len)) { > + pr_err("invalid bmap record length: node %u, > len %u\n", > + =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 node->this, len); > + hfs_bnode_put(node); > + return; > + } > =C2=A0 } > =C2=A0 > =C2=A0 res =3D hfs_bmap_clear_bit(node, nidx); > diff --git a/fs/hfsplus/hfsplus_fs.h b/fs/hfsplus/hfsplus_fs.h > index ec04b82ad927..90fcf1eac913 100644 > --- a/fs/hfsplus/hfsplus_fs.h > +++ b/fs/hfsplus/hfsplus_fs.h > @@ -587,6 +587,85 @@ bool is_bnode_offset_valid(struct hfs_bnode > *node, u32 off) > =C2=A0 return is_valid; > =C2=A0} > =C2=A0 > +static inline > +bool hfs_bnode_num_recs_invalid(struct hfs_bnode *node) > +{ > + u32 node_size; > + u32 table_size; > + u32 area_size; > + u32 rec_size =3D sizeof(__be16); > + u32 desc_size =3D sizeof(struct hfs_bnode_desc); > + > + if (!node || !node->tree) > + return true; > + > + node_size =3D node->tree->node_size; > + if (node_size < desc_size) > + return true; > + > + area_size =3D node_size - desc_size; > + table_size =3D ((u32)node->num_recs + 1) * rec_size; > + > + return table_size > area_size; > +} > + > +static inline > +bool hfs_brec_record_invalid(struct hfs_bnode *node, int record) > +{ > + if (hfs_bnode_num_recs_invalid(node)) > + return true; > + if (record < 0) > + return true; > + > + return record >=3D node->num_recs; > +} > + > +static inline > +bool hfs_brec_offsets_invalid(struct hfs_bnode *node, u16 off, u16 > next_off) > +{ > + u32 table_size; > + u32 table_start; > + u32 rec_size =3D sizeof(__be16); > + u32 desc_size =3D sizeof(struct hfs_bnode_desc); > + > + if (!node || !node->tree) > + return true; > + > + if (off < desc_size || (off & 1)) > + return true; > + > + if (next_off <=3D off || > + =C2=A0=C2=A0=C2=A0 next_off > node->tree->node_size || > + =C2=A0=C2=A0=C2=A0 (next_off & 1)) > + return true; > + > + table_size =3D ((u32)node->num_recs + 1) * rec_size; > + table_start =3D node->tree->node_size - table_size; > + if (next_off > table_start) > + return true; > + > + return false; > +} > + > +static inline > +bool hfs_brec_len_invalid(struct hfs_bnode *node, u16 len) > +{ > + if (!node || !node->tree) > + return true; > + > + return len =3D=3D 0 || len > node->tree->node_size; > +} > + > +static inline > +void hfs_find_result_init(struct hfs_find_data *fd) > +{ > + fd->record =3D -1; > + fd->keyoffset =3D -1; > + fd->keylength =3D -1; > + fd->entryoffset =3D -1; > + fd->entrylength =3D -1; > +} > + > =C2=A0static inline > =C2=A0u32 check_and_correct_requested_length(struct hfs_bnode *node, u32 > off, u32 len) > =C2=A0{ Looks good. Reviewed-by: Viacheslav Dubeyko Tested-by: Viacheslav Dubeyko [1] https://elixir.bootlin.com/linux/v7.2-rc6/source/fs/hfsplus/extents.c#L113 Thanks, Slava.