Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.169 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796118172; b=Mw60EmHM1vnx+pwwlvinsR5JoE9+WycnvYcm74iN7QOUvB1ckb06ZCf9skRQKyQwG4AI auTwJsgUyMs1cmA61Mafo9pv3lwAuyKV3bHmAxxoF9dih0H3l9oC7fpAJl1pYpl8re0am f8yyofUmFsKUNos13xjsLNr20es0S52Q9doNIAQ//OTcTO3XpVDJc9DeE00pKwmYK4S8J yDeONXrBuulXh7091U/IsHk+Dq6SHMn+3HwclKfXKJ0MMP2QbLxJf2LEjv+xBNVZUDT28 rZ5S/4XASWV/3dfZsiy6wfe58E+kpJ0cVrBHls/mgYfwG38tn2zZGQawZdpG6jL1MYw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796118172; 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=hZjf0KbNzmKj4ZF0+RF7FGgfkf63D3bu2i7NRaeYy3s=; b=yLs/vLVBM1+5gq3NE8YAx9FiIMeNARUDpa5wi0G5UPRcgznTxlpDQVu/Utaj8IQ+vq/J 1K1Zb0R3AnVjSY0mVHVA/5xK8uNkyocpgcsr9YXE02YNLTUJvB9AwfeAmAYPNOkAqWAls KfG70T8IEBdre2nC++iVW+9RyOr4IYS1tCIrn7gZVMoXDUalkZEq6gUTmYlfZYLMokcoz Si5mlCmRSV4vJYsbzyqy6ml+6CxBPmlsB+aM36R1+17NqLTVfwHjBMhf/kSy7M0j41NhJ hwX+3oNEHJZnZ82LemMjRGhtvo1KsZp3Al9XpzzTHUmYlWVEZ3x+Oy9M2Kdw79P9DgA== 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=ZuMaUHmZ; arc=none smtp.remote-ip=74.125.82.169 Received: from mail-dy1-f169.google.com (mail-dy1-f169.google.com [74.125.82.169]) (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 E22BA606A32A for ; Tue, 1 Dec 2026 09:42:51 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org E22BA606A32A 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=ZuMaUHmZ Received: by mail-dy1-f169.google.com with SMTP id 5a478bee46e88-385c497bf94so115936eec.1 for ; Tue, 01 Dec 2026 01:42:51 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796118171; x=1796722971; 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=hZjf0KbNzmKj4ZF0+RF7FGgfkf63D3bu2i7NRaeYy3s=; b=ZuMaUHmZ9km1WoR3wQdkbE9WpZxEKbyVNcz0V3CIy+ZfvcJ4hVc/oi05b0VnZGyrsZ TGMvl5UcUjX7Bxz+cm92rqmmpvSrBAuEWzg4z8BJLn6SVH4EB0LIwvmW47cmm6W9Qw6e EI3+sT7wBTSLEG+UIKv+D1CszEmcnga0KkxHqdxM0cPtIBQwGQSnCfWZJaGSiDOfeV5y LD0IRahBTDfnaOq+rGZgt1rvmf/FzcbmmwIDJGHMifSMcpLCtcmWD68DuwFTI4Wdh1jv rih8INAG6rYz+IBjE8EXgW21TTX5XNMv5Jm5KAQ/HjcZOaNTpkgkMXrvpX4kUeZCjghv 0mNg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796118171; x=1796722971; 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=hZjf0KbNzmKj4ZF0+RF7FGgfkf63D3bu2i7NRaeYy3s=; b=sUvcT2dNWiKwVECS7STUpGDYci40YMVtLn+hQdfp0n/m3x7cKEMH8J0FcEyS4HYdpn v942+C39QOpeGL5EZWpr6S201qeMSNU5NUWaFgQBJSdYcTQXE6Lohlo5BYxTrq+A0rqT aZCHkW0OhsTcEv8112BLiDih/OvCMzuA2cEYAqota/KDzxvbTFxOkfrpA8cYetT4mDGT QHFOGVtAe99UJplbuybBx/daiI0SpRzfQjdycsOxHDIMGIe2Yf33z1sCb4VqsooPiA00 Yvf81qAnITUj1kgkBXB8xcJVr2SFMWTj5FpliPZccawyrfm7vnvOa+aJ9wxBX1hoyFd9 rpRw== X-Gm-Message-State: APuU4Z/uSDoyTeW6i3aGq5+7POZtVFWxG29+kzVzz54nJJ8OxJBeL3VE w4P5KI/dw6RXC5N6Q95GokYFKeLoMJn43Zqyq1NjBd9ye6r6UUTVQtPQ X-Gm-Gg: AZFdTZdBgMW1nYeKeoQPf/+AztyLxu7xCLCPVcwFdn1Wl1nXF7M1QlR7Yclumb7mF2m nLp/1A9NcYC3oiNRRhYM1fgFMx1/V3jCBeZB/hmDSkyyc/pIrCVxn1TbnzhPOcO18P91NADiWfH gam5lPM0FgQERISa4Nnfxek9XAS/m1q36xp1nEuxxbPL0bxOOmoIDDnNMQTs8kC3W6IyyfvqdmR xrPZkvrbG3dRcKn/m4xaMTG3J6uRtaQYjU4v1PSANR/tTo7Hk/83hzJPyeqvJM/QGv8ywKnlxj6 oejOGiwDSXAPEHqw1OzpzWEdbmb19wLZD4LePFSsf8E50oZ4WpbVE9TyiE2vrAkgHVwuKFpLYoJ faIAQmxzhxR1Xb0DSA8NM87hiSqs0PYNzZ9zI/2nA9MuaTwbdV+EhAdDt5lqO5tmw7gztYu5Qrg /eemEG6H9mlRzg/rEhBQJuB4iaMBeuZOh06RLCFiZF9vQ8SpLuDrmvTatooC9kSpLvVthokbLVm Ukmm4uWrE0YNHpMFdgjQOiwZwsOtzmMhuJ+SAtp7PDuKwOLwznsTqOZisTA8NZvDWjEZC31FuBt WU9V9VQ0c8XNSWZR+vVDo+UfP2sPh0UtGzxtltnPStNEv1zXLW2xJqoxXvwGZ60ex97neWScDvW rQjaiV1ThXfW2MDP8giiUGi8VEtowg7vdCC19k8fhBmZD34a6+w== X-Received: by 2002:a05:7300:d0af:b0:386:a3fe:4503 with SMTP id 5a478bee46e88-386a3fe46b5mr28352233eec.41.1796118170929; Tue, 01 Dec 2026 01:42:50 -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.49 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Tue, 01 Dec 2026 01:42:50 -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 0/3] hfs/hfsplus: pass the key buffer size to hfs_bnode_read_key() Date: Tue, 1 Dec 2026 01:42:43 -0800 Message-ID: <20261201094246.80197-1-ozgunkultekin@gmail.com> X-Mailer: git-send-email 2.56.0 In-Reply-To: <20261127045416.1160-1-ozgunkultekin@gmail.com> References: <20261127045416.1160-1-ozgunkultekin@gmail.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 hfs_bnode_read_key() checks the on-disk key length against the size of the key union, but the buffers from hfs_find_init() are only max_key_len + 2 bytes each. A crafted extents leaf key overflows them. Changes in v5, following Viacheslav's review of v4: - One allocation or two: two kzalloc() calls would not be measurably slower, since both buffers are small. But every hfs_find_init() would allocate twice, and the error path and hfs_find_exit() would need a second kfree(). I kept the single allocation so the fixes stay small for stable, and wrote it the way you suggested: kzalloc(fd->key_capacity * 2, ...) with a comment saying that search_key and key share it. HFS_FIND_KEY_BUFS is gone. If you would rather have two allocations, I can send that as a separate patch. - HFSPLUS_KEY_LEN_SIZE is gone. hfs_find_init() now has the local constant you proposed: const size_t key_len_size = sizeof_field(hfsplus_btree_key, key_len); HFS_KEY_LEN_MAX_SIZE is gone from hfs as well. Its local is sizeof(u16), because the key length is a u16 in HFS_TREE_BIGKEYS trees, and a comment says so. The allocation size is unchanged in both drivers. - key_len < 1: it came in with bb5e07cb9277 ("hfs/hfsplus: fix slab-out-of-bounds in hfs_bnode_read_key"), and neither the commit message nor its thread says why. As far as I can see it is never true. In hfsplus key_len is either hfs_bnode_read_u16() + 2 (2 to 65537) or max_key_len + 2. In hfs it is hfs_bnode_read_u8() + 1 or max_key_len + 1. hfs_bnode_read() zeroes the buffer before it checks the offset, so even a bad offset gives a length of 2 (1 in hfs). The original patch printed key_len with %d, so it was probably an int at the time, but even then 0 and negative values could not reach the check. Now that key_len is a u32, a negative value is not possible at all. I left the check as it is so the fixes change only what they need to. If you agree, it can go in a separate cleanup. - 3/3, fd->key_capacity in hfs_btree_inc_height(): no, that function only gets the tree, while its callers have the hfs_find_data. The key_size it reads from disk is used only to copy the key from the old root to the new root node and to place the child pointer. hfs_bnode_copy() and hfs_bnode_write() clamp that to the node through check_and_correct_requested_length(), and nothing is copied into search_key or key there. The tree-level bound would be max_key_len + 2, the same value as key_capacity. Rejecting a longer key there changes behaviour, so it belongs in its own fix, not in this no-functional-change cleanup. I can send that separately. - 3/3, map_node_bits in hfsplus/xattr.c is now (node_size - HFS_BNODE_DESC_SIZE - (sizeof(u16) * 2) - 2) * 8. - for-next: v4 did conflict with "hfs: fix orphaned b-tree node in hfs_bnode_split()". v5 is based on for-next (d536283b12c4). 1/3 and 2/3 applied unchanged. In 3/3, hfs_bnode_split() in hfs now has the node_desc_size local from that commit, and it is converted the same way as the hfsplus one. Tested on for-next d536283b12c4 (7.4-rc1) with KASAN in QEMU, with and without the series: - hfsplus crafted images: without the series, the mount of the first image fails with EINVAL after the overflow has corrupted the heap. KASAN then reports a use-after-free in rcu_cblist_dequeue() and the kernel oopses on a NULL pointer. With the series the key is rejected with "Invalid key length: 516", there is no KASAN report, and the two other images mount. - hfsplus image made by mkfs.hfsplus: create, rename, delete, fill to ENOSPC, xattrs, appends and remount work, and fsck.hfsplus reports it clean, with and without the series. - hfs image made by hformat: 400 creates, 200 deletes, fragmented appends and remount work, and hfsutils reads the result, with and without the series. checkpatch --strict shows no errors. The remaining items are the "Unknown commit id" warning for 1da177e4c3f4, which my shallow clone does not have, and the extern CHECK on the hfs prototype, which is kept to match its neighbours. Each patch builds on its own with W=1 and no warnings. 1/3 and 2/3 still apply to 6.12.y and 6.6.y, except for the same context conflict in hfs_find_init() as in v4. v4: https://lore.kernel.org/all/20261127045416.1160-1-ozgunkultekin@gmail.com/ v3: https://lore.kernel.org/all/20261119005943.37851-1-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/ Ozgun Kultekin (3): hfsplus: pass the key buffer size to hfs_bnode_read_key() hfs: pass the key buffer size to hfs_bnode_read_key() hfs/hfsplus: name the node descriptor size and the first record offset fs/hfs/bfind.c | 15 ++++++++-- fs/hfs/bnode.c | 9 +++--- fs/hfs/brec.c | 49 +++++++++++++++++++------------- fs/hfs/btree.c | 9 +++--- fs/hfs/btree.h | 4 ++- fs/hfsplus/bfind.c | 11 ++++++-- fs/hfsplus/bnode.c | 9 +++--- fs/hfsplus/brec.c | 57 +++++++++++++++++++++++--------------- fs/hfsplus/btree.c | 9 +++--- fs/hfsplus/hfsplus_fs.h | 19 +++++++------ fs/hfsplus/xattr.c | 8 +++--- include/linux/hfs_common.h | 6 ++++ 12 files changed, 129 insertions(+), 76 deletions(-) base-commit: d536283b12c4597f8fc8cc65a034edb8753a9880 -- 2.39.5