changeset 176:db1c9a21a7c3

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().
author John "Elwin" Edwards
date Sun, 02 Aug 2015 12:14:47 -0400
parents 10c273a62228
children 7bdac632ab9d 7c059ec2a2c7
files srogue/main.c
diffstat 1 files changed, 15 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- 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 */
         }
     }