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

Commit23b5a85

Browse files
committed
Prevent privilege escalation in explicit calls to PL validators.
The primary role of PL validators is to be called implicitly duringCREATE FUNCTION, but they are also normal functions that a user can callexplicitly. Add a permissions check to each validator to ensure that auser cannot use explicit validator calls to achieve things he could nototherwise achieve. Back-patch to 8.4 (all supported versions).Non-core procedural language extensions ought to make the same two-linechange to their own validators.Andres Freund, reviewed by Tom Lane and Noah Misch.Security:CVE-2014-0061
1 parent5d320a1 commit23b5a85

File tree

8 files changed

+109
-2
lines changed

8 files changed

+109
-2
lines changed

‎doc/src/sgml/plhandler.sgml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,10 @@ CREATE LANGUAGE plsample
179179
or updated a function written in the procedural language.
180180
The passed-in OID is the OID of the function's <classname>pg_proc</>
181181
row. The validator must fetch this row in the usual way, and do
182-
whatever checking is appropriate. Typical checks include verifying
182+
whatever checking is appropriate.
183+
First, call <function>CheckFunctionValidatorAccess()</> to diagnose
184+
explicit calls to the validator that the user could not achieve through
185+
<command>CREATE FUNCTION</>. Typical checks then include verifying
183186
that the function's argument and result types are supported by the
184187
language, and that the function's body is syntactically correct
185188
in the language. If the validator finds the function to be okay,

‎src/backend/catalog/pg_proc.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -694,6 +694,9 @@ fmgr_internal_validator(PG_FUNCTION_ARGS)
694694
Datumtmp;
695695
char*prosrc;
696696

697+
if (!CheckFunctionValidatorAccess(fcinfo->flinfo->fn_oid,funcoid))
698+
PG_RETURN_VOID();
699+
697700
/*
698701
* We do not honor check_function_bodies since it's unlikely the function
699702
* name will be found later if it isn't there now.
@@ -739,6 +742,9 @@ fmgr_c_validator(PG_FUNCTION_ARGS)
739742
char*prosrc;
740743
char*probin;
741744

745+
if (!CheckFunctionValidatorAccess(fcinfo->flinfo->fn_oid,funcoid))
746+
PG_RETURN_VOID();
747+
742748
/*
743749
* It'd be most consistent to skip the check if !check_function_bodies,
744750
* but the purpose of that switch is to be helpful for pg_dump loading,
@@ -790,6 +796,9 @@ fmgr_sql_validator(PG_FUNCTION_ARGS)
790796
boolhaspolyarg;
791797
inti;
792798

799+
if (!CheckFunctionValidatorAccess(fcinfo->flinfo->fn_oid,funcoid))
800+
PG_RETURN_VOID();
801+
793802
tuple=SearchSysCache1(PROCOID,ObjectIdGetDatum(funcoid));
794803
if (!HeapTupleIsValid(tuple))
795804
elog(ERROR,"cache lookup failed for function %u",funcoid);

‎src/backend/commands/functioncmds.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -963,7 +963,6 @@ CreateFunction(CreateFunctionStmt *stmt, const char *queryString)
963963
prorows);
964964
}
965965

966-
967966
/*
968967
* RemoveFunction
969968
*Deletes a function.

‎src/backend/utils/fmgr/fmgr.c

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include"miscadmin.h"
2525
#include"nodes/nodeFuncs.h"
2626
#include"pgstat.h"
27+
#include"utils/acl.h"
2728
#include"utils/builtins.h"
2829
#include"utils/fmgrtab.h"
2930
#include"utils/guc.h"
@@ -2445,3 +2446,86 @@ get_call_expr_arg_stable(Node *expr, int argnum)
24452446

24462447
return false;
24472448
}
2449+
2450+
/*-------------------------------------------------------------------------
2451+
*Support routines for procedural language implementations
2452+
*-------------------------------------------------------------------------
2453+
*/
2454+
2455+
/*
2456+
* Verify that a validator is actually associated with the language of a
2457+
* particular function and that the user has access to both the language and
2458+
* the function. All validators should call this before doing anything
2459+
* substantial. Doing so ensures a user cannot achieve anything with explicit
2460+
* calls to validators that he could not achieve with CREATE FUNCTION or by
2461+
* simply calling an existing function.
2462+
*
2463+
* When this function returns false, callers should skip all validation work
2464+
* and call PG_RETURN_VOID(). This never happens at present; it is reserved
2465+
* for future expansion.
2466+
*
2467+
* In particular, checking that the validator corresponds to the function's
2468+
* language allows untrusted language validators to assume they process only
2469+
* superuser-chosen source code. (Untrusted language call handlers, by
2470+
* definition, do assume that.) A user lacking the USAGE language privilege
2471+
* would be unable to reach the validator through CREATE FUNCTION, so we check
2472+
* that to block explicit calls as well. Checking the EXECUTE privilege on
2473+
* the function is often superfluous, because most users can clone the
2474+
* function to get an executable copy. It is meaningful against users with no
2475+
* database TEMP right and no permanent schema CREATE right, thereby unable to
2476+
* create any function. Also, if the function tracks persistent state by
2477+
* function OID or name, validating the original function might permit more
2478+
* mischief than creating and validating a clone thereof.
2479+
*/
2480+
bool
2481+
CheckFunctionValidatorAccess(OidvalidatorOid,OidfunctionOid)
2482+
{
2483+
HeapTupleprocTup;
2484+
HeapTuplelangTup;
2485+
Form_pg_procprocStruct;
2486+
Form_pg_languagelangStruct;
2487+
AclResultaclresult;
2488+
2489+
/* Get the function's pg_proc entry */
2490+
procTup=SearchSysCache1(PROCOID,ObjectIdGetDatum(functionOid));
2491+
if (!HeapTupleIsValid(procTup))
2492+
elog(ERROR,"cache lookup failed for function %u",functionOid);
2493+
procStruct= (Form_pg_proc)GETSTRUCT(procTup);
2494+
2495+
/*
2496+
* Fetch pg_language entry to know if this is the correct validation
2497+
* function for that pg_proc entry.
2498+
*/
2499+
langTup=SearchSysCache1(LANGOID,ObjectIdGetDatum(procStruct->prolang));
2500+
if (!HeapTupleIsValid(langTup))
2501+
elog(ERROR,"cache lookup failed for language %u",procStruct->prolang);
2502+
langStruct= (Form_pg_language)GETSTRUCT(langTup);
2503+
2504+
if (langStruct->lanvalidator!=validatorOid)
2505+
ereport(ERROR,
2506+
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
2507+
errmsg("language validation function %u called for language %u instead of %u",
2508+
validatorOid,procStruct->prolang,
2509+
langStruct->lanvalidator)));
2510+
2511+
/* first validate that we have permissions to use the language */
2512+
aclresult=pg_language_aclcheck(procStruct->prolang,GetUserId(),
2513+
ACL_USAGE);
2514+
if (aclresult!=ACLCHECK_OK)
2515+
aclcheck_error(aclresult,ACL_KIND_LANGUAGE,
2516+
NameStr(langStruct->lanname));
2517+
2518+
/*
2519+
* Check whether we are allowed to execute the function itself. If we can
2520+
* execute it, there should be no possible side-effect of
2521+
* compiling/validation that execution can't have.
2522+
*/
2523+
aclresult=pg_proc_aclcheck(functionOid,GetUserId(),ACL_EXECUTE);
2524+
if (aclresult!=ACLCHECK_OK)
2525+
aclcheck_error(aclresult,ACL_KIND_PROC,NameStr(procStruct->proname));
2526+
2527+
ReleaseSysCache(procTup);
2528+
ReleaseSysCache(langTup);
2529+
2530+
return true;
2531+
}

‎src/include/fmgr.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -624,6 +624,7 @@ extern Oidget_fn_expr_argtype(FmgrInfo *flinfo, int argnum);
624624
externOidget_call_expr_argtype(fmNodePtrexpr,intargnum);
625625
externboolget_fn_expr_arg_stable(FmgrInfo*flinfo,intargnum);
626626
externboolget_call_expr_arg_stable(fmNodePtrexpr,intargnum);
627+
externboolCheckFunctionValidatorAccess(OidvalidatorOid,OidfunctionOid);
627628

628629
/*
629630
* Routines in dfmgr.c

‎src/pl/plperl/plperl.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1846,6 +1846,9 @@ plperl_validator(PG_FUNCTION_ARGS)
18461846
boolistrigger= false;
18471847
inti;
18481848

1849+
if (!CheckFunctionValidatorAccess(fcinfo->flinfo->fn_oid,funcoid))
1850+
PG_RETURN_VOID();
1851+
18491852
/* Get the new function's pg_proc entry */
18501853
tuple=SearchSysCache1(PROCOID,ObjectIdGetDatum(funcoid));
18511854
if (!HeapTupleIsValid(tuple))
@@ -1925,6 +1928,7 @@ PG_FUNCTION_INFO_V1(plperlu_validator);
19251928
Datum
19261929
plperlu_validator(PG_FUNCTION_ARGS)
19271930
{
1931+
/* call plperl validator with our fcinfo so it gets our oid */
19281932
returnplperl_validator(fcinfo);
19291933
}
19301934

‎src/pl/plpgsql/src/pl_handler.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,9 @@ plpgsql_validator(PG_FUNCTION_ARGS)
227227
boolistrigger= false;
228228
inti;
229229

230+
if (!CheckFunctionValidatorAccess(fcinfo->flinfo->fn_oid,funcoid))
231+
PG_RETURN_VOID();
232+
230233
/* Get the new function's pg_proc entry */
231234
tuple=SearchSysCache1(PROCOID,ObjectIdGetDatum(funcoid));
232235
if (!HeapTupleIsValid(tuple))

‎src/pl/plpython/plpython.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,9 @@ plpython_validator(PG_FUNCTION_ARGS)
519519
Form_pg_procprocStruct;
520520
boolis_trigger;
521521

522+
if (!CheckFunctionValidatorAccess(fcinfo->flinfo->fn_oid,funcoid))
523+
PG_RETURN_VOID();
524+
522525
if (!check_function_bodies)
523526
{
524527
PG_RETURN_VOID();
@@ -5012,6 +5015,7 @@ PG_FUNCTION_INFO_V1(plpython2_validator);
50125015
Datum
50135016
plpython2_validator(PG_FUNCTION_ARGS)
50145017
{
5018+
/* call plpython validator with our fcinfo so it gets our oid */
50155019
returnplpython_validator(fcinfo);
50165020
}
50175021

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp