소스 검색

Be more careful calling wcstombs

The function is not guaranteed to NUL-terminate its output.  It
*is*, however, guaranteed not to generate more than two bytes per
multibyte character (plus terminating nul), so the general approach
I'm taking is to try to allocate enough space, AND to manually add a
NUL at the end of each buffer just in case I screwed up the "enough
space" thing.

Fixes bug 5909.
Nick Mathewson 12 년 전
부모
커밋
1e5683b167
6개의 변경된 파일30개의 추가작업 그리고 12개의 파일을 삭제
  1. 5 0
      changes/bug5909
  2. 16 7
      src/common/compat.c
  3. 2 1
      src/common/util.c
  4. 3 2
      src/or/config.c
  5. 2 1
      src/or/eventdns.c
  6. 2 1
      src/or/ntmain.c

+ 5 - 0
changes/bug5909

@@ -0,0 +1,5 @@
+  o Major bugfixes:
+    - When building Tor on Windows with -DUNICODE (not default),
+      ensure that error messages, filenames, and DNS server names are
+      always NUL-terminated when we convert them to a single-byte
+      encoding.  Fixes bug 5909; bugfix on 0.2.2.16-alpha.

+ 16 - 7
src/common/compat.c

@@ -3046,28 +3046,37 @@ format_win32_error(DWORD err)
 {
 {
   TCHAR *str = NULL;
   TCHAR *str = NULL;
   char *result;
   char *result;
+  DWORD n;
 
 
   /* Somebody once decided that this interface was better than strerror(). */
   /* Somebody once decided that this interface was better than strerror(). */
-  FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
+  n = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
                  FORMAT_MESSAGE_FROM_SYSTEM |
                  FORMAT_MESSAGE_FROM_SYSTEM |
                  FORMAT_MESSAGE_IGNORE_INSERTS,
                  FORMAT_MESSAGE_IGNORE_INSERTS,
                  NULL, err,
                  NULL, err,
                  MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
                  MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
-                (LPVOID)&str,
+                 (LPVOID)&str,
                  0, NULL);
                  0, NULL);
 
 
-  if (str) {
+  if (str && n) {
 #ifdef UNICODE
 #ifdef UNICODE
-    char abuf[1024] = {0};
-    wcstombs(abuf,str,1024);
-    result = tor_strdup(abuf);
+    size_t len;
+    if (n > 128*1024)
+      len = (128 * 1024) * 2 + 1; /* This shouldn't be possible, but let's
+                                   * make sure. */
+    else
+      len = n * 2 + 1;
+    result = tor_malloc(len);
+    wcstombs(result,str,len);
+    result[len-1] = '\0';
 #else
 #else
     result = tor_strdup(str);
     result = tor_strdup(str);
 #endif
 #endif
-    LocalFree(str); /* LocalFree != free() */
   } else {
   } else {
     result = tor_strdup("<unformattable error>");
     result = tor_strdup("<unformattable error>");
   }
   }
+  if (str) {
+    LocalFree(str); /* LocalFree != free() */
+  }
   return result;
   return result;
 }
 }
 #endif
 #endif

+ 2 - 1
src/common/util.c

@@ -2855,7 +2855,7 @@ tor_listdir(const char *dirname)
 #ifdef _WIN32
 #ifdef _WIN32
   char *pattern=NULL;
   char *pattern=NULL;
   TCHAR tpattern[MAX_PATH] = {0};
   TCHAR tpattern[MAX_PATH] = {0};
-  char name[MAX_PATH] = {0};
+  char name[MAX_PATH*2+1] = {0};
   HANDLE handle;
   HANDLE handle;
   WIN32_FIND_DATA findData;
   WIN32_FIND_DATA findData;
   tor_asprintf(&pattern, "%s\\*", dirname);
   tor_asprintf(&pattern, "%s\\*", dirname);
@@ -2872,6 +2872,7 @@ tor_listdir(const char *dirname)
   while (1) {
   while (1) {
 #ifdef UNICODE
 #ifdef UNICODE
     wcstombs(name,findData.cFileName,MAX_PATH);
     wcstombs(name,findData.cFileName,MAX_PATH);
+    name[sizeof(name)-1] = '\0';
 #else
 #else
     strlcpy(name,findData.cFileName,sizeof(name));
     strlcpy(name,findData.cFileName,sizeof(name));
 #endif
 #endif

+ 3 - 2
src/or/config.c

@@ -4302,7 +4302,7 @@ static char *
 get_windows_conf_root(void)
 get_windows_conf_root(void)
 {
 {
   static int is_set = 0;
   static int is_set = 0;
-  static char path[MAX_PATH+1];
+  static char path[MAX_PATH*2+1];
   TCHAR tpath[MAX_PATH] = {0};
   TCHAR tpath[MAX_PATH] = {0};
 
 
   LPITEMIDLIST idl;
   LPITEMIDLIST idl;
@@ -4332,7 +4332,8 @@ get_windows_conf_root(void)
   /* Convert the path from an "ID List" (whatever that is!) to a path. */
   /* Convert the path from an "ID List" (whatever that is!) to a path. */
   result = SHGetPathFromIDList(idl, tpath);
   result = SHGetPathFromIDList(idl, tpath);
 #ifdef UNICODE
 #ifdef UNICODE
-  wcstombs(path,tpath,MAX_PATH);
+  wcstombs(path,tpath,sizeof(path));
+  path[sizeof(path)-1] = '\0';
 #else
 #else
   strlcpy(path,tpath,sizeof(path));
   strlcpy(path,tpath,sizeof(path));
 #endif
 #endif

+ 2 - 1
src/or/eventdns.c

@@ -3213,7 +3213,7 @@ static int
 config_nameserver_from_reg_key(HKEY key, const TCHAR *subkey)
 config_nameserver_from_reg_key(HKEY key, const TCHAR *subkey)
 {
 {
 	char *buf;
 	char *buf;
-  char ansibuf[MAX_PATH] = {0};
+	char ansibuf[MAX_PATH] = {0};
 	DWORD bufsz = 0, type = 0;
 	DWORD bufsz = 0, type = 0;
 	int status = 0;
 	int status = 0;
 
 
@@ -3226,6 +3226,7 @@ config_nameserver_from_reg_key(HKEY key, const TCHAR *subkey)
 	if (RegQueryValueEx(key, subkey, 0, &type, (LPBYTE)buf, &bufsz)
 	if (RegQueryValueEx(key, subkey, 0, &type, (LPBYTE)buf, &bufsz)
 		== ERROR_SUCCESS && bufsz > 1) {
 		== ERROR_SUCCESS && bufsz > 1) {
 		wcstombs(ansibuf,(wchar_t*)buf,MAX_PATH);/*XXXX UNICODE */
 		wcstombs(ansibuf,(wchar_t*)buf,MAX_PATH);/*XXXX UNICODE */
+		abuf[MAX_PATH-1] = '\0';
 		status = evdns_nameserver_ip_add_line(ansibuf);
 		status = evdns_nameserver_ip_add_line(ansibuf);
 	}
 	}
 
 

+ 2 - 1
src/or/ntmain.c

@@ -455,7 +455,7 @@ static char *
 nt_service_command_line(int *using_default_torrc)
 nt_service_command_line(int *using_default_torrc)
 {
 {
   TCHAR tor_exe[MAX_PATH+1];
   TCHAR tor_exe[MAX_PATH+1];
-  char tor_exe_ascii[MAX_PATH+1];
+  char tor_exe_ascii[MAX_PATH*2+1];
   char *command=NULL, *options=NULL;
   char *command=NULL, *options=NULL;
   smartlist_t *sl;
   smartlist_t *sl;
   int i;
   int i;
@@ -483,6 +483,7 @@ nt_service_command_line(int *using_default_torrc)
 
 
 #ifdef UNICODE
 #ifdef UNICODE
   wcstombs(tor_exe_ascii, tor_exe, sizeof(tor_exe_ascii));
   wcstombs(tor_exe_ascii, tor_exe, sizeof(tor_exe_ascii));
+  tor_exe_ascii[sizeof(tor_exe_ascii)-1] = '\0';
 #else
 #else
   strlcpy(tor_exe_ascii, tor_exe, sizeof(tor_exe_ascii));
   strlcpy(tor_exe_ascii, tor_exe, sizeof(tor_exe_ascii));
 #endif
 #endif