From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.224.44 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796341728; b=E9U9H+Mg4a4gW/Sv7Q+PjNI4QF+0Nd4E/+hqMPPBQ3qDeCOv6aRPACksN5XPng1t3g2l vv2G3huTs/+FQsdzRhOoBDGi4gencOKqspDPrh9HL/G2rvjz1XvFuGBz3KxRDu8PhWPh4 wQ7tJSZJuxhQXyLbl+hel+lvc9rSJDGdI/2OwlJgDDmuDbPEuSb+q2nFQXf8F+3TKyHjm 8pkTdENc/ut53rk9G2jmnP7lnTui8W77nMSlXRRiJ7S4AO3qBL7zmUIhL3ndXpehWXaqO wzIrRih6y5+48QVPBfncpEeBFJI+qGgb4EGb4PJLtr1T8C+a1UgfMNg5innUrRRZILg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796341728; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: Message-ID:Subject:From:To:Cc:Date:In-Reply-To:References:Autocrypt: Content-Type:Content-Transfer-Encoding:User-Agent:MIME-Version; bh=XHi8+14T0Oo1ABoTseyzrOQm1zwxpUfSoOnky8YpfaA=; b=5UqszcCnUIzOFmsgBtC8v0EjSGTupGMw/fPwYK7cMpKxBTJj/mR82xkRus1L7JYuNyAN H2kTCqRux82K9w6JZnw0zXdUk64EgyeyooEhjyOZd+2jFPdPkrpJymq03dnPWRpd2ecWf bR50QN3hoJ5s3n+STBtWkLpb3qccYr3GoVd/cT1mrPgN6jBMtQwmZoFTT7tZylShvqRPO DDeCdVcC1HvF56NCQoYy28qNy2GoAUry1PVsM0bRpBF4t4iZXSNeUBJ8QKKwIS2EEcJQ+ UugS86RAHqka31jTs7kejiZq7cTtWRvnEVA0BGpmuomZg9w6GUwjndzOkbedUundZOA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none header.from=dubeyko.com; spf=pass smtp.mailfrom=dubeyko.com; dkim=pass header.d=dubeyko-com.20260707.gappssmtp.com header.i=@dubeyko-com.20260707.gappssmtp.com header.a=rsa-sha256 header.s=20260707 header.b=bscGeC5z; arc=none smtp.remote-ip=74.125.224.44 Received: from mail-yx1-f44.google.com (mail-yx1-f44.google.com [74.125.224.44]) (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 96A7B606A32F for ; Thu, 3 Dec 2026 23:48:47 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 96A7B606A32F 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.20260707.gappssmtp.com header.i=@dubeyko-com.20260707.gappssmtp.com header.a=rsa-sha256 header.s=20260707 header.b=bscGeC5z Received: by mail-yx1-f44.google.com with SMTP id 956f58d0204a3-67dae7d3f7dso399096d50.2 for ; Thu, 03 Dec 2026 15:48:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=dubeyko-com.20260707.gappssmtp.com; s=20260707; t=1796341726; x=1796946526; 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=XHi8+14T0Oo1ABoTseyzrOQm1zwxpUfSoOnky8YpfaA=; b=bscGeC5zUSVDs/eFQ2u+YRIgq2EIitTwAOoSpwWEmbPjU9yAQC2SbDoMpJCFX+Z/fD C9LOABfSC3FbxfK1BshCUbaWJ1L9HbV0LXFP9fH1j4hM46d5bd7Kuj+IPCjLajxlEgl/ CuUvts3KjuF5ifbJm+6cA4IM6yWQiKcX9bZOpHRcq3bE4Kq5SRtkhH+Pr/wFP7OJdJr6 dBMG6Pzq6TAyTBg1V3omRkTSr8K42MMbM1RW/2Qtqw3WToRDIlaBPtB2wXOJdhrVfdzv F+pioVPdqRGQGYZ3NfMvLthcdVcZspLJ2ORFS0AvqMDays6P8htLBEdfQl8iJrXHhXB4 My2Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796341726; x=1796946526; 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=XHi8+14T0Oo1ABoTseyzrOQm1zwxpUfSoOnky8YpfaA=; b=wtIVgR0mVAQinc34FQ+2ptG62W8p+ijq7OMxG6N72RUqwMTqchenkdJBgQ7St1yQ6r rUvf3sWADQqePaW4FcozgZC5JzdDxxu0mGiZbuFv1LCVgqDvkv8ssjpVDPxkLmtgLzXN UzWG1JtBj0GfDisFmnJkGkcL71ABz0z8lEsYL4pyn5XKnqQ6Fk0ohm6EvBNXt3229NB2 q5TtOdwLOkIImzMXSCuD858B90VcR74uyF/xKnk3dC/pNZEme150Xn4u/rd/sXvR8pt4 SrQw7DdT24+EqmgQM/jSyAaSSb3qKBafaELK4BovqBM2JHRqiAXMvSf43ViSMb3jP+Zu fcVg== X-Forwarded-Encrypted: i=1; AL+hErGDbygupRkewKq7gWq0BdL3irbvXrZrVkRasaE3hHNb0Y3P4bMR7p1LylbIRcEXBJzPNKG685P8Rq9lykU=@vger.kernel.org X-Gm-Message-State: APuU4Z872IIyR4GL9WWsP4+XIvWZJszuHeeEbKbrofV3QTj1oRRCagEa NSLSHGwm9w71Nc7YeD3QUpOLGBdavHzJZujBZLNJAuOr1QvmUKxrsDj2wHi01GVpxDk= X-Gm-Gg: AZFdTZdtYBWzoEyOMEv6BtBJxnWi6ZgOB6QGvfUeYmsQq8WBgWAbNEg9SBRzgrW1IiR rHTYEtj62NMkMul/C6UShRzr7MgnY25qAVqfIdzVPwlhtu62N7/PN6/9R3wkyeQfa++9RtUMt9n On5yQEe8NKOVF9JZSwbDvRM3rpxq70Ei6nZiFY00z+w8cDGcAep9fXzG7zDTIAQsuROcSOfENHZ T0ohBRX0jfSHZA8pRGUWHgCLWqlqxPFCgV5e2xwMC9h0jR2p0cW6yu3wJGjiVqncWIoBOf7MiJg aCTwGYC9ICRbfYjTf1lMTkmoaBxAWdPYUB9FCGTxwLaIPqaU9IUa5U1luWR08Yrm5MgWoq6rGpa deooP14xJByvMDb7uB+phhsmL7CiF2cxtA6BACvNhtYLGO8/duEOLruyURydxoRw04LWmLBiUA5 mVF2v27QoK4/nxuboVuR7SJrOud23qcEh54g4G5HeTR6HQOpXUjjatffihi2Hedg1ksbcg/n702 3lczSmMeBPBkVecwdFEjpNhDmh1IblrTtA7PUdRayL6tNpceDmv/XnxK84VdW1GLNbBBipIuBIW +vQGgEi9XGGanL/AMgtE9CcjMyTXVsfqKYL8S22D8EzhmHXnB/56vG05c2WrPtZnRSZ3t2vnwF0 /mtaR/DkC8uDpIyx8VqeNnmU5QdYEIUMunUuD054gcWdSjjs8U3cvC8BQedp+CfAXaV5IOpyfMj c9IcQ07A2JjOAXfU2bmyA= X-Received: by 2002:a05:690e:bc8:b0:67b:fe2a:42e with SMTP id 956f58d0204a3-67f7249526amr127983d50.154.1796341726479; Thu, 03 Dec 2026 15:48:46 -0800 (PST) Received: from ?IPv6:2600:1700:6476:1430:8a82:9efd:4df6:308d? ([2600:1700:6476:1430:8a82:9efd:4df6:308d]) by smtp.gmail.com with ESMTPSA id 956f58d0204a3-67f0cd1b54fsm343093d50.15.2026.12.03.15.48.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 03 Dec 2026 15:48:45 -0800 (PST) Message-ID: <03ff15b3673a0a027b7a9abaa7ac14d86ce6fae7.camel@dubeyko.com> Subject: Re: [PATCH] hfs: validate catalog key name length From: Viacheslav Dubeyko To: Kyuwon Cho , John Paul Adrian Glaubitz , Yangtao Li Cc: linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org Date: Thu, 03 Dec 2026 15:48:44 -0800 In-Reply-To: <179619831256.1663618.8521911735854986516@skku.edu> References: <179619831256.1663618.8521911735854986516@skku.edu> 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+kjMBHaiJ6B7BIlDmlffyR4f3oR/5hxgy dvIxMocqyc03xVyM6tA4ZrshKkwDgZIFEKkx37ec22ZJczNwGywKQW2TGXUTZVbdooiG4tXbRBLxe ga/NTZ52ZdEkSxAUGw/l0y0InTtdDIWvfUT+WXtQcEPRBE6HHhoeFehLzWL/o7w5Hog+0hXhNjqte fzKpI2fWmYzoIb6ueNmE/8sP9fWXo6Av9m8B5hRvF/hVWfEysr/2LSqN+xjt9NEbg8WNRMLy/Y0MS p5fgf9pmGF78waFiBvgZIQNuQnHrM+0BmYOhR0JKoHjt7r5wLyNiKFc8b7xXndyCDYfniO3ljbr0j tXWRGxx4to6FwARAQABtCpWaWFjaGVzbGF2IER1YmV5a28gPFNsYXZhLkR1YmV5a29AaWJtLmNvbT 6JAlQEEwEK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WMFKVK 4P5XGnHU9A2JOtRhnOWQfPTgFhy69BSYyOeG3e5J+xPyUUBeHajbMVPKnD10NMCx8Ux1D2bv8OWbv 3DvpUNx9D46UhBmkwIfrQWBkr0OGD2AXFfPqo08c3jcJMOAEc2vBI08Fij9o1gHwP2suFaG0X6jUN sy6KPOKg7TVMXtMzxNsKGyFxC1pG7lv+oaEBT6TJSVs/pm71ZrOezQANRzvBeXNUixWienTdAEG8a x4uOf3EF5WvYUhDiY8XBJyRv2ZnKJmBxZ3jPStadSuh3Bmo4mNgRVpFXrfb7h8oJC1l1WGdJa9aWi xUedku0/nH+nZMTqrtWYkXoLxaMswVM5S4f3pYKd7VlHxtlZiQ95RqQd5uWYcEN6xaSvAO3Cs4Co8 D6etTHtKPcx1+d0x6BvkBvqNJ5yCiuzPPnKE6E0ss52KKvQt3injPyfe8ffofbW4s2nlEL1s0DoBv JslCebuQ6+Orb/Sl6zictIsv3XMq3jGFeZcDg2hXzngOC255+b0Z/To7jINTxYQot8jCOJQOLplxc NFdvziOa4FgUBC4X71upsZIoZJeii2V/Jtc3B4g== Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable User-Agent: Evolution 3.56.2-9 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 On Fri, 2026-12-04 at 01:53 +0900, Kyuwon Cho wrote: > A catalog key carries both an outer key length and an embedded Pascal > string length. hfs_brec_keylen() validates the former, but > hfs_cat_keycmp() trusts the latter when comparing names. A corrupted > catalog can therefore make hfs_strcmp() read beyond the key buffer. >=20 > Require the two lengths to describe the same key before copying it > out > of the B-tree node. Thanks for the report. The issue is real: hfs_strcmp() doesn't limit the length at all, while the key buffers have room for HFS_NAMELEN (31) characters only. However, this patch breaks valid HFS volumes. Please, see my comments below. >=20 > Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2") > Signed-off-by: Kyuwon Cho > --- > The issue was found by an LLM-guided test generator that we are > developing as a research prototype. >=20 > The reproducer creates and mounts a crafted HFS image, then resumes a > directory read at a catalog record whose embedded name length exceeds > the key. Mounting the image requires root in the initial namespace. >=20 > The decoded trace is: >=20 > =C2=A0 BUG: KASAN: slab-out-of-bounds in hfs_strcmp (fs/hfs/string.c:87) > =C2=A0 Read of size 1 at addr ff11000011bd1ace by task test/115 > =C2=A0 Call Trace: > =C2=A0=C2=A0 hfs_strcmp (fs/hfs/string.c:87) > =C2=A0=C2=A0 __hfs_brec_find (fs/hfs/bfind.c:78) > =C2=A0=C2=A0 hfs_brec_find (fs/hfs/bfind.c:147) > =C2=A0=C2=A0 hfs_readdir (fs/hfs/dir.c:103) > =C2=A0=C2=A0 iterate_dir (fs/readdir.c:114) > =C2=A0=C2=A0 __x64_sys_getdents64 (fs/readdir.c:404 fs/readdir.c:389) >=20 > The patch was tested on v7.4-rc5 and rebased onto the HFS tree commit > a2c27efbbd43 ("hfsplus: fix UAF in hfsplus_cat_write_inode()"). >=20 > =C2=A0fs/hfs/brec.c | 17 +++++++++++++++++ > =C2=A01 file changed, 17 insertions(+) >=20 > diff --git a/fs/hfs/brec.c b/fs/hfs/brec.c > index 1ca1ee9f73ef..aa6ac734e926 100644 > --- a/fs/hfs/brec.c > +++ b/fs/hfs/brec.c > @@ -59,6 +59,23 @@ u16 hfs_brec_keylen(struct hfs_bnode *node, u16 > rec) > =C2=A0 } > =C2=A0 } > =C2=A0 } > + if (node->tree->cnid =3D=3D HFS_CAT_CNID) { We check this multiple times in multiple places. So, we need a spacial helper(s) fir likewise check. This check is executed for index nodes too. But HFS catalog index keys have fixed size: the key length byte is always max_key_len (37), independently of the name length. Mac OS creates the index keys this way, and so does the Linux driver: fs/hfs/brec.c, hfs_brec_insert(): fd->search_key->key_len =3D tree->max_key_len; fs/hfs/brec.c, hfs_btree_inc_height(): hfs_bnode_write_u8(new_node, 14, tree->max_key_len); So, for any index record with a name shorter than 31 characters, the check below fails, hfs_brec_keylen() returns 0, and __hfs_brec_find() returns -EINVAL. As a result, any catalog lookup that goes through an index node will fail. It means that any volume whose catalog has more than one leaf node becomes unusable. The existing code already handles fixed-size index keys in the first branch of hfs_brec_keylen(), but the new check ignores it. > + u8 key_len, name_len; > + > + recoff =3D hfs_bnode_read_u16(node, node->tree- > >node_size - > + =C2=A0=C2=A0=C2=A0 (rec + 1) * 2); > + if (!recoff) > + return 0; > + > + key_len =3D hfs_bnode_read_u8(node, recoff); The code above this check distinguishes HFS_TREE_BIGKEYS, but here the key length is always read as u8. A crafted header with BIGKEYS set for the catalog tree makes this read the wrong byte. Also, the record length is not checked before reading CName.len. > + name_len =3D hfs_bnode_read_u8(node, recoff + > + offsetof(struct hfs_cat_key, > CName.len)); > + if (key_len !=3D offsetof(struct hfs_cat_key, > CName.name) - > + sizeof(u8) + name_len) { Even for leaf records, equality is stricter than we need. For memory safety it is enough that the name fits into the key (name_len <=3D key_len - 6) and name_len <=3D HFS_NAMELEN. I would prefer not to reject old volumes because of the key padding. > + pr_err("invalid catalog name length\n"); Could you please make the message more informative (node ID, record index, key and name lengths)? > + return 0; > + } > + } > =C2=A0 return retval; > =C2=A0} > =C2=A0 Moreover, as with the HFS+ patch, hfs_brec_keylen() is not the only path that copies a key from a b-tree node. hfs_bnode_read_key() reads keys into fd->search_key in hfs_brec_insert() and hfs_brec_update_parent() without calling hfs_brec_keylen(), and hfs_cat_keycmp() trusts the name length of fd->search_key too. I think the simplest and most robust fix for HFS is to limit both name lengths to HFS_NAMELEN in hfs_cat_keycmp(), the same way as hfs_hash_dentry() already does. The fd->key and fd->search_key buffers are always max_key_len + 2 bytes long, so the comparison will never go beyond the buffer, independently of which path has filled it in. It doesn't affect fixed-size index keys at all. If we would like to report such records as corrupted, then an additional check (name_len <=3D key_len - 6) could be added for leaf records only. And could you please share the reproducer (or the crafted image)? Thanks, Slava.