Avoid possibly-unsafe use of Windows' FormatMessage() function.
authorTom Lane <[email protected]>
Tue, 29 Mar 2016 15:54:57 +0000 (11:54 -0400)
committerTom Lane <[email protected]>
Tue, 29 Mar 2016 15:55:19 +0000 (11:55 -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 7f1ae8c137af6351637f21e06801924e8533b7d4..899da712f7ad7ab609a4298460f0e0d8a8a50467 100644 (file)
@@ -1011,7 +1011,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 d1ea578b1f691af0ba40e4c1f122806cad68bb04..747304cee4295a67640c72cf1296a76b046a5e7e 100644 (file)
@@ -658,7 +658,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 cd863a5db1acab62c89f5505bc97894d3a743bb4..d23726215b7fcce5ab5b0412dbf81d5b8f5148b7 100644 (file)
@@ -234,7 +234,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\n",
                                                  mprefix, (unsigned int) r);
index 8053d16ebfd4273ec24eed1812f42ea168affa27..fe2b815ff6a3f0160b9b3668b2a4239686e86ff3 100644 (file)
@@ -206,7 +206,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);
@@ -281,7 +283,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);