# HG changeset patch # User John "Elwin" Edwards # Date 1438532087 14400 # Node ID db1c9a21a7c3164c842abed8ac527562fed748b7 # Parent 10c273a62228763d5c7140d6749e453f577b487f 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(). diff -r 10c273a62228 -r db1c9a21a7c3 srogue/main.c --- a/srogue/main.c Sat Aug 01 16:31:03 2015 -0400 +++ b/srogue/main.c Sun Aug 02 12:14:47 2015 -0400 @@ -69,7 +69,9 @@ 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 @@ 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); - - end = &path[strlen(path)-1]; - + /* 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'; - 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 */ } }