Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 56aa2dc

Browse files
committed
Specify the encoding of input to fmtId()
This commit adds fmtIdEnc() and fmtQualifiedIdEnc(), which allow to specify the encoding as an explicit argument. Additionally setFmtEncoding() is provided, which defines the encoding when no explicit encoding is provided, to avoid breaking all code using fmtId(). All users of fmtId()/fmtQualifiedId() are either converted to the explicit version or a call to setFmtEncoding() has been added. This commit does not yet utilize the now well-defined encoding, that will happen in a subsequent commit. Reviewed-by: Noah Misch <[email protected]> Reviewed-by: Tom Lane <[email protected]> Backpatch-through: 13 Security: CVE-2025-1094
1 parent 6e05b19 commit 56aa2dc

File tree

13 files changed

+112
-22
lines changed

13 files changed

+112
-22
lines changed

src/bin/pg_dump/pg_backup_archiver.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2713,6 +2713,7 @@ processEncodingEntry(ArchiveHandle *AH, TocEntry *te)
27132713
pg_fatal("unrecognized encoding \"%s\"",
27142714
ptr1);
27152715
AH->public.encoding = encoding;
2716+
setFmtEncoding(encoding);
27162717
}
27172718
else
27182719
pg_fatal("invalid ENCODING item: %s",

src/bin/pg_dump/pg_dump.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1172,6 +1172,7 @@ setup_connection(Archive *AH, const char *dumpencoding,
11721172
* we know how to escape strings.
11731173
*/
11741174
AH->encoding = PQclientEncoding(conn);
1175+
setFmtEncoding(AH->encoding);
11751176

11761177
std_strings = PQparameterStatus(conn, "standard_conforming_strings");
11771178
AH->std_strings = (std_strings && strcmp(std_strings, "on") == 0);

src/bin/pg_dump/pg_dumpall.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,7 @@ main(int argc, char *argv[])
519519
* we know how to escape strings.
520520
*/
521521
encoding = PQclientEncoding(conn);
522+
setFmtEncoding(encoding);
522523
std_strings = PQparameterStatus(conn, "standard_conforming_strings");
523524
if (!std_strings)
524525
std_strings = "off";

src/bin/psql/command.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1318,6 +1318,7 @@ exec_command_encoding(PsqlScanState scan_state, bool active_branch)
13181318
/* save encoding info into psql internal data */
13191319
pset.encoding = PQclientEncoding(pset.db);
13201320
pset.popt.topt.encoding = pset.encoding;
1321+
setFmtEncoding(pset.encoding);
13211322
SetVariable(pset.vars, "ENCODING",
13221323
pg_encoding_to_char(pset.encoding));
13231324
}
@@ -3867,6 +3868,8 @@ SyncVariables(void)
38673868
pset.popt.topt.encoding = pset.encoding;
38683869
pset.sversion = PQserverVersion(pset.db);
38693870

3871+
setFmtEncoding(pset.encoding);
3872+
38703873
SetVariable(pset.vars, "DBNAME", PQdb(pset.db));
38713874
SetVariable(pset.vars, "USER", PQuser(pset.db));
38723875
SetVariable(pset.vars, "HOST", PQhost(pset.db));

src/bin/scripts/common.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,9 @@ appendQualifiedRelation(PQExpBuffer buf, const char *spec,
112112
exit(1);
113113
}
114114
appendPQExpBufferStr(buf,
115-
fmtQualifiedId(PQgetvalue(res, 0, 1),
116-
PQgetvalue(res, 0, 0)));
115+
fmtQualifiedIdEnc(PQgetvalue(res, 0, 1),
116+
PQgetvalue(res, 0, 0),
117+
PQclientEncoding(conn)));
117118
appendPQExpBufferStr(buf, columns);
118119
PQclear(res);
119120
termPQExpBuffer(&sql);

src/bin/scripts/createdb.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,8 @@ main(int argc, char *argv[])
193193

194194
conn = connectMaintenanceDatabase(&cparams, progname, echo);
195195

196+
setFmtEncoding(PQclientEncoding(conn));
197+
196198
initPQExpBuffer(&sql);
197199

198200
appendPQExpBuffer(&sql, "CREATE DATABASE %s",

src/bin/scripts/createuser.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,8 @@ main(int argc, char *argv[])
292292

293293
conn = connectMaintenanceDatabase(&cparams, progname, echo);
294294

295+
setFmtEncoding(PQclientEncoding(conn));
296+
295297
initPQExpBuffer(&sql);
296298

297299
printfPQExpBuffer(&sql, "CREATE ROLE %s", fmtId(newuser));

src/bin/scripts/dropdb.c

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -129,13 +129,6 @@ main(int argc, char *argv[])
129129
exit(0);
130130
}
131131

132-
initPQExpBuffer(&sql);
133-
134-
appendPQExpBuffer(&sql, "DROP DATABASE %s%s%s;",
135-
(if_exists ? "IF EXISTS " : ""),
136-
fmtId(dbname),
137-
force ? " WITH (FORCE)" : "");
138-
139132
/* Avoid trying to drop postgres db while we are connected to it. */
140133
if (maintenance_db == NULL && strcmp(dbname, "postgres") == 0)
141134
maintenance_db = "template1";
@@ -149,6 +142,12 @@ main(int argc, char *argv[])
149142

150143
conn = connectMaintenanceDatabase(&cparams, progname, echo);
151144

145+
initPQExpBuffer(&sql);
146+
appendPQExpBuffer(&sql, "DROP DATABASE %s%s%s;",
147+
(if_exists ? "IF EXISTS " : ""),
148+
fmtIdEnc(dbname, PQclientEncoding(conn)),
149+
force ? " WITH (FORCE)" : "");
150+
152151
if (echo)
153152
printf("%s\n", sql.data);
154153
result = PQexec(conn, sql.data);

src/bin/scripts/dropuser.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,8 @@ main(int argc, char *argv[])
143143

144144
initPQExpBuffer(&sql);
145145
appendPQExpBuffer(&sql, "DROP ROLE %s%s;",
146-
(if_exists ? "IF EXISTS " : ""), fmtId(dropuser));
146+
(if_exists ? "IF EXISTS " : ""),
147+
fmtIdEnc(dropuser, PQclientEncoding(conn)));
147148

148149
if (echo)
149150
printf("%s\n", sql.data);

src/bin/scripts/reindexdb.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,8 @@ run_reindex_command(PGconn *conn, ReindexType type, const char *name,
489489

490490
if (tablespace)
491491
{
492-
appendPQExpBuffer(&sql, "%sTABLESPACE %s", sep, fmtId(tablespace));
492+
appendPQExpBuffer(&sql, "%sTABLESPACE %s", sep,
493+
fmtIdEnc(tablespace, PQclientEncoding(conn)));
493494
sep = comma;
494495
}
495496

@@ -529,7 +530,8 @@ run_reindex_command(PGconn *conn, ReindexType type, const char *name,
529530
{
530531
case REINDEX_DATABASE:
531532
case REINDEX_SYSTEM:
532-
appendPQExpBufferStr(&sql, fmtId(name));
533+
appendPQExpBufferStr(&sql,
534+
fmtIdEnc(name, PQclientEncoding(conn)));
533535
break;
534536
case REINDEX_INDEX:
535537
case REINDEX_TABLE:
@@ -699,8 +701,9 @@ get_parallel_object_list(PGconn *conn, ReindexType type,
699701
for (i = 0; i < ntups; i++)
700702
{
701703
appendPQExpBufferStr(&buf,
702-
fmtQualifiedId(PQgetvalue(res, i, 1),
703-
PQgetvalue(res, i, 0)));
704+
fmtQualifiedIdEnc(PQgetvalue(res, i, 1),
705+
PQgetvalue(res, i, 0),
706+
PQclientEncoding(conn)));
704707

705708
simple_string_list_append(tables, buf.data);
706709
resetPQExpBuffer(&buf);

src/bin/scripts/vacuumdb.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -795,8 +795,9 @@ vacuum_one_database(ConnParams *cparams,
795795
for (i = 0; i < ntups; i++)
796796
{
797797
appendPQExpBufferStr(&buf,
798-
fmtQualifiedId(PQgetvalue(res, i, 1),
799-
PQgetvalue(res, i, 0)));
798+
fmtQualifiedIdEnc(PQgetvalue(res, i, 1),
799+
PQgetvalue(res, i, 0),
800+
PQclientEncoding(conn)));
800801

801802
if (objects_listed && !PQgetisnull(res, i, 2))
802803
appendPQExpBufferStr(&buf, PQgetvalue(res, i, 2));

src/fe_utils/string_utils.c

Lines changed: 78 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,16 @@
1919

2020
#include "common/keywords.h"
2121
#include "fe_utils/string_utils.h"
22+
#include "mb/pg_wchar.h"
2223

2324
static PQExpBuffer defaultGetLocalPQExpBuffer(void);
2425

2526
/* Globals exported by this file */
2627
int quote_all_identifiers = 0;
2728
PQExpBuffer (*getLocalPQExpBuffer) (void) = defaultGetLocalPQExpBuffer;
2829

30+
static int fmtIdEncoding = -1;
31+
2932

3033
/*
3134
* Returns a temporary PQExpBuffer, valid until the next call to the function.
@@ -54,14 +57,48 @@ defaultGetLocalPQExpBuffer(void)
5457
return id_return;
5558
}
5659

60+
/*
61+
* Set the encoding that fmtId() and fmtQualifiedId() use.
62+
*
63+
* This is not safe against multiple connections having different encodings,
64+
* but there is no real other way to address the need to know the encoding for
65+
* fmtId()/fmtQualifiedId() input for safe escaping. Eventually we should get
66+
* rid of fmtId().
67+
*/
68+
void
69+
setFmtEncoding(int encoding)
70+
{
71+
fmtIdEncoding = encoding;
72+
}
73+
74+
/*
75+
* Return the currently configured encoding for fmtId() and fmtQualifiedId().
76+
*/
77+
static int
78+
getFmtEncoding(void)
79+
{
80+
if (fmtIdEncoding != -1)
81+
return fmtIdEncoding;
82+
83+
/*
84+
* In assertion builds it seems best to fail hard if the encoding was not
85+
* set, to make it easier to find places with missing calls. But in
86+
* production builds that seems like a bad idea, thus we instead just
87+
* default to UTF-8.
88+
*/
89+
Assert(fmtIdEncoding != -1);
90+
91+
return PG_UTF8;
92+
}
93+
5794
/*
5895
* Quotes input string if it's not a legitimate SQL identifier as-is.
5996
*
60-
* Note that the returned string must be used before calling fmtId again,
97+
* Note that the returned string must be used before calling fmtIdEnc again,
6198
* since we re-use the same return buffer each time.
6299
*/
63100
const char *
64-
fmtId(const char *rawid)
101+
fmtIdEnc(const char *rawid, int encoding)
65102
{
66103
PQExpBuffer id_return = getLocalPQExpBuffer();
67104

@@ -134,25 +171,42 @@ fmtId(const char *rawid)
134171
}
135172

136173
/*
137-
* fmtQualifiedId - construct a schema-qualified name, with quoting as needed.
174+
* Quotes input string if it's not a legitimate SQL identifier as-is.
175+
*
176+
* Note that the returned string must be used before calling fmtId again,
177+
* since we re-use the same return buffer each time.
178+
*
179+
* NB: This assumes setFmtEncoding() previously has been called to configure
180+
* the encoding of rawid. It is preferable to use fmtIdEnc() with an
181+
* explicit encoding.
182+
*/
183+
const char *
184+
fmtId(const char *rawid)
185+
{
186+
return fmtIdEnc(rawid, getFmtEncoding());
187+
}
188+
189+
/*
190+
* fmtQualifiedIdEnc - construct a schema-qualified name, with quoting as
191+
* needed.
138192
*
139193
* Like fmtId, use the result before calling again.
140194
*
141195
* Since we call fmtId and it also uses getLocalPQExpBuffer() we cannot
142196
* use that buffer until we're finished with calling fmtId().
143197
*/
144198
const char *
145-
fmtQualifiedId(const char *schema, const char *id)
199+
fmtQualifiedIdEnc(const char *schema, const char *id, int encoding)
146200
{
147201
PQExpBuffer id_return;
148202
PQExpBuffer lcl_pqexp = createPQExpBuffer();
149203

150204
/* Some callers might fail to provide a schema name */
151205
if (schema && *schema)
152206
{
153-
appendPQExpBuffer(lcl_pqexp, "%s.", fmtId(schema));
207+
appendPQExpBuffer(lcl_pqexp, "%s.", fmtIdEnc(schema, encoding));
154208
}
155-
appendPQExpBufferStr(lcl_pqexp, fmtId(id));
209+
appendPQExpBufferStr(lcl_pqexp, fmtIdEnc(id, encoding));
156210

157211
id_return = getLocalPQExpBuffer();
158212

@@ -162,6 +216,24 @@ fmtQualifiedId(const char *schema, const char *id)
162216
return id_return->data;
163217
}
164218

219+
/*
220+
* fmtQualifiedId - construct a schema-qualified name, with quoting as needed.
221+
*
222+
* Like fmtId, use the result before calling again.
223+
*
224+
* Since we call fmtId and it also uses getLocalPQExpBuffer() we cannot
225+
* use that buffer until we're finished with calling fmtId().
226+
*
227+
* NB: This assumes setFmtEncoding() previously has been called to configure
228+
* the encoding of schema/id. It is preferable to use fmtQualifiedIdEnc()
229+
* with an explicit encoding.
230+
*/
231+
const char *
232+
fmtQualifiedId(const char *schema, const char *id)
233+
{
234+
return fmtQualifiedIdEnc(schema, id, getFmtEncoding());
235+
}
236+
165237

166238
/*
167239
* Format a Postgres version number (in the PG_VERSION_NUM integer format

src/include/fe_utils/string_utils.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@ extern PQExpBuffer (*getLocalPQExpBuffer) (void);
2525

2626
/* Functions */
2727
extern const char *fmtId(const char *rawid);
28+
extern const char *fmtIdEnc(const char *rawid, int encoding);
2829
extern const char *fmtQualifiedId(const char *schema, const char *id);
30+
extern const char *fmtQualifiedIdEnc(const char *schema, const char *id, int encoding);
31+
extern void setFmtEncoding(int encoding);
2932

3033
extern char *formatPGVersionNumber(int version_number, bool include_minor,
3134
char *buf, size_t buflen);

0 commit comments

Comments
 (0)