public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] stdlib: realpath use malloc replace __alloca to reduce stack overflow risks [BZ #26341]
@ 2020-08-06 14:32 Xiaoming Ni
  2020-08-06 19:24 ` Adhemerval Zanella
  0 siblings, 1 reply; 2+ messages in thread
From: Xiaoming Ni @ 2020-08-06 14:32 UTC (permalink / raw)
  To: libc-alpha, glibc-bugs, unassigned, drepper.fsp, roland, carlos
  Cc: nixiaoming, wangle6, yukeji

Realpath() cyclically invokes __alloca() when processing soft link files,
which may consume 164 KB stack space.
Therefore, replace __alloca with malloc to reduce stack overflow risks

Signed-off-by: Xiaoming Ni <nixiaoming@huawei.com>
---
 stdlib/canonicalize.c | 28 +++++++++++++++++++++++++---
 1 file changed, 25 insertions(+), 3 deletions(-)

diff --git a/stdlib/canonicalize.c b/stdlib/canonicalize.c
index cbd885a3c5..d3130d81f0 100644
--- a/stdlib/canonicalize.c
+++ b/stdlib/canonicalize.c
@@ -163,27 +163,46 @@ __realpath (const char *name, char *resolved)
 
 	  if (S_ISLNK (st.st_mode))
 	    {
-	      char *buf = __alloca (path_max);
+	      char *buf = malloc (path_max);
 	      size_t len;
 
+	      if (!buf)
+	        {
+	          __set_errno (ENOMEM);
+	          goto error;
+	        }
+
 	      if (++num_links > __eloop_threshold ())
 		{
 		  __set_errno (ELOOP);
+		  free(buf);
 		  goto error;
 		}
 
 	      n = __readlink (rpath, buf, path_max - 1);
 	      if (n < 0)
-		goto error;
+	        {
+	          free(buf);
+	          goto error;
+	        }
 	      buf[n] = '\0';
 
 	      if (!extra_buf)
-		extra_buf = __alloca (path_max);
+	        {
+	          extra_buf = malloc (path_max);
+	          if (!extra_buf)
+	            {
+	              free(buf);
+	              __set_errno (ENOMEM);
+	              goto error;
+	            }
+	        }
 
 	      len = strlen (end);
 	      if (path_max - n <= len)
 		{
 		  __set_errno (ENAMETOOLONG);
+		  free(buf);
 		  goto error;
 		}
 
@@ -197,6 +216,7 @@ __realpath (const char *name, char *resolved)
 		/* Back up to previous component, ignore if at root already: */
 		if (dest > rpath + 1)
 		  while ((--dest)[-1] != '/');
+	      free(buf);
 	    }
 	  else if (!S_ISDIR (st.st_mode) && *end != '\0')
 	    {
@@ -210,12 +230,14 @@ __realpath (const char *name, char *resolved)
   *dest = '\0';
 
   assert (resolved == NULL || resolved == rpath);
+  free(extra_buf);
   return rpath;
 
 error:
   assert (resolved == NULL || resolved == rpath);
   if (resolved == NULL)
     free (rpath);
+  free(extra_buf);
   return NULL;
 }
 libc_hidden_def (__realpath)
-- 
2.27.0


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2020-08-06 19:24 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-08-06 14:32 [PATCH] stdlib: realpath use malloc replace __alloca to reduce stack overflow risks [BZ #26341] Xiaoming Ni
2020-08-06 19:24 ` Adhemerval Zanella

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).