Przeglądaj źródła

avoid stat/open race, and actually check for regular file (in demo)

Christian Grothoff 10 lat temu
rodzic
commit
91e7665945
1 zmienionych plików z 13 dodań i 6 usunięć
  1. 13 6
      src/examples/demo_https.c

+ 13 - 6
src/examples/demo_https.c

@@ -693,12 +693,19 @@ generate_page (void *cls,
 
       if (0 != strcmp (method, MHD_HTTP_METHOD_GET))
 	return MHD_NO;  /* unexpected method (we're not polite...) */
-      if ( (0 == stat (&url[1], &buf)) &&
-	   (NULL == strstr (&url[1], "..")) &&
-	   ('/' != url[1]))
-	fd = open (&url[1], O_RDONLY);
-      else
-	fd = -1;
+      fd = -1;
+
+      if ( (NULL == strstr (&url[1], "..")) &&
+	   ('/' != url[1]) )
+        {
+          fd = open (&url[1], O_RDONLY);
+          if ( (0 != fstat (fd, &buf)) ||
+               (! S_ISREG (buf.st_mode)) )
+            {
+              (void) close (fd);
+              fd = -1;
+            }
+        }
       if (-1 == fd)
 	return MHD_queue_response (connection,
 				   MHD_HTTP_NOT_FOUND,