postgres.git / summary / log / commit / refs

commit    5eaab71a7393d4b5e19ae69b267146eb764664fe
Author:   Tom Lane <tgl@sss.pgh.pa.us>
Commit:   Noah Misch <noah@leadboat.com>
Date:     Mon Nov 09 14:20:35 2026 +0000
First released in: 14.25, 15.20, 16.16, 17.12, 18.7, 19.0

    Avoid use-after-free hazards in re-entrant use of pltcl.
    
    When pltcl invokes a SQL datatype input function, it was passing a
    string that is owned by the Tcl interpreter.  This is hazardous
    since the input function could re-entrantly call the same Tcl
    interpreter (for example, via a pltcl function in a domain check
    constraint), and that call could modify or free the string we're
    depending on.  Avoid the hazard by ensuring that we've pstrdup'd
    whatever Tcl returned.
    
    In all of these call sites, there's no great need to worry about
    pfree'ing the extra string immediately, since we're either inside a
    short-lived subtransaction or about to return from the pltcl function.
    So I didn't complicate the code by doing that.
    
    Reported-by: Anthropic OSS program
    Author: Tom Lane <tgl@sss.pgh.pa.us>
    Backpatch-through: 14
    Security: CVE-2026-116323


src/pl/tcl/pltcl.c | 73 +++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 53 insertions(+), 20 deletions(-) diff --git a/src/pl/tcl/pltcl.c b/src/pl/tcl/pltcl.c index e2c6d99a6de..ad551ad734e 100644 --- a/src/pl/tcl/pltcl.c +++ b/src/pl/tcl/pltcl.c @@ -107,6 +107,17 @@ utf_e2u(const char *src) #define UTF_E2U(x) \ (_pltcl_utf_dst = utf_e2u(_pltcl_utf_src = (x))) +/* This variant always makes a pstrdup'd copy */ +static inline char * +utf_u2e_copy(const char *src) +{ + char *dst = pg_any_to_server(src, strlen(src), PG_UTF8); + + if ((const char *) dst == src) + dst = pstrdup(dst); + return dst; +} + /********************************************************************** * Information associated with a Tcl interpreter. We have one interpreter @@ -1045,10 +1056,20 @@ pltcl_func_handler(PG_FUNCTION_ARGS, pltcl_call_state *call_state, retval = HeapTupleGetDatum(tup); } else + { + /* + * Notice that we forcibly copy the result of Tcl_GetStringResult, so + * that the result_in_func is applied to data that doesn't belong to + * the Tcl interpreter. This avoids use-after-free problems if the + * result_in_func calls another pltcl function. No need to worry + * about leaking the string, since this happens just once per pltcl + * function call. + */ retval = InputFunctionCall(&prodesc->result_in_func, - utf_u2e(Tcl_GetStringResult(interp)), + utf_u2e_copy(Tcl_GetStringResult(interp)), prodesc->result_typioparam, -1); + } return retval; } @@ -1384,7 +1405,7 @@ throw_tcl_error(Tcl_Interp *interp, const char *proname) char *econtext; int emsglen; - emsg = pstrdup(utf_u2e(Tcl_GetStringResult(interp))); + emsg = utf_u2e_copy(Tcl_GetStringResult(interp)); econtext = utf_u2e(Tcl_GetVar(interp, "errorInfo", TCL_GLOBAL_ONLY)); /* @@ -2319,8 +2340,9 @@ pltcl_returnnext(ClientData cdata, Tcl_Interp *interp, if (call_state->ret_tupdesc->natts != 1) elog(ERROR, "wrong result type supplied in return_next"); + /* as in pltcl_func_handler, copy the string out of Tcl */ retval = InputFunctionCall(&prodesc->result_in_func, - utf_u2e((char *) Tcl_GetString(objv[1])), + utf_u2e_copy(Tcl_GetString(objv[1])), prodesc->result_typioparam, -1); tuplestore_putvalues(call_state->tuple_store, call_state->ret_tupdesc, @@ -2778,6 +2800,7 @@ pltcl_SPI_execute_plan(ClientData cdata, Tcl_Interp *interp, int count = 0; Tcl_Size callObjc; Tcl_Obj **callObjv = NULL; + char **argstrings; Datum *argvalues; MemoryContext oldcontext = CurrentMemoryContext; ResourceOwner oldowner = CurrentResourceOwner; @@ -2918,6 +2941,23 @@ pltcl_SPI_execute_plan(ClientData cdata, Tcl_Interp *interp, PG_TRY(); { + /************************************************************ + * As in pltcl_func_handler, copy all the argument strings out + * of Tcl before calling any input function. An input + * function can re-enter the Tcl interpreter, which might free + * not only the strings but also the callObjv array itself, if + * the argument list object gets converted to some other type. + ************************************************************/ + argstrings = palloc_array(char *, callObjc); + + for (j = 0; j < callObjc; j++) + { + if (nulls && nulls[j] == 'n') + argstrings[j] = NULL; + else + argstrings[j] = utf_u2e_copy(Tcl_GetString(callObjv[j])); + } + /************************************************************ * Setup the value array for SPI_execute_plan() using * the type specific input functions @@ -2926,22 +2966,10 @@ pltcl_SPI_execute_plan(ClientData cdata, Tcl_Interp *interp, for (j = 0; j < callObjc; j++) { - if (nulls && nulls[j] == 'n') - { - argvalues[j] = InputFunctionCall(&qdesc->arginfuncs[j], - NULL, - qdesc->argtypioparams[j], - -1); - } - else - { - UTF_BEGIN; - argvalues[j] = InputFunctionCall(&qdesc->arginfuncs[j], - UTF_U2E(Tcl_GetString(callObjv[j])), - qdesc->argtypioparams[j], - -1); - UTF_END; - } + argvalues[j] = InputFunctionCall(&qdesc->arginfuncs[j], + argstrings[j], + qdesc->argtypioparams[j], + -1); } /************************************************************ @@ -3335,7 +3363,12 @@ pltcl_build_tuple_result(Tcl_Interp *interp, Tcl_Obj **kvObjv, int kvObjc, errmsg("cannot set generated column \"%s\"", fieldName))); - values[attn - 1] = utf_u2e(Tcl_GetString(kvObjv[i + 1])); + /* + * As in pltcl_func_handler, copy each string out of the Tcl + * interpreter before proceeding, to ensure it can't get freed or + * overwritten by re-entrant use of the Tcl interpreter. + */ + values[attn - 1] = utf_u2e_copy(Tcl_GetString(kvObjv[i + 1])); } tuple = BuildTupleFromCStrings(attinmeta, values); [parent: 82ffa9d00db9]