Skip to content

Commit ea829d8

Browse files
committed
security: fix buffer overflows in winprefs.c
- LoadImageComma: dynamically allocate buffer based on path lengths instead of fixed-size stack buffer - LoadPreferences: use snprintf to avoid overflow when constructing ~/.XWinrc path
1 parent c1fcd91 commit ea829d8

1 file changed

Lines changed: 20 additions & 12 deletions

File tree

hw/xwin/winprefs.c

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -514,22 +514,30 @@ LoadImageComma(char *fname, char *iconDirectory, int sx, int sy, int flags)
514514
MAKEINTRESOURCE(i), IMAGE_ICON, sx, sy, flags);
515515
}
516516
else {
517-
char *file = calloc(1, PATH_MAX + NAME_MAX + 2);
518-
517+
size_t len_icon = 0, len_fname = strlen(fname);
518+
size_t needed;
519+
char *file;
520+
521+
if (iconDirectory)
522+
len_icon = strlen(iconDirectory);
523+
needed = len_icon;
524+
if (len_icon > 0 && iconDirectory[len_icon - 1] != '\\')
525+
needed++; // space for backslash
526+
needed += len_fname + 1; // +1 for null terminator
527+
528+
file = malloc(needed);
519529
if (!file)
520530
return NULL;
521-
522-
file[0] = 0;
531+
file[0] = '\0';
523532

524533
/* If fname starts 'X:\', it's an absolute Windows path, do nothing */
525534
if (!(fname[0] && fname[1] == ':' && fname[2] == '\\')) {
526535
if (iconDirectory) {
527536
/* Otherwise, prepend the default icon directory, which
528537
currently must be in absolute Windows path form */
529538
strcpy(file, iconDirectory);
530-
if (iconDirectory[0])
531-
if (iconDirectory[strlen(iconDirectory) - 1] != '\\')
532-
strcat(file, "\\");
539+
if (iconDirectory[0] && file[strlen(file) - 1] != '\\')
540+
strcat(file, "\\");
533541
}
534542
}
535543
strcat(file, fname);
@@ -657,11 +665,11 @@ LoadPreferences(void)
657665
/* Now try and find a ~/.xwinrc file */
658666
home = getenv("HOME");
659667
if (home) {
660-
strcpy(fname, home);
661-
if (fname[strlen(fname) - 1] != '/')
662-
strcat(fname, "/");
663-
strcat(fname, ".XWinrc");
664-
parsed = winPrefsLoadPreferences(fname);
668+
if (snprintf(fname, sizeof(fname), "%s/.XWinrc", home) >= (int)sizeof(fname)) {
669+
ErrorF("HOME path too long, ignoring ~/.XWinrc\n");
670+
} else {
671+
parsed = winPrefsLoadPreferences(fname);
672+
}
665673
}
666674

667675
/* No home file found, check system default */

0 commit comments

Comments
 (0)