From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.171 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796118174; b=s/ijiNXgHZ2UtkmAqSdRRqOIrlzZ5KEOSclc0WZw0WJ7HAD9DBKgZQAZgi/3erLK4JFl Ho8R2r0HxZUXm4J3G6mdY6/7Ea36Lfv6Wqis33Pb3WrHuMyt858T6uzIZQWvDxDHSWPEr QIzOyi8JN6zwzObx1LJORC1KePrvZTHGsbrHwRTMR7TDg7k162hCvDHvrFhJuIpT2YyWQ brOQbZsOHbsLAL4BXoIkpOJSEkuWoMR7FW3em89mhuJAI9j+fFw6QMzfCSfhI2oHkQDtX uHInTQ8lA2BEbDdeVujg0g+t2zlFN07TztVJFHLoxOL21T0ZGr3pzk1etXVTxUOx1Ww== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796118174; 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=22wx0SK86i3Dqs7SdK7q/6ZJXoE8UGiPHlQRmbZhJUA=; b=2ER60+BJKzL7kcdL0XfvjNsr95Oms3tPjSMdcWUFkNh2YILfDqwPolpWYIUxd9UhkiQL Wy4mScP8gaZ95WPR3OZ2CSjtoe7Zgjt41pFA2zKc+HC9K3cNHzLGU89OtYGu5hDMtUJ03 MkEiNYv0HWqJARfiusa+VxSfp642SOQxyR9RD7/N59prUICPpaQnqwYATlisK4TM3N4nB O+EzDTDjl/BF3hJbTcAA0X/anQVrOf6DBiJkn/+ufCfH4Ep7OcvVErjUwikf1sr5hdIuo jQfL/PA30rkWq06p7qOjMZgqskHhZ0zhuy3KnK2Ajt9HbaC5vbQA5RZWm78N1EhnZgA== 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=Mvjy21KN; arc=none smtp.remote-ip=74.125.82.171 Received: from mail-dy1-f171.google.com (mail-dy1-f171.google.com [74.125.82.171]) (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 12636606A330 for ; Tue, 1 Dec 2026 09:42:53 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 12636606A330 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=Mvjy21KN Received: by mail-dy1-f171.google.com with SMTP id 5a478bee46e88-3866a784546so16192800eec.0 for ; Tue, 01 Dec 2026 01:42:53 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796118173; x=1796722973; 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=22wx0SK86i3Dqs7SdK7q/6ZJXoE8UGiPHlQRmbZhJUA=; b=Mvjy21KN53uPoNiKmZsCW+RsNZv9TjnztwLExC/JwvrJP+HvhZtihbk+TwNBE9jLaO y4lnwSu+4eDoR3qVmdOBlICpmmLIFHw+BTB08fs3k6pmkvlzuMIHK0aB1hSm2Hu3p0Nr wk+dZKvlJCS7KeT4nM6RK5HkgN21EJAiraFDeGISd1KjK+/HeK12pLR6zKJ+rAvNMtCD Cs178jXa3+Zj4yEAKOSi0Spcm1T9uw9C67LKWTovHU4weDw4bn2mK7IK+nciixS6c6K6 9HaumnvkNELfhbYhJYxOYsj57/4Sq8oUum/659idyWubJhCkZL1DWRRJbGDJc539mNfR 2c1g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796118173; x=1796722973; 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=22wx0SK86i3Dqs7SdK7q/6ZJXoE8UGiPHlQRmbZhJUA=; b=YHHYVw7IV4eYyn6OOjBt0w0+aRMbgU3hJXBFNd6k1adtHrZJp3HxRAJKcKVjQMDhDE psO06cOe++IaGAPS4vkmq6FDFSwqBLqrZIyGgKWtuAYGm7W6yOkzd2zfz4mslJlexto/ ZufQbwuqNGNSk9/YDe7ZK6xhDqrysANvV/lkSVLXpFaVXgBY5lE54RbxKcUIeI2rZzNJ tqTGF//6wdoWDZb1Ev1sXBH3w2uX8lhYE5Yj/lonGt/mj0SQPDFN+cfC0MvHnXZiIMBS Y+WWPW58IGsjIkHQav0SiIeCW8q4GsMRPEdOA2oJAZAiJlTO9Qg7fByARdiim681HkMV FmlQ== X-Forwarded-Encrypted: i=1; AL+hErEvAv9QVeyT4LA45EOgPuoYEC1P0hJ4StONiw9tl5aUsU0g1nuSsoCaReMhCqLTZvc3rzNJfKBUbYYxY6Q=@vger.kernel.org X-Gm-Message-State: APuU4Z/mv7tYgK5aI59kq2OtkMe2yeBMPWlxQJuZGQuHWEbjl7tyL3I/ c9J3Sd3mZlMi6ximDARia6+/TD0joaucB6zVcPbENDbP+VesyizzGVwr X-Gm-Gg: AZFdTZc35fbRF7mXjc2MYe3GiDzxXvz9f2txJWfTe4R/ePadDxlmSUtvNFxpdfChI3u 48AjgyWxO4wfidn6GVrUU3rgXzj2QeUJ5uRFO6phRZTCKQyhA03lK5nXBHXL0iTTMO9FsPE1gZ2 /ELNpX4DETohnafUcvqwrKFypx+UeoSlJmTt/UToWyLoJdMOHETOJW1H4nGq2p5wVufiP39VmeD xI86ylL5ccRCGdW3eXdpu4BxsoKFUNXo4quiQJqIYVT6aj8WNx1q12SQsEinh1YuHJ4RJvkL+F9 O6cYdDAuB882LgCwM2k8hsKjB1X6/+GDL0RI4uVx3VhqKDp17wYaSJ1sb+PSWY1mEvaLUqV4YaO ko/Ym91egBNPPXdCEPizU8CjEXD1hsYZOkghIEi+7Krey5HBfpxP3qfnH0/oWkaOFaj0cVMhCaD SlTG4v6XeNGLdtyyscMpPzna1RYTr6FBhvPTdOOXsYYsdNi88QCt/HNE/YpCgOJFcQYv7S0+vBT CcL3oLy0mLeskCID6g/rRhZYS0Sq3lZ7OaEFvx0dnpJpC4XfWH+jehB0/N7IbP4Kg/zv26FQZk5 FnNHWEAb0Ipvc5mSiXlPe4dKlY0Xh59ZTYCyK6eEk6L37zN3WnAeU81TjSMx2tUm+HYJ3lvMajo eM5RJ9dioWCimQ9iZ4CbQLeJ1a44L X-Received: by 2002:a05:7300:de44:b0:375:c5e1:931d with SMTP id 5a478bee46e88-3857073ebabmr35750387eec.25.1796118173127; Tue, 01 Dec 2026 01:42:53 -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.51 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Tue, 01 Dec 2026 01:42:52 -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 1/3] hfsplus: pass the key buffer size to hfs_bnode_read_key() Date: Tue, 1 Dec 2026 01:42:44 -0800 Message-ID: <20261201094246.80197-2-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 and attributes trees) from disk and only checks it against sizeof(hfsplus_btree_key), 518 bytes. 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 __be16 key_len field, as hfs_btree_open() checks (HFSPLUS_EXT_KEYLEN - sizeof(u16) and so on). For the extents tree max_key_len is fixed at 10, so the whole allocation is 24 bytes and a crafted leaf key writes up to 516 bytes from the image into kmalloc-32. The error path clears 518 bytes of the same buffer. hfs_brec_insert() reaches this with a key that was never validated: when the tree root is 0 it reads the key of record 0 of the leaf it inserts into, and after a split it reads the first key of the new node. A read-write mount that extends the catalog, or an append to a file whose extents spill into the extents tree, is enough. Do not let hfs_bnode_read_key() assume the size of the buffer. 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 the size of the key_len field. 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: search_key and key stay in one allocation, now written as key_capacity * 2 with a comment, and the size of key_len is a local constant; HFS_FIND_KEY_BUFS and HFSPLUS_KEY_LEN_SIZE are gone. Rebased on the hfs tree for-next branch. As suggested by Viacheslav. v4: https://lore.kernel.org/all/20261127045416.1160-2-ozgunkultekin@gmail.com/ v3: https://lore.kernel.org/all/20261119005943.37851-3-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/hfsplus/bfind.c | 11 +++++++++-- fs/hfsplus/bnode.c | 7 ++++--- fs/hfsplus/brec.c | 12 ++++++++---- fs/hfsplus/hfsplus_fs.h | 11 +++++++---- 4 files changed, 28 insertions(+), 13 deletions(-) diff --git a/fs/hfsplus/bfind.c b/fs/hfsplus/bfind.c index ca9813f58a..657f22df1a 100644 --- a/fs/hfsplus/bfind.c +++ b/fs/hfsplus/bfind.c @@ -14,16 +14,23 @@ int hfs_find_init(struct hfs_btree *tree, struct hfs_find_data *fd) { + const size_t key_len_size = sizeof_field(hfsplus_btree_key, key_len); void *ptr; fd->tree = tree; fd->bnode = NULL; hfs_find_result_init(fd); - ptr = kzalloc(tree->max_key_len * 2 + 4, GFP_KERNEL); + /* + * max_key_len does not count the key_len 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)); mutex_lock_nested(&tree->tree_lock, diff --git a/fs/hfsplus/bnode.c b/fs/hfsplus/bnode.c index 3a1a1fa6f2..144348bdbf 100644 --- a/fs/hfsplus/bnode.c +++ b/fs/hfsplus/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; @@ -84,8 +85,8 @@ void hfs_bnode_read_key(struct hfs_bnode *node, void *key, u32 off) else key_len = tree->max_key_len + 2; - if (key_len > sizeof(hfsplus_btree_key) || key_len < 1) { - memset(key, 0, sizeof(hfsplus_btree_key)); + if (key_len > key_capacity || key_len < 1) { + memset(key, 0, key_capacity); pr_err("hfsplus: Invalid key length: %u\n", key_len); return; } diff --git a/fs/hfsplus/brec.c b/fs/hfsplus/brec.c index f416e562d4..c9fb6873ac 100644 --- a/fs/hfsplus/brec.c +++ b/fs/hfsplus/brec.c @@ -148,7 +148,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); } @@ -166,7 +167,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_find_rec_by_key); hfs_bnode_put(new_node); @@ -464,7 +466,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, hfs_find_rec_by_key); @@ -476,7 +479,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/hfsplus/hfsplus_fs.h b/fs/hfsplus/hfsplus_fs.h index dd8f717943..3d33f2df0b 100644 --- a/fs/hfsplus/hfsplus_fs.h +++ b/fs/hfsplus/hfsplus_fs.h @@ -286,13 +286,15 @@ static inline void hfsplus_mark_inode_dirty(struct inode *inode, } struct hfs_find_data { - /* filled by caller */ + /* set by hfs_find_init() */ hfsplus_btree_key *search_key; hfsplus_btree_key *key; - /* filled by find */ struct hfs_btree *tree; + /* filled by find */ struct hfs_bnode *bnode; - /* filled by findrec */ + /* set by hfs_find_init(): size of each of search_key and key */ + u32 key_capacity; + /* filled by findrec, reset by hfs_find_result_init() */ int record; int keyoffset, keylength; int entryoffset, entrylength; @@ -403,7 +405,8 @@ void hfs_bmap_free(struct hfs_bnode *node); void hfs_bnode_read(struct hfs_bnode *node, void *buf, u32 off, u32 len); u16 hfs_bnode_read_u16(struct hfs_bnode *node, u32 off); u8 hfs_bnode_read_u8(struct hfs_bnode *node, u32 off); -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); void hfs_bnode_write(struct hfs_bnode *node, void *buf, u32 off, u32 len); void hfs_bnode_write_u16(struct hfs_bnode *node, u32 off, u16 data); void hfs_bnode_clear(struct hfs_bnode *node, u32 off, u32 len); -- 2.39.5