Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit71a6f8b

Browse files
committed
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 thatinclude file and getrlimit().
1 parent1c16029 commit71a6f8b

File tree

9 files changed

+136
-52
lines changed

9 files changed

+136
-52
lines changed

‎configure

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7589,7 +7589,8 @@ done
75897589

75907590

75917591

7592-
for ac_header in crypt.h dld.h endian.h fp_class.h getopt.h ieeefp.h langinfo.h poll.h pwd.h sys/ipc.h sys/poll.h sys/pstat.h sys/select.h sys/sem.h sys/socket.h sys/shm.h sys/time.h sys/un.h termios.h utime.h wchar.h wctype.h kernel/OS.h kernel/image.h SupportDefs.h
7592+
7593+
for ac_header in crypt.h dld.h endian.h fp_class.h getopt.h ieeefp.h langinfo.h poll.h pwd.h sys/ipc.h sys/poll.h sys/pstat.h sys/resource.h sys/select.h sys/sem.h sys/socket.h sys/shm.h sys/time.h sys/un.h termios.h utime.h wchar.h wctype.h kernel/OS.h kernel/image.h SupportDefs.h
75937594
do
75947595
as_ac_Header=`echo "ac_cv_header_$ac_header" | $as_tr_sh`
75957596
if eval "test \"\${$as_ac_Header+set}\" = set"; then
@@ -13454,7 +13455,8 @@ fi
1345413455

1345513456

1345613457

13457-
for ac_func in cbrt dlopen fcvt fdatasync getpeereid memmove poll pstat readlink setproctitle setsid sigprocmask symlink sysconf towlower utime utimes waitpid wcstombs
13458+
13459+
for ac_func in cbrt dlopen fcvt fdatasync getpeereid getrlimit memmove poll pstat readlink setproctitle setsid sigprocmask symlink sysconf towlower utime utimes waitpid wcstombs
1345813460
do
1345913461
as_ac_var=`echo "ac_cv_func_$ac_func" | $as_tr_sh`
1346013462
echo "$as_me:$LINENO: checking for $ac_func" >&5

‎configure.in

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
dnl Process this file with autoconf to produce a configure script.
2-
dnl $PostgreSQL: pgsql/configure.in,v 1.481 2006/10/05 00:07:45 tgl Exp $
2+
dnl $PostgreSQL: pgsql/configure.in,v 1.482 2006/10/07 19:25:28 tgl Exp $
33
dnl
44
dnl Developers, please strive to achieve this order:
55
dnl
@@ -717,7 +717,7 @@ fi
717717
##
718718

719719
dnl sys/socket.h is required by AC_FUNC_ACCEPT_ARGTYPES
720-
AC_CHECK_HEADERS([crypt.h dld.h endian.h fp_class.h getopt.h ieeefp.h langinfo.h poll.h pwd.h sys/ipc.h sys/poll.h sys/pstat.h sys/select.h sys/sem.h sys/socket.h sys/shm.h sys/time.h sys/un.h termios.h utime.h wchar.h wctype.h kernel/OS.h kernel/image.h SupportDefs.h])
720+
AC_CHECK_HEADERS([crypt.h dld.h endian.h fp_class.h getopt.h ieeefp.h langinfo.h poll.h pwd.h sys/ipc.h sys/poll.h sys/pstat.h sys/resource.h sys/select.h sys/sem.h sys/socket.h sys/shm.h sys/time.h sys/un.h termios.h utime.h wchar.h wctype.h kernel/OS.h kernel/image.h SupportDefs.h])
721721

722722
# At least on IRIX, cpp test for netinet/tcp.h will fail unless
723723
# netinet/in.h is included first.
@@ -865,7 +865,7 @@ PGAC_VAR_INT_TIMEZONE
865865
AC_FUNC_ACCEPT_ARGTYPES
866866
PGAC_FUNC_GETTIMEOFDAY_1ARG
867867

868-
AC_CHECK_FUNCS([cbrt dlopen fcvt fdatasync getpeereid memmove poll pstat readlink setproctitle setsid sigprocmask symlink sysconf towlower utime utimes waitpid wcstombs])
868+
AC_CHECK_FUNCS([cbrt dlopen fcvt fdatasync getpeereidgetrlimitmemmove poll pstat readlink setproctitle setsid sigprocmask symlink sysconf towlower utime utimes waitpid wcstombs])
869869

870870
AC_CHECK_DECLS(fdatasync, [], [], [#include <unistd.h>])
871871
AC_CHECK_DECLS(posix_fadvise, [], [], [#include <fcntl.h>])

‎contrib/pgbench/pgbench.c

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* $PostgreSQL: pgsql/contrib/pgbench/pgbench.c,v 1.56 2006/10/04 00:29:45 momjian Exp $
2+
* $PostgreSQL: pgsql/contrib/pgbench/pgbench.c,v 1.57 2006/10/07 19:25:28 tgl Exp $
33
*
44
* pgbench: a simple benchmark program for PostgreSQL
55
* written by Tatsuo Ishii
@@ -37,8 +37,9 @@
3737
#include<sys/select.h>
3838
#endif
3939

40-
/* for getrlimit */
41-
#include<sys/resource.h>
40+
#ifdefHAVE_SYS_RESOURCE_H
41+
#include<sys/resource.h>/* for getrlimit */
42+
#endif
4243
#endif/* ! WIN32 */
4344

4445
externchar*optarg;
@@ -1172,7 +1173,7 @@ main(int argc, char **argv)
11721173
intnsocks;/* return from select(2) */
11731174
intmaxsock;/* max socket number to be waited */
11741175

1175-
#if !(defined(__CYGWIN__)|| defined(__MINGW32__))
1176+
#ifdefHAVE_GETRLIMIT
11761177
structrlimitrlim;
11771178
#endif
11781179

@@ -1233,8 +1234,8 @@ main(int argc, char **argv)
12331234
fprintf(stderr,"invalid number of clients: %d\n",nclients);
12341235
exit(1);
12351236
}
1236-
#if !(defined(__CYGWIN__)|| defined(__MINGW32__))
1237-
#ifdefRLIMIT_NOFILE/* mostplatform uses RLIMIT_NOFILE */
1237+
#ifdefHAVE_GETRLIMIT
1238+
#ifdefRLIMIT_NOFILE/* mostplatforms use RLIMIT_NOFILE */
12381239
if (getrlimit(RLIMIT_NOFILE,&rlim)==-1)
12391240
#else/* but BSD doesn't ... */
12401241
if (getrlimit(RLIMIT_OFILE,&rlim)==-1)
@@ -1245,11 +1246,11 @@ main(int argc, char **argv)
12451246
}
12461247
if (rlim.rlim_cur <= (nclients+2))
12471248
{
1248-
fprintf(stderr,"You need at least %d open filesresourcebut you are only allowed to use %ld.\n",nclients+2, (long)rlim.rlim_cur);
1249-
fprintf(stderr,"Use limit/ulimt to increase the limit before using pgbench.\n");
1249+
fprintf(stderr,"You need at least %d open files but you are only allowed to use %ld.\n",nclients+2, (long)rlim.rlim_cur);
1250+
fprintf(stderr,"Use limit/ulimit to increase the limit before using pgbench.\n");
12501251
exit(1);
12511252
}
1252-
#endif
1253+
#endif/* HAVE_GETRLIMIT */
12531254
break;
12541255
case'C':
12551256
is_connect=1;

‎doc/src/sgml/config.sgml

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!-- $PostgreSQL: pgsql/doc/src/sgml/config.sgml,v 1.89 2006/09/25 22:12:24 tgl Exp $ -->
1+
<!-- $PostgreSQL: pgsql/doc/src/sgml/config.sgml,v 1.90 2006/10/07 19:25:28 tgl Exp $ -->
22

33
<chapter Id="runtime-config">
44
<title>Server Configuration</title>
@@ -846,14 +846,22 @@ SET ENABLE_SEQSCAN TO OFF;
846846
equivalent), less a safety margin of a megabyte or so. The safety
847847
margin is needed because the stack depth is not checked in every
848848
routine in the server, but only in key potentially-recursive routines
849-
such as expression evaluation. Setting the parameter higher than
850-
the actual kernel limit will mean that a runaway recursive function
851-
can crash an individual backend process. The default setting is
849+
such as expression evaluation. The default setting is
852850
2048 KB (two megabytes), which is conservatively small and unlikely
853851
to risk crashes. However, it may be too small to allow execution
854852
of complex functions.
855853
Only superusers can change this setting.
856854
</para>
855+
856+
<para>
857+
Setting <varname>max_stack_depth</> higher than
858+
the actual kernel limit will mean that a runaway recursive function
859+
can crash an individual backend process. On platforms where
860+
<productname>PostgreSQL</productname> can determine the kernel limit,
861+
it will not let you set this variable to an unsafe value. However,
862+
not all platforms provide the information, so caution is recommended
863+
in selecting a value.
864+
</para>
857865
</listitem>
858866
</varlistentry>
859867

@@ -2173,19 +2181,6 @@ SELECT * FROM parent WHERE key = 2400;
21732181
</listitem>
21742182
</varlistentry>
21752183

2176-
<varlistentry id="guc-gin-fuzzy-search-limit" xreflabel="gin_fuzzy_search_limit">
2177-
<term><varname>gin_fuzzy_search_limit</varname> (<type>integer</type>)</term>
2178-
<indexterm>
2179-
<primary><varname>gin_fuzzy_search_limit</> configuration parameter</primary>
2180-
</indexterm>
2181-
<listitem>
2182-
<para>
2183-
Soft upper limit of the size of the set returned by GIN index. For more
2184-
information see <xref linkend="gin-tips">.
2185-
</para>
2186-
</listitem>
2187-
</varlistentry>
2188-
21892184
</variablelist>
21902185
</sect2>
21912186
</sect1>
@@ -3718,6 +3713,19 @@ dynamic_library_path = 'C:\tools\postgresql;H:\my_project\lib;$libdir'
37183713
</para>
37193714
</listitem>
37203715
</varlistentry>
3716+
3717+
<varlistentry id="guc-gin-fuzzy-search-limit" xreflabel="gin_fuzzy_search_limit">
3718+
<term><varname>gin_fuzzy_search_limit</varname> (<type>integer</type>)</term>
3719+
<indexterm>
3720+
<primary><varname>gin_fuzzy_search_limit</> configuration parameter</primary>
3721+
</indexterm>
3722+
<listitem>
3723+
<para>
3724+
Soft upper limit of the size of the set returned by GIN index. For more
3725+
information see <xref linkend="gin-tips">.
3726+
</para>
3727+
</listitem>
3728+
</varlistentry>
37213729

37223730
<varlistentry id="guc-local-preload-libraries" xreflabel="local_preload_libraries">
37233731
<term><varname>local_preload_libraries</varname> (<type>string</type>)</term>

‎src/backend/tcop/postgres.c

Lines changed: 56 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/tcop/postgres.c,v 1.511 2006/10/0716:43:28 tgl Exp $
11+
* $PostgreSQL: pgsql/src/backend/tcop/postgres.c,v 1.512 2006/10/0719:25:28 tgl Exp $
1212
*
1313
* NOTES
1414
* this is the "main" module of the postgres backend and
@@ -23,13 +23,20 @@
2323
#include<signal.h>
2424
#include<fcntl.h>
2525
#include<sys/socket.h>
26-
#ifHAVE_SYS_SELECT_H
26+
#ifdefHAVE_SYS_SELECT_H
2727
#include<sys/select.h>
2828
#endif
29+
#ifdefHAVE_SYS_RESOURCE_H
30+
#include<sys/resource.h>
31+
#endif
2932
#ifdefHAVE_GETOPT_H
3033
#include<getopt.h>
3134
#endif
3235

36+
#ifndefHAVE_GETRUSAGE
37+
#include"rusagestub.h"
38+
#endif
39+
3340
#include"access/printtup.h"
3441
#include"access/xact.h"
3542
#include"catalog/pg_type.h"
@@ -78,7 +85,7 @@ boolLog_disconnections = false;
7885
LogStmtLevellog_statement=LOGSTMT_NONE;
7986

8087
/* GUC variable for maximum stack depth (measured in kilobytes) */
81-
intmax_stack_depth=2048;
88+
intmax_stack_depth=100;
8289

8390
/* wait N seconds to allow attach from a debugger */
8491
intPostAuthDelay=0;
@@ -91,7 +98,7 @@ intPostAuthDelay = 0;
9198
*/
9299

93100
/* max_stack_depth converted to bytes for speed of checking */
94-
staticlongmax_stack_depth_bytes=2048*1024L;
101+
staticlongmax_stack_depth_bytes=100*1024L;
95102

96103
/*
97104
* Stack base pointer -- initialized by PostgresMain. This is not static
@@ -2490,9 +2497,7 @@ ProcessInterrupts(void)
24902497
* This should be called someplace in any recursive routine that might possibly
24912498
* recurse deep enough to overflow the stack. Most Unixen treat stack
24922499
* overflow as an unrecoverable SIGSEGV, so we want to error out ourselves
2493-
* before hitting the hardware limit. Unfortunately we have no direct way
2494-
* to detect the hardware limit, so we have to rely on the admin to set a
2495-
* GUC variable for it ...
2500+
* before hitting the hardware limit.
24962501
*/
24972502
void
24982503
check_stack_depth(void)
@@ -2530,13 +2535,24 @@ check_stack_depth(void)
25302535
}
25312536
}
25322537

2533-
/* GUC assign hookto update max_stack_depth_bytes from max_stack_depth */
2538+
/* GUC assign hookfor max_stack_depth */
25342539
bool
25352540
assign_max_stack_depth(intnewval,booldoit,GucSourcesource)
25362541
{
2537-
/* Range check was already handled by guc.c */
2542+
longnewval_bytes=newval*1024L;
2543+
longstack_rlimit=get_stack_depth_rlimit();
2544+
2545+
if (stack_rlimit>0&&newval_bytes>stack_rlimit-STACK_DEPTH_SLOP)
2546+
{
2547+
ereport((source >=PGC_S_INTERACTIVE) ?ERROR :LOG,
2548+
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2549+
errmsg("\"max_stack_depth\" must not exceed %ldkB",
2550+
(stack_rlimit-STACK_DEPTH_SLOP) /1024L),
2551+
errhint("Increase the platform's stack depth limit via \"ulimit -s\" or local equivalent.")));
2552+
return false;
2553+
}
25382554
if (doit)
2539-
max_stack_depth_bytes=newval*1024L;
2555+
max_stack_depth_bytes=newval_bytes;
25402556
return true;
25412557
}
25422558

@@ -3635,11 +3651,36 @@ PostgresMain(int argc, char *argv[], const char *username)
36353651
return1;/* keep compiler quiet */
36363652
}
36373653

3638-
#ifndefHAVE_GETRUSAGE
3639-
#include"rusagestub.h"
3640-
#else
3641-
#include<sys/resource.h>
3642-
#endif/* HAVE_GETRUSAGE */
3654+
3655+
/*
3656+
* Obtain platform stack depth limit (in bytes)
3657+
*
3658+
* Return -1 if unlimited or not known
3659+
*/
3660+
long
3661+
get_stack_depth_rlimit(void)
3662+
{
3663+
#if defined(HAVE_GETRLIMIT)&& defined(RLIMIT_STACK)
3664+
staticlongval=0;
3665+
3666+
/* This won't change after process launch, so check just once */
3667+
if (val==0)
3668+
{
3669+
structrlimitrlim;
3670+
3671+
if (getrlimit(RLIMIT_STACK,&rlim)<0)
3672+
val=-1;
3673+
elseif (rlim.rlim_cur==RLIM_INFINITY)
3674+
val=-1;
3675+
else
3676+
val=rlim.rlim_cur;
3677+
}
3678+
returnval;
3679+
#else/* no getrlimit */
3680+
return-1;
3681+
#endif
3682+
}
3683+
36433684

36443685
staticstructrusageSave_r;
36453686
staticstructtimevalSave_t;

‎src/backend/utils/misc/guc.c

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
* Written by Peter Eisentraut <peter_e@gmx.net>.
1111
*
1212
* IDENTIFICATION
13-
* $PostgreSQL: pgsql/src/backend/utils/misc/guc.c,v 1.355 2006/10/06 17:14:00 petere Exp $
13+
* $PostgreSQL: pgsql/src/backend/utils/misc/guc.c,v 1.356 2006/10/07 19:25:28 tgl Exp $
1414
*
1515
*--------------------------------------------------------------------
1616
*/
@@ -1214,7 +1214,7 @@ static struct config_int ConfigureNamesInt[] =
12141214
GUC_UNIT_KB
12151215
},
12161216
&max_stack_depth,
1217-
2048,100,MAX_KILOBYTES,assign_max_stack_depth,NULL
1217+
100,100,MAX_KILOBYTES,assign_max_stack_depth,NULL
12181218
},
12191219

12201220
{
@@ -1610,7 +1610,7 @@ static struct config_int ConfigureNamesInt[] =
16101610
},
16111611

16121612
{
1613-
{"gin_fuzzy_search_limit",PGC_USERSET,UNGROUPED,
1613+
{"gin_fuzzy_search_limit",PGC_USERSET,CLIENT_CONN_OTHER,
16141614
gettext_noop("Sets the maximum allowed result for exact search by GIN."),
16151615
NULL,
16161616
0
@@ -2702,6 +2702,7 @@ InitializeGUCOptions(void)
27022702
{
27032703
inti;
27042704
char*env;
2705+
longstack_rlimit;
27052706

27062707
/*
27072708
* Build sorted array of all GUC variables.
@@ -2839,6 +2840,27 @@ InitializeGUCOptions(void)
28392840
env=getenv("PGCLIENTENCODING");
28402841
if (env!=NULL)
28412842
SetConfigOption("client_encoding",env,PGC_POSTMASTER,PGC_S_ENV_VAR);
2843+
2844+
/*
2845+
* rlimit isn't exactly an "environment variable", but it behaves about
2846+
* the same. If we can identify the platform stack depth rlimit, increase
2847+
* default stack depth setting up to whatever is safe (but at most 2MB).
2848+
*/
2849+
stack_rlimit=get_stack_depth_rlimit();
2850+
if (stack_rlimit>0)
2851+
{
2852+
intnew_limit= (stack_rlimit-STACK_DEPTH_SLOP) /1024L;
2853+
2854+
if (new_limit>100)
2855+
{
2856+
charlimbuf[16];
2857+
2858+
new_limit=Min(new_limit,2048);
2859+
sprintf(limbuf,"%d",new_limit);
2860+
SetConfigOption("max_stack_depth",limbuf,
2861+
PGC_POSTMASTER,PGC_S_ENV_VAR);
2862+
}
2863+
}
28422864
}
28432865

28442866

‎src/include/pg_config.h.in

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,9 @@
161161
/* Define to 1 if you have the `getpwuid_r' function. */
162162
#undef HAVE_GETPWUID_R
163163

164+
/* Define to 1 if you have the `getrlimit' function. */
165+
#undef HAVE_GETRLIMIT
166+
164167
/* Define to 1 if you have the `getrusage' function. */
165168
#undef HAVE_GETRUSAGE
166169

@@ -460,6 +463,9 @@
460463
/* Define to 1 if you have the <sys/pstat.h> header file. */
461464
#undef HAVE_SYS_PSTAT_H
462465

466+
/* Define to 1 if you have the <sys/resource.h> header file. */
467+
#undef HAVE_SYS_RESOURCE_H
468+
463469
/* Define to 1 if you have the <sys/select.h> header file. */
464470
#undef HAVE_SYS_SELECT_H
465471

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp