Avoid possibly-unsafe use of Windows' FormatMessage() function.
authorTom Lane <tgl@sss.pgh.pa.us>
Tue, 29 Mar 2016 15:54:58 +0000 (11:54 -0400)
committerTom Lane <tgl@sss.pgh.pa.us>
Tue, 29 Mar 2016 15:54:58 +0000 (11:54 -0400)
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.

src/backend/libpq/auth.c
src/backend/port/win32/socket.c
src/interfaces/libpq/fe-auth.c
src/port/dirmod.c

index e70f4729afff31d48d59537d506b88cb9aea31a9..8c6ff8e6efc7612f7afe31a135f01af068a64f6c 100644 (file)
@@ -1226,7 +1226,9 @@ pg_SSPI_error(int severity, const char *errmsg, SECURITY_STATUS r)
 {
    char        sysmsg[256];
 
-   if (FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, r, 0,
+   if (FormatMessage(FORMAT_MESSAGE_IGNORE_INSERTS |
+                     FORMAT_MESSAGE_FROM_SYSTEM,
+                     NULL, r, 0,
                      sysmsg, sizeof(sysmsg), NULL) == 0)
        ereport(severity,
                (errmsg_internal("%s", errmsg),
index 5ec9cc29cba19a89dfd4e437388e9889994ade15..658463000fbfa23b5360f96396bf85b701a50f08 100644 (file)
@@ -656,7 +656,9 @@ pgwin32_socket_strerror(int err)
    }
 
    ZeroMemory(&wserrbuf, sizeof(wserrbuf));
-   if (FormatMessage(FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_FROM_HMODULE,
+   if (FormatMessage(FORMAT_MESSAGE_IGNORE_INSERTS |
+                     FORMAT_MESSAGE_FROM_SYSTEM |
+                     FORMAT_MESSAGE_FROM_HMODULE,
                      handleDLL,
                      err,
                      MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT),
index e3612e54b7b32a7f493c429e7e3ed6e0f6814936..23333e66fcfb480f84a0a4e64b07e019c35f575e 100644 (file)
@@ -480,7 +480,9 @@ pg_SSPI_error(PGconn *conn, const char *mprefix, SECURITY_STATUS r)
 {
    char        sysmsg[256];
 
-   if (FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, r, 0,
+   if (FormatMessage(FORMAT_MESSAGE_IGNORE_INSERTS |
+                     FORMAT_MESSAGE_FROM_SYSTEM,
+                     NULL, r, 0,
                      sysmsg, sizeof(sysmsg), NULL) == 0)
        printfPQExpBuffer(&conn->errorMessage, "%s: SSPI error %x",
                          mprefix, (unsigned int) r);
index f61d68b301a5f6d15435c78c46125c484987343a..280d6847a57ac7de91b74d1a43b5063715a0b096 100644 (file)
@@ -273,7 +273,9 @@ pgsymlink(const char *oldpath, const char *newpath)
        LPSTR       msg;
 
        errno = 0;
-       FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
+       FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
+                     FORMAT_MESSAGE_IGNORE_INSERTS |
+                     FORMAT_MESSAGE_FROM_SYSTEM,
                      NULL, GetLastError(),
                      MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT),
                      (LPSTR) &msg, 0, NULL);
@@ -348,7 +350,9 @@ pgreadlink(const char *path, char *buf, size_t size)
        LPSTR       msg;
 
        errno = 0;
-       FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
+       FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
+                     FORMAT_MESSAGE_IGNORE_INSERTS |
+                     FORMAT_MESSAGE_FROM_SYSTEM,
                      NULL, GetLastError(),
                      MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT),
                      (LPSTR) &msg, 0, NULL);