From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.51 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796118176; b=0eC3LH+QNy67Gx8iye+yH0gfTxSvVVmjODa5ZhkxzH4aVxvaVJ1Dem/O8MMP3ACkqyke SuKnEpMHJYOdeCSn1clhZ7QIsMF26DL1joclHm+Nrpifv93XrydffRiZMqzsGq7R3YXEd Qs/uHw/wD4vnEPMN/NhAv1y7ku79sTRvR4sjoGNF4TBMfznk68ROXGB+IgUHiAAbiXiZ1 4sV2TR9n6mgSF5JKei0NI8BOHPyCR8Mpq1FOeaW6+kAHro58eeesrvc4ddpcZsV5nOkOs 1WDhNIsQFOYc9dON+SW5NesInOw+l7QiO3EKkCt4ske4XByK3qZDDqAI8eJ7Kl0GxWw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796118176; 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=HSw+oZH0yWmrWlP02XtspWJrhGrStaX3Q7Esr4Z5ZO8=; b=qRzAgQd8Vcjbmh9/iS+ZBsFE9vCmqYO0AFw/jSW4FFLSqx9pVxKMDI9O+Wps37fN38F7 qBz8herHdasTkim2ZxN6aHWDLn3y/2Sg6Puxiae/o69O9sZRkBe1ZM71YQRqxbTnEUw9i ZqPXOSn7zKsssLjr4HReyahen3CJLNy6vwHeO8kXrg4l73gM9BFFXnERBgPok+lB77BtI JjgDqlBwLTGuOCI302jqCipKArbz++xHcLVe5a7Tcjd/GmAALB9cLDvr+RUDOrOAz6GPw lH4Cds2bEXQwk4QPVfSSWohUlmytOMbgRT6+fVUEPxp1Kb9eQi5FjmoBo22gtHcepDQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=FsQm8bF5; arc=none smtp.remote-ip=74.125.82.51 Received: from mail-dl1-f51.google.com (mail-dl1-f51.google.com [74.125.82.51]) (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 DDF0E606A681 for ; Tue, 1 Dec 2026 09:42:55 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org DDF0E606A681 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=FsQm8bF5 Received: by mail-dl1-f51.google.com with SMTP id a92af1059eb24-1b5786e099cso11576345c88.0 for ; Tue, 01 Dec 2026 01:42:55 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796118175; x=1796722975; 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=HSw+oZH0yWmrWlP02XtspWJrhGrStaX3Q7Esr4Z5ZO8=; b=FsQm8bF52Hsi7UDfEL6JO03XYllboiJWn0NpHVgVeBtLlvHZRGi++fcEFuXL93fc41 k2IDv3SweU+MRrXGZYbXbgximch/nsc5rR6pPppNERAr0TdlYti4NW59/+JXos/gPnUa M/m5HVpUk1g4arhsPWVX3wUiD20MRiMxm+zkqZmQGsTBLZPS9F/kUASmJelps8Aavnxa jZ1OXO/tmPJWpxzsts2t+LXmKrQ3YnCKMuf9As2RLBM3gy5sSw7/SBokWSqUnzneG8si 3fd1UT2Wb3EHU6Agy34m1pdWJ2YvHyxpPsyZNyOz34S8xub84D/W6y7t3ZJ0AqqzjbzC d3vw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796118175; x=1796722975; 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=HSw+oZH0yWmrWlP02XtspWJrhGrStaX3Q7Esr4Z5ZO8=; b=L261l517IGKnCcdf+/Lfy2c8Nmj74Trhc0iSiCedQAfuloRoyTqoZd8UByQ69O3NfK GqhjoKmEimFv9+LB6NodEBlHpC1ume1S4X4CBI30FnrXzmINPqjfb69BUHKOTlqEghW9 MM02F9WLQ/qLwmYsDSnXGmDY/RMDtsE+thhVh7Odn+kmjojPIgU7kS5LTjXt5y5Q4lmh S31J5y58qYAWIf7dZ84YIn2h7dmuEJXwsVQEObC7Gb7k+xzn4mVeDfG6kEZa6IsK0aA6 1sFhnEpzPJp2S81B0ccT79MJrHxW8EqbC1AzJzzJqnaiOfUUaEQo2yWhCSp7GKFBEUO1 H6iQ== X-Forwarded-Encrypted: i=1; AL+hErHvDVW1E7pasG4n2i228UGDfP2dpzCek0z/zY86Cffl4tAVas1JbK9oJdX9AhMazNedCK+iqHnqysuxXUk=@vger.kernel.org X-Gm-Message-State: APuU4Z/B7ZT/17tVCpJqBPfg1lfqNZDe5ia0HIDZxOQTvDSGLHHToB+N W7An4X+5E+Uk5ygKhMTb1N8NQDNQiT8zSGFwonvD0HBF5mqJo+M9M56mVrRp0RpM X-Gm-Gg: AZFdTZdeP5yFIpcsh9GX+A8HFM9o7UNftW/TP8f/RynLyC1v9rdCvZreau8F/bQ1iq0 mJSSHmX4KsxyRjdgUsvLBDah0xrv0xQigB7uL40OKij5N4vkbIiqzEfN4h3UqGuJWtbUJ1Rl40l DR6pWU2ctmiH7mhnYAtl3dc7sS5lLdbIU21sw5qDNs2RGHU3Zn5nYXS7YPsesahZ+AtdlT1G9om J3Fz88sddD59aNyJyVLFsg/daUuIfWSET/j2FWsba3kvIJt8TIOf8cXx0q0yb5lsOgQ/cofVKGl xB/G924YDzuK71CGpTTzmzchVvms2imTujk0ai1HCm4sL/JFVfOM20vzoY7K9fMFYHbMs95quJx 664x3G8fAs1mhG/UO/ydiCzQPCyI3kQsekSazEw/uFgsHxhAWAFTO55eC5tSHMpLgLFO0rLuQNA ZO7ugZcIsmHUK6Z4Gw7P0UNbs/td5OuxbYYXrhzXYqglFT52KPUg2y9Se7P0kFguiMqLHHVrzz+ 0naZT5T3bRavcL/r0XKMIaDA2iyBx0OhYZH8f1DZYNh628DOicV69ANJrSoIzYLPotg0dNjnVB4 EWZa8pKeIiTj5eCDxCM9goFqW8no8EXC3H5qN6tqLK5xlxIbPodF56ANAzzO4KGewfEGxbI1Yd9 RJNFtNo/tJ6wmjXPh3knI2jp8dQKv X-Received: by 2002:a05:7301:140b:b0:386:ca0e:430a with SMTP id 5a478bee46e88-386ca0e44c5mr21266718eec.24.1796118174885; Tue, 01 Dec 2026 01:42:54 -0800 (PST) Received: from localhost.localdomain ([2601:646:4300:d042:f49c:45c4:c676:a80f]) by smtp.gmail.com with ESMTPSA id 5a478bee46e88-386d605c894sm49138495eec.2.2026.12.01.01.42.53 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Tue, 01 Dec 2026 01:42:54 -0800 (PST) From: Ozgun Kultekin To: slava@dubeyko.com, glaubitz@physik.fu-berlin.de, frank.li@vivo.com Cc: linux-fsdevel@vger.kernel.org, yangguikun19@qq.com, stable@vger.kernel.org, linux-kernel@vger.kernel.org, kovalev@altlinux.org, brauner@kernel.org, cengiz.can@canonical.com, Ozgun Kultekin Subject: [PATCH v5 2/3] hfs: pass the key buffer size to hfs_bnode_read_key() Date: Tue, 1 Dec 2026 01:42:45 -0800 Message-ID: <20261201094246.80197-3-ozgunkultekin@gmail.com> X-Mailer: git-send-email 2.56.0 In-Reply-To: <20261201094246.80197-1-ozgunkultekin@gmail.com> References: <20261127045416.1160-1-ozgunkultekin@gmail.com> <20261201094246.80197-1-ozgunkultekin@gmail.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 hfs_bnode_read_key() reads the key length of leaf records (and of all records of variable-key trees) from disk and only checks it against sizeof(hfs_btree_key). The destination is always one of the two key buffers hfs_find_init() allocates, each tree->max_key_len + 2 bytes long: max_key_len in the b-tree header record does not count the key length field, as hfs_btree_open() checks (HFS_MAX_EXT_KEYLEN is sizeof(struct hfs_ext_key) - sizeof(u8)). For the extents tree max_key_len is 7, so the whole allocation is 18 bytes and a crafted leaf key writes up to 256 bytes from the image past it. The error path clears sizeof(hfs_btree_key) bytes of the same buffer. hfs_brec_insert() and hfs_brec_update_parent() reach this with keys that were never validated, the same way as in hfsplus. Store the capacity of each key buffer in struct hfs_find_data as key_capacity in hfs_find_init(), allocate the two buffers from it, and pass it to every hfs_bnode_read_key() call. Keys longer than the buffer are rejected as before, and the error path clears only the buffer. The allocation size does not change: max_key_len * 2 + 4 is now key_capacity * 2, where key_capacity is max_key_len plus sizeof(u16), room for the 16-bit key length of HFS_TREE_BIGKEYS trees. key_capacity goes after bnode, where it pairs with record, so struct hfs_find_data stays 56 bytes on 64-bit. Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2") Fixes: bb5e07cb9277 ("hfs/hfsplus: fix slab-out-of-bounds in hfs_bnode_read_key") Reported-by: Guikun Yang Closes: https://lore.kernel.org/all/tencent_F85118A9AFDF9B230D4E8FA672510A3E2F08@qq.com/ Suggested-by: Viacheslav Dubeyko Cc: stable@vger.kernel.org Signed-off-by: Ozgun Kultekin --- Notes: v5: the same changes as in the hfsplus patch: key_capacity * 2 with a comment, a local key_len_size, HFS_FIND_KEY_BUFS and HFS_KEY_LEN_MAX_SIZE are gone. Rebased on the hfs tree for-next branch. As suggested by Viacheslav. v4: https://lore.kernel.org/all/20261127045416.1160-3-ozgunkultekin@gmail.com/ v3: https://lore.kernel.org/all/20261119005943.37851-4-ozgunkultekin@gmail.com/ v2: https://lore.kernel.org/all/20261118013337.67813-1-ozgunkultekin@gmail.com/ v1: https://lore.kernel.org/all/20261114043522.48764-1-ozgunkultekin@gmail.com/ fs/hfs/bfind.c | 15 +++++++++++++-- fs/hfs/bnode.c | 7 ++++--- fs/hfs/brec.c | 12 ++++++++---- fs/hfs/btree.h | 4 +++- 4 files changed, 28 insertions(+), 10 deletions(-) diff --git a/fs/hfs/bfind.c b/fs/hfs/bfind.c index d56e47bdc5..98e2774fd6 100644 --- a/fs/hfs/bfind.c +++ b/fs/hfs/bfind.c @@ -14,6 +14,11 @@ int hfs_find_init(struct hfs_btree *tree, struct hfs_find_data *fd) { + /* + * The key length field is a u8, or a u16 in HFS_TREE_BIGKEYS trees + * (see hfs_brec_keylen()); leave room for the larger one. + */ + const size_t key_len_size = sizeof(u16); void *ptr; if (!tree || !fd) @@ -21,11 +26,17 @@ int hfs_find_init(struct hfs_btree *tree, struct hfs_find_data *fd) fd->tree = tree; fd->bnode = NULL; - ptr = kzalloc(tree->max_key_len * 2 + 4, GFP_KERNEL); + /* + * max_key_len does not count the key length field at the start of + * every key, so each key buffer needs max_key_len + key_len_size + * bytes. search_key and key share one allocation of two buffers. + */ + fd->key_capacity = tree->max_key_len + key_len_size; + ptr = kzalloc(fd->key_capacity * 2, GFP_KERNEL); if (!ptr) return -ENOMEM; fd->search_key = ptr; - fd->key = ptr + tree->max_key_len + 2; + fd->key = ptr + fd->key_capacity; hfs_dbg("cnid %d, caller %ps\n", tree->cnid, __builtin_return_address(0)); switch (tree->cnid) { diff --git a/fs/hfs/bnode.c b/fs/hfs/bnode.c index ec1b0189a4..f2781afea7 100644 --- a/fs/hfs/bnode.c +++ b/fs/hfs/bnode.c @@ -71,7 +71,8 @@ u8 hfs_bnode_read_u8(struct hfs_bnode *node, u32 off) return data; } -void hfs_bnode_read_key(struct hfs_bnode *node, void *key, u32 off) +void hfs_bnode_read_key(struct hfs_bnode *node, void *key, u32 off, + u32 key_capacity) { struct hfs_btree *tree; u32 key_len; @@ -83,8 +84,8 @@ void hfs_bnode_read_key(struct hfs_bnode *node, void *key, u32 off) else key_len = tree->max_key_len + 1; - if (key_len > sizeof(hfs_btree_key) || key_len < 1) { - memset(key, 0, sizeof(hfs_btree_key)); + if (key_len > key_capacity || key_len < 1) { + memset(key, 0, key_capacity); pr_err("hfs: Invalid key length: %u\n", key_len); return; } diff --git a/fs/hfs/brec.c b/fs/hfs/brec.c index 1ca1ee9f73..15520b2b15 100644 --- a/fs/hfs/brec.c +++ b/fs/hfs/brec.c @@ -138,7 +138,8 @@ int hfs_brec_insert(struct hfs_find_data *fd, void *entry, u32 entry_len) * at the start of the node and it is not the new node */ if (!rec && new_node != node) { - hfs_bnode_read_key(node, fd->search_key, data_off + size); + hfs_bnode_read_key(node, fd->search_key, data_off + size, + fd->key_capacity); hfs_brec_update_parent(fd); } @@ -156,7 +157,8 @@ int hfs_brec_insert(struct hfs_find_data *fd, void *entry, u32 entry_len) entry_len = sizeof(cnid); /* get index key */ - hfs_bnode_read_key(new_node, fd->search_key, 14); + hfs_bnode_read_key(new_node, fd->search_key, 14, + fd->key_capacity); __hfs_brec_find(fd->bnode, fd); hfs_bnode_put(new_node); @@ -458,7 +460,8 @@ static int hfs_brec_update_parent(struct hfs_find_data *fd) } fd->bnode = hfs_bnode_find(tree, new_node->parent); /* create index key and entry */ - hfs_bnode_read_key(new_node, fd->search_key, 14); + hfs_bnode_read_key(new_node, fd->search_key, 14, + fd->key_capacity); cnid = cpu_to_be32(new_node->this); __hfs_brec_find(fd->bnode, fd); @@ -470,7 +473,8 @@ static int hfs_brec_update_parent(struct hfs_find_data *fd) if (new_node == node) goto out; /* restore search_key */ - hfs_bnode_read_key(node, fd->search_key, 14); + hfs_bnode_read_key(node, fd->search_key, 14, + fd->key_capacity); } new_node = NULL; } diff --git a/fs/hfs/btree.h b/fs/hfs/btree.h index 1f703f2c53..b42c47b212 100644 --- a/fs/hfs/btree.h +++ b/fs/hfs/btree.h @@ -79,6 +79,7 @@ struct hfs_find_data { btree_key *search_key; struct hfs_btree *tree; struct hfs_bnode *bnode; + u32 key_capacity; int record; int keyoffset, keylength; int entryoffset, entrylength; @@ -117,7 +118,8 @@ extern void hfs_bmap_free(struct hfs_bnode *node); extern void hfs_bnode_read(struct hfs_bnode *node, void *buf, u32 off, u32 len); extern u16 hfs_bnode_read_u16(struct hfs_bnode *node, u32 off); extern u8 hfs_bnode_read_u8(struct hfs_bnode *node, u32 off); -extern void hfs_bnode_read_key(struct hfs_bnode *node, void *key, u32 off); +extern void hfs_bnode_read_key(struct hfs_bnode *node, void *key, u32 off, + u32 key_capacity); extern void hfs_bnode_write(struct hfs_bnode *node, void *buf, u32 off, u32 len); extern void hfs_bnode_write_u16(struct hfs_bnode *node, u32 off, u16 data); extern void hfs_bnode_write_u8(struct hfs_bnode *node, u32 off, u8 data); -- 2.39.5