Re: Unable to connect to PostgreSQL DB as root user when private key is owned by root with permission 640

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: "Suralkar, Yogendra (Yogendra)" <suralkary(at)avaya(dot)com>
Cc: "pgsql-bugs(at)lists(dot)postgresql(dot)org" <pgsql-bugs(at)lists(dot)postgresql(dot)org>, David Steele <david(at)pgmasters(dot)net>, "Porob, Dattaram (Datta)" <porobd(at)avaya(dot)com>, "Oswal, Prashant (Prashant) **CTR**" <poswal(at)avaya(dot)com>, "Patil, Parag (Parag)" <paragp(at)avaya(dot)com>, "Devaraj, Sankar (Sankar)" <devarajs(at)avaya(dot)com>, "Singh, Payal (Payal) **CTR**" <payals(at)avaya(dot)com>
Subject: Re: Unable to connect to PostgreSQL DB as root user when private key is owned by root with permission 640
Date: 2022-05-20 14:49:46
Message-ID: 335500.1653058186@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

"Suralkar, Yogendra (Yogendra)" <suralkary(at)avaya(dot)com> writes:
> Recently we updated to PostgreSQL 13.7 (Please see list of rpms used below).
> After update we have noticed an issue when connecting to Database as 'root' user when private key file is owned by root and has permission 640.

TBH, my immediate reaction is "what are you doing running database
accesses as root?". But given that you are, I see the problem: the test
is coded like

if ((buf.st_uid == geteuid() && buf.st_mode & (S_IRWXG | S_IRWXO)) ||
(buf.st_uid == 0 && buf.st_mode & (S_IWGRP | S_IXGRP | S_IRWXO)))

which was copied verbatim from the equivalent test in the backend.
However, in the backend it's safe to assume that geteuid() != 0.
libpq apparently shouldn't assume that, meaning that the two arms
of the if aren't disjoint cases anymore, and it matters which one
we check first.

The repeat call of geteuid() is a waste of cycles anyway, so maybe better
like

if (buf.st_uid != 0 ?
buf.st_mode & (S_IRWXG | S_IRWXO) :
buf.st_mode & (S_IWGRP | S_IXGRP | S_IRWXO))

This feels kind of wrong, in that root's privacy check is now strictly
weaker than anyone else's, but root ought to know what she's doing anyway.

regards, tom lane

In response to

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message Daniel Gustafsson 2022-05-20 15:07:29 Re: BUG #17486: [pg_restore] Restoring a view fails if this view contains an attribute without alias name.
Previous Message Tom Lane 2022-05-20 14:34:20 Re: BUG #17486: [pg_restore] Restoring a view fails if this view contains an attribute without alias name.