Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rKcCr-003ZTq-7Q for pgsql-bugs@arkaria.postgresql.org; Tue, 02 Jan 2024 10:41:10 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1rKcCp-00FCcl-GN for pgsql-bugs@arkaria.postgresql.org; Tue, 02 Jan 2024 10:41:07 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rKc7G-00FCJ7-RJ for pgsql-bugs@lists.postgresql.org; Tue, 02 Jan 2024 10:35:23 +0000 Received: from [120.133.73.169] (helo=mail.esgyn.cn) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rKc7D-00DM84-0J for pgsql-bugs@lists.postgresql.org; Tue, 02 Jan 2024 10:35:21 +0000 Received: from mail.esgyn.cn (localhost [127.0.0.1]) by mail.esgyn.cn (Postfix) with ESMTP id 4T48PZ08vZz4SB25q for ; Tue, 2 Jan 2024 18:35:14 +0800 (CST) Authentication-Results: mail.esgyn.cn (amavisd-new); dkim=pass (2048-bit key) reason="pass (just generated, assumed good)" header.d=esgyn.cn DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=esgyn.cn; h=to :subject:from:user-agent:mime-version:date:message-id :content-type; s=dkim; t=1704191713; x=1706783714; bh=u0ARrvYoqn jRKAix6Pkp8SCRoP+8hZvVURtazdlaiFc=; b=cyRvJa++2w8Cj3ExYqsAeexew3 d/iEaII1q9nWbTxvX+FA7DmgsH6XV5YDB08U5WSdZ45h8kFyvDYSVurL8eFsrn0Y hrCtvuFNeqm3Kst/s0uPcxgtKaCH7wbFPhF/+AjybvzW/YYoGp03OWWmxEmInyZx FQ7TjI5QFBhRrEr6OD2MNjGQLHcLsjyaB8yRK4pFf9C+rgsqn6Jvwec+Bl7vEA2W h0n1ADgxa2LvFk+ae01w+qiozACfcvko54rVCdNNFIRn9c2hERewlTyUIGiaERzE /EfZ4a5l57LjZjbtJACAP6KRNSSthivwBYdPfQY7XeoT1vCB78oNrSZjuFkA== X-Virus-Scanned: amavisd-new at mail.esgyn.cn Received: from mail.esgyn.cn ([127.0.0.1]) by mail.esgyn.cn (mail.esgyn.cn [127.0.0.1]) (amavisd-new, port 10026) with ESMTP id sB7lukNvw8rV for ; Tue, 2 Jan 2024 18:35:13 +0800 (CST) Received: from [10.12.60.29] (unknown [43.243.137.210]) by mail.esgyn.cn (Postfix) with ESMTPSA id 4T48PY6Djdz4S9vTh for ; Tue, 2 Jan 2024 18:35:13 +0800 (CST) Content-Type: multipart/alternative; boundary="------------M0IxDv0glOaMi3Zo0OgBWPBB" Message-ID: Date: Tue, 2 Jan 2024 18:31:00 +0800 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:108.0) Gecko/20100101 Thunderbird/108.0 From: liulang Subject: lost status 'STATUS_EOF' for authentication when using 'MD5' or 'scram-sha-256' To: pgsql-bugs@lists.postgresql.org Disposition-Notification-To: liulang X-Host-Lookup-Failed: Reverse DNS lookup failed for 120.133.73.169 (failed) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk This is a multi-part message in MIME format. --------------M0IxDv0glOaMi3Zo0OgBWPBB Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: quoted-printable Hello: =C2=A0 I'm an extension writer and I wrote an extension to lock users wh= o=20 tried to enter the wrong password too much. This extension is hooking=20 'ClientAuthentication_hook' and checking the hook's 'status' parameter to count the times of wrong password. It=E2=80=99s work fine when auth is= =20 'password', but get double count when auth is'MD5'or'scram-sha-256'. problem reappearance: 1. *create an user without password* 2. set pg_hba.conf with =E2=80=98MD5=E2=80=99 or =E2=80=98scram-sha-256=E2= =80=99 3. use psql without -W or -w or .pgpass file or env PGPASSFILE 4. It's ok when client is pgadmin or another 5. all of version pg can reappearance when I read the source, find an incorret logical way on=20 CheckPWChallengeAuth at src/backend/libpq/auth.c Like =E2=80=8Bstatic int CheckPWChallengeAuth(Port *port, char **logdetail) { =C2=A0=C2=A0 ... =C2=A0=C2=A0 /* the CheckMD5Auth or=C2=A0CheckSCRAMAuth returns STATUS_E= OF because=20 psql is not provide password, =C2=A0=C2=A0=C2=A0 * It will prompt user to type and send the password t= o other=20 backend process next time. =C2=A0=C2=A0=C2=A0 * The current backend will exit normally. =C2=A0=C2=A0=C2=A0 */ =C2=A0=C2=A0 if (port->hba->auth_method =3D=3D uaMD5 && pwtype =3D=3D PA= SSWORD_TYPE_MD5) =C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0=C2=A0 auth_result =3D CheckMD5Auth(port,= shadow_pass, logdetail); =C2=A0=C2=A0 else =C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0=C2=A0 auth_result =3D CheckSCRAMAuth(por= t, shadow_pass, logdetail); =C2=A0=C2=A0=C2=A0 ... =C2=A0=C2=A0 =C2=A0/* =C2=A0=C2=A0 =C2=A0 * If get_role_password() returned error, return erro= r, even if the =C2=A0=C2=A0 =C2=A0 * authentication succeeded. =C2=A0=C2=A0 =C2=A0 */ =C2=A0=C2=A0=C2=A0 /* The get_role_password returns NULL when the user w= ithout password */ =C2=A0=C2=A0=C2=A0 if (!shadow_pass) =C2=A0=C2=A0 =C2=A0{ =C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0=C2=A0 Assert(auth_result !=3D STATUS_OK)= ; =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 /* lost STATUS_EOF */ *return STATUS_ERROR;* =C2=A0=C2=A0 =C2=A0} =C2=A0=C2=A0 ... } The above code does not affect the database execution,but=20 ClientAuthentication_hook will be confused whether the password is=20 incorrect or not currently entered? so.. The CheckPWChallengeAuth should returns STATUS_EOF when It is, I thi= nk. Try to change the code =E2=80=8Bstatic int CheckPWChallengeAuth(Port *port, char **logdetail) { =C2=A0=C2=A0 if (port->hba->auth_method =3D=3D uaMD5 && pwtype =3D=3D PA= SSWORD_TYPE_MD5) =C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0=C2=A0 auth_result =3D CheckMD5Auth(port,= shadow_pass, logdetail); =C2=A0=C2=A0 else =C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0=C2=A0 auth_result =3D CheckSCRAMAuth(por= t, shadow_pass, logdetail); =C2=A0=C2=A0=C2=A0 ... =C2=A0=C2=A0=C2=A0 if (STATUS_EOF =3D=3D auth_result) =C2=A0=C2=A0=C2=A0 { */* do nothing */* =C2=A0=C2=A0=C2=A0 } else if (!shadow_pass) =C2=A0=C2=A0 =C2=A0{ =C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0=C2=A0 Assert(auth_result !=3D STATUS_OK)= ; =C2=A0=C2=A0=C2=A0=C2=A0 return STATUS_ERROR; =C2=A0=C2=A0 =C2=A0} else if (auth_result =3D=3D STATUS_OK) =C2=A0=C2=A0=C2=A0 set_authn_id(port, port->user_name); return auth_result; } I don't know if this is right, please point out. thanks! --------------M0IxDv0glOaMi3Zo0OgBWPBB Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
Hello:

=C2=A0 I'm an exten= sion writer and I wrote an extension to lock users who tried to enter the wrong password too much. This extension is hooking 'ClientAuthentication_hook' and checking the hook's 'status' parameter
to count the times of wrong password. It=E2=80=99s work fine when auth is 'password', = but get double count when auth is'MD5'or'scram-sha-256'.

problem reappearance:

1. create an user without password
2. set pg_hba.conf with =E2=80=98MD5=E2=80=99 or =E2=80=98scram-sha-256=E2=80=99
3. use psql without -W or -w or .pgpass file or env PGPASSFILE
4.= It's ok when client is pgadmin or another
5.= all of version pg can reappearance

when I read the source, find an incorret logical way on CheckPWChallengeAuth at src/backend/libpq/auth.c

Like
=E2=80=8Bstatic int=
CheckPWChallengeAuth(Port *port, char **logdetail)
{
=C2=A0=C2=A0 ...
=C2=A0=C2=A0 /* the CheckMD5Auth or=C2=A0CheckSCRAMAuth returns STATUS_EOF because psql= is not provide password,
=C2=A0=C2=A0=C2=A0 = * It will prompt user to type and send the password to other backend process next time.
=C2=A0=C2=A0=C2=A0 = * The current backend will exit normally.
=C2=A0=C2=A0=C2=A0 = */

=C2=A0=C2=A0 if (port->hba->auth_method =3D=3D uaMD5 && pwtype =3D=3D PASSWORD_TYPE_MD5)
=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0=C2=A0 auth_result =3D CheckMD5Auth(= port, shadow_pass, logdetail);
=C2=A0=C2=A0 else
=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0=C2=A0 auth_result =3D CheckSCRAMAut= h(port, shadow_pass, logdetail);
=C2=A0=C2=A0=C2=A0 ...

=C2=A0=C2=A0 =C2=A0/*
=C2=A0=C2=A0 =C2=A0 * If get_role_password() returned error, return= error, even if the
=C2=A0=C2=A0 =C2=A0 * authentication succeeded.
=C2=A0=C2=A0 =C2=A0 */
=C2=A0=C2=A0=C2=A0 = /* The get_role_password returns NULL when the user without password */
=C2=A0=C2=A0=C2=A0 = if (!shadow_pass)
=C2=A0=C2=A0 =C2=A0{
=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0=C2=A0 Assert(auth_result !=3D STATU= S_OK);
=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0 /* lost STATUS_EOF */
=C2=A0=C2=A0 =C2=A0= =C2=A0=C2=A0=C2=A0 return STATUS_ERROR;
=C2=A0=C2=A0 =C2=A0}
=C2=A0=C2=A0 ...
}

The above code does not affect the database execution,but ClientAuthentication_hook will be confused whether the password is incorrect or not currently entered?
so.. The CheckPWChallengeAuth should returns STATUS_EOF when It is, I think.

Try to change the code
=E2= =80=8Bstatic int
CheckPWChallengeAuth(Port *port, char **logdetail)
{<= br>
= =C2=A0=C2=A0 if (port->hba->auth_method =3D=3D uaMD5 && pwtype =3D= =3D PASSWORD_TYPE_MD5)
=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0=C2=A0 auth_result =3D CheckMD5A= uth(port, shadow_pass, logdetail);
=C2=A0=C2=A0 else
=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0=C2=A0 auth_result =3D CheckSCRA= MAuth(port, shadow_pass, logdetail);
=C2=A0=C2=A0=C2=A0 ...

=C2=A0=C2=A0=C2=A0 if (STATUS_EOF =3D=3D auth_result)
= =C2=A0=C2=A0=C2=A0 {
= =C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 /* do nothing */
= =C2=A0=C2=A0=C2=A0 }
= =C2=A0=C2=A0=C2=A0 else if (!shadow_pass)
=C2=A0=C2=A0 =C2=A0{
=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0=C2=A0 Assert(auth_result !=3D S= TATUS_OK);
= =C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0=C2=A0 return STATUS_ERROR;
=C2=A0=C2=A0 =C2=A0}
= =C2=A0=C2=A0=C2=A0 else if (auth_result =3D=3D STATUS_OK)
= =C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 set_authn_id(port, port->user_name);
= =C2=A0=C2=A0=C2=A0 return auth_result;
= }
=
= I don't know if this is right, please point out. thanks!
--------------M0IxDv0glOaMi3Zo0OgBWPBB--