Skip to content

Commit 5f644ea

Browse files
committed
Add fstat / S_ISDIR checks to make sure we're not trying to use a
directory for COPY TO/FROM. Brent Verner
1 parent ec4027f commit 5f644ea

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

src/backend/commands/copy.c

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
*
99
* IDENTIFICATION
10-
* $Header: /cvsroot/pgsql/src/backend/commands/copy.c,v 1.145 2002/02/12 21:25:41 tgl Exp $
10+
* $Header: /cvsroot/pgsql/src/backend/commands/copy.c,v 1.146 2002/02/23 21:46:02 momjian Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -326,12 +326,20 @@ DoCopy(char *relname, bool binary, bool oids, bool from, bool pipe,
326326
}
327327
else
328328
{
329+
struct stat st;
329330
fp = AllocateFile(filename, PG_BINARY_R);
330-
if (fp == NULL)
331+
332+
if (fp == NULL)
331333
elog(ERROR, "COPY command, running in backend with "
332334
"effective uid %d, could not open file '%s' for "
333335
"reading. Errno = %s (%d).",
334336
(int) geteuid(), filename, strerror(errno), errno);
337+
338+
fstat(fileno(fp),&st);
339+
if( S_ISDIR(st.st_mode) ){
340+
fclose(fp);
341+
elog(ERROR,"COPY: %s is a directory.",filename);
342+
}
335343
}
336344
CopyFrom(rel, binary, oids, fp, delim, null_print);
337345
}
@@ -360,6 +368,7 @@ DoCopy(char *relname, bool binary, bool oids, bool from, bool pipe,
360368
else
361369
{
362370
mode_t oumask; /* Pre-existing umask value */
371+
struct stat st;
363372

364373
/*
365374
* Prevent write to relative path ... too easy to shoot
@@ -378,6 +387,11 @@ DoCopy(char *relname, bool binary, bool oids, bool from, bool pipe,
378387
"effective uid %d, could not open file '%s' for "
379388
"writing. Errno = %s (%d).",
380389
(int) geteuid(), filename, strerror(errno), errno);
390+
fstat(fileno(fp),&st);
391+
if( S_ISDIR(st.st_mode) ){
392+
fclose(fp);
393+
elog(ERROR,"COPY: %s is a directory.",filename);
394+
}
381395
}
382396
CopyTo(rel, binary, oids, fp, delim, null_print);
383397
}

src/bin/psql/copy.c

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@
33
*
44
* Copyright 2000 by PostgreSQL Global Development Group
55
*
6-
* $Header: /cvsroot/pgsql/src/bin/psql/copy.c,v 1.19 2001/06/02 18:25:18 petere Exp $
6+
* $Header: /cvsroot/pgsql/src/bin/psql/copy.c,v 1.20 2002/02/23 21:46:03 momjian Exp $
77
*/
88
#include "postgres_fe.h"
99
#include "copy.h"
1010

1111
#include <errno.h>
1212
#include <assert.h>
1313
#include <signal.h>
14+
#include <sys/stat.h>
1415
#ifndef WIN32
1516
#include <unistd.h> /* for isatty */
1617
#else
@@ -233,6 +234,7 @@ do_copy(const char *args)
233234
struct copy_options *options;
234235
PGresult *result;
235236
bool success;
237+
struct stat st;
236238

237239
/* parse options */
238240
options = parse_slash_copy(args);
@@ -292,7 +294,16 @@ do_copy(const char *args)
292294
free_copy_options(options);
293295
return false;
294296
}
295-
297+
/* make sure the specified file is not a directory */
298+
fstat(fileno(copystream),&st);
299+
if( S_ISDIR(st.st_mode) ){
300+
fclose(copystream);
301+
psql_error("%s: cannot COPY TO/FROM a directory\n",
302+
options->file);
303+
free_copy_options(options);
304+
return false;
305+
}
306+
296307
result = PSQLexec(query);
297308

298309
switch (PQresultStatus(result))

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy