Commit 63edc5c4 authored by Bruce Momjian's avatar Bruce Momjian

Fix security problem with psql \e where temp file could be an existing

symlink created by someone else, and therefore modifyable by someone else.
parent 3f199872
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
* *
* Copyright 2000 by PostgreSQL Global Development Group * Copyright 2000 by PostgreSQL Global Development Group
* *
* $Header: /cvsroot/pgsql/src/bin/psql/command.c,v 1.38 2000/11/13 23:37:53 momjian Exp $ * $Header: /cvsroot/pgsql/src/bin/psql/command.c,v 1.39 2000/11/25 06:21:54 momjian Exp $
*/ */
#include "postgres.h" #include "postgres.h"
#include "command.h" #include "command.h"
...@@ -13,7 +13,8 @@ ...@@ -13,7 +13,8 @@
#include <ctype.h> #include <ctype.h>
#ifndef WIN32 #ifndef WIN32
#include <sys/types.h> /* for umask() */ #include <sys/types.h> /* for umask() */
#include <sys/stat.h> /* for umask(), stat() */ #include <sys/stat.h> /* for stat() */
#include <fcntl.h> /* open() flags */
#include <unistd.h> /* for geteuid(), getpid(), stat() */ #include <unistd.h> /* for geteuid(), getpid(), stat() */
#else #else
#include <win32.h> #include <win32.h>
...@@ -1397,7 +1398,8 @@ do_edit(const char *filename_arg, PQExpBuffer query_buf) ...@@ -1397,7 +1398,8 @@ do_edit(const char *filename_arg, PQExpBuffer query_buf)
FILE *stream; FILE *stream;
const char *fname; const char *fname;
bool error = false; bool error = false;
int fd;
#ifndef WIN32 #ifndef WIN32
struct stat before, struct stat before,
after; after;
...@@ -1411,7 +1413,6 @@ do_edit(const char *filename_arg, PQExpBuffer query_buf) ...@@ -1411,7 +1413,6 @@ do_edit(const char *filename_arg, PQExpBuffer query_buf)
{ {
/* make a temp file to edit */ /* make a temp file to edit */
#ifndef WIN32 #ifndef WIN32
mode_t oldumask;
const char *tmpdirenv = getenv("TMPDIR"); const char *tmpdirenv = getenv("TMPDIR");
sprintf(fnametmp, "%s/psql.edit.%ld.%ld", sprintf(fnametmp, "%s/psql.edit.%ld.%ld",
...@@ -1422,15 +1423,11 @@ do_edit(const char *filename_arg, PQExpBuffer query_buf) ...@@ -1422,15 +1423,11 @@ do_edit(const char *filename_arg, PQExpBuffer query_buf)
#endif #endif
fname = (const char *) fnametmp; fname = (const char *) fnametmp;
#ifndef WIN32 fd = open(fname, O_WRONLY|O_CREAT|O_EXCL, 0600);
oldumask = umask(0177); if (fd != -1)
#endif stream = fdopen(fd, "w");
stream = fopen(fname, "w");
#ifndef WIN32
umask(oldumask);
#endif
if (!stream) if (fd == -1 || !stream)
{ {
psql_error("couldn't open temp file %s: %s\n", fname, strerror(errno)); psql_error("couldn't open temp file %s: %s\n", fname, strerror(errno));
error = true; error = true;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment