* cygcheck.cc (cygwin_info): Add some more bounds checking. From Matt Harget

<matt@use.net>:
* utils/cygcheck.cc (keyeprint): Move declaration before other functions so it
could be used by all functions to report error messages.  Add comment.
(add_path): Check for NULL return from malloc.
(find_on_path): Check initial uses of pointers for NULL.
(rva_to_offset): Ditto.
(init_paths): Add checking for return values of Win32 calls.
(get_dword): Ditto.
(get_word): Ditto.
(dll_info): Ditto, also add NULL pointer checks.
(scan_registry): Ditto.
(check_keys): Ditto.
(dump_sysinfo): Ditto.  Add default case to switch.  Add error reporting if
GetVolumeInformation fails, except when it returns ERROR_NOT_READY.
(track_down): Add checking for NULL pointers and return values of Win32 calls.
(cygwin_info): Ditto.  Correct small memory leak.
This commit is contained in:
Christopher Faylor 2000-12-16 04:53:30 +00:00
parent 085ec17c5d
commit 64069abe03
2 changed files with 205 additions and 49 deletions

View File

@ -1,3 +1,29 @@
Fri Dec 15 23:41:48 2000 Christopher Faylor <cgf@cygnus.com>
* cygcheck.cc (cygwin_info): Add some more bounds checking.
Fri Dec 15 23:41:34 2000 Christopher Faylor <cgf@cygnus.com>
From Matt Harget <matt@use.net>:
* utils/cygcheck.cc (keyeprint): Move declaration before other
functions so it could be used by all functions to report error
messages. Add comment.
(add_path): Check for NULL return from malloc.
(find_on_path): Check initial uses of pointers for NULL.
(rva_to_offset): Ditto.
(init_paths): Add checking for return values of Win32 calls.
(get_dword): Ditto.
(get_word): Ditto.
(dll_info): Ditto, also add NULL pointer checks.
(scan_registry): Ditto.
(check_keys): Ditto.
(dump_sysinfo): Ditto. Add default case to switch. Add error
reporting if GetVolumeInformation fails, except when it
returns ERROR_NOT_READY.
(track_down): Add checking for NULL pointers and
return values of Win32 calls.
(cygwin_info): Ditto. Correct small memory leak.
Sun Dec 10 19:08:58 2000 Christopher Faylor <cgf@cygnus.com> Sun Dec 10 19:08:58 2000 Christopher Faylor <cgf@cygnus.com>
* Makefile.in: Compile/link dumper.exe with c++. * Makefile.in: Compile/link dumper.exe with c++.
@ -16,9 +42,9 @@ Tue Dec 7 11:15:00 2000 Chris Abbey <cabbey@bresnanlink.net>
Sun Dec 3 00:40:47 2000 Christopher Faylor <cgf@cygnus.com> Sun Dec 3 00:40:47 2000 Christopher Faylor <cgf@cygnus.com>
* Makefile.in: Use CXX to build the DLL. * Makefile.in: Use CXX to build the DLL.
* configure.in: Find correct c++ compiler. * configure.in: Find correct c++ compiler.
* configure: Regenerate. * configure: Regenerate.
Sat Nov 18 23:53:59 2000 Christopher Faylor <cgf@cygnus.com> Sat Nov 18 23:53:59 2000 Christopher Faylor <cgf@cygnus.com>

View File

@ -77,6 +77,16 @@ common_apps[] =
int num_paths = 0, max_paths = 0; int num_paths = 0, max_paths = 0;
char **paths = 0; char **paths = 0;
/*
* keyeprint() is used to report failure modes
*/
int
keyeprint (const char *name)
{
fprintf (stderr, "cygcheck: %s failed: %lu\n", name, GetLastError ());
return 1;
}
void void
add_path (char *s, int maxlen) add_path (char *s, int maxlen)
{ {
@ -89,6 +99,11 @@ add_path (char *s, int maxlen)
paths = (char **) malloc (max_paths * sizeof (char *)); paths = (char **) malloc (max_paths * sizeof (char *));
} }
paths[num_paths] = (char *) malloc (maxlen + 1); paths[num_paths] = (char *) malloc (maxlen + 1);
if (paths[num_paths] == NULL)
{
keyeprint ("add_path: malloc()");
return;
}
memcpy (paths[num_paths], s, maxlen); memcpy (paths[num_paths], s, maxlen);
paths[num_paths][maxlen] = 0; paths[num_paths][maxlen] = 0;
char *e = paths[num_paths] + strlen (paths[num_paths]); char *e = paths[num_paths] + strlen (paths[num_paths]);
@ -106,8 +121,10 @@ init_paths ()
char tmp[4000], *sl; char tmp[4000], *sl;
add_path ((char *) ".", 1); /* to be replaced later */ add_path ((char *) ".", 1); /* to be replaced later */
add_path ((char *) ".", 1); /* the current directory */ add_path ((char *) ".", 1); /* the current directory */
GetSystemDirectory (tmp, 4000); if (GetSystemDirectory (tmp, 4000))
add_path (tmp, strlen (tmp)); add_path (tmp, strlen (tmp));
else
keyeprint ("init_paths: GetSystemDirectory()");
sl = strrchr (tmp, '\\'); sl = strrchr (tmp, '\\');
if (sl) if (sl)
{ {
@ -146,6 +163,18 @@ find_on_path (char *file, char *default_extension,
static char rv[4000]; static char rv[4000];
char tmp[4000], *ptr = rv; char tmp[4000], *ptr = rv;
if (file == NULL)
{
keyeprint ("find_on_path: NULL pointer for file");
return 0;
}
if (default_extension == NULL)
{
keyeprint ("find_on_path: NULL pointer for default_extension");
return 0;
}
if (strchr (file, ':') || strchr (file, '\\') || strchr (file, '/')) if (strchr (file, ':') || strchr (file, '\\') || strchr (file, '/'))
return file; return file;
@ -159,7 +188,7 @@ find_on_path (char *file, char *default_extension,
if (i == 0 || !search_sysdirs || strcasecmp (paths[i], paths[0])) if (i == 0 || !search_sysdirs || strcasecmp (paths[i], paths[0]))
{ {
sprintf (ptr, "%s\\%s%s", paths[i], file, default_extension); sprintf (ptr, "%s\\%s%s", paths[i], file, default_extension);
if (GetFileAttributes (ptr) != (DWORD) -1) if (GetFileAttributes (ptr) != (DWORD) - 1)
{ {
if (showall) if (showall)
printf ("Found: %s\n", ptr); printf ("Found: %s\n", ptr);
@ -208,8 +237,14 @@ get_word (HANDLE fh, int offset)
{ {
short rv; short rv;
unsigned r; unsigned r;
SetFilePointer (fh, offset, 0, FILE_BEGIN);
ReadFile (fh, &rv, 2, (DWORD *) &r, 0); if (SetFilePointer (fh, offset, 0, FILE_BEGIN) == INVALID_SET_FILE_POINTER
&& GetLastError () != NO_ERROR)
keyeprint ("get_word: SetFilePointer()");
if (!ReadFile (fh, &rv, 2, (DWORD *) & r, 0))
keyeprint ("get_word: Readfile()");
return rv; return rv;
} }
@ -218,8 +253,14 @@ get_dword (HANDLE fh, int offset)
{ {
int rv; int rv;
unsigned r; unsigned r;
SetFilePointer (fh, offset, 0, FILE_BEGIN);
ReadFile (fh, &rv, 4, (DWORD *) &r, 0); if (SetFilePointer (fh, offset, 0, FILE_BEGIN) == INVALID_SET_FILE_POINTER
&& GetLastError () != NO_ERROR)
keyeprint ("get_word: SetFilePointer()");
if (!ReadFile (fh, &rv, 4, (DWORD *) & r, 0))
keyeprint ("get_dword: Readfile()");
return rv; return rv;
} }
@ -236,6 +277,13 @@ int
rva_to_offset (int rva, char *sections, int nsections, int *sz) rva_to_offset (int rva, char *sections, int nsections, int *sz)
{ {
int i; int i;
if (sections == NULL)
{
keyeprint ("rva_to_offset: NULL passed for sections");
return 0;
}
for (i = 0; i < nsections; i++) for (i = 0; i < nsections; i++)
{ {
Section *s = (Section *) (sections + i * 40); Section *s = (Section *) (sections + i * 40);
@ -280,8 +328,7 @@ void track_down (char *file, char *suffix, int lvl);
static void static void
cygwin_info (HANDLE h) cygwin_info (HANDLE h)
{ {
char *buf, *bufend; char *buf, *bufend, *buf_start = NULL;
char *major, *minor;
const char *hello = " Cygwin DLL version info:\n"; const char *hello = " Cygwin DLL version info:\n";
DWORD size = GetFileSize (h, NULL); DWORD size = GetFileSize (h, NULL);
DWORD n; DWORD n;
@ -289,16 +336,21 @@ cygwin_info (HANDLE h)
if (size == 0xffffffff) if (size == 0xffffffff)
return; return;
buf = (char *) malloc (size); buf_start = buf = (char *) calloc (1, size + 1);
if (!buf) if (buf == NULL)
return; {
keyeprint ("cygwin_info: malloc()");
return;
}
(void) SetFilePointer (h, 0, NULL, FILE_BEGIN); (void) SetFilePointer (h, 0, NULL, FILE_BEGIN);
if (!ReadFile (h, buf, size, &n, NULL)) if (!ReadFile (h, buf, size, &n, NULL))
return; {
free (buf_start);
return;
}
bufend = buf + size; bufend = buf + size;
major = minor = NULL;
while (buf < bufend) while (buf < bufend)
if ((buf = (char *) memchr (buf, '%', bufend - buf)) == NULL) if ((buf = (char *) memchr (buf, '%', bufend - buf)) == NULL)
break; break;
@ -307,6 +359,8 @@ cygwin_info (HANDLE h)
else else
{ {
char *p = strchr (buf += CYGPREFIX, '\n'); char *p = strchr (buf += CYGPREFIX, '\n');
if (!p)
break;
fputs (hello, stdout); fputs (hello, stdout);
fputs (" ", stdout); fputs (" ", stdout);
fwrite (buf, 1 + p - buf, 1, stdout); fwrite (buf, 1 + p - buf, 1, stdout);
@ -315,6 +369,8 @@ cygwin_info (HANDLE h)
if (!*hello) if (!*hello)
puts (""); puts ("");
free (buf_start);
return; return;
} }
@ -326,13 +382,26 @@ dll_info (const char *path, HANDLE fh, int lvl, int recurse)
int pe_header_offset = get_dword (fh, 0x3c); int pe_header_offset = get_dword (fh, 0x3c);
int opthdr_ofs = pe_header_offset + 4 + 20; int opthdr_ofs = pe_header_offset + 4 + 20;
unsigned short v[6]; unsigned short v[6];
SetFilePointer (fh, opthdr_ofs + 40, 0, FILE_BEGIN);
ReadFile (fh, &v, sizeof (v), &junk, 0); if (path == NULL)
{
keyeprint ("dll_info: NULL passed for path");
return;
}
if (SetFilePointer (fh, opthdr_ofs + 40, 0, FILE_BEGIN) == INVALID_SET_FILE_POINTER
&& GetLastError () != NO_ERROR)
keyeprint ("dll_info: SetFilePointer()");
if (!ReadFile (fh, &v, sizeof (v), &junk, 0))
keyeprint ("dll_info: Readfile()");
if (verbose) if (verbose)
printf (" - os=%d.%d img=%d.%d sys=%d.%d\n", printf (" - os=%d.%d img=%d.%d sys=%d.%d\n",
v[0], v[1], v[2], v[3], v[4], v[5]); v[0], v[1], v[2], v[3], v[4], v[5]);
else else
printf ("\n"); printf ("\n");
int num_entries = get_dword (fh, opthdr_ofs + 92); int num_entries = get_dword (fh, opthdr_ofs + 92);
int export_rva = get_dword (fh, opthdr_ofs + 96); int export_rva = get_dword (fh, opthdr_ofs + 96);
int export_size = get_dword (fh, opthdr_ofs + 100); int export_size = get_dword (fh, opthdr_ofs + 100);
@ -341,21 +410,32 @@ dll_info (const char *path, HANDLE fh, int lvl, int recurse)
int nsections = get_word (fh, pe_header_offset + 4 + 2); int nsections = get_word (fh, pe_header_offset + 4 + 2);
char *sections = (char *) malloc (nsections * 40); char *sections = (char *) malloc (nsections * 40);
SetFilePointer (fh,
pe_header_offset + 4 + 20 + get_word (fh, if (SetFilePointer (fh, pe_header_offset + 4 + 20 +
pe_header_offset + 4 + get_word (fh, pe_header_offset + 4 + 16), 0,
16), 0, FILE_BEGIN); FILE_BEGIN) == INVALID_SET_FILE_POINTER
ReadFile (fh, sections, nsections * 40, &junk, 0); && GetLastError () != NO_ERROR)
keyeprint ("dll_info: SetFilePointer()");
if (!ReadFile (fh, sections, nsections * 40, &junk, 0))
keyeprint ("dll_info: Readfile()");
if (verbose && num_entries >= 1 && export_size > 0) if (verbose && num_entries >= 1 && export_size > 0)
{ {
int expsz; int expsz;
int expbase = rva_to_offset (export_rva, sections, nsections, &expsz); int expbase = rva_to_offset (export_rva, sections, nsections, &expsz);
if (expbase) if (expbase)
{ {
SetFilePointer (fh, expbase, 0, FILE_BEGIN); if (SetFilePointer (fh, expbase, 0, FILE_BEGIN) == INVALID_SET_FILE_POINTER
&& GetLastError () != NO_ERROR)
keyeprint ("dll_info: SetFilePointer()");
unsigned char *exp = (unsigned char *) malloc (expsz); unsigned char *exp = (unsigned char *) malloc (expsz);
ReadFile (fh, exp, expsz, &junk, 0);
if (!ReadFile (fh, exp, expsz, &junk, 0))
keyeprint ("dll_info: Readfile()");
ExpDirectory *ed = (ExpDirectory *) exp; ExpDirectory *ed = (ExpDirectory *) exp;
int ofs = ed->name_rva - export_rva; int ofs = ed->name_rva - export_rva;
struct tm *tm = localtime ((const time_t *) &(ed->timestamp)); struct tm *tm = localtime ((const time_t *) &(ed->timestamp));
@ -378,9 +458,20 @@ dll_info (const char *path, HANDLE fh, int lvl, int recurse)
int impbase = rva_to_offset (import_rva, sections, nsections, &impsz); int impbase = rva_to_offset (import_rva, sections, nsections, &impsz);
if (impbase) if (impbase)
{ {
SetFilePointer (fh, impbase, 0, FILE_BEGIN); if (SetFilePointer (fh, impbase, 0, FILE_BEGIN) == INVALID_SET_FILE_POINTER
&& GetLastError () != NO_ERROR)
keyeprint ("dll_info: SetFilePointer()");
unsigned char *imp = (unsigned char *) malloc (impsz); unsigned char *imp = (unsigned char *) malloc (impsz);
ReadFile (fh, imp, impsz, &junk, 0); if (imp == NULL)
{
keyeprint ("dll_info: malloc()");
return;
}
if (!ReadFile (fh, imp, impsz, &junk, 0))
keyeprint ("dll_info: Readfile()");
ImpDirectory *id = (ImpDirectory *) imp; ImpDirectory *id = (ImpDirectory *) imp;
for (i = 0; id[i].name_rva; i++) for (i = 0; id[i].name_rva; i++)
{ {
@ -397,6 +488,18 @@ dll_info (const char *path, HANDLE fh, int lvl, int recurse)
void void
track_down (char *file, char *suffix, int lvl) track_down (char *file, char *suffix, int lvl)
{ {
if (file == NULL)
{
keyeprint ("track_down: malloc()");
return;
}
if (suffix == NULL)
{
keyeprint ("track_down: malloc()");
return;
}
char *path = find_on_path (file, suffix, 0, 1); char *path = find_on_path (file, suffix, 0, 1);
if (!path) if (!path)
{ {
@ -427,6 +530,8 @@ track_down (char *file, char *suffix, int lvl)
printf (" (already done)\n"); printf (" (already done)\n");
} }
return; return;
default:
break;
} }
if (lvl) if (lvl)
@ -453,7 +558,8 @@ track_down (char *file, char *suffix, int lvl)
dll_info (path, fh, lvl, 1); dll_info (path, fh, lvl, 1);
d->state = DID_INACTIVE; d->state = DID_INACTIVE;
CloseHandle (fh); if (!CloseHandle (fh))
keyeprint ("track_down: CloseHandle()");
} }
void void
@ -462,15 +568,20 @@ ls (char *f)
HANDLE h = CreateFile (f, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, HANDLE h = CreateFile (f, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE,
0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0); 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
BY_HANDLE_FILE_INFORMATION info; BY_HANDLE_FILE_INFORMATION info;
GetFileInformationByHandle (h, &info);
if (!GetFileInformationByHandle (h, &info))
keyeprint ("ls: GetFileInformationByHandle()");
SYSTEMTIME systime; SYSTEMTIME systime;
FileTimeToSystemTime (&info.ftLastWriteTime, &systime);
if (!FileTimeToSystemTime (&info.ftLastWriteTime, &systime))
keyeprint ("ls: FileTimeToSystemTime()");
printf ("%5dk %04d/%02d/%02d %s", printf ("%5dk %04d/%02d/%02d %s",
(((int) info.nFileSizeLow) + 512) / 1024, (((int) info.nFileSizeLow) + 512) / 1024,
systime.wYear, systime.wMonth, systime.wDay, f); systime.wYear, systime.wMonth, systime.wDay, f);
dll_info (f, h, 16, 0); dll_info (f, h, 16, 0);
CloseHandle (h); if (!CloseHandle (h))
keyeprint ("ls: CloseHandle()");
} }
void void
@ -551,8 +662,20 @@ scan_registry (RegInfo * prev, HKEY hKey, char *name, int cygnus)
if (cygnus) if (cygnus)
{ {
show_reg (&ri, 0); show_reg (&ri, 0);
char *value_name = (char *) malloc (max_value_len + 1); char *value_name = (char *) malloc (max_value_len + 1);
if (value_name == NULL)
{
keyeprint ("scan_registry: malloc()");
return;
}
char *value_data = (char *) malloc (max_valdata_len + 1); char *value_data = (char *) malloc (max_valdata_len + 1);
if (value_data == NULL)
{
keyeprint ("scan_registry: malloc()");
return;
}
for (i = 0; i < num_values; i++) for (i = 0; i < num_values; i++)
{ {
@ -593,7 +716,8 @@ scan_registry (RegInfo * prev, HKEY hKey, char *name, int cygnus)
== ERROR_SUCCESS) == ERROR_SUCCESS)
{ {
scan_registry (&ri, sKey, subkey_name, cygnus); scan_registry (&ri, sKey, subkey_name, cygnus);
RegCloseKey (sKey); if (RegCloseKey (sKey) != ERROR_SUCCESS)
keyeprint ("scan_registry: RegCloseKey()");
} }
} }
} }
@ -614,7 +738,8 @@ dump_sysinfo ()
OSVERSIONINFO osversion; OSVERSIONINFO osversion;
osversion.dwOSVersionInfoSize = sizeof (osversion); osversion.dwOSVersionInfoSize = sizeof (osversion);
GetVersionEx (&osversion); if (!GetVersionEx (&osversion))
keyeprint ("dump_sysinfo: GetVersionEx()");
char *osname = (char *) "unknown OS"; char *osname = (char *) "unknown OS";
switch (osversion.dwPlatformId) switch (osversion.dwPlatformId)
{ {
@ -660,8 +785,10 @@ dump_sysinfo ()
s = e + 1; s = e + 1;
} }
GetSystemDirectory (tmp, 4000); if (!GetSystemDirectory (tmp, 4000))
keyeprint ("dump_sysinfo: GetSystemDirectory()");
printf ("\nSysDir: %s\n", tmp); printf ("\nSysDir: %s\n", tmp);
GetWindowsDirectory (tmp, 4000); GetWindowsDirectory (tmp, 4000);
printf ("WinDir: %s\n\n", tmp); printf ("WinDir: %s\n\n", tmp);
@ -757,8 +884,13 @@ dump_sysinfo ()
DWORD serno = 0, maxnamelen = 0, flags = 0; DWORD serno = 0, maxnamelen = 0, flags = 0;
name[0] = name[0] = fsname[0] = 0; name[0] = name[0] = fsname[0] = 0;
sprintf (drive, "%c:\\", i + 'a'); sprintf (drive, "%c:\\", i + 'a');
GetVolumeInformation (drive, name, sizeof (name), &serno, &maxnamelen, /* Report all errors, except if the Volume is ERROR_NOT_READY.
&flags, fsname, sizeof (fsname)); ERROR_NOT_READY is returned when removeable media drives are empty
(CD, floppy, etc.) */
if (!GetVolumeInformation (drive, name, sizeof (name), &serno, &maxnamelen, &flags,
fsname, sizeof (fsname))
&& GetLastError () != ERROR_NOT_READY)
keyeprint ("dump_sysinfo: GetVolumeInformation()");
int dtype = GetDriveType (drive); int dtype = GetDriveType (drive);
char drive_type[4] = "unk"; char drive_type[4] = "unk";
@ -779,6 +911,8 @@ dump_sysinfo ()
case DRIVE_RAMDISK: case DRIVE_RAMDISK:
strcpy (drive_type, "ram"); strcpy (drive_type, "ram");
break; break;
default:
strcpy (drive_type, "unk");
} }
long capacity_mb = -1; long capacity_mb = -1;
@ -822,7 +956,8 @@ dump_sysinfo ()
name); name);
} }
FreeLibrary (k32); if (!FreeLibrary (k32))
keyeprint ("dump_sysinfo: FreeLibrary()");
SetErrorMode (prev_mode); SetErrorMode (prev_mode);
if (givehelp) if (givehelp)
{ {
@ -904,13 +1039,6 @@ dump_sysinfo ()
} }
} }
int
keyeprint (const char *name)
{
fprintf (stderr, "cygcheck: %s failed: %lu\n", name, GetLastError ());
return 1;
}
int int
check_keys () check_keys ()
{ {
@ -919,17 +1047,17 @@ check_keys ()
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (h == INVALID_HANDLE_VALUE || h == NULL) if (h == INVALID_HANDLE_VALUE || h == NULL)
return keyeprint ("Opening CONIN$"); return (keyeprint ("check_key: Opening CONIN$"));
DWORD mode; DWORD mode;
if (!GetConsoleMode (h, &mode)) if (!GetConsoleMode (h, &mode))
keyeprint ("GetConsoleMode"); keyeprint ("check_keys: GetConsoleMode()");
else else
{ {
mode &= ~ENABLE_PROCESSED_INPUT; mode &= ~ENABLE_PROCESSED_INPUT;
if (!SetConsoleMode (h, mode)) if (!SetConsoleMode (h, mode))
keyeprint ("GetConsoleMode"); keyeprint ("check_keys: GetConsoleMode()");
} }
fputs ("\nThis key check works only in a console window,", stderr); fputs ("\nThis key check works only in a console window,", stderr);
@ -983,6 +1111,8 @@ check_keys ()
fputc ('\n', stdout); fputc ('\n', stdout);
break; break;
default:
break;
} }
} }
while (in.EventType != KEY_EVENT || while (in.EventType != KEY_EVENT ||