|
|
Message-ID: <20261120122314.GH5716@brightrain.aerifal.cx>
Date: Fri, 20 Nov 2026 07:23:14 -0500
From: Rich Felker <dalias@...c.org>
To: Solar Designer <solar@...nwall.com>
Cc: Lukas Tribus <lukas@...i.eu>, musl@...ts.openwall.com
Subject: Re: crypt: unsupported hash behavior
On Fri, Nov 20, 2026 at 10:53:00AM +0100, Solar Designer wrote:
> On Thu, Nov 19, 2026 at 05:32:49PM -0500, Rich Felker wrote:
> > How does the attached version look?
> >
> > Rich
>
> > >From 5dd7e5c3e9b989fd0ff3aae0b5b5afe3ff93e438 Mon Sep 17 00:00:00 2001
> > From: Rich Felker <dalias@...ifal.cx>
> > Date: Thu, 12 Nov 2026 10:33:32 -0500
> > Subject: [PATCH] crypt: drop support for non-strict des salts in new hashes
> >
> > our descrypt implementation followed a historical practice of
> > interpreting salt characters outside the set [./0-9A-Za-z] as
> > alternate/noncanonical encodings. this served basically no purpose
> > except possibly supporting des hashes made on ancient systems
> > enforcing the salt character set. however, allowing this for new
>
> I guess you meant "not enforcing".
Yes, thanks for catching it.
> > hashes has several negative consequences, including:
> >
> > 1. unrecognized hash types encoded with "$n$" notation get interpreted
> > as very weak, fixed-value des salts.
> >
> > 2. with low but plausible probability, the result of des hashing a
> > salt consisting of a '$' followed by a digit can produce hash bits
> > that mimic the setting for a different hash type, resulting in a hash
> > that will be unmatchable later (user locked out).
> >
> > with this change, noncanonical salt characters are still interpreted
> > and result in a hash being computed, but the result is thrown away and
> > replaced with failure unless the full hash containing them was passed
> > as the setting and the password matches.
> >
> > the self-test in top-level __crypt_des is updated to canonicalize the
> > test salt; without this change, the self-test would always have
> > failed.
> > ---
> > src/crypt/crypt_des.c | 32 ++++++++++++++++++++++++++++++--
> > 1 file changed, 30 insertions(+), 2 deletions(-)
> >
> > diff --git a/src/crypt/crypt_des.c b/src/crypt/crypt_des.c
> > index 38e63395..90c0093a 100644
> > --- a/src/crypt/crypt_des.c
> > +++ b/src/crypt/crypt_des.c
> > @@ -672,6 +672,15 @@ static inline int ascii_is_unsafe(unsigned char ch)
> > return !ch || ch == '\n' || ch == ':';
> > }
> >
> > +/*
> > + * We only allow noncanonical salt characters for checking existing
> > + * hashes, not producing new ones.
> > + */
> > +static inline int ascii_is_noncanonical(unsigned char ch)
> > +{
> > + return ch!='.' && ch!='/' && (ch|32)-'a'>=26U && ch-'0'>=10U;
> > +}
> > +
> > static uint32_t setup_salt(uint32_t salt)
> > {
> > uint32_t obit, saltbit, saltbits;
> > @@ -978,6 +987,25 @@ static char *_crypt_extended_r_uut(const char *_key, const char *_setting, char
> > *p++ = ascii64[l & 0x3f];
> > *p = 0;
> >
> > + /*
> > + * Fail non-canonically-encoded salts unless the entire
> > + * matching hash was passed as setting.
> > + */
> > + if (output[0] != _PASSWORD_EFMT1 &&
> > + (ascii_is_noncanonical(output[0]) ||
> > + ascii_is_noncanonical(output[1]))) {
> > + if (strlen(_setting) != 13) return NULL;
> > + /*
> > + * Intended as constant-time to avoid disclosing length
> > + * of partial hash match, which may be sensitive for DES.
> > + * In any case, only executed for non-canonical salts.
> > + */
> > + unsigned sum = 0;
> > + for (i = 0; i < 13; i++)
> > + sum += (output[i] - _setting[i]) & 255;
>
> I'd write this using the canonical OR/XOR approach instead of ADD/SUB...
> but perhaps you had reasons? Signed types and integer promotion, trying
> to reduce risk of this getting optimized to a conditional branch?
>
> Anyway, I looked at the generated code for your version and it's good,
> so let's leave it as you wrote it above.
>
> Incidentally, the "& 255" is in fact needed when we ADD rather than OR
> because otherwise a subsequent addition of a negative value could bring
> the result back to zero.
I wrote this version without looking and compared afterwards with what
others do where I saw the xor. I like my approach better because the
xor version admits trivial short-circuiting of the loop if the
compiler sees that the predicate is permanently satisfied once it's
satisfied once.
With addition, the compiler needs to be able to prove that the sum can
never wrap back to zero, which requires range analysis and use of the
number of loop iterations. This can be done too because the number of
iterations is visible/constant, but it's way beyond what any compilers
are known to do.
> > + if (sum) return NULL;
> > + }
> > +
> > return output;
> > }
> >
> > @@ -992,8 +1020,8 @@ char *__crypt_des(const char *key, const char *setting, char *output)
> > const char *p;
> >
> > if (*setting != _PASSWORD_EFMT1) {
> > - test_setting = "\x80x";
> > - test_hash = "\x80x22/wK52ZKGA";
> > + test_setting = "Gx";
> > + test_hash = "Gx22/wK52ZKGA";
> > }
> >
> > /*
>
> Not addressing the issue of leaving the hash in memory yet, but I assume
> that's intentional not to address in this same patch?
Yeah I wasn't sure what to best do with that. I thought of having the
loop that computes the sum also xor the setting string onto the
output, which would be a great way to keep a dependency and avoid
short-circuiting the loop, but that leaks setting (good hash) on
failure which is even worse. I guess I could just make the loop
overwrite output as it runs -- that might be worthwhile.
Rich
Powered by blists - more mailing lists
Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.