postgres.git / summary / log / commit / refs

commit    4e47b02834827fa700627290fae02f89a450368c
Author:   Michael Paquier <michael@paquier.xyz>
Date:     Mon Jun 07 23:53:12 2021 +0000
First released in: 14.0

    Reorder superuser check in pg_log_backend_memory_contexts()
    
    The use of this function is limited to superusers and the code includes
    a hardcoded check for that.  However, the code would look for the PGPROC
    entry to signal for the memory dump before checking if the user is a
    superuser or not, which does not make sense if we know that an error
    will be returned.  Note that the code would let one know if a process
    was a PostgreSQL process or not even for non-authorized users, which is
    not the case now, but this avoids taking ProcArrayLock that will most
    likely finish by being unnecessary.
    
    Thanks to Julien Rouhaud and Tom Lane for the discussion.
    
    Discussion: https://postgr.es/m/YLxw1uVGIAP5uMPl@paquier.xyz


src/backend/utils/adt/mcxtfuncs.c | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/backend/utils/adt/mcxtfuncs.c b/src/backend/utils/adt/mcxtfuncs.c index 2984768d199..0d52613bc32 100644 --- a/src/backend/utils/adt/mcxtfuncs.c +++ b/src/backend/utils/adt/mcxtfuncs.c @@ -175,7 +175,15 @@ Datum pg_log_backend_memory_contexts(PG_FUNCTION_ARGS) { int pid = PG_GETARG_INT32(0); - PGPROC *proc = BackendPidGetProc(pid); + PGPROC *proc; + + /* Only allow superusers to log memory contexts. */ + if (!superuser()) + ereport(ERROR, + (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE), + errmsg("must be a superuser to log memory contexts"))); + + proc = BackendPidGetProc(pid); /* * BackendPidGetProc returns NULL if the pid isn't valid; but by the time @@ -197,12 +205,6 @@ pg_log_backend_memory_contexts(PG_FUNCTION_ARGS) PG_RETURN_BOOL(false); } - /* Only allow superusers to log memory contexts. */ - if (!superuser()) - ereport(ERROR, - (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE), - errmsg("must be a superuser to log memory contexts"))); - if (SendProcSignal(pid, PROCSIG_LOG_MEMORY_CONTEXT, proc->backendId) < 0) { /* Again, just a warning to allow loops */ [parent: 3bb309be7533]