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.
This commit is contained in:
parent
55eaeef0b9
commit
6d934e4aae
@ -368,6 +368,7 @@ readfile(char *path)
|
|||||||
int maxlength = 0,
|
int maxlength = 0,
|
||||||
linelen = 0;
|
linelen = 0;
|
||||||
int nlines = 0;
|
int nlines = 0;
|
||||||
|
int n;
|
||||||
char **result;
|
char **result;
|
||||||
char *buffer;
|
char *buffer;
|
||||||
int c;
|
int c;
|
||||||
@ -408,16 +409,13 @@ readfile(char *path)
|
|||||||
/* now reprocess the file and store the lines */
|
/* now reprocess the file and store the lines */
|
||||||
|
|
||||||
rewind(infile);
|
rewind(infile);
|
||||||
nlines = 0;
|
n = 0;
|
||||||
while (fgets(buffer, maxlength + 1, infile) != NULL)
|
while (fgets(buffer, maxlength + 1, infile) != NULL && n < nlines)
|
||||||
{
|
result[n++] = xstrdup(buffer);
|
||||||
result[nlines] = xstrdup(buffer);
|
|
||||||
nlines++;
|
|
||||||
}
|
|
||||||
|
|
||||||
fclose(infile);
|
fclose(infile);
|
||||||
free(buffer);
|
free(buffer);
|
||||||
result[nlines] = NULL;
|
result[n] = NULL;
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
@ -20,6 +20,7 @@
|
|||||||
#include "postgres_fe.h"
|
#include "postgres_fe.h"
|
||||||
#include "libpq-fe.h"
|
#include "libpq-fe.h"
|
||||||
|
|
||||||
|
#include <fcntl.h>
|
||||||
#include <locale.h>
|
#include <locale.h>
|
||||||
#include <signal.h>
|
#include <signal.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
@ -294,50 +295,77 @@ get_pgpid(void)
|
|||||||
static char **
|
static char **
|
||||||
readfile(const char *path)
|
readfile(const char *path)
|
||||||
{
|
{
|
||||||
FILE *infile;
|
int fd;
|
||||||
int maxlength = 1,
|
int nlines;
|
||||||
linelen = 0;
|
|
||||||
int nlines = 0;
|
|
||||||
char **result;
|
char **result;
|
||||||
char *buffer;
|
char *buffer;
|
||||||
int c;
|
char *linebegin;
|
||||||
|
int i;
|
||||||
|
int n;
|
||||||
|
int len;
|
||||||
|
struct stat statbuf;
|
||||||
|
|
||||||
if ((infile = fopen(path, "r")) == NULL)
|
/*
|
||||||
|
* Slurp the file into memory.
|
||||||
|
*
|
||||||
|
* The file can change concurrently, so we read the whole file into memory
|
||||||
|
* with a single read() call. That's not guaranteed to get an atomic
|
||||||
|
* snapshot, but in practice, for a small file, it's close enough for the
|
||||||
|
* current use.
|
||||||
|
*/
|
||||||
|
fd = open(path, O_RDONLY | PG_BINARY, 0);
|
||||||
|
if (fd < 0)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
if (fstat(fd, &statbuf) < 0)
|
||||||
/* pass over the file twice - the first time to size the result */
|
return NULL;
|
||||||
|
if (statbuf.st_size == 0)
|
||||||
while ((c = fgetc(infile)) != EOF)
|
|
||||||
{
|
{
|
||||||
linelen++;
|
/* empty file */
|
||||||
if (c == '\n')
|
result = (char **) pg_malloc(sizeof(char *));
|
||||||
|
*result = NULL;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
buffer = pg_malloc(statbuf.st_size + 1);
|
||||||
|
|
||||||
|
len = read(fd, buffer, statbuf.st_size + 1);
|
||||||
|
close(fd);
|
||||||
|
if (len != statbuf.st_size)
|
||||||
{
|
{
|
||||||
nlines++;
|
/* oops, the file size changed between fstat and read */
|
||||||
if (linelen > maxlength)
|
free(buffer);
|
||||||
maxlength = linelen;
|
return NULL;
|
||||||
linelen = 0;
|
}
|
||||||
}
|
|
||||||
}
|
/* count newlines */
|
||||||
|
nlines = 0;
|
||||||
/* handle last line without a terminating newline (yuck) */
|
for (i = 0; i < len - 1; i++)
|
||||||
if (linelen)
|
{
|
||||||
nlines++;
|
if (buffer[i] == '\n')
|
||||||
if (linelen > maxlength)
|
nlines++;
|
||||||
maxlength = linelen;
|
}
|
||||||
|
nlines++; /* account for the last line */
|
||||||
/* set up the result and the line buffer */
|
|
||||||
result = (char **) pg_malloc((nlines + 1) * sizeof(char *));
|
/* set up the result buffer */
|
||||||
buffer = (char *) pg_malloc(maxlength + 1);
|
result = (char **) pg_malloc((nlines + 1) * sizeof(char *));
|
||||||
|
|
||||||
/* now reprocess the file and store the lines */
|
/* now split the buffer into lines */
|
||||||
rewind(infile);
|
linebegin = buffer;
|
||||||
nlines = 0;
|
n = 0;
|
||||||
while (fgets(buffer, maxlength + 1, infile) != NULL)
|
for (i = 0; i < len; i++)
|
||||||
result[nlines++] = xstrdup(buffer);
|
{
|
||||||
|
if (buffer[i] == '\n' || i == len - 1)
|
||||||
fclose(infile);
|
{
|
||||||
|
int slen = &buffer[i] - linebegin + 1;
|
||||||
|
char *linebuf = pg_malloc(slen + 1);
|
||||||
|
memcpy(linebuf, linebegin, slen);
|
||||||
|
linebuf[slen] = '\0';
|
||||||
|
result[n++] = linebuf;
|
||||||
|
linebegin = &buffer[i + 1];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
result[n] = NULL;
|
||||||
|
|
||||||
free(buffer);
|
free(buffer);
|
||||||
result[nlines] = NULL;
|
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user