diff options
author | Tom Lane <tgl@sss.pgh.pa.us> | 2006-10-07 19:25:29 +0000 |
---|---|---|
committer | Tom Lane <tgl@sss.pgh.pa.us> | 2006-10-07 19:25:29 +0000 |
commit | 71a6f8b85b9f748dc7f33c1212c4474e8beb901a (patch) | |
tree | d3ae369b5edb5b1c199f4cae9d1a637432c75b15 /src | |
parent | 1c160291ef61f46800616cb88d5a3a02156de4e3 (diff) | |
download | postgresql-71a6f8b85b9f748dc7f33c1212c4474e8beb901a.tar.gz postgresql-71a6f8b85b9f748dc7f33c1212c4474e8beb901a.zip |
On platforms that have getrlimit(RLIMIT_STACK), use it to ensure that
max_stack_depth is not set to an unsafe value.
This commit also provides configure-time checking for <sys/resource.h>,
and cleans up some perhaps-unportable code associated with use of that
include file and getrlimit().
Diffstat (limited to 'src')
-rw-r--r-- | src/backend/tcop/postgres.c | 71 | ||||
-rw-r--r-- | src/backend/utils/misc/guc.c | 28 | ||||
-rw-r--r-- | src/include/pg_config.h.in | 6 | ||||
-rw-r--r-- | src/include/tcop/tcopprot.h | 6 | ||||
-rw-r--r-- | src/include/utils/pg_rusage.h | 4 |
5 files changed, 94 insertions, 21 deletions
diff --git a/src/backend/tcop/postgres.c b/src/backend/tcop/postgres.c index 9145904b49a..ee63220d956 100644 --- a/src/backend/tcop/postgres.c +++ b/src/backend/tcop/postgres.c @@ -8,7 +8,7 @@ * * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/tcop/postgres.c,v 1.511 2006/10/07 16:43:28 tgl Exp $ + * $PostgreSQL: pgsql/src/backend/tcop/postgres.c,v 1.512 2006/10/07 19:25:28 tgl Exp $ * * NOTES * this is the "main" module of the postgres backend and @@ -23,13 +23,20 @@ #include <signal.h> #include <fcntl.h> #include <sys/socket.h> -#if HAVE_SYS_SELECT_H +#ifdef HAVE_SYS_SELECT_H #include <sys/select.h> #endif +#ifdef HAVE_SYS_RESOURCE_H +#include <sys/resource.h> +#endif #ifdef HAVE_GETOPT_H #include <getopt.h> #endif +#ifndef HAVE_GETRUSAGE +#include "rusagestub.h" +#endif + #include "access/printtup.h" #include "access/xact.h" #include "catalog/pg_type.h" @@ -78,7 +85,7 @@ bool Log_disconnections = false; LogStmtLevel log_statement = LOGSTMT_NONE; /* GUC variable for maximum stack depth (measured in kilobytes) */ -int max_stack_depth = 2048; +int max_stack_depth = 100; /* wait N seconds to allow attach from a debugger */ int PostAuthDelay = 0; @@ -91,7 +98,7 @@ int PostAuthDelay = 0; */ /* max_stack_depth converted to bytes for speed of checking */ -static long max_stack_depth_bytes = 2048 * 1024L; +static long max_stack_depth_bytes = 100 * 1024L; /* * Stack base pointer -- initialized by PostgresMain. This is not static @@ -2490,9 +2497,7 @@ ProcessInterrupts(void) * This should be called someplace in any recursive routine that might possibly * recurse deep enough to overflow the stack. Most Unixen treat stack * overflow as an unrecoverable SIGSEGV, so we want to error out ourselves - * before hitting the hardware limit. Unfortunately we have no direct way - * to detect the hardware limit, so we have to rely on the admin to set a - * GUC variable for it ... + * before hitting the hardware limit. */ void check_stack_depth(void) @@ -2530,13 +2535,24 @@ check_stack_depth(void) } } -/* GUC assign hook to update max_stack_depth_bytes from max_stack_depth */ +/* GUC assign hook for max_stack_depth */ bool assign_max_stack_depth(int newval, bool doit, GucSource source) { - /* Range check was already handled by guc.c */ + long newval_bytes = newval * 1024L; + long stack_rlimit = get_stack_depth_rlimit(); + + if (stack_rlimit > 0 && newval_bytes > stack_rlimit - STACK_DEPTH_SLOP) + { + ereport((source >= PGC_S_INTERACTIVE) ? ERROR : LOG, + (errcode(ERRCODE_INVALID_PARAMETER_VALUE), + errmsg("\"max_stack_depth\" must not exceed %ldkB", + (stack_rlimit - STACK_DEPTH_SLOP) / 1024L), + errhint("Increase the platform's stack depth limit via \"ulimit -s\" or local equivalent."))); + return false; + } if (doit) - max_stack_depth_bytes = newval * 1024L; + max_stack_depth_bytes = newval_bytes; return true; } @@ -3635,11 +3651,36 @@ PostgresMain(int argc, char *argv[], const char *username) return 1; /* keep compiler quiet */ } -#ifndef HAVE_GETRUSAGE -#include "rusagestub.h" -#else -#include <sys/resource.h> -#endif /* HAVE_GETRUSAGE */ + +/* + * Obtain platform stack depth limit (in bytes) + * + * Return -1 if unlimited or not known + */ +long +get_stack_depth_rlimit(void) +{ +#if defined(HAVE_GETRLIMIT) && defined(RLIMIT_STACK) + static long val = 0; + + /* This won't change after process launch, so check just once */ + if (val == 0) + { + struct rlimit rlim; + + if (getrlimit(RLIMIT_STACK, &rlim) < 0) + val = -1; + else if (rlim.rlim_cur == RLIM_INFINITY) + val = -1; + else + val = rlim.rlim_cur; + } + return val; +#else /* no getrlimit */ + return -1; +#endif +} + static struct rusage Save_r; static struct timeval Save_t; diff --git a/src/backend/utils/misc/guc.c b/src/backend/utils/misc/guc.c index 96244ec91f8..d118755a3c3 100644 --- a/src/backend/utils/misc/guc.c +++ b/src/backend/utils/misc/guc.c @@ -10,7 +10,7 @@ * Written by Peter Eisentraut <peter_e@gmx.net>. * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/utils/misc/guc.c,v 1.355 2006/10/06 17:14:00 petere Exp $ + * $PostgreSQL: pgsql/src/backend/utils/misc/guc.c,v 1.356 2006/10/07 19:25:28 tgl Exp $ * *-------------------------------------------------------------------- */ @@ -1214,7 +1214,7 @@ static struct config_int ConfigureNamesInt[] = GUC_UNIT_KB }, &max_stack_depth, - 2048, 100, MAX_KILOBYTES, assign_max_stack_depth, NULL + 100, 100, MAX_KILOBYTES, assign_max_stack_depth, NULL }, { @@ -1610,7 +1610,7 @@ static struct config_int ConfigureNamesInt[] = }, { - {"gin_fuzzy_search_limit", PGC_USERSET, UNGROUPED, + {"gin_fuzzy_search_limit", PGC_USERSET, CLIENT_CONN_OTHER, gettext_noop("Sets the maximum allowed result for exact search by GIN."), NULL, 0 @@ -2702,6 +2702,7 @@ InitializeGUCOptions(void) { int i; char *env; + long stack_rlimit; /* * Build sorted array of all GUC variables. @@ -2839,6 +2840,27 @@ InitializeGUCOptions(void) env = getenv("PGCLIENTENCODING"); if (env != NULL) SetConfigOption("client_encoding", env, PGC_POSTMASTER, PGC_S_ENV_VAR); + + /* + * rlimit isn't exactly an "environment variable", but it behaves about + * the same. If we can identify the platform stack depth rlimit, increase + * default stack depth setting up to whatever is safe (but at most 2MB). + */ + stack_rlimit = get_stack_depth_rlimit(); + if (stack_rlimit > 0) + { + int new_limit = (stack_rlimit - STACK_DEPTH_SLOP) / 1024L; + + if (new_limit > 100) + { + char limbuf[16]; + + new_limit = Min(new_limit, 2048); + sprintf(limbuf, "%d", new_limit); + SetConfigOption("max_stack_depth", limbuf, + PGC_POSTMASTER, PGC_S_ENV_VAR); + } + } } diff --git a/src/include/pg_config.h.in b/src/include/pg_config.h.in index c6fe8879868..0043e9afaf2 100644 --- a/src/include/pg_config.h.in +++ b/src/include/pg_config.h.in @@ -161,6 +161,9 @@ /* Define to 1 if you have the `getpwuid_r' function. */ #undef HAVE_GETPWUID_R +/* Define to 1 if you have the `getrlimit' function. */ +#undef HAVE_GETRLIMIT + /* Define to 1 if you have the `getrusage' function. */ #undef HAVE_GETRUSAGE @@ -460,6 +463,9 @@ /* Define to 1 if you have the <sys/pstat.h> header file. */ #undef HAVE_SYS_PSTAT_H +/* Define to 1 if you have the <sys/resource.h> header file. */ +#undef HAVE_SYS_RESOURCE_H + /* Define to 1 if you have the <sys/select.h> header file. */ #undef HAVE_SYS_SELECT_H diff --git a/src/include/tcop/tcopprot.h b/src/include/tcop/tcopprot.h index e6637edeb7f..2006b7c6828 100644 --- a/src/include/tcop/tcopprot.h +++ b/src/include/tcop/tcopprot.h @@ -7,7 +7,7 @@ * Portions Copyright (c) 1996-2006, PostgreSQL Global Development Group * Portions Copyright (c) 1994, Regents of the University of California * - * $PostgreSQL: pgsql/src/include/tcop/tcopprot.h,v 1.84 2006/10/04 00:30:10 momjian Exp $ + * $PostgreSQL: pgsql/src/include/tcop/tcopprot.h,v 1.85 2006/10/07 19:25:29 tgl Exp $ * * OLD COMMENTS * This file was created so that other c files could get the two @@ -23,6 +23,9 @@ #include "utils/guc.h" +/* Required daylight between max_stack_depth and the kernel limit, in bytes */ +#define STACK_DEPTH_SLOP (512 * 1024L) + extern CommandDest whereToSendOutput; extern DLLIMPORT const char *debug_query_string; extern int max_stack_depth; @@ -62,6 +65,7 @@ extern void FloatExceptionHandler(SIGNAL_ARGS); extern void prepare_for_client_read(void); extern void client_read_ended(void); extern int PostgresMain(int argc, char *argv[], const char *username); +extern long get_stack_depth_rlimit(void); extern void ResetUsage(void); extern void ShowUsage(const char *title); extern int check_log_duration(char *msec_str, bool was_logged); diff --git a/src/include/utils/pg_rusage.h b/src/include/utils/pg_rusage.h index 32e409076e1..bb11812da80 100644 --- a/src/include/utils/pg_rusage.h +++ b/src/include/utils/pg_rusage.h @@ -7,7 +7,7 @@ * Portions Copyright (c) 1996-2006, PostgreSQL Global Development Group * Portions Copyright (c) 1994, Regents of the University of California * - * $PostgreSQL: pgsql/src/include/utils/pg_rusage.h,v 1.2 2006/03/05 15:59:07 momjian Exp $ + * $PostgreSQL: pgsql/src/include/utils/pg_rusage.h,v 1.3 2006/10/07 19:25:29 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -16,7 +16,7 @@ #include <sys/time.h> -#ifdef HAVE_GETRUSAGE +#ifdef HAVE_SYS_RESOURCE_H #include <sys/resource.h> #else #include "rusagestub.h" |