diff options
author | Tom Lane <tgl@sss.pgh.pa.us> | 2013-04-01 14:00:51 -0400 |
---|---|---|
committer | Tom Lane <tgl@sss.pgh.pa.us> | 2013-04-01 14:00:51 -0400 |
commit | 17fe2793ea7fe269ed616cb305150b6cf38dbaa8 (patch) | |
tree | 1ae089183980d11e9776d1e73b9c81e9db09367c /src/backend/tcop/postgres.c | |
parent | ce9ab88981495d975aade8fc664f99f68fc18e2b (diff) | |
download | postgresql-17fe2793ea7fe269ed616cb305150b6cf38dbaa8.tar.gz postgresql-17fe2793ea7fe269ed616cb305150b6cf38dbaa8.zip |
Fix insecure parsing of server command-line switches.
An oversight in commit e710b65c1c56ca7b91f662c63d37ff2e72862a94 allowed
database names beginning with "-" to be treated as though they were secure
command-line switches; and this switch processing occurs before client
authentication, so that even an unprivileged remote attacker could exploit
the bug, needing only connectivity to the postmaster's port. Assorted
exploits for this are possible, some requiring a valid database login,
some not. The worst known problem is that the "-r" switch can be invoked
to redirect the process's stderr output, so that subsequent error messages
will be appended to any file the server can write. This can for example be
used to corrupt the server's configuration files, so that it will fail when
next restarted. Complete destruction of database tables is also possible.
Fix by keeping the database name extracted from a startup packet fully
separate from command-line switches, as had already been done with the
user name field.
The Postgres project thanks Mitsumasa Kondo for discovering this bug,
Kyotaro Horiguchi for drafting the fix, and Noah Misch for recognizing
the full extent of the danger.
Security: CVE-2013-1899
Diffstat (limited to 'src/backend/tcop/postgres.c')
-rw-r--r-- | src/backend/tcop/postgres.c | 41 |
1 files changed, 21 insertions, 20 deletions
diff --git a/src/backend/tcop/postgres.c b/src/backend/tcop/postgres.c index 587d065f1cc..f0783031808 100644 --- a/src/backend/tcop/postgres.c +++ b/src/backend/tcop/postgres.c @@ -3247,13 +3247,14 @@ get_stats_option_name(const char *arg) * coming from the client, or PGC_SUSET for insecure options coming from * a superuser client. * - * Returns the database name extracted from the command line, if any. + * If a database name is present in the command line arguments, it's + * returned into *dbname (this is allowed only if *dbname is initially NULL). * ---------------------------------------------------------------- */ -const char * -process_postgres_switches(int argc, char *argv[], GucContext ctx) +void +process_postgres_switches(int argc, char *argv[], GucContext ctx, + const char **dbname) { - const char *dbname; bool secure = (ctx == PGC_POSTMASTER); int errs = 0; GucSource gucsource; @@ -3304,7 +3305,8 @@ process_postgres_switches(int argc, char *argv[], GucContext ctx) case 'b': /* Undocumented flag used for binary upgrades */ - IsBinaryUpgrade = true; + if (secure) + IsBinaryUpgrade = true; break; case 'C': @@ -3321,7 +3323,8 @@ process_postgres_switches(int argc, char *argv[], GucContext ctx) break; case 'E': - EchoQuery = true; + if (secure) + EchoQuery = true; break; case 'e': @@ -3346,7 +3349,8 @@ process_postgres_switches(int argc, char *argv[], GucContext ctx) break; case 'j': - UseNewLine = 0; + if (secure) + UseNewLine = 0; break; case 'k': @@ -3464,13 +3468,10 @@ process_postgres_switches(int argc, char *argv[], GucContext ctx) } /* - * Should be no more arguments except an optional database name, and - * that's only in the secure case. + * Optional database name should be there only if *dbname is NULL. */ - if (!errs && secure && argc - optind >= 1) - dbname = strdup(argv[optind++]); - else - dbname = NULL; + if (!errs && dbname && *dbname == NULL && argc - optind >= 1) + *dbname = strdup(argv[optind++]); if (errs || argc != optind) { @@ -3499,8 +3500,6 @@ process_postgres_switches(int argc, char *argv[], GucContext ctx) #ifdef HAVE_INT_OPTRESET optreset = 1; /* some systems need this too */ #endif - - return dbname; } @@ -3510,14 +3509,16 @@ process_postgres_switches(int argc, char *argv[], GucContext ctx) * * argc/argv are the command line arguments to be used. (When being forked * by the postmaster, these are not the original argv array of the process.) - * username is the (possibly authenticated) PostgreSQL user name to be used - * for the session. + * dbname is the name of the database to connect to, or NULL if the database + * name should be extracted from the command line arguments or defaulted. + * username is the PostgreSQL user name to be used for the session. * ---------------------------------------------------------------- */ void -PostgresMain(int argc, char *argv[], const char *username) +PostgresMain(int argc, char *argv[], + const char *dbname, + const char *username) { - const char *dbname; int firstchar; StringInfoData input_message; sigjmp_buf local_sigjmp_buf; @@ -3564,7 +3565,7 @@ PostgresMain(int argc, char *argv[], const char *username) /* * Parse command-line options. */ - dbname = process_postgres_switches(argc, argv, PGC_POSTMASTER); + process_postgres_switches(argc, argv, PGC_POSTMASTER, &dbname); /* Must have gotten a database name, or have a default (the username) */ if (dbname == NULL) |