PostgreSQL Source Code git master
Loading...
Searching...
No Matches
be-secure-gssapi.c
Go to the documentation of this file.
1/*-------------------------------------------------------------------------
2 *
3 * be-secure-gssapi.c
4 * GSSAPI encryption support
5 *
6 * Portions Copyright (c) 2018-2026, PostgreSQL Global Development Group
7 *
8 * IDENTIFICATION
9 * src/backend/libpq/be-secure-gssapi.c
10 *
11 *-------------------------------------------------------------------------
12 */
13
14#include "postgres.h"
15
16#include <unistd.h>
17
18#include "libpq/auth.h"
20#include "libpq/libpq.h"
21#include "miscadmin.h"
22#include "pgstat.h"
23#include "port/pg_bswap.h"
24#include "storage/latch.h"
26#include "utils/memutils.h"
27
28
29/*
30 * Handle the encryption/decryption of data using GSSAPI.
31 *
32 * In the encrypted data stream on the wire, we break up the data
33 * into packets where each packet starts with a uint32-size length
34 * word (in network byte order), then encrypted data of that length
35 * immediately following. Decryption yields the same data stream
36 * that would appear when not using encryption.
37 *
38 * Encrypted data typically ends up being larger than the same data
39 * unencrypted, so we use fixed-size buffers for handling the
40 * encryption/decryption which are larger than PQComm's buffer will
41 * typically be to minimize the times where we have to make multiple
42 * packets (and therefore multiple recv/send calls for a single
43 * read/write call to us).
44 *
45 * NOTE: The client and server have to agree on the max packet size,
46 * because we have to pass an entire packet to GSSAPI at a time and we
47 * don't want the other side to send arbitrarily huge packets as we
48 * would have to allocate memory for them to then pass them to GSSAPI.
49 *
50 * Therefore, this #define is effectively part of the protocol
51 * spec and can't ever be changed.
52 */
53#define PQ_GSS_MAX_PACKET_SIZE 16384 /* includes uint32 header word */
54
55/*
56 * However, during the authentication exchange we must cope with whatever
57 * message size the GSSAPI library wants to send (because our protocol
58 * doesn't support splitting those messages). Depending on configuration
59 * those messages might be as much as 64kB.
60 */
61#define PQ_GSS_AUTH_BUFFER_SIZE 65536 /* includes uint32 header word */
62
63/*
64 * Since we manage at most one GSS-encrypted connection per backend,
65 * we can just keep all this state in static variables. The char *
66 * variables point to buffers that are allocated once and re-used.
67 */
68static char *PqGSSSendBuffer; /* Encrypted data waiting to be sent */
69static int PqGSSSendLength; /* End of data available in PqGSSSendBuffer */
70static int PqGSSSendNext; /* Next index to send a byte from
71 * PqGSSSendBuffer */
72static int PqGSSSendConsumed; /* Number of source bytes encrypted but not
73 * yet reported as sent */
74
75static char *PqGSSRecvBuffer; /* Received, encrypted data */
76static int PqGSSRecvLength; /* End of data available in PqGSSRecvBuffer */
77
78static char *PqGSSResultBuffer; /* Decryption of data in gss_RecvBuffer */
79static int PqGSSResultLength; /* End of data available in PqGSSResultBuffer */
80static int PqGSSResultNext; /* Next index to read a byte from
81 * PqGSSResultBuffer */
82
83static uint32 PqGSSMaxPktSize; /* Maximum size we can encrypt and fit the
84 * results into our output buffer */
85
86
87/*
88 * Attempt to write len bytes of data from ptr to a GSSAPI-encrypted connection.
89 *
90 * The connection must be already set up for GSSAPI encryption (i.e., GSSAPI
91 * transport negotiation is complete).
92 *
93 * On success, returns the number of data bytes consumed (possibly less than
94 * len). On failure, returns -1 with errno set appropriately. For retryable
95 * errors, caller should call again (passing the same or more data) once the
96 * socket is ready.
97 *
98 * Dealing with fatal errors here is a bit tricky: we can't invoke elog(FATAL)
99 * since it would try to write to the client, probably resulting in infinite
100 * recursion. Instead, use elog(COMMERROR) to log extra info about the
101 * failure if necessary, and then return an errno indicating connection loss.
102 */
104be_gssapi_write(Port *port, const void *ptr, size_t len)
105{
107 minor;
109 output;
110 size_t bytes_to_encrypt;
111 size_t bytes_encrypted;
112 gss_ctx_id_t gctx = port->gss->ctx;
113
114 /*
115 * When we get a retryable failure, we must not tell the caller we have
116 * successfully transmitted everything, else it won't retry. For
117 * simplicity, we claim we haven't transmitted anything until we have
118 * successfully transmitted all "len" bytes. Between calls, the amount of
119 * the current input data that's already been encrypted and placed into
120 * PqGSSSendBuffer (and perhaps transmitted) is remembered in
121 * PqGSSSendConsumed. On a retry, the caller *must* be sending that data
122 * again, so if it offers a len less than that, something is wrong.
123 *
124 * Note: it may seem attractive to report partial write completion once
125 * we've successfully sent any encrypted packets. However, doing that
126 * expands the state space of this processing and has been responsible for
127 * bugs in the past (cf. commit d053a879b). We won't save much,
128 * typically, by letting callers discard data early, so don't risk it.
129 */
131 {
132 elog(COMMERROR, "GSSAPI caller failed to retransmit all data needing to be retried");
134 return -1;
135 }
136
137 /* Discount whatever source data we already encrypted. */
140
141 /*
142 * Loop through encrypting data and sending it out until it's all done or
143 * secure_raw_write() complains (which would likely mean that the socket
144 * is non-blocking and the requested send() would block, or there was some
145 * kind of actual error).
146 */
148 {
149 int conf_state = 0;
151
152 /*
153 * Check if we have data in the encrypted output buffer that needs to
154 * be sent (possibly left over from a previous call), and if so, try
155 * to send it. If we aren't able to, return that fact back up to the
156 * caller.
157 */
158 if (PqGSSSendLength)
159 {
160 ssize_t ret;
162
164 if (ret <= 0)
165 return ret;
166
167 /*
168 * Check if this was a partial write, and if so, move forward that
169 * far in our buffer and try again.
170 */
171 if (ret < amount)
172 {
173 PqGSSSendNext += ret;
174 continue;
175 }
176
177 /* We've successfully sent whatever data was in the buffer. */
179 }
180
181 /*
182 * Check if there are any bytes left to encrypt. If not, we're done.
183 */
184 if (!bytes_to_encrypt)
185 break;
186
187 /*
188 * Check how much we are being asked to send, if it's too much, then
189 * we will have to loop and possibly be called multiple times to get
190 * through all the data.
191 */
193 input.length = PqGSSMaxPktSize;
194 else
195 input.length = bytes_to_encrypt;
196
197 input.value = (char *) ptr + bytes_encrypted;
198
199 output.value = NULL;
200 output.length = 0;
201
202 /*
203 * Create the next encrypted packet. Any failure here is considered a
204 * hard failure, so we return -1 even if some data has been sent.
205 */
207 &input, &conf_state, &output);
208 if (major != GSS_S_COMPLETE)
209 {
210 pg_GSS_error(_("GSSAPI wrap error"), major, minor);
212 return -1;
213 }
214 if (conf_state == 0)
215 {
217 (errmsg("outgoing GSSAPI message would not use confidentiality")));
219 return -1;
220 }
221 if (output.length > PQ_GSS_MAX_PACKET_SIZE - sizeof(uint32))
222 {
224 (errmsg("server tried to send oversize GSSAPI packet (%zu > %zu)",
225 (size_t) output.length,
226 PQ_GSS_MAX_PACKET_SIZE - sizeof(uint32))));
228 return -1;
229 }
230
231 bytes_encrypted += input.length;
232 bytes_to_encrypt -= input.length;
233 PqGSSSendConsumed += input.length;
234
235 /* 4 network-order bytes of length, then payload */
236 netlen = pg_hton32(output.length);
238 PqGSSSendLength += sizeof(uint32);
239
241 PqGSSSendLength += output.length;
242
243 /* Release buffer storage allocated by GSSAPI */
245 }
246
247 /* If we get here, our counters should all match up. */
250
251 /* We're reporting all the data as sent, so reset PqGSSSendConsumed. */
253
254 return bytes_encrypted;
255}
256
257/*
258 * Read up to len bytes of data into ptr from a GSSAPI-encrypted connection.
259 *
260 * The connection must be already set up for GSSAPI encryption (i.e., GSSAPI
261 * transport negotiation is complete).
262 *
263 * Returns the number of data bytes read, or on failure, returns -1
264 * with errno set appropriately. For retryable errors, caller should call
265 * again once the socket is ready.
266 *
267 * We treat fatal errors the same as in be_gssapi_write(), even though the
268 * argument about infinite recursion doesn't apply here.
269 */
271be_gssapi_read(Port *port, void *ptr, size_t len)
272{
274 minor;
276 output;
277 ssize_t ret;
278 size_t bytes_returned = 0;
279 gss_ctx_id_t gctx = port->gss->ctx;
280
281 /*
282 * The plan here is to read one incoming encrypted packet into
283 * PqGSSRecvBuffer, decrypt it into PqGSSResultBuffer, and then dole out
284 * data from there to the caller. When we exhaust the current input
285 * packet, read another.
286 */
287 while (bytes_returned < len)
288 {
289 int conf_state = 0;
290
291 /* Check if we have data in our buffer that we can return immediately */
293 {
296
297 /*
298 * Copy the data from our result buffer into the caller's buffer,
299 * at the point where we last left off filling their buffer.
300 */
304
305 /*
306 * At this point, we've either filled the caller's buffer or
307 * emptied our result buffer. Either way, return to caller. In
308 * the second case, we could try to read another encrypted packet,
309 * but the odds are good that there isn't one available. (If this
310 * isn't true, we chose too small a max packet size.) In any
311 * case, there's no harm letting the caller process the data we've
312 * already returned.
313 */
314 break;
315 }
316
317 /* Result buffer is empty, so reset buffer pointers */
319
320 /*
321 * Because we chose above to return immediately as soon as we emit
322 * some data, bytes_returned must be zero at this point. Therefore
323 * the failure exits below can just return -1 without worrying about
324 * whether we already emitted some data.
325 */
327
328 /*
329 * At this point, our result buffer is empty with more bytes being
330 * requested to be read. We are now ready to load the next packet and
331 * decrypt it (entirely) into our result buffer.
332 */
333
334 /* Collect the length if we haven't already */
335 if (PqGSSRecvLength < sizeof(uint32))
336 {
338 sizeof(uint32) - PqGSSRecvLength);
339
340 /* If ret <= 0, secure_raw_read already set the correct errno */
341 if (ret <= 0)
342 return ret;
343
344 PqGSSRecvLength += ret;
345
346 /* If we still haven't got the length, return to the caller */
347 if (PqGSSRecvLength < sizeof(uint32))
348 {
350 return -1;
351 }
352 }
353
354 /* Decode the packet length and check for overlength packet */
355 input.length = pg_ntoh32(*(uint32 *) PqGSSRecvBuffer);
356
357 if (input.length > PQ_GSS_MAX_PACKET_SIZE - sizeof(uint32))
358 {
360 (errmsg("oversize GSSAPI packet sent by the client (%zu > %zu)",
361 (size_t) input.length,
362 PQ_GSS_MAX_PACKET_SIZE - sizeof(uint32))));
364 return -1;
365 }
366
367 /*
368 * Read as much of the packet as we are able to on this call into
369 * wherever we left off from the last time we were called.
370 */
372 input.length - (PqGSSRecvLength - sizeof(uint32)));
373 /* If ret <= 0, secure_raw_read already set the correct errno */
374 if (ret <= 0)
375 return ret;
376
377 PqGSSRecvLength += ret;
378
379 /* If we don't yet have the whole packet, return to the caller */
380 if (PqGSSRecvLength - sizeof(uint32) < input.length)
381 {
383 return -1;
384 }
385
386 /*
387 * We now have the full packet and we can perform the decryption and
388 * refill our result buffer, then loop back up to pass data back to
389 * the caller.
390 */
391 output.value = NULL;
392 output.length = 0;
393 input.value = PqGSSRecvBuffer + sizeof(uint32);
394
396 if (major != GSS_S_COMPLETE)
397 {
398 pg_GSS_error(_("GSSAPI unwrap error"), major, minor);
400 return -1;
401 }
402 if (conf_state == 0)
403 {
405 (errmsg("incoming GSSAPI message did not use confidentiality")));
407 return -1;
408 }
409
410 memcpy(PqGSSResultBuffer, output.value, output.length);
411 PqGSSResultLength = output.length;
412
413 /* Our receive buffer is now empty, reset it */
414 PqGSSRecvLength = 0;
415
416 /* Release buffer storage allocated by GSSAPI */
418 }
419
420 return bytes_returned;
421}
422
423/*
424 * Read the specified number of bytes off the wire, waiting using
425 * WaitLatchOrSocket if we would block.
426 *
427 * Results are read into PqGSSRecvBuffer.
428 *
429 * Will always return either -1, to indicate a permanent error, or len.
430 */
431static ssize_t
433{
434 ssize_t ret;
435
436 /*
437 * Keep going until we either read in everything we were asked to, or we
438 * error out.
439 */
440 while (PqGSSRecvLength < len)
441 {
443
444 /*
445 * If we got back an error and it wasn't just
446 * EWOULDBLOCK/EAGAIN/EINTR, then give up.
447 */
448 if (ret < 0 &&
449 !(errno == EWOULDBLOCK || errno == EAGAIN || errno == EINTR))
450 return -1;
451
452 /*
453 * Ok, we got back either a positive value, zero, or a negative result
454 * indicating we should retry.
455 *
456 * If it was zero or negative, then we wait on the socket to be
457 * readable again.
458 */
459 if (ret <= 0)
460 {
464
465 /*
466 * If we got back zero bytes, and then waited on the socket to be
467 * readable and got back zero bytes on a second read, then this is
468 * EOF and the client hung up on us.
469 *
470 * If we did get data here, then we can just fall through and
471 * handle it just as if we got data the first time.
472 *
473 * Otherwise loop back to the top and try again.
474 */
475 if (ret == 0)
476 {
478 if (ret == 0)
479 return -1;
480 }
481 if (ret < 0)
482 continue;
483 }
484
485 PqGSSRecvLength += ret;
486 }
487
488 return len;
489}
490
491/*
492 * Start up a GSSAPI-encrypted connection. This performs GSSAPI
493 * authentication; after this function completes, it is safe to call
494 * be_gssapi_read and be_gssapi_write. Returns -1 and logs on failure;
495 * otherwise, returns 0 and marks the connection as ready for GSSAPI
496 * encryption.
497 *
498 * Note that unlike the be_gssapi_read/be_gssapi_write functions, this
499 * function WILL block on the socket to be ready for read/write (using
500 * WaitLatchOrSocket) as appropriate while establishing the GSSAPI
501 * session.
502 */
505{
506 bool complete_next = false;
508 minor;
510
511 INJECTION_POINT("backend-gssapi-startup", NULL);
512
513 /*
514 * Allocate subsidiary Port data for GSSAPI operations.
515 */
516 port->gss = (pg_gssinfo *)
518
520 port->gss->delegated_creds = false;
521
522 /*
523 * Allocate buffers and initialize state variables. By malloc'ing the
524 * buffers at this point, we avoid wasting static data space in processes
525 * that will never use them, and we ensure that the buffers are
526 * sufficiently aligned for the length-word accesses that we do in some
527 * places in this file.
528 *
529 * We'll use PQ_GSS_AUTH_BUFFER_SIZE-sized buffers until transport
530 * negotiation is complete, then switch to PQ_GSS_MAX_PACKET_SIZE.
531 */
538 errmsg("out of memory")));
541
542 /*
543 * Use the configured keytab, if there is one. As we now require MIT
544 * Kerberos, we might consider using the credential store extensions in
545 * the future instead of the environment variable.
546 */
548 {
549 if (setenv("KRB5_KTNAME", pg_krb_server_keyfile, 1) != 0)
550 {
551 /* The only likely failure cause is OOM, so use that errcode */
554 errmsg("could not set environment: %m")));
555 }
556 }
557
558 while (true)
559 {
560 ssize_t ret;
563
564 /*
565 * The client always sends first, so try to go ahead and read the
566 * length and wait on the socket to be readable again if that fails.
567 */
568 ret = read_or_wait(port, sizeof(uint32));
569 if (ret < 0)
570 return ret;
571
572 /*
573 * Get the length for this packet from the length header.
574 */
575 input.length = pg_ntoh32(*(uint32 *) PqGSSRecvBuffer);
576
577 /* Done with the length, reset our buffer */
578 PqGSSRecvLength = 0;
579
580 /*
581 * During initialization, packets are always fully consumed and
582 * shouldn't ever be over PQ_GSS_AUTH_BUFFER_SIZE in total length.
583 *
584 * Verify on our side that the client doesn't do something funny.
585 */
586 if (input.length > PQ_GSS_AUTH_BUFFER_SIZE - sizeof(uint32))
587 {
589 (errmsg("oversize GSSAPI packet sent by the client (%zu > %zu)",
590 (size_t) input.length,
591 PQ_GSS_AUTH_BUFFER_SIZE - sizeof(uint32))));
592 return -1;
593 }
594
595 /*
596 * Get the rest of the packet so we can pass it to GSSAPI to accept
597 * the context.
598 */
599 ret = read_or_wait(port, input.length);
600 if (ret < 0)
601 return ret;
602
603 input.value = PqGSSRecvBuffer;
604
605 /* Process incoming data. (The client sends first.) */
606 major = gss_accept_sec_context(&minor, &port->gss->ctx,
609 &port->gss->name, NULL, &output, NULL,
611
612 if (GSS_ERROR(major))
613 {
614 pg_GSS_error(_("could not accept GSSAPI security context"),
615 major, minor);
617 return -1;
618 }
619 else if (!(major & GSS_S_CONTINUE_NEEDED))
620 {
621 /*
622 * rfc2744 technically permits context negotiation to be complete
623 * both with and without a packet to be sent.
624 */
625 complete_next = true;
626 }
627
629 {
631 port->gss->delegated_creds = true;
632 }
633
634 /* Done handling the incoming packet, reset our buffer */
635 PqGSSRecvLength = 0;
636
637 /*
638 * Check if we have data to send and, if we do, make sure to send it
639 * all
640 */
641 if (output.length > 0)
642 {
643 uint32 netlen = pg_hton32(output.length);
644
645 if (output.length > PQ_GSS_AUTH_BUFFER_SIZE - sizeof(uint32))
646 {
648 (errmsg("server tried to send oversize GSSAPI packet (%zu > %zu)",
649 (size_t) output.length,
650 PQ_GSS_AUTH_BUFFER_SIZE - sizeof(uint32))));
652 return -1;
653 }
654
656 PqGSSSendLength += sizeof(uint32);
657
659 PqGSSSendLength += output.length;
660
661 /* we don't bother with PqGSSSendConsumed here */
662
664 {
667
668 /*
669 * If we got back an error and it wasn't just
670 * EWOULDBLOCK/EAGAIN/EINTR, then give up.
671 */
672 if (ret < 0 &&
673 !(errno == EWOULDBLOCK || errno == EAGAIN || errno == EINTR))
674 {
676 return -1;
677 }
678
679 /* Wait and retry if we couldn't write yet */
680 if (ret <= 0)
681 {
685 continue;
686 }
687
688 PqGSSSendNext += ret;
689 }
690
691 /* Done sending the packet, reset our buffer */
693
695 }
696
697 /*
698 * If we got back that the connection is finished being set up, now
699 * that we've sent the last packet, exit our loop.
700 */
701 if (complete_next)
702 break;
703 }
704
705 /*
706 * Release the large authentication buffers and allocate the ones we want
707 * for normal operation.
708 */
718 errmsg("out of memory")));
721
722 /*
723 * Determine the max packet size which will fit in our buffer, after
724 * accounting for the length. be_gssapi_write will need this.
725 */
729
730 if (GSS_ERROR(major))
731 {
732 pg_GSS_error(_("GSSAPI size check error"), major, minor);
733 return -1;
734 }
735
736 port->gss->enc = true;
737
738 return 0;
739}
740
741/*
742 * Return if GSSAPI authentication was used on this connection.
743 */
744bool
746{
747 if (!port || !port->gss)
748 return false;
749
750 return port->gss->auth;
751}
752
753/*
754 * Return if GSSAPI encryption is enabled and being used on this connection.
755 */
756bool
758{
759 if (!port || !port->gss)
760 return false;
761
762 return port->gss->enc;
763}
764
765/*
766 * Return the GSSAPI principal used for authentication on this connection
767 * (NULL if we did not perform GSSAPI authentication).
768 */
769const char *
771{
772 if (!port || !port->gss)
773 return NULL;
774
775 return port->gss->princ;
776}
777
778/*
779 * Return if GSSAPI delegated credentials were included on this
780 * connection.
781 */
782bool
784{
785 if (!port || !port->gss)
786 return false;
787
788 return port->gss->delegated_creds;
789}
char * pg_krb_server_keyfile
Definition auth.c:173
bool pg_gss_accept_delegation
Definition auth.c:175
void pg_store_delegated_credential(gss_cred_id_t cred)
void pg_GSS_error(const char *errmsg, OM_uint32 maj_stat, OM_uint32 min_stat)
static int PqGSSRecvLength
static int PqGSSResultLength
#define PQ_GSS_AUTH_BUFFER_SIZE
static char * PqGSSSendBuffer
ssize_t be_gssapi_write(Port *port, const void *ptr, size_t len)
bool be_gssapi_get_auth(Port *port)
static int PqGSSSendConsumed
ssize_t be_gssapi_read(Port *port, void *ptr, size_t len)
static ssize_t read_or_wait(Port *port, ssize_t len)
ssize_t secure_open_gssapi(Port *port)
static char * PqGSSRecvBuffer
static int PqGSSResultNext
static uint32 PqGSSMaxPktSize
bool be_gssapi_get_enc(Port *port)
static int PqGSSSendLength
static int PqGSSSendNext
static char * PqGSSResultBuffer
#define PQ_GSS_MAX_PACKET_SIZE
const char * be_gssapi_get_princ(Port *port)
bool be_gssapi_get_delegation(Port *port)
ssize_t secure_raw_read(Port *port, void *ptr, size_t len)
Definition be-secure.c:269
ssize_t secure_raw_write(Port *port, const void *ptr, size_t len)
Definition be-secure.c:378
#define Min(x, y)
Definition c.h:1019
#define Assert(condition)
Definition c.h:885
uint32_t uint32
Definition c.h:558
int errcode(int sqlerrcode)
Definition elog.c:874
int errmsg(const char *fmt,...)
Definition elog.c:1093
#define _(x)
Definition elog.c:95
#define COMMERROR
Definition elog.h:33
#define FATAL
Definition elog.h:41
#define elog(elevel,...)
Definition elog.h:226
#define ereport(elevel,...)
Definition elog.h:150
FILE * input
FILE * output
#define INJECTION_POINT(name, arg)
int WaitLatchOrSocket(Latch *latch, int wakeEvents, pgsocket sock, long timeout, uint32 wait_event_info)
Definition latch.c:223
void * MemoryContextAllocZero(MemoryContext context, Size size)
Definition mcxt.c:1266
MemoryContext TopMemoryContext
Definition mcxt.c:166
#define pg_ntoh32(x)
Definition pg_bswap.h:125
#define pg_hton32(x)
Definition pg_bswap.h:121
const void size_t len
static int port
Definition pg_regress.c:115
static int fb(int x)
#define free(a)
#define malloc(a)
#define WL_SOCKET_READABLE
#define WL_EXIT_ON_PM_DEATH
#define WL_SOCKET_WRITEABLE
#define EINTR
Definition win32_port.h:361
#define EWOULDBLOCK
Definition win32_port.h:367
#define setenv(x, y, z)
Definition win32_port.h:542
#define ECONNRESET
Definition win32_port.h:371
#define EAGAIN
Definition win32_port.h:359