From de013693fdd1ecde0d79e8cce78fd9c903e22e1a Mon Sep 17 00:00:00 2001 From: "John \"Elwin\" Edwards" Date: Sun, 2 Aug 2015 12:14:47 -0400 Subject: [PATCH] srogue: prevent overflowing the score file name. If SCOREFILE is not defined, roguehome() is called to find a directory for the score file. It copies up to PATH_MAX-20 bytes from an environment variable to a static buffer. Later these are strcpy()'d to scorefile, which is of size LINLEN. Unfortunately LINLEN is 80 and PATH_MAX is at least 256. On Linux, it happens to be 4096. I haven't yet managed to crash or exploit it, but there are surely no beneficial consequences, so roguehome() has been modified to check the length, and the string it returns is also checked in main(). --- srogue/main.c | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/srogue/main.c b/srogue/main.c index a3d942f..9b7b647 100644 --- a/srogue/main.c +++ b/srogue/main.c @@ -69,7 +69,9 @@ char **envp; scorefile[LINLEN - 1] = '\0'; #else - strcpy(scorefile, homedir); + strncpy(scorefile, homedir, LINLEN-11); + if (scorefile[LINLEN-12] != '\0') + scorefile[0] = '\0'; if (*scorefile) strcat(scorefile,"/"); @@ -444,23 +446,25 @@ directory_exists(char *dirname) char * roguehome() { - static char path[1024]; + static char path[LINLEN+16]; char *end,*home; if ( (home = getenv("ROGUEHOME")) != NULL) { if (*home) { - strncpy(path, home, PATH_MAX - 20); + /* LINLEN - 11 is all that will fit into scorefile */ + strncpy(path, home, LINLEN - 11); + if (path[LINLEN - 12] == '\0') + { + end = &path[strlen(path)-1]; + while( (end >= path) && ((*end == '/') || (*end == '\\'))) + *end-- = '\0'; - end = &path[strlen(path)-1]; - - - while( (end >= path) && ((*end == '/') || (*end == '\\'))) - *end-- = '\0'; - - if (directory_exists(path)) - return(path); + if (directory_exists(path)) + return(path); + } + /* Otherwise home was truncated and should be ignored */ } }