Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit fc4a04a

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

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
@@ -178,7 +178,10 @@ CREATE LANGUAGE plsample
178178
or updated a function written in the procedural language.
179179
The passed-in OID is the OID of the function's <classname>pg_proc</>
180180
row. The validator must fetch this row in the usual way, and do
181-
whatever checking is appropriate. Typical checks include verifying
181+
whatever checking is appropriate.
182+
First, call <function>CheckFunctionValidatorAccess()</> to diagnose
183+
explicit calls to the validator that the user could not achieve through
184+
<command>CREATE FUNCTION</>. Typical checks then include verifying
182185
that the function's argument and result types are supported by the
183186
language, and that the function's body is syntactically correct
184187
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
@@ -723,6 +723,9 @@ fmgr_internal_validator(PG_FUNCTION_ARGS)
723723
Datum tmp;
724724
char *prosrc;
725725

726+
if (!CheckFunctionValidatorAccess(fcinfo->flinfo->fn_oid, funcoid))
727+
PG_RETURN_VOID();
728+
726729
/*
727730
* We do not honor check_function_bodies since it's unlikely the function
728731
* name will be found later if it isn't there now.
@@ -768,6 +771,9 @@ fmgr_c_validator(PG_FUNCTION_ARGS)
768771
char *prosrc;
769772
char *probin;
770773

774+
if (!CheckFunctionValidatorAccess(fcinfo->flinfo->fn_oid, funcoid))
775+
PG_RETURN_VOID();
776+
771777
/*
772778
* It'd be most consistent to skip the check if !check_function_bodies,
773779
* but the purpose of that switch is to be helpful for pg_dump loading,
@@ -819,6 +825,9 @@ fmgr_sql_validator(PG_FUNCTION_ARGS)
819825
bool haspolyarg;
820826
int i;
821827

828+
if (!CheckFunctionValidatorAccess(fcinfo->flinfo->fn_oid, funcoid))
829+
PG_RETURN_VOID();
830+
822831
tuple = SearchSysCache1(PROCOID, ObjectIdGetDatum(funcoid));
823832
if (!HeapTupleIsValid(tuple))
824833
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
@@ -986,7 +986,6 @@ CreateFunction(CreateFunctionStmt *stmt, const char *queryString)
986986
prorows);
987987
}
988988

989-
990989
/*
991990
* Guts of function deletion.
992991
*

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"
@@ -2471,3 +2472,86 @@ get_fn_expr_variadic(FmgrInfo *flinfo)
24712472
else
24722473
return false;
24732474
}
2475+
2476+
/*-------------------------------------------------------------------------
2477+
* Support routines for procedural language implementations
2478+
*-------------------------------------------------------------------------
2479+
*/
2480+
2481+
/*
2482+
* Verify that a validator is actually associated with the language of a
2483+
* particular function and that the user has access to both the language and
2484+
* the function. All validators should call this before doing anything
2485+
* substantial. Doing so ensures a user cannot achieve anything with explicit
2486+
* calls to validators that he could not achieve with CREATE FUNCTION or by
2487+
* simply calling an existing function.
2488+
*
2489+
* When this function returns false, callers should skip all validation work
2490+
* and call PG_RETURN_VOID(). This never happens at present; it is reserved
2491+
* for future expansion.
2492+
*
2493+
* In particular, checking that the validator corresponds to the function's
2494+
* language allows untrusted language validators to assume they process only
2495+
* superuser-chosen source code. (Untrusted language call handlers, by
2496+
* definition, do assume that.) A user lacking the USAGE language privilege
2497+
* would be unable to reach the validator through CREATE FUNCTION, so we check
2498+
* that to block explicit calls as well. Checking the EXECUTE privilege on
2499+
* the function is often superfluous, because most users can clone the
2500+
* function to get an executable copy. It is meaningful against users with no
2501+
* database TEMP right and no permanent schema CREATE right, thereby unable to
2502+
* create any function. Also, if the function tracks persistent state by
2503+
* function OID or name, validating the original function might permit more
2504+
* mischief than creating and validating a clone thereof.
2505+
*/
2506+
bool
2507+
CheckFunctionValidatorAccess(Oid validatorOid, Oid functionOid)
2508+
{
2509+
HeapTuple procTup;
2510+
HeapTuple langTup;
2511+
Form_pg_proc procStruct;
2512+
Form_pg_language langStruct;
2513+
AclResult aclresult;
2514+
2515+
/* Get the function's pg_proc entry */
2516+
procTup = SearchSysCache1(PROCOID, ObjectIdGetDatum(functionOid));
2517+
if (!HeapTupleIsValid(procTup))
2518+
elog(ERROR, "cache lookup failed for function %u", functionOid);
2519+
procStruct = (Form_pg_proc) GETSTRUCT(procTup);
2520+
2521+
/*
2522+
* Fetch pg_language entry to know if this is the correct validation
2523+
* function for that pg_proc entry.
2524+
*/
2525+
langTup = SearchSysCache1(LANGOID, ObjectIdGetDatum(procStruct->prolang));
2526+
if (!HeapTupleIsValid(langTup))
2527+
elog(ERROR, "cache lookup failed for language %u", procStruct->prolang);
2528+
langStruct = (Form_pg_language) GETSTRUCT(langTup);
2529+
2530+
if (langStruct->lanvalidator != validatorOid)
2531+
ereport(ERROR,
2532+
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
2533+
errmsg("language validation function %u called for language %u instead of %u",
2534+
validatorOid, procStruct->prolang,
2535+
langStruct->lanvalidator)));
2536+
2537+
/* first validate that we have permissions to use the language */
2538+
aclresult = pg_language_aclcheck(procStruct->prolang, GetUserId(),
2539+
ACL_USAGE);
2540+
if (aclresult != ACLCHECK_OK)
2541+
aclcheck_error(aclresult, ACL_KIND_LANGUAGE,
2542+
NameStr(langStruct->lanname));
2543+
2544+
/*
2545+
* Check whether we are allowed to execute the function itself. If we can
2546+
* execute it, there should be no possible side-effect of
2547+
* compiling/validation that execution can't have.
2548+
*/
2549+
aclresult = pg_proc_aclcheck(functionOid, GetUserId(), ACL_EXECUTE);
2550+
if (aclresult != ACLCHECK_OK)
2551+
aclcheck_error(aclresult, ACL_KIND_PROC, NameStr(procStruct->proname));
2552+
2553+
ReleaseSysCache(procTup);
2554+
ReleaseSysCache(langTup);
2555+
2556+
return true;
2557+
}

src/include/fmgr.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -625,6 +625,7 @@ extern Oid get_call_expr_argtype(fmNodePtr expr, int argnum);
625625
extern bool get_fn_expr_arg_stable(FmgrInfo *flinfo, int argnum);
626626
extern bool get_call_expr_arg_stable(fmNodePtr expr, int argnum);
627627
extern bool get_fn_expr_variadic(FmgrInfo *flinfo);
628+
extern bool CheckFunctionValidatorAccess(Oid validatorOid, Oid functionOid);
628629

629630
/*
630631
* Routines in dfmgr.c

src/pl/plperl/plperl.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1856,6 +1856,9 @@ plperl_validator(PG_FUNCTION_ARGS)
18561856
bool istrigger = false;
18571857
int i;
18581858

1859+
if (!CheckFunctionValidatorAccess(fcinfo->flinfo->fn_oid, funcoid))
1860+
PG_RETURN_VOID();
1861+
18591862
/* Get the new function's pg_proc entry */
18601863
tuple = SearchSysCache1(PROCOID, ObjectIdGetDatum(funcoid));
18611864
if (!HeapTupleIsValid(tuple))
@@ -1935,6 +1938,7 @@ PG_FUNCTION_INFO_V1(plperlu_validator);
19351938
Datum
19361939
plperlu_validator(PG_FUNCTION_ARGS)
19371940
{
1941+
/* call plperl validator with our fcinfo so it gets our oid */
19381942
return plperl_validator(fcinfo);
19391943
}
19401944

src/pl/plpgsql/src/pl_handler.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,9 @@ plpgsql_validator(PG_FUNCTION_ARGS)
235235
bool is_event_trigger = false;
236236
int i;
237237

238+
if (!CheckFunctionValidatorAccess(fcinfo->flinfo->fn_oid, funcoid))
239+
PG_RETURN_VOID();
240+
238241
/* Get the new function's pg_proc entry */
239242
tuple = SearchSysCache1(PROCOID, ObjectIdGetDatum(funcoid));
240243
if (!HeapTupleIsValid(tuple))

src/pl/plpython/plpy_main.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,9 @@ plpython_validator(PG_FUNCTION_ARGS)
160160
Form_pg_proc procStruct;
161161
bool is_trigger;
162162

163+
if (!CheckFunctionValidatorAccess(fcinfo->flinfo->fn_oid, funcoid))
164+
PG_RETURN_VOID();
165+
163166
if (!check_function_bodies)
164167
{
165168
PG_RETURN_VOID();
@@ -185,6 +188,7 @@ plpython_validator(PG_FUNCTION_ARGS)
185188
Datum
186189
plpython2_validator(PG_FUNCTION_ARGS)
187190
{
191+
/* call plpython validator with our fcinfo so it gets our oid */
188192
return plpython_validator(fcinfo);
189193
}
190194
#endif /* PY_MAJOR_VERSION < 3 */

0 commit comments

Comments
 (0)