Skip to content

Commit 11cc7bb

Browse files
committed
Avoid possibly-unsafe use of Windows' FormatMessage() function.
Whenever this function is used with the FORMAT_MESSAGE_FROM_SYSTEM flag, it's good practice to include FORMAT_MESSAGE_IGNORE_INSERTS as well. Otherwise, if the message contains any %n insertion markers, the function will try to fetch argument strings to substitute --- which we are not passing, possibly leading to a crash. This is exactly analogous to the rule about not giving printf() a format string you're not in control of. Noted and patched by Christian Ullrich. Back-patch to all supported branches.
1 parent a3c6439 commit 11cc7bb

File tree

4 files changed

+15
-5
lines changed

4 files changed

+15
-5
lines changed

src/backend/libpq/auth.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1225,7 +1225,9 @@ pg_SSPI_error(int severity, const char *errmsg, SECURITY_STATUS r)
12251225
{
12261226
char sysmsg[256];
12271227

1228-
if (FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, r, 0,
1228+
if (FormatMessage(FORMAT_MESSAGE_IGNORE_INSERTS |
1229+
FORMAT_MESSAGE_FROM_SYSTEM,
1230+
NULL, r, 0,
12291231
sysmsg, sizeof(sysmsg), NULL) == 0)
12301232
ereport(severity,
12311233
(errmsg_internal("%s", errmsg),

src/backend/port/win32/socket.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -657,7 +657,9 @@ pgwin32_socket_strerror(int err)
657657
}
658658

659659
ZeroMemory(&wserrbuf, sizeof(wserrbuf));
660-
if (FormatMessage(FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_FROM_HMODULE,
660+
if (FormatMessage(FORMAT_MESSAGE_IGNORE_INSERTS |
661+
FORMAT_MESSAGE_FROM_SYSTEM |
662+
FORMAT_MESSAGE_FROM_HMODULE,
661663
handleDLL,
662664
err,
663665
MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT),

src/interfaces/libpq/fe-auth.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,9 @@ pg_SSPI_error(PGconn *conn, const char *mprefix, SECURITY_STATUS r)
480480
{
481481
char sysmsg[256];
482482

483-
if (FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, r, 0,
483+
if (FormatMessage(FORMAT_MESSAGE_IGNORE_INSERTS |
484+
FORMAT_MESSAGE_FROM_SYSTEM,
485+
NULL, r, 0,
484486
sysmsg, sizeof(sysmsg), NULL) == 0)
485487
printfPQExpBuffer(&conn->errorMessage, "%s: SSPI error %x",
486488
mprefix, (unsigned int) r);

src/port/dirmod.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,9 @@ pgsymlink(const char *oldpath, const char *newpath)
207207
LPSTR msg;
208208

209209
errno = 0;
210-
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
210+
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
211+
FORMAT_MESSAGE_IGNORE_INSERTS |
212+
FORMAT_MESSAGE_FROM_SYSTEM,
211213
NULL, GetLastError(),
212214
MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT),
213215
(LPSTR) &msg, 0, NULL);
@@ -282,7 +284,9 @@ pgreadlink(const char *path, char *buf, size_t size)
282284
LPSTR msg;
283285

284286
errno = 0;
285-
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
287+
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
288+
FORMAT_MESSAGE_IGNORE_INSERTS |
289+
FORMAT_MESSAGE_FROM_SYSTEM,
286290
NULL, GetLastError(),
287291
MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT),
288292
(LPSTR) &msg, 0, NULL);

0 commit comments

Comments
 (0)