Skip to content

Commit f9396e1

Browse files
Chao Li (Evan)hackorum
authored andcommitted
Fix gzip stream validation in pg_restore.
With zlib 1.3.2, pg_restore can reject valid gzip files whose compressed and uncompressed sizes end at buffer boundaries, while accepting truncated files. Relying on gzread() and gzclose() does not reliably detect stream completion. Extract the gzip decompression logic from astreamer_gzip into a shared frontend helper using zlib's inflate API, and use it directly in pg_restore. Validate the stream through EOF, including any unread data when closing the file, and support concatenated gzip members. Extend the gzip regression test to cover incomplete trailers, concatenated members, invalid input, and compressed TOC files, and check the truncation diagnostic. Register the test with Meson and add coverage for reading gzip data through the shared file-reader callbacks. Bug: #19754 Reported-by: Stefan Graßl <stefangrassl@gmx.net> Author: Chao Li <lic@highgo.com> Discussion: https://postgr.es/m/C482E34B-347F-4AD2-ACE1-96142F6088B2@gmail.com
1 parent cfa0e78 commit f9396e1

14 files changed

Lines changed: 580 additions & 169 deletions

File tree

‎src/bin/pg_basebackup/nls.mk‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ GETTEXT_FILES = $(FRONTEND_COMMON_GETTEXT_FILES) \
1919
../../fe_utils/astreamer_lz4.c \
2020
../../fe_utils/astreamer_tar.c \
2121
../../fe_utils/astreamer_zstd.c \
22+
../../fe_utils/gzip.c \
2223
../../fe_utils/option_utils.c \
2324
../../fe_utils/recovery_gen.c \
2425
../../fe_utils/string_utils.c \

‎src/bin/pg_dump/compress_gzip.c‎

Lines changed: 160 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,17 @@
1212
*-------------------------------------------------------------------------
1313
*/
1414
#include "postgres_fe.h"
15+
#include <fcntl.h>
1516
#include <unistd.h>
1617

1718
#include "compress_gzip.h"
19+
#include "fe_utils/gzip.h"
1820
#include "pg_backup_utils.h"
1921

2022
#ifdef HAVE_LIBZ
2123
#include <zlib.h>
2224

23-
/*
24-
* We don't use the gzgetc() macro, because zlib's configuration logic is not
25-
* robust enough to guarantee that the macro will have the same ideas about
26-
* struct field layout as the library itself does; see for example
27-
* https://gnats.netbsd.org/cgi-bin/query-pr-single.pl?number=59711
28-
* Instead, #undef the macro and fall back to the underlying function.
29-
*/
30-
#undef gzgetc
25+
#define GZIP_BUFFER_SIZE 8192
3126

3227
/*----------------------
3328
* Compressor API
@@ -261,45 +256,103 @@ InitCompressorGzip(CompressorState *cs,
261256
* Compress File API
262257
*----------------------
263258
*/
259+
typedef struct GzipFileState
260+
{
261+
bool reading;
262+
union
263+
{
264+
gzFile gzfp;
265+
struct
266+
{
267+
int fd;
268+
GzipDecompressor decoder;
269+
char *input;
270+
size_t input_len;
271+
size_t input_pos;
272+
char *output;
273+
size_t output_len;
274+
size_t output_pos;
275+
bool eof;
276+
bool done;
277+
} read;
278+
};
279+
} GzipFileState;
264280

281+
/*
282+
* Use strict gzip parsing: empty physical files, plain input, and trailing
283+
* non-member bytes are errors. Valid empty and concatenated members work.
284+
*/
265285
static size_t
266286
Gzip_read(void *ptr, size_t size, CompressFileHandle *CFH)
267287
{
268-
gzFile gzfp = (gzFile) CFH->private_data;
269-
int gzret;
288+
GzipFileState *state = (GzipFileState *) CFH->private_data;
289+
size_t copied = 0;
270290

271-
/* Reading zero bytes must be a no-op */
272-
if (size == 0)
273-
return 0;
291+
while (copied < size)
292+
{
293+
size_t bytes;
274294

275-
gzret = gzread(gzfp, ptr, size);
295+
if (state->read.output_pos == state->read.output_len)
296+
{
297+
/*
298+
* No buffered output remains; refill it unless decoding is
299+
* complete.
300+
*/
301+
size_t consumed;
302+
GzipDecompressResult result;
276303

277-
/*
278-
* gzread returns zero on EOF as well as some error conditions, and less
279-
* than zero on other error conditions, so we need to inspect for EOF on
280-
* zero.
281-
*/
282-
if (gzret <= 0)
283-
{
284-
int errnum;
285-
const char *errmsg;
304+
if (state->read.done)
305+
break;
286306

287-
if (gzret == 0 && gzeof(gzfp))
288-
return 0;
307+
if (state->read.input_pos == state->read.input_len && !state->read.eof)
308+
{
309+
/* The input buffer is empty and EOF has not been reached */
310+
ssize_t readlen;
311+
312+
do
313+
{
314+
readlen = read(state->read.fd, state->read.input, GZIP_BUFFER_SIZE);
315+
/* retry if a signal interrupts the read */
316+
} while (readlen < 0 && errno == EINTR);
317+
318+
if (readlen < 0)
319+
pg_fatal("could not read from input file: %m");
320+
state->read.input_pos = 0;
321+
state->read.input_len = readlen;
322+
state->read.eof = readlen == 0;
323+
}
289324

290-
errmsg = gzerror(gzfp, &errnum);
325+
/* Produce more decompressed output */
326+
state->read.output_pos = 0;
327+
result = gzip_decompressor_read(&state->read.decoder,
328+
state->read.input + state->read.input_pos,
329+
state->read.input_len - state->read.input_pos,
330+
state->read.output, DEFAULT_IO_BUFFER_SIZE,
331+
state->read.eof, &consumed,
332+
&state->read.output_len);
333+
state->read.input_pos += consumed;
334+
if (result == GZIP_DECOMPRESS_ERROR)
335+
pg_fatal("could not decompress data: %s", state->read.decoder.error);
336+
state->read.done = result == GZIP_DECOMPRESS_DONE;
337+
338+
/* Headers and empty members may produce no output. */
339+
if (state->read.output_len == 0)
340+
continue;
341+
}
291342

292-
pg_fatal("could not read from input file: %s",
293-
errnum == Z_ERRNO ? strerror(errno) : errmsg);
343+
bytes = Min(size - copied, state->read.output_len - state->read.output_pos);
344+
memcpy((char *) ptr + copied, state->read.output + state->read.output_pos, bytes);
345+
state->read.output_pos += bytes;
346+
copied += bytes;
294347
}
295-
296-
return (size_t) gzret;
348+
return copied;
297349
}
298350

299351
static void
300352
Gzip_write(const void *ptr, size_t size, CompressFileHandle *CFH)
301353
{
302-
gzFile gzfp = (gzFile) CFH->private_data;
354+
GzipFileState *state = (GzipFileState *) CFH->private_data;
355+
gzFile gzfp = state->gzfp;
303356
int errnum;
304357
const char *errmsg;
305358

@@ -314,56 +367,82 @@ Gzip_write(const void *ptr, size_t size, CompressFileHandle *CFH)
314367
static int
315368
Gzip_getc(CompressFileHandle *CFH)
316369
{
317-
gzFile gzfp = (gzFile) CFH->private_data;
318-
int ret;
370+
unsigned char ret;
319371

320-
errno = 0;
321-
ret = gzgetc(gzfp);
322-
if (ret == EOF)
323-
{
324-
if (!gzeof(gzfp))
325-
pg_fatal("could not read from input file: %m");
326-
else
327-
pg_fatal("could not read from input file: end of file");
328-
}
372+
if (Gzip_read(&ret, 1, CFH) != 1)
373+
pg_fatal("could not read from input file: end of file");
329374

330375
return ret;
331376
}
332377

333378
static char *
334379
Gzip_gets(char *ptr, int size, CompressFileHandle *CFH)
335380
{
336-
gzFile gzfp = (gzFile) CFH->private_data;
381+
int i;
337382

338-
return gzgets(gzfp, ptr, size);
383+
Assert(size > 0);
384+
for (i = 0; i < size - 1; i++)
385+
{
386+
if (Gzip_read(&ptr[i], 1, CFH) != 1)
387+
break;
388+
if (ptr[i] == '\n')
389+
{
390+
i++;
391+
break;
392+
}
393+
}
394+
ptr[i] = '\0';
395+
return i > 0 ? ptr : NULL;
339396
}
340397

341398
static bool
342399
Gzip_close(CompressFileHandle *CFH)
343400
{
344-
gzFile gzfp = (gzFile) CFH->private_data;
401+
GzipFileState *state = (GzipFileState *) CFH->private_data;
402+
bool ok = true;
345403

404+
if (state->reading)
405+
{
406+
char buf[GZIP_BUFFER_SIZE];
407+
408+
/* Consume and validate any input the caller did not read. */
409+
while (Gzip_read(buf, sizeof(buf), CFH) > 0)
410+
;
411+
if (!gzip_decompressor_end(&state->read.decoder))
412+
pg_fatal("could not decompress data: %s", state->read.decoder.error);
413+
pg_free(state->read.input);
414+
pg_free(state->read.output);
415+
if (close(state->read.fd) != 0)
416+
ok = false;
417+
}
418+
else if (gzclose(state->gzfp) != Z_OK)
419+
ok = false;
346420
CFH->private_data = NULL;
347-
348-
return gzclose(gzfp) == Z_OK;
421+
pg_free(state);
422+
return ok;
349423
}
350424

351425
static bool
352426
Gzip_eof(CompressFileHandle *CFH)
353427
{
354-
gzFile gzfp = (gzFile) CFH->private_data;
428+
GzipFileState *state = (GzipFileState *) CFH->private_data;
355429

356-
return gzeof(gzfp) == 1;
430+
return state->reading ?
431+
(state->read.done && state->read.output_pos == state->read.output_len) :
432+
gzeof(state->gzfp) == 1;
357433
}
358434

359435
static const char *
360436
Gzip_get_error(CompressFileHandle *CFH)
361437
{
362-
gzFile gzfp = (gzFile) CFH->private_data;
438+
GzipFileState *state = (GzipFileState *) CFH->private_data;
363439
const char *errmsg;
364440
int errnum;
365441

366-
errmsg = gzerror(gzfp, &errnum);
442+
if (state->reading)
443+
return state->read.decoder.error ? state->read.decoder.error : "";
444+
445+
errmsg = gzerror(state->gzfp, &errnum);
367446
if (errnum == Z_ERRNO)
368447
errmsg = strerror(errno);
369448

@@ -373,7 +452,8 @@ Gzip_get_error(CompressFileHandle *CFH)
373452
static bool
374453
Gzip_open(const char *path, int fd, const char *mode, CompressFileHandle *CFH)
375454
{
376-
gzFile gzfp;
455+
GzipFileState *state;
456+
gzFile gzfp = NULL;
377457
char mode_compression[32];
378458

379459
if (CFH->compression_spec.level != Z_DEFAULT_COMPRESSION)
@@ -387,27 +467,53 @@ Gzip_open(const char *path, int fd, const char *mode, CompressFileHandle *CFH)
387467
else
388468
strcpy(mode_compression, mode);
389469

390-
if (fd >= 0)
470+
state = pg_malloc0_object(GzipFileState);
471+
state->reading = mode[0] == 'r';
472+
if (state->reading)
473+
{
474+
state->read.fd = fd >= 0 ? dup(fd) : open(path, O_RDONLY | PG_BINARY, 0);
475+
if (state->read.fd < 0)
476+
{
477+
pg_free(state);
478+
return false;
479+
}
480+
if (!gzip_decompressor_init(&state->read.decoder))
481+
pg_fatal("could not initialize compression library: %s",
482+
state->read.decoder.error);
483+
state->read.input = pg_malloc(GZIP_BUFFER_SIZE);
484+
state->read.output = pg_malloc(DEFAULT_IO_BUFFER_SIZE);
485+
}
486+
else if (fd >= 0)
391487
{
392488
int dup_fd = dup(fd);
393489

394490
if (dup_fd < 0)
491+
{
492+
pg_free(state);
395493
return false;
494+
}
396495
gzfp = gzdopen(dup_fd, mode_compression);
397496
if (gzfp == NULL)
398497
{
399498
close(dup_fd);
499+
pg_free(state);
400500
return false;
401501
}
402502
}
403503
else
404504
{
405505
gzfp = gzopen(path, mode_compression);
406506
if (gzfp == NULL)
507+
{
508+
pg_free(state);
407509
return false;
510+
}
408511
}
409512

410-
CFH->private_data = gzfp;
513+
if (!state->reading)
514+
state->gzfp = gzfp;
515+
516+
CFH->private_data = state;
411517

412518
return true;
413519
}

‎src/bin/pg_dump/meson.build‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ tests += {
104104
't/004_pg_dump_parallel.pl',
105105
't/005_pg_dump_filterfile.pl',
106106
't/006_pg_dump_compress.pl',
107+
't/007_pg_dump_gzip.pl',
107108
't/010_dump_connstr.pl',
108109
't/011_restore.pl',
109110
],

‎src/bin/pg_dump/nls.mk‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ GETTEXT_FILES = $(FRONTEND_COMMON_GETTEXT_FILES) \
2929
../../common/fe_memutils.c \
3030
../../common/file_utils.c \
3131
../../common/wait_error.c \
32+
../../fe_utils/gzip.c \
3233
../../fe_utils/option_utils.c \
3334
../../fe_utils/string_utils.c
3435
GETTEXT_TRIGGERS = $(FRONTEND_COMMON_GETTEXT_TRIGGERS) \

‎src/bin/pg_dump/t/006_pg_dump_compress.pl‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -744,6 +744,27 @@ sub truncate_custom_compressed_data
744744
}
745745
}
746746
747+
SKIP:
748+
{
749+
my ($directory_path) = grep { -s $_ }
750+
glob("$tempdir/compression_gzip_dir/*.dat.gz");
751+
752+
skip "gzip compression not supported by this build", 2 if !$supports_gzip;
753+
die "could not find compressed data file for gzip"
754+
if !defined($directory_path);
755+
my $directory_size = -s $directory_path;
756+
truncate($directory_path, $directory_size - 1)
757+
or die "could not truncate $directory_path: $!";
758+
$node->command_fails_like(
759+
[
760+
'pg_restore',
761+
'--file' => "$tempdir/gzip_directory_bad.sql",
762+
"$tempdir/compression_gzip_dir"
763+
],
764+
qr/could not decompress data: compressed stream is incomplete/,
765+
"gzip directory archive with truncated compressed data fails");
766+
}
767+
747768
SKIP:
748769
{
749770
# Zstd needs empty-input processing at end-of-input to flush buffered

0 commit comments

Comments
 (0)