Skip to content

Commit 1c95b5e

Browse files
committed
Fix race condition in pg_ctl reading postmaster.pid.
If postmaster changed postmaster.pid while pg_ctl was reading it, pg_ctl could overrun the buffer it allocated for the file. Fix by reading the whole file to memory with one read() call. initdb contains an identical copy of the readfile() function, but the files that initdb reads are static, not modified concurrently. Nevertheless, add a simple bounds-check there, if only to silence static analysis tools. Per report from Dave Vitek. Backpatch to all supported branches.
1 parent 5c07de4 commit 1c95b5e

File tree

2 files changed

+63
-34
lines changed

2 files changed

+63
-34
lines changed

src/bin/initdb/initdb.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,7 @@ readfile(const char *path)
368368
int maxlength = 1,
369369
linelen = 0;
370370
int nlines = 0;
371+
int n;
371372
char **result;
372373
char *buffer;
373374
int c;
@@ -405,13 +406,13 @@ readfile(const char *path)
405406

406407
/* now reprocess the file and store the lines */
407408
rewind(infile);
408-
nlines = 0;
409-
while (fgets(buffer, maxlength + 1, infile) != NULL)
410-
result[nlines++] = xstrdup(buffer);
409+
n = 0;
410+
while (fgets(buffer, maxlength + 1, infile) != NULL && n < nlines)
411+
result[n++] = xstrdup(buffer);
411412

412413
fclose(infile);
413414
free(buffer);
414-
result[nlines] = NULL;
415+
result[n] = NULL;
415416

416417
return result;
417418
}

src/bin/pg_ctl/pg_ctl.c

Lines changed: 58 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "postgres_fe.h"
2121
#include "libpq-fe.h"
2222

23+
#include <fcntl.h>
2324
#include <locale.h>
2425
#include <signal.h>
2526
#include <sys/types.h>
@@ -297,50 +298,77 @@ get_pgpid(void)
297298
static char **
298299
readfile(const char *path)
299300
{
300-
FILE *infile;
301-
int maxlength = 1,
302-
linelen = 0;
303-
int nlines = 0;
301+
int fd;
302+
int nlines;
304303
char **result;
305304
char *buffer;
306-
int c;
305+
char *linebegin;
306+
int i;
307+
int n;
308+
int len;
309+
struct stat statbuf;
307310

308-
if ((infile = fopen(path, "r")) == NULL)
311+
/*
312+
* Slurp the file into memory.
313+
*
314+
* The file can change concurrently, so we read the whole file into memory
315+
* with a single read() call. That's not guaranteed to get an atomic
316+
* snapshot, but in practice, for a small file, it's close enough for the
317+
* current use.
318+
*/
319+
fd = open(path, O_RDONLY | PG_BINARY, 0);
320+
if (fd < 0)
309321
return NULL;
322+
if (fstat(fd, &statbuf) < 0)
323+
return NULL;
324+
if (statbuf.st_size == 0)
325+
{
326+
/* empty file */
327+
result = (char **) pg_malloc(sizeof(char *));
328+
*result = NULL;
329+
return result;
330+
}
331+
buffer = pg_malloc(statbuf.st_size + 1);
310332

311-
/* pass over the file twice - the first time to size the result */
333+
len = read(fd, buffer, statbuf.st_size + 1);
334+
close(fd);
335+
if (len != statbuf.st_size)
336+
{
337+
/* oops, the file size changed between fstat and read */
338+
free(buffer);
339+
return NULL;
340+
}
312341

313-
while ((c = fgetc(infile)) != EOF)
342+
/* count newlines */
343+
nlines = 0;
344+
for (i = 0; i < len - 1; i++)
314345
{
315-
linelen++;
316-
if (c == '\n')
317-
{
346+
if (buffer[i] == '\n')
318347
nlines++;
319-
if (linelen > maxlength)
320-
maxlength = linelen;
321-
linelen = 0;
322-
}
323348
}
349+
nlines++; /* account for the last line */
324350

325-
/* handle last line without a terminating newline (yuck) */
326-
if (linelen)
327-
nlines++;
328-
if (linelen > maxlength)
329-
maxlength = linelen;
330-
331-
/* set up the result and the line buffer */
351+
/* set up the result buffer */
332352
result = (char **) pg_malloc((nlines + 1) * sizeof(char *));
333-
buffer = (char *) pg_malloc(maxlength + 1);
334353

335-
/* now reprocess the file and store the lines */
336-
rewind(infile);
337-
nlines = 0;
338-
while (fgets(buffer, maxlength + 1, infile) != NULL)
339-
result[nlines++] = xstrdup(buffer);
354+
/* now split the buffer into lines */
355+
linebegin = buffer;
356+
n = 0;
357+
for (i = 0; i < len; i++)
358+
{
359+
if (buffer[i] == '\n' || i == len - 1)
360+
{
361+
int slen = &buffer[i] - linebegin + 1;
362+
char *linebuf = pg_malloc(slen + 1);
363+
memcpy(linebuf, linebegin, slen);
364+
linebuf[slen] = '\0';
365+
result[n++] = linebuf;
366+
linebegin = &buffer[i + 1];
367+
}
368+
}
369+
result[n] = NULL;
340370

341-
fclose(infile);
342371
free(buffer);
343-
result[nlines] = NULL;
344372

345373
return result;
346374
}

0 commit comments

Comments
 (0)