Bug 28255

Summary: A locale with zero collation rules cause fnmatch, regexec, and regcomp failures.
Product: glibc Reporter: Carlos O'Donell <carlos>
Component: localeAssignee: Not yet assigned to anyone <unassigned>
Status: REOPENED ---    
Severity: normal CC: fweimer
Priority: P2 Flags: fweimer: security-
Version: 2.35   
Target Milestone: 2.35   
Host: Target:
Build: Last reconfirmed:

Description Carlos O'Donell 2021-08-20 22:05:15 UTC
It should be possible to build a locale with zero collation rules. Such a locale would fall back to using strcmp and wcscmp during collation.

Such a collation, one with zero collation rules, would also lack any data for _NL_COLLATE_COLLSEQMB, and _NL_COLLATE_COLLSEQWC. Any code using these tables must be gated on nrules != 0.

However, as-of today, the only such locale in glibc was the builtin C locale, and even though it has 0 collation rules, it has identity tables for _NL_COLLATE_COLLSEQMB, and _NL_COLLATE_COLLSEQWC.

These identity tables are used unconditionally by fnmatch, regexec, and regcomp, which means that when you don't generate them, the code does not properly handle regular expressions.

The fix for this is quite extensive, and I didn't want to include it in glibc 2.34, but the fixes are present here in v4 of my C.UTF-8 patch series:
https://patchwork.sourceware.org/project/glibc/patch/20210729063515.1541388-2-carlos@redhat.com/

The fix is also insufficient because statically linked applications would not have the fixes, so we must still keep the identity tables to allow static applications to continue working with the new binary locales (until we manage to make C.UTF-8 builtin).
Comment 1 Florian Weimer 2021-09-16 20:18:02 UTC
Fixed for glibc 2.35 via:

commit f5117c6504888fab5423282a4607c552b90fd3f9
Author: Carlos O'Donell <carlos@redhat.com>
Date:   Thu Jul 29 22:45:39 2021 -0400

    Add 'codepoint_collation' support for LC_COLLATE.
    
    Support a new directive 'codepoint_collation' in the LC_COLLATE
    section of a locale source file. This new directive causes all
    collation rules to be dropped and instead STRCMP (strcmp or
    wcscmp) is used for collation of the input character set. This
    is required to allow for a C.UTF-8 that contains zero collation
    rules (minimal size) and sorts using code point sorting.
    
    To date the only implementation of a locale with zero collation
    rules is the C/POSIX locale. The C/POSIX locale provides
    identity tables for _NL_COLLATE_COLLSEQMB and
    _NL_COLLATE_COLLSEQWC that map to ASCII even though it has zero
    rules. This has lead to existing fnmatch, regexec, and regcomp
    implementations that require these tables. It is not correct
    to use these tables when nrules == 0, but the conservative fix
    is to provide these tables when nrules == 0. This assures that
    existing static applications using a new C.UTF-8 locale with
    'codepoint_collation' at least have functional range expressions
    with ASCII e.g. [0-9] or [a-z]. Such static applications would
    not have the fixes to fnmatch, regexec and regcomp that avoid
    the use of the tables when nrules == 0. Future fixes to fnmatch,
    regexec, and regcomp would allow range expressions to use the
    full set of code points for such ranges.
    
    Tested on x86_64 and i686 without regression.
    
    Reviewed-by: Florian Weimer <fweimer@redhat.com>
Comment 2 Carlos O'Donell 2021-12-16 22:32:02 UTC
The commit f5117c6504888fab5423282a4607c552b90fd3f9 does not solve this issue.

The fix for this issue is to cleanup the regular expression range code:
https://sourceware.org/pipermail/libc-alpha/2021-July/129588.html

We dropped these fixes in the final C.UTF-8 implementation because it was possible to implement C.UTF-8 without fixing regexec et. al.

If we fix the regular expression range handling code it will then work for all code point ranges correctly e.g. use the wide character values as range end points.
Comment 3 Carlos O'Donell 2021-12-16 22:32:51 UTC
As of today C.UTF-8 is limited to ASCII ranges because of this fix it missing. And all static applications until recompiled will be limited to ASCII ranges.