Re: Fix error handling in be_tls_open_server()

From: Sergey Shinderuk <s(dot)shinderuk(at)postgrespro(dot)ru>
To: Michael Paquier <michael(at)paquier(dot)xyz>, Daniel Gustafsson <daniel(at)yesql(dot)se>
Cc: Jacob Champion <jchampion(at)timescale(dot)com>, PostgreSQL Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Fix error handling in be_tls_open_server()
Date: 2023-09-19 08:06:25
Message-ID: c1db30cf-6731-37c5-83c7-ff8c8d798031@postgrespro.ru
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On 19.09.2023 03:54, Michael Paquier wrote:
> One doubt that I have is if we shouldn't let X509_NAME_print_ex() be
> as it is now, and not force a failure on the bio if this calls fails.

If malloc fails inside X509_NAME_print_ex, then we will be left with
empty port->peer_dn. Here is a gdb session showing this:

(gdb) b X509_NAME_print_ex
Breakpoint 1 at 0x7f539f6c0cf0
(gdb) c
Continuing.

Breakpoint 1, 0x00007f539f6c0cf0 in X509_NAME_print_ex () from
/lib/x86_64-linux-gnu/libcrypto.so.3
(gdb) bt
#0 0x00007f539f6c0cf0 in X509_NAME_print_ex () from
/lib/x86_64-linux-gnu/libcrypto.so.3
#1 0x000056026d2fbe8d in be_tls_open_server
(port=port(at)entry=0x56026ed5d730) at be-secure-openssl.c:635
#2 0x000056026d2eefa5 in secure_open_server
(port=port(at)entry=0x56026ed5d730) at be-secure.c:118
#3 0x000056026d3dc412 in ProcessStartupPacket
(port=port(at)entry=0x56026ed5d730, ssl_done=ssl_done(at)entry=false,
gss_done=gss_done(at)entry=false) at postmaster.c:2065
#4 0x000056026d3dcd8e in BackendInitialize
(port=port(at)entry=0x56026ed5d730) at postmaster.c:4377
#5 0x000056026d3def6a in BackendStartup
(port=port(at)entry=0x56026ed5d730) at postmaster.c:4155
#6 0x000056026d3df115 in ServerLoop () at postmaster.c:1781
#7 0x000056026d3e0645 in PostmasterMain (argc=argc(at)entry=20,
argv=argv(at)entry=0x56026ec5a0d0) at postmaster.c:1465
#8 0x000056026d2fcd7c in main (argc=20, argv=0x56026ec5a0d0) at main.c:198
(gdb) b malloc
Breakpoint 2 at 0x7f539eca5120: file ./malloc/malloc.c, line 3287.
(gdb) c
Continuing.

Breakpoint 2, __GI___libc_malloc (bytes=4) at ./malloc/malloc.c:3287
3287 ./malloc/malloc.c: No such file or directory.
(gdb) bt
#0 __GI___libc_malloc (bytes=4) at ./malloc/malloc.c:3287
#1 0x00007f539f6f6e09 in BUF_MEM_grow_clean () from
/lib/x86_64-linux-gnu/libcrypto.so.3
#2 0x00007f539f6e4fb8 in ?? () from /lib/x86_64-linux-gnu/libcrypto.so.3
#3 0x00007f539f6d22fb in ?? () from /lib/x86_64-linux-gnu/libcrypto.so.3
#4 0x00007f539f6d5c06 in ?? () from /lib/x86_64-linux-gnu/libcrypto.so.3
#5 0x00007f539f6d5d37 in BIO_write () from
/lib/x86_64-linux-gnu/libcrypto.so.3
#6 0x00007f539f6bdb41 in ?? () from /lib/x86_64-linux-gnu/libcrypto.so.3
#7 0x00007f539f6c0b7d in ?? () from /lib/x86_64-linux-gnu/libcrypto.so.3
#8 0x000056026d2fbe8d in be_tls_open_server
(port=port(at)entry=0x56026ed5d730) at be-secure-openssl.c:635
#9 0x000056026d2eefa5 in secure_open_server
(port=port(at)entry=0x56026ed5d730) at be-secure.c:118
#10 0x000056026d3dc412 in ProcessStartupPacket
(port=port(at)entry=0x56026ed5d730, ssl_done=ssl_done(at)entry=false,
gss_done=gss_done(at)entry=false) at postmaster.c:2065
#11 0x000056026d3dcd8e in BackendInitialize
(port=port(at)entry=0x56026ed5d730) at postmaster.c:4377
#12 0x000056026d3def6a in BackendStartup
(port=port(at)entry=0x56026ed5d730) at postmaster.c:4155
#13 0x000056026d3df115 in ServerLoop () at postmaster.c:1781
#14 0x000056026d3e0645 in PostmasterMain (argc=argc(at)entry=20,
argv=argv(at)entry=0x56026ec5a0d0) at postmaster.c:1465
#15 0x000056026d2fcd7c in main (argc=20, argv=0x56026ec5a0d0) at main.c:198
(gdb) return 0
Make __GI___libc_malloc return now? (y or n) y
#0 0x00007f539f6f6e09 in BUF_MEM_grow_clean () from
/lib/x86_64-linux-gnu/libcrypto.so.3
(gdb) delete
Delete all breakpoints? (y or n) y
(gdb) c
Continuing.

And in the server log we see:

DEBUG: SSL connection from DN:"" CN:"ssltestuser"

While in the normal case we get:

DEBUG: SSL connection from DN:"CN=ssltestuser" CN:"ssltestuser"\

Probably we shouldn't ignore the error from X509_NAME_print_ex?

--
Sergey Shinderuk https://postgrespro.com/

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Peter Eisentraut 2023-09-19 08:07:49 Re: Standardize type of variable when extending Buffers
Previous Message Andy Fan 2023-09-19 07:55:48 Re: Questioning an errcode and message in jsonb.c