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

Started by PG Bug reporting form2 months ago6 messagesbugs
Jump to latest
#1PG 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.

#2shihao zhong
zhong950419@gmail.com
In reply to: PG Bug reporting form (#1)
Re: BUG #19714: pgcrypto pgp_sym_encrypt accepts nonnumeric s2k-mode as mode 0

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

Attachments:

t253898_2
v1-0002-pgcrypto-Add-tests-for-malformed-integer-PGP-opti.patchapplication/octet-stream; name=v1-0002-pgcrypto-Add-tests-for-malformed-integer-PGP-opti.patchDownload+19-1
v1-0001-pgcrypto-Reject-malformed-integer-values-in-PGP-o.patchapplication/octet-stream; name=v1-0001-pgcrypto-Reject-malformed-integer-values-in-PGP-o.patchDownload+34-17
#3Sho Ito
i.sho.0628@gmail.com
In reply to: shihao zhong (#2)
Re: BUG #19714: pgcrypto pgp_sym_encrypt accepts nonnumeric s2k-mode as mode 0

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)*

E-mail: i.sho.0628@gmail.com
LinkedIn: *https://www.linkedin.com/in/sho-ito-0628/
<https://www.linkedin.com/in/sho-ito-0628/>*
GitHub: https://github.com/sean0628/
=======================================

#4Daniel Gustafsson
daniel@yesql.se
In reply to: Sho Ito (#3)
Re: BUG #19714: pgcrypto pgp_sym_encrypt accepts nonnumeric s2k-mode as mode 0

On 10 Nov 2026, at 06:18, Sho Ito <i.sho.0628@gmail.com> 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

Attachments:

t253898_4
v2-0001-pgcrypto-reject-invalid-PGP-options.patchapplication/octet-stream; name=v2-0001-pgcrypto-reject-invalid-PGP-options.patch; x-unix-mode=0644Download+65-18
#5shihao zhong
zhong950419@gmail.com
In reply to: Daniel Gustafsson (#4)
Re: BUG #19714: pgcrypto pgp_sym_encrypt accepts nonnumeric s2k-mode as mode 0

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

#6Daniel Gustafsson
daniel@yesql.se
In reply to: shihao zhong (#5)
Re: BUG #19714: pgcrypto pgp_sym_encrypt accepts nonnumeric s2k-mode as mode 0

On 11 Nov 2026, at 02:03, shihao zhong <zhong950419@gmail.com> 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

Attachments:

t253898_6
v3-0001-pgcrypto-reject-invalid-PGP-options.patchapplication/octet-stream; name=v3-0001-pgcrypto-reject-invalid-PGP-options.patch; x-unix-mode=0644Download+67-18