Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit b8b34b7

Browse files
committed
When reading pg_hba.conf and similar files, do not treat @file as an inclusion
unless (1) the @ isn't quoted and (2) the filename isn't empty. This guards against unexpectedly treating usernames or other strings in "flat files" as inclusion requests, as seen in a recent trouble report from Ed L. The empty-filename case would be guaranteed to misbehave anyway, because our subsequent path-munging behavior results in trying to read the directory containing the current input file. I think this might finally explain the report at http://archives.postgresql.org/pgsql-bugs/2004-05/msg00132.php of a crash after printing "authentication file token too long, skipping", since I was able to duplicate that message (though not a crash) on a platform where stdio doesn't refuse to read directories. We never got far in investigating that problem, but now I'm suspicious that the trigger condition was an @ in the flat password file. Back-patch to all active branches since the problem can be demonstrated in all branches except HEAD. The test case, creating a user named "@", doesn't cause a problem in HEAD since we got rid of the flat password file. Nonetheless it seems like a good idea to not consider quoted @ as a file inclusion spec, so I changed HEAD too.
1 parent 8eb8194 commit b8b34b7

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

src/backend/libpq/hba.c

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
*
1111
*
1212
* IDENTIFICATION
13-
* $PostgreSQL: pgsql/src/backend/libpq/hba.c,v 1.200 2010/03/03 20:31:08 tgl Exp $
13+
* $PostgreSQL: pgsql/src/backend/libpq/hba.c,v 1.201 2010/03/06 00:45:49 tgl Exp $
1414
*
1515
*-------------------------------------------------------------------------
1616
*/
@@ -90,6 +90,10 @@ pg_isblank(const char c)
9090
* double quotes (this allows the inclusion of blanks, but not newlines).
9191
*
9292
* The token, if any, is returned at *buf (a buffer of size bufsz).
93+
* Also, we set *initial_quote to indicate whether there was quoting before
94+
* the first character. (We use that to prevent "@x" from being treated
95+
* as a file inclusion request. Note that @"x" should be so treated;
96+
* we want to allow that to support embedded spaces in file paths.)
9397
*
9498
* If successful: store null-terminated token at *buf and return TRUE.
9599
* If no more tokens on line: set *buf = '\0' and return FALSE.
@@ -104,7 +108,7 @@ pg_isblank(const char c)
104108
* token.
105109
*/
106110
static bool
107-
next_token(FILE *fp, char *buf, int bufsz)
111+
next_token(FILE *fp, char *buf, int bufsz, bool *initial_quote)
108112
{
109113
int c;
110114
char *start_buf = buf;
@@ -113,8 +117,11 @@ next_token(FILE *fp, char *buf, int bufsz)
113117
bool was_quote = false;
114118
bool saw_quote = false;
115119

120+
/* end_buf reserves two bytes to ensure we can append \n and \0 */
116121
Assert(end_buf > start_buf);
117122

123+
*initial_quote = false;
124+
118125
/* Move over initial whitespace and commas */
119126
while ((c = getc(fp)) != EOF && (pg_isblank(c) || c == ','))
120127
;
@@ -173,6 +180,8 @@ next_token(FILE *fp, char *buf, int bufsz)
173180
{
174181
in_quote = !in_quote;
175182
saw_quote = true;
183+
if (buf == start_buf)
184+
*initial_quote = true;
176185
}
177186

178187
c = getc(fp);
@@ -216,12 +225,13 @@ next_token_expand(const char *filename, FILE *file)
216225
char *comma_str = pstrdup("");
217226
bool got_something = false;
218227
bool trailing_comma;
228+
bool initial_quote;
219229
char *incbuf;
220230
int needed;
221231

222232
do
223233
{
224-
if (!next_token(file, buf, sizeof(buf)))
234+
if (!next_token(file, buf, sizeof(buf), &initial_quote))
225235
break;
226236

227237
got_something = true;
@@ -235,7 +245,7 @@ next_token_expand(const char *filename, FILE *file)
235245
trailing_comma = false;
236246

237247
/* Is this referencing a file? */
238-
if (buf[0] == '@')
248+
if (!initial_quote && buf[0] == '@' && buf[1] != '\0')
239249
incbuf = tokenize_inc_file(filename, buf + 1);
240250
else
241251
incbuf = pstrdup(buf);

0 commit comments

Comments
 (0)