Received: from magus.postgresql.org (magus.postgresql.org [87.238.57.229]) by mail.postgresql.org (Postfix) with ESMTP id 14E17C3830A for ; Thu, 14 Jun 2012 02:28:44 -0300 (ADT) Received: from szxga01-in.huawei.com ([119.145.14.64]) by magus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1Sf2bn-0001Wt-E8 for pgsql-bugs@postgresql.org; Thu, 14 Jun 2012 05:28:43 +0000 Received: from 172.24.2.119 (EHLO szxeml209-edg.china.huawei.com) ([172.24.2.119]) by szxrg01-dlp.huawei.com (MOS 4.1.9-GA FastPath queued) with ESMTP id AKD07150; Thu, 14 Jun 2012 13:26:27 +0800 (CST) Received: from SZXEML402-HUB.china.huawei.com (10.82.67.32) by szxeml209-edg.china.huawei.com (172.24.2.184) with Microsoft SMTP Server (TLS) id 14.1.323.3; Thu, 14 Jun 2012 13:24:23 +0800 Received: from blrprnc09ns (10.18.96.98) by szxeml402-hub.china.huawei.com (10.82.67.32) with Microsoft SMTP Server id 14.1.323.3; Thu, 14 Jun 2012 13:24:20 +0800 From: Amit Kapila To: "'Alvaro Herrera'" , "'Tom Lane'" CC: "'Edmund Horner'" , "'Pg Bugs'" , "'Bruce Momjian'" References: <9940.1339467780@sss.pgh.pa.us> <4fd73c8b.25d3440a.7d9b.7fd2SMTPIN_ADDED@mx.google.com> <002501cd4920$85617430$90245c90$@kapila@huawei.com> <1339601668-sup-4658@alvh.no-ip.org> In-Reply-To: <1339601668-sup-4658@alvh.no-ip.org> Subject: Re: 9.2 beta2 - pg_ctl crashes on Win32 when neither PGDATA nor -D specified Date: Thu, 14 Jun 2012 10:54:18 +0530 Message-ID: <003a01cd49ed$f32ea690$d98bf3b0$@kapila@huawei.com> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----=_NextPart_000_003B_01CD4A1C.0CE6E290" X-Mailer: Microsoft Office Outlook 12.0 Thread-Index: Ac1JfKZkpAicr3SbR7Clx74Y7xHQ3gAcOW1g Content-Language: en-us X-Originating-IP: [10.18.96.98] X-CFilter-Loop: Reflected X-Pg-Spam-Score: -3.2 (---) X-Archive-Number: 201206/128 X-Sequence-Number: 33355 ------=_NextPart_000_003B_01CD4A1C.0CE6E290 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Please Find the patch based on idea I have suggested attached with this = mail. Let me know your comments regarding the same. -----Original Message----- From: pgsql-bugs-owner@postgresql.org = [mailto:pgsql-bugs-owner@postgresql.org] On Behalf Of Alvaro Herrera Sent: Wednesday, June 13, 2012 9:23 PM To: Amit Kapila Cc: 'Edmund Horner'; Tom Lane; Pg Bugs; Bruce Momjian Subject: Re: [BUGS] 9.2 beta2 - pg_ctl crashes on Win32 when neither = PGDATA nor -D specified Excerpts from Amit Kapila's message of mi=C3=A9 jun 13 00:53:47 -0400 = 2012: > > Unfortunately in src/backend/main/main.c it only does a cursory = check > > for --help and --version. So it would need to become a little more > > complicated to scan for -C options at that stage. It's not too much > > if you can assume -C always appears first like the other special > > options detected in that file. >=20 > I am doubtful whether we should make such an exception for -C option, = as > this will > be a change in behavior as compare to previous versions.=20 > How to do in code is next step. > According to me the solution I have proposed is safer and already = initdb > handles in same way. >=20 > I am waiting for other people opinion on this issue. I agree with you. The fact that we drop privileges is not only a security measure; it's a robustness one as well. With the current setup, we can confidently say "it's not Postgres' fault" when the system crashes with some weird kernel error. A process running with administrative privs is capable of doing privileged stuff that may override safe interfaces provided by the operating system; a process without admin privs is more constrained and should not be able to cause the system to crash. Any kernel crash, then, is not our responsibility. If we allow -C to run with admin privs, we lose that. --=20 =C3=81lvaro Herrera The PostgreSQL Company - Command Prompt, Inc. PostgreSQL Replication, Consulting, Custom Development, 24x7 support --=20 Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs ------=_NextPart_000_003B_01CD4A1C.0CE6E290 Content-Type: application/octet-stream; name="pgctldefectfix.patch" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="pgctldefectfix.patch" diff --git a/src/bin/pg_ctl/pg_ctl.c b/src/bin/pg_ctl/pg_ctl.c=0A= index 329cc24..4462749 100644=0A= --- a/src/bin/pg_ctl/pg_ctl.c=0A= +++ b/src/bin/pg_ctl/pg_ctl.c=0A= @@ -1961,10 +1961,16 @@ main(int argc, char **argv)=0A= int c;=0A= pgpid_t killproc =3D 0;=0A= =0A= +#ifdef WIN32=0A= + char *restrict_env;=0A= +#endif=0A= +=0A= #if defined(WIN32) || defined(__CYGWIN__)=0A= setvbuf(stderr, NULL, _IONBF, 0);=0A= #endif=0A= =0A= +=0A= +=0A= progname =3D get_progname(argv[0]);=0A= set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pg_ctl"));=0A= start_time =3D time(NULL);=0A= @@ -2182,6 +2188,49 @@ main(int argc, char **argv)=0A= canonicalize_path(pg_config);=0A= pg_data =3D xstrdup(pg_config);=0A= }=0A= +#ifdef WIN32=0A= +=0A= + /*=0A= + * Before we execute another program, make sure that we are running = with a=0A= + * restricted token. If not, re-execute ourselves with one.=0A= + */=0A= +=0A= + if ((restrict_env =3D getenv("PG_RESTRICT_EXEC")) =3D=3D NULL=0A= + || strcmp(restrict_env, "1") !=3D 0)=0A= + {=0A= + PROCESS_INFORMATION pi;=0A= + char *cmdline;=0A= +=0A= + ZeroMemory(&pi, sizeof(pi));=0A= +=0A= + cmdline =3D xstrdup(GetCommandLine());=0A= +=0A= + putenv("PG_RESTRICT_EXEC=3D1");=0A= +=0A= + if (!CreateRestrictedProcess(cmdline, &pi, false))=0A= + {=0A= + write_stderr(_("%s: could not re-exec with restricted token: error = code %lu\n"), progname, GetLastError());=0A= + }=0A= + else=0A= + {=0A= + /*=0A= + * Successfully re-execed. Now wait for child process to capture=0A= + * exitcode.=0A= + */=0A= + DWORD x;=0A= +=0A= + CloseHandle(pi.hThread);=0A= + WaitForSingleObject(pi.hProcess, INFINITE);=0A= +=0A= + if (!GetExitCodeProcess(pi.hProcess, &x))=0A= + {=0A= + write_stderr(_("%s: could not get exit code from subprocess: error = code %lu\n"), progname, GetLastError());=0A= + exit(1);=0A= + }=0A= + exit(x);=0A= + }=0A= + }=0A= +#endif =0A= =0A= adjust_data_dir();=0A= =0A= ------=_NextPart_000_003B_01CD4A1C.0CE6E290--