BUG #19714: pgcrypto pgp_sym_encrypt accepts nonnumeric s2k-mode as mode 0

Поиск
Список
Период
Сортировка
Искать

BUG #19714: pgcrypto pgp_sym_encrypt accepts nonnumeric s2k-mode as mode 0

От:
PG Bug reporting form <noreply@postgresql.org>
Дата:
The following bug has been logged on the website:

Bug reference:      19714
Logged by:          Qifan Liu
Email address:      imchifan@163.com
PostgreSQL version: 18.6
Operating system:   Linux on amd64
Description:        

pgp_sym_encrypt accepts the malformed option s2k-mode=not_a_number and
produces usable ciphertext. The documented s2k-mode values are numeric modes
0, 1, and 3, so nonnumeric text should be rejected rather than silently
selecting mode 0. This can cause encryption to use a different string-to-key
mode than the caller specified. The impact is localized to pgcrypto option
validation.

Steps to reproduce
------------------
CREATE EXTENSION pgcrypto;

SELECT pgp_sym_decrypt(
         pgp_sym_encrypt('payload',
                         'key',
                         's2k-mode=not_a_number'),
         'key') = 'payload' AS malformed_s2k_mode_accepted;

Actual result
-------------
 malformed_s2k_mode_accepted
-----------------------------
 t
(1 row)

The malformed value is accepted, and the produced ciphertext decrypts
successfully.

Expected result
---------------
pgp_sym_encrypt should reject s2k-mode=not_a_number with an error because
s2k-mode accepts only the documented numeric values. It should not interpret
malformed text as mode 0 or produce ciphertext.

Additional information
----------------------
The issue was reproduced on PostgreSQL 20devel, PostgreSQL 18.6, and
PostgreSQL 17.11.
Inference: the behavior is consistent with numeric conversion that maps text
without a valid numeric prefix to zero before validating the resulting mode.




Re: BUG #19714: pgcrypto pgp_sym_encrypt accepts nonnumeric s2k-mode as mode 0

От:
shihao zhong <zhong950419@gmail.com>
Дата:
Вложения:
Hi

All the integer PGP options are parsed with atoi(), so trailing junk
("s2k-mode=3x") and values that wrap around int ("s2k-mode=4294967299"
gives 3) get through too.

This is not a common case. It only happens when the caller writes a bad
option string, and the caller could ask for mode 0 directly anyway, so
it is not a security problem. The one case worth fixing is s2k-mode.
Junk there gives the unsalted mode 0, and decryption still works, so
nobody would notice. "s2k-mode=salted" is an easy mistake to make,
since the other S2K options take names.

0001 parses the values with strtoint() and raises the existing "Illegal
argument to function" error. 0002 adds tests and is optional.

This makes some inputs that work today fail, so I'd keep it to master.
I can do back-branch versions if a committer wants it back-patched.

Shihao

Re: BUG #19714: pgcrypto pgp_sym_encrypt accepts nonnumeric s2k-mode as mode 0

От:
Sho Ito <i.sho.0628@gmail.com>
Дата:
Hi Shihao,

I reviewed v1 patches on master with assertions enabled. All
pgcrypto regression tests pass. Without 0001, all three new cases produce
ciphertext; with it, they raise the expected error.

I found no blocking issues. Looks good to me.

This makes some inputs that work today fail, so I'd keep it to master.
I can do back-branch versions if a committer wants it back-patched.

I agree with keeping this on master. Back-patching would make previously
accepted malformed integer options fail across the PGP functions.

Thanks,
Sho

--
=======================================
Sho Ito (Sean)

=======================================

Re: BUG #19714: pgcrypto pgp_sym_encrypt accepts nonnumeric s2k-mode as mode 0

От:
Daniel Gustafsson <daniel@yesql.se>
Дата:
Вложения:
> On 10 Nov 2026, at 06:18, Sho Ito  wrote:

> I found no blocking issues. Looks good to me.

I agree that this looks overall good, but I did tweak it slightly in the
attached v2 to add range checking as well as some minor cleanup.  I've yet to
do much more than test compile it, but it has the overall shape I was looking
for.

> This makes some inputs that work today fail, so I'd keep it to master.
> I can do back-branch versions if a committer wants it back-patched.

Still thinking about this, but off the cuff I'd prefer to backpatch.

--
Daniel Gustafsson

Re: BUG #19714: pgcrypto pgp_sym_encrypt accepts nonnumeric s2k-mode as mode 0

От:
shihao zhong <zhong950419@gmail.com>
Дата:
Hi Daniel,

Thanks for v2. expect-s2k-mode now goes through PARSE_INT_AS_BOOL, so
the existing expect-s2k-mode=3 tests in pgp-encrypt fail. 

It should use the same range as s2k-mode:

  ex->s2k_mode = parse_int_arg(val, PGP_S2K_SIMPLE, PGP_S2K_ISALTED);

> Still thinking about this, but off the cuff I'd prefer to backpatch.
Backpatching is fine with me. I think that should go back to 14

Thanks,
Shihao

Re: BUG #19714: pgcrypto pgp_sym_encrypt accepts nonnumeric s2k-mode as mode 0

От:
Daniel Gustafsson <daniel@yesql.se>
Дата:
Вложения:
> On 11 Nov 2026, at 02:03, shihao zhong  wrote:

> Thanks for v2. expect-s2k-mode now goes through PARSE_INT_AS_BOOL, so
> the existing expect-s2k-mode=3 tests in pgp-encrypt fail. 

Yes, fixed in the attached.

I also expanded the check for compression which allows the undocumented (and
not working) bzip2 option now as well which the previous coding did.  Attached
is what I have lined up for later today.

--
Daniel Gustafsson

FAQ