From: Viacheslav Dubeyko <slava@dubeyko.com>
To: Shawn Shao <shawnq1@163.com>
Cc: John Paul Adrian Glaubitz <glaubitz@physik.fu-berlin.de>,
Yangtao Li <frank.li@vivo.com>,
linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH v3] hfsplus: reorder complete combining runs for dentry keys
Date: Tue, 01 Dec 2026 16:37:57 -0800 [thread overview]
Message-ID: <4d40982a598dff9bc50fccbaee34a313fb896fb0.camel@dubeyko.com> (raw)
In-Reply-To: <179613469562.187341.17619275012718061046.1796134695.7-1-shawnq1@163.com>
On Tue, 2026-12-01 at 14:18 +0000, Shawn Shao wrote:
> Hello,
>
> Thanks for asking for a clearer explanation. HFSPLUS_MAX_STRLEN is
Please, send it as formal patch.
> the
> maximum number of UTF-16 code units in an HFS+ catalog name. The
> catalog
> conversion decomposes and canonically reorders names within this
> bound.
> Dentry hashing and comparison must use the same ordering for every
> name
> that the catalog conversion accepts.
>
> The iterator's 32-unit limit splits a longer combining run. If a
> later
> mark has a lower combining class than marks in the earlier chunk, it
> cannot be moved before those marks. Two spellings that convert to the
> same catalog key can then get different dentry hashes or compare
> unequal.
> Using HFSPLUS_MAX_STRLEN lets the iterator reorder any run within the
> accepted catalog-name limit; it does not increase that limit.
OK. I think that bug takes place. But I cannot accept the fix.
The run[] grows from 64 to 510 bytes per iterator.
hfsplus_compare_dentry() keeps two iterators (it1, it2) on its stack,
so it needs about 1.1 KB. It runs as ->d_compare in RCU path walk,
often deep in the call stack.
Canonical reordering is a stable sort by combining class. A long run
can be produced in order without buffering it: remember where the run
starts in the input, then for each class present, in ascending order,
rescan the run and emit the marks of that class. That uses constant
memory and only costs anything for these pathological runs. A simpler
alternative: keep the 32-unit fast path and, when a run overflows,
decompose the whole name in a slow path. It has to work without
sleeping in RCU walk; on failure it can return -ECHILD from a non-RCU
context, or the reorder can be bounded some other way.
Please, rework the patch.
Thanks,
Slava.
>
> The KUnit test uses two spellings that differ only in the order of a
> 33-mark combining run. `hfsplus_asc2uni()` stores both as the same
> canonical key, so hashing and dentry comparison must also treat them
> as
> equal. With the old 32-unit limit, the iterator sorts the run in two
> pieces and the test catches the resulting hash/comparison mismatch.
> This exercises the exact boundary that caused the bug without needing
> a
> mounted image.
> The QEMU reproducer also checks the lookup behavior before and after
> the
> change.
>
> Tested source:
> HEAD commit: d536283b12c4597f8fc8cc65a034edb8753a9880
> Git tree: 7399834a6b1da5c9c4c420886536a105b63eff89
> Source: HFS for-next, Linux 7.4.0-rc1
> Compiler: gcc 11.4.0, GNU ld 2.38
> QEMU: 6.2.0, x86-64 TCG
>
> Reproducer and test results:
> https://gist.github.com/shaos26/73b61181ce48283492d2a10f69bb11eb
>
> console output:
> https://gist.githubusercontent.com/shaos26/73b61181ce48283492d2a10f69bb11eb/raw/e7d209d77bd88674ea5306b437539d8f1c02693b/console-output.txt
> kernel configuration:
> https://gist.githubusercontent.com/shaos26/73b61181ce48283492d2a10f69bb11eb/raw/e7d209d77bd88674ea5306b437539d8f1c02693b/kernel-config.txt
> userspace reproducer:
> https://gist.githubusercontent.com/shaos26/73b61181ce48283492d2a10f69bb11eb/raw/e7d209d77bd88674ea5306b437539d8f1c02693b/poc.c
> HFS+ image builder:
> https://gist.githubusercontent.com/shaos26/73b61181ce48283492d2a10f69bb11eb/raw/e7d209d77bd88674ea5306b437539d8f1c02693b/make_hfsplus.c
> initramfs init:
> https://gist.githubusercontent.com/shaos26/73b61181ce48283492d2a10f69bb11eb/raw/e7d209d77bd88674ea5306b437539d8f1c02693b/init
> initramfs builder:
> https://gist.githubusercontent.com/shaos26/73b61181ce48283492d2a10f69bb11eb/raw/e7d209d77bd88674ea5306b437539d8f1c02693b/build-initramfs.sh
> QEMU runner:
> https://gist.githubusercontent.com/shaos26/73b61181ce48283492d2a10f69bb11eb/raw/e7d209d77bd88674ea5306b437539d8f1c02693b/run-qemu.sh
> KUnit configuration:
> https://gist.githubusercontent.com/shaos26/73b61181ce48283492d2a10f69bb11eb/raw/e7d209d77bd88674ea5306b437539d8f1c02693b/kunit-config.txt
> KUnit results:
> https://gist.githubusercontent.com/shaos26/73b61181ce48283492d2a10f69bb11eb/raw/e7d209d77bd88674ea5306b437539d8f1c02693b/kunit-results.txt
>
> Build the HFS for-next tree at the tested revision and run the
> baseline:
>
> git clone \
>
> https://git.kernel.org/pub/scm/linux/kernel/git/vdubeyko/hfs.git linu
> x
> cd linux
> git checkout d536283b12c4597f8fc8cc65a034edb8753a9880
> mkdir build
> cp ../kernel-config.txt build/.config
> make O=build olddefconfig
> make O=build -j"$(nproc)" bzImage
> cd ..
> gcc -O2 -Wall -Wextra -Werror -o make_hfsplus make_hfsplus.c
> ./make_hfsplus hfsplus.img
> sh ./build-initramfs.sh initramfs.cpio.gz
> sh ./run-qemu.sh linux/build/arch/x86/boot/bzImage \
> initramfs.cpio.gz hfsplus.img baseline.log vulnerable
>
> The baseline reports POS_BAD_AFTER_CREATE=0 and
> POS_BAD_AFTER_REMOUNT=1; the 31-mark control resolves immediately.
> After applying the patch below and rebuilding, run:
>
> sh ./run-qemu.sh linux/build/arch/x86/boot/bzImage \
> initramfs.cpio.gz hfsplus.img fixed.log fixed
>
> The fixed run reports POS_BAD_AFTER_CREATE=1 and the fixed oracle
> passes.
> The x86-64 build also completed with CONFIG_FRAME_WARN=1280 and
> KCFLAGS=-Wframe-larger-than=1280 without frame-size warnings. Run the
> KUnit regression suite with:
>
> cd linux
> python3 tools/testing/kunit/kunit.py run \
> --build_dir=build-kunit --kunitconfig=../kunit-config.txt \
> --jobs="$(nproc)" hfsplus_unicode --summary
>
> The hfsplus_unicode suite passed all 36 tests.
>
> Changes in v3:
> - Explain why the iterator limit follows HFSPLUS_MAX_STRLEN.
> - Base the patch on the HFS for-next tree and use its renamed KUnit
> file.
> - Make the reproducer report separate vulnerable and fixed oracles.
>
> Fixes: bbe5ebcbc006 ("hfsplus: canonicalize decomposed catalog names
> like fsck_hfs expects")
> Thanks,
> Shawn Shao
>
> Signed-off-by: Shawn Shao <shawnq1@163.com>
> ---
> fs/hfsplus/hfsplus_unicode_test.c | 73
> +++++++++++++++++++++++++++++++++++++++
> fs/hfsplus/unicode.c | 19 +++++------
> 2 files changed, 80 insertions(+), 12 deletions(-)
>
> diff --git a/fs/hfsplus/hfsplus_unicode_test.c
> b/fs/hfsplus/hfsplus_unicode_test.c
> index cb237a40c..80ba1d499 100644
> --- a/fs/hfsplus/hfsplus_unicode_test.c
> +++ b/fs/hfsplus/hfsplus_unicode_test.c
> @@ -1533,6 +1533,78 @@ static void
> hfsplus_hash_dentry_combining_reorder_test(struct kunit *test)
> free_mock_sb(mock_sb);
> }
>
> +static void hfsplus_dentry_combining_reorder_long_test(struct kunit
> *test)
> +{
> + static const char acute[] = "\xcc\x81";
> + static const char dot_below[] = "\xcc\x96";
> + enum { NR_ACUTE = 32, NAME_LEN = 1 + (NR_ACUTE + 1) * 2 + 1
> };
> + char acute_first[NAME_LEN], dot_first[NAME_LEN];
> + struct hfsplus_unistr *stored1, *stored2;
> + struct test_mock_sb *mock_sb;
> + struct qstr str1, str2;
> + char *p;
> + int i, result;
> +
> + mock_sb = setup_mock_sb();
> + KUNIT_ASSERT_NOT_NULL(test, mock_sb);
> +
> + stored1 = kunit_kzalloc(test, sizeof(*stored1), GFP_KERNEL);
> + KUNIT_ASSERT_NOT_NULL(test, stored1);
> + stored2 = kunit_kzalloc(test, sizeof(*stored2), GFP_KERNEL);
> + KUNIT_ASSERT_NOT_NULL(test, stored2);
> +
> + setup_mock_dentry(&mock_sb->sb);
> + mock_sb->nls.char2uni = test_char2uni_utf8;
> +
> + p = acute_first;
> + *p++ = 'e';
> + for (i = 0; i < NR_ACUTE; i++) {
> + memcpy(p, acute, sizeof(acute) - 1);
> + p += sizeof(acute) - 1;
> + }
> + memcpy(p, dot_below, sizeof(dot_below) - 1);
> + p += sizeof(dot_below) - 1;
> + *p = '\0';
> +
> + p = dot_first;
> + *p++ = 'e';
> + memcpy(p, dot_below, sizeof(dot_below) - 1);
> + p += sizeof(dot_below) - 1;
> + for (i = 0; i < NR_ACUTE; i++) {
> + memcpy(p, acute, sizeof(acute) - 1);
> + p += sizeof(acute) - 1;
> + }
> + *p = '\0';
> +
> + create_qstr(&str1, acute_first);
> + create_qstr(&str2, dot_first);
> + result = hfsplus_hash_dentry(&test_dentry, &str1);
> + KUNIT_ASSERT_EQ(test, 0, result);
> + result = hfsplus_hash_dentry(&test_dentry, &str2);
> + KUNIT_ASSERT_EQ(test, 0, result);
> + KUNIT_EXPECT_EQ(test, str1.hash, str2.hash);
> +
> + KUNIT_EXPECT_EQ(test, 0,
> + hfsplus_compare_dentry(&test_dentry,
> str1.len,
> + str1.name, &str2));
> +
> + result = hfsplus_asc2uni(&mock_sb->sb, stored1,
> HFSPLUS_MAX_STRLEN,
> + str1.name, str1.len,
> HFS_REGULAR_NAME);
> + KUNIT_ASSERT_EQ(test, 0, result);
> + result = hfsplus_asc2uni(&mock_sb->sb, stored2,
> HFSPLUS_MAX_STRLEN,
> + str2.name, str2.len,
> HFS_REGULAR_NAME);
> + KUNIT_ASSERT_EQ(test, 0, result);
> + KUNIT_EXPECT_EQ(test, be16_to_cpu(stored1->length),
> + be16_to_cpu(stored2->length));
> + KUNIT_EXPECT_EQ(test, 34, be16_to_cpu(stored1->length));
> + KUNIT_EXPECT_EQ(test,
> + memcmp(stored1->unicode, stored2->unicode,
> + be16_to_cpu(stored1->length) *
> sizeof(stored1->unicode[0])),
> + 0);
> +
> + free_mock_sb(mock_sb);
> +}
> +
> /* Test hfsplus_compare_dentry basic functionality */
> static void hfsplus_compare_dentry_basic_test(struct kunit *test)
> {
> @@ -1935,6 +2007,7 @@ static struct kunit_case
> hfsplus_unicode_test_cases[] = {
> KUNIT_CASE(hfsplus_hash_dentry_consistency_test),
> KUNIT_CASE(hfsplus_hash_dentry_edge_cases_test),
> KUNIT_CASE(hfsplus_hash_dentry_combining_reorder_test),
> + KUNIT_CASE(hfsplus_dentry_combining_reorder_long_test),
> KUNIT_CASE(hfsplus_compare_dentry_basic_test),
> KUNIT_CASE(hfsplus_compare_dentry_casefold_test),
> KUNIT_CASE(hfsplus_compare_dentry_special_chars_test),
> diff --git a/fs/hfsplus/unicode.c b/fs/hfsplus/unicode.c
> index 59b8788a6..840a11560 100644
> --- a/fs/hfsplus/unicode.c
> +++ b/fs/hfsplus/unicode.c
> @@ -577,21 +577,15 @@ int hfsplus_asc2uni(struct super_block *sb,
> EXPORT_SYMBOL_IF_KUNIT(hfsplus_asc2uni);
>
> /*
> - * Maximum length of a single maximal run of nonzero-combining-class
> code
> - * units that hfsplus_decompose_iter_next() below will canonically
> - * reorder. Every code unit with combining class 0 starts a new run,
> so
> - * this only bounds how many *consecutive* combining marks between
> two
> - * base characters get sorted - real text, and even deliberately
> - * adversarial "Zalgo" text, essentially never approaches this.
> Keeping
> - * it small means hfsplus_hash_dentry() and hfsplus_compare_dentry()
> only
> - * ever need a tiny amount of lookahead state, rather than buffering
> an
> - * entire (up to 255-unit) name.
> + * A maximal combining run can contain every code unit in an HFS+
> catalog
> + * name. Keep the iterator limit aligned with the name limit so it
> can
> + * reorder the complete run like hfsplus_decompose_str().
> */
> -#define HFSPLUS_CCC_RUN_MAX 32
> +#define HFSPLUS_CCC_RUN_MAX HFSPLUS_MAX_STRLEN
>
> /*
> * Iterator that produces the canonically-ordered, decomposed code
> units
> - * of a Linux name one at a time, without materializing the whole
> name.
> + * of a Linux name one combining run at a time.
> * hfsplus_hash_dentry() and hfsplus_compare_dentry() use this so
> that
> * hashing and comparison always agree with what hfsplus_asc2uni()
> would
> * actually store, while still comparing lazily (stopping at the
> first
> @@ -630,7 +624,8 @@ static void hfsplus_decompose_iter_init(struct
> hfsplus_decompose_iter *it,
> * we know the class of the first unit it produces, so decoding it
> is
> * speculative until that's decided.
> */
> -static bool hfsplus_decompose_iter_refill(struct
> hfsplus_decompose_iter *it)
> +static noinline_for_stack bool
> +hfsplus_decompose_iter_refill(struct hfsplus_decompose_iter *it)
> {
> it->run_pos = 0;
> it->run_len = 0;
prev parent reply other threads:[~2026-12-02 0:38 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-26 16:07 [PATCH] " shaos
2026-11-27 3:52 ` kernel test robot
2026-11-27 17:53 ` Viacheslav Dubeyko
2026-11-30 7:48 ` [PATCH v2] " Shawn Shao
2026-11-30 21:12 ` Viacheslav Dubeyko
2026-12-01 14:18 ` [PATCH v3] " Shawn Shao
2026-12-02 0:37 ` Viacheslav Dubeyko [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=4d40982a598dff9bc50fccbaee34a313fb896fb0.camel@dubeyko.com \
--to=slava@dubeyko.com \
--cc=frank.li@vivo.com \
--cc=glaubitz@physik.fu-berlin.de \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=shawnq1@163.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®