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().
This commit is contained in:
parent
3c781f89fe
commit
de013693fd
1 changed files with 15 additions and 11 deletions
|
|
@ -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 */
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue