From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.179 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796118172; b=06baK98v1kX7V39RkOroSnq+g6SA+xBE7eDHRXYXkLW5wkFHqdiKSM0sPG2fIj6PpZyR erUgLH3aNQZM0ZrQiGCnZpEV7BQGd1I1jMW1KTky8IsBYWpu0I9JNLr4WaGlqBp0rKdkk 7w2Mkd4LO+8uDCraQgmxXgMCJPWDHJ99acbLII2KvLqGRauu8gJTbfTw2x0a+UhxVji4R GvY90Q3EY1AfsR4bqsISr4rrp29zeA7dAxGukR7mr4zgY7Q6dVisCvgaD2Q86fCH4QQFW FrmNwzSUhYCPpIGHBe4flWZ/SGcYgf5Bpjzevnc85eVd67jMLPuf9VGN++5OqW0wGNw== 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-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=hZjf0KbNzmKj4ZF0+RF7FGgfkf63D3bu2i7NRaeYy3s=; b=k4FSfv08kPYqOxCYc4pOJqfP3pLXHXDnZOCZSl9Vln69n7Sagq4rSK3r+Lik/9OyIY4y NFVv7DDZERxxgcRmahgeNdgu4oCqGbiBs6BJHgv5EWZB3S2JFIWUUCxcD00jW6HsIeZuF Va7T4S4EqSWuHxb2KRagNMHTWTdEnxG7+QInjT1vMonYMcml/8JcUXcQpmXxWexxclAjA /jaWiUatGz4siu7LCp0+tpzTp/sGSOyZ5l16MSstumKjtmmzxO9Wy5celWw+nk41JtUXT /u2HJdRCy5GQZvcGQ42M55gyoyCM7BaZKT8GY5KwwFEPb1I3DzvfAvNuUdGQrUt0HXA== 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.179 Received: from mail-dy1-f179.google.com (mail-dy1-f179.google.com [74.125.82.179]) (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 CF70C606A327 for ; Tue, 1 Dec 2026 09:42:51 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org CF70C606A327 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-f179.google.com with SMTP id 5a478bee46e88-38692f62b22so92630eec.0 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=I2hj44XwXZnWA/iY4UlFLIrY4O3yFx+QuqO/ZjBTYo3KftP0KcDb2Xli+YbS6hyWGn yMHD+RVNTvTMrho6BG6STRenEy091ythhfFM4uWd+1ih2E4w49LG1/ryKJGeNSLzptH3 7HM6KcGtlk+WC1Bjm/WI/lesWympTaw/ARmwEeqBVu9Os2cPvEUytpDLSogEsZlmAJVl 7F7+kK6rqgNazbdyAIEArcw7Vtctzur3+Tm9W0BhRPBCT7US0SkY0TWHDegPOHjaiZmw lA6g1U5JYmF869IZ/a46Zz5h1/Co/zl1eYL5JOixo2A9excqlflQbigPS6n6yDetAFlq WgLQ== X-Forwarded-Encrypted: i=1; AL+hErF/XRV4wkod0aZFgD+X6FSrbEK5UkaABInCBMiwB9cI0RiySz8luGY4jOmUW5xie40jrRWsxIkMB3pSa7M=@vger.kernel.org X-Gm-Message-State: APuU4Z8we/zAGa1jVsJoRte8AJbv3ujkMyML/dB22SAgQPc0Dqp33cZa IUhPtao3OxHsHFoX6T31BZvr69sn42z/7j7vdJ7/RJH7uBFteIX+uP/x X-Gm-Gg: AZFdTZfvBBg1Z7nAKFohA38MXwVlzRXew0PeHTfPhzku9dUjWZIZXG7sHiH6JBuDeGr Zn0GHYTyTo/RVhyb56YYyY7jCsFuGUHtFas4P693+PzDmS6hLV3y27D5JS7wdWLdEno7CCgBsKp HEYrfyltGrip7veUY8/+WBprcK+ARBXydDLB3Cfp+6MjrtxLfrl14X/dcs412NFMxWcql/FwJfL MZDNocpmG5Ijn63mUeNqco05TgubFYuUbPlY5YIAdvtRGNOldtzGBO3rk6+HKb3T/EtuC4nqHsI B8im19IYmacfXNZfQoEt7ekbFIeW50hO0971zdu06pK3BQKKcNT8Q5Zd8FH28/M40LcGjNUaj7I NIwjglWJpSOF3x7V7LkTU3t9KKHJ0MZs5VNCcousd0CFFm8ix5FfwlgWofVjCEPXvjras5bMY8+ JiYy1PchDUrp6NGf0G1OP+8CSDuHq5h19dGKwxQVcSjt25AgqmrHRicP/VMGBM+/WsgYiOLwSeQ 9x0b7I5gt2C/514AI7xldG7o1D+hePirrQE7e2g5IGWu2htBIPJHsKG4JhS9TSy1nrQUADBgr0q EoJe3MpKgxKmBS76symmIWmb8KCI+BZhrWKVn5CQ9TYZWbVZLJNYPndbd19rVz/jqwMJlM2N5Z7 iODGJKulAlLIlWnHwP1gE9cK//7zrn7q6ELbLRTeurkduWQwf0g== 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-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() 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