public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Paul Eggert <eggert@cs.ucla.edu>
To: Xiaoming Ni <nixiaoming@huawei.com>,
	libc-alpha@sourceware.org, glibc-bugs@sourceware.org,
	unassigned@sourceware.org, drepper.fsp@gmail.com, roland@gnu.org,
	carlos@redhat.com, adhemerval.zanella@linaro.org
Cc: yukeji@huawei.com, wangle6@huawei.com
Subject: Re: [PATCH v2] stdlib: realpath use malloc replace __alloca to reduce stack overflow risks [BZ #26341]
Date: Fri, 7 Aug 2020 16:56:51 -0700	[thread overview]
Message-ID: <059aea74-2976-c16a-0a6d-fbde2123ac87@cs.ucla.edu> (raw)
In-Reply-To: <20200807101601.61670-1-nixiaoming@huawei.com>

[-- Attachment #1: Type: text/plain, Size: 587 bytes --]

On 8/7/20 3:16 AM, Xiaoming Ni wrote:
> 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

I don't understand why malloc is required here.

Isn't the problem that the buffer is being allocated over and over again? And if 
you fix that bug, the function should allocate only 8 KiB via __alloca on 
GNU/Linux. Something like the attached (untested) patch, say. So why not keep 
using the stack? That would be more efficient than resorting to the heap.

[-- Attachment #2: realpath.diff --]
[-- Type: text/x-patch, Size: 850 bytes --]

diff --git a/stdlib/canonicalize.c b/stdlib/canonicalize.c
index cbd885a3c5..ddeea3e2e9 100644
--- a/stdlib/canonicalize.c
+++ b/stdlib/canonicalize.c
@@ -42,7 +42,7 @@
 char *
 __realpath (const char *name, char *resolved)
 {
-  char *rpath, *dest, *extra_buf = NULL;
+  char *rpath, *dest, *buf = NULL, *extra_buf = NULL;
   const char *start, *end, *rpath_limit;
   long int path_max;
   int num_links = 0;
@@ -163,7 +163,6 @@ __realpath (const char *name, char *resolved)
 
 	  if (S_ISLNK (st.st_mode))
 	    {
-	      char *buf = __alloca (path_max);
 	      size_t len;
 
 	      if (++num_links > __eloop_threshold ())
@@ -172,6 +171,8 @@ __realpath (const char *name, char *resolved)
 		  goto error;
 		}
 
+	      if (!buf)
+		buf = __alloca (path_max);
 	      n = __readlink (rpath, buf, path_max - 1);
 	      if (n < 0)
 		goto error;

  parent reply	other threads:[~2020-08-07 23:56 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-08-07 10:16 Xiaoming Ni
2020-08-07 19:43 ` Adhemerval Zanella
2020-08-08  0:00   ` Paul Eggert
2020-08-08  9:14   ` Xiaoming Ni
2020-08-07 23:56 ` Paul Eggert [this message]
2020-08-08  8:54   ` Xiaoming Ni
2020-08-09  8:44     ` Paul Eggert
2020-08-09 12:38       ` Florian Weimer
2020-08-09 17:22         ` Paul Eggert
2020-08-10 13:40           ` Adhemerval Zanella
2020-08-11  9:54             ` Paul Eggert

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=059aea74-2976-c16a-0a6d-fbde2123ac87@cs.ucla.edu \
    --to=eggert@cs.ucla.edu \
    --cc=adhemerval.zanella@linaro.org \
    --cc=carlos@redhat.com \
    --cc=drepper.fsp@gmail.com \
    --cc=glibc-bugs@sourceware.org \
    --cc=libc-alpha@sourceware.org \
    --cc=nixiaoming@huawei.com \
    --cc=roland@gnu.org \
    --cc=unassigned@sourceware.org \
    --cc=wangle6@huawei.com \
    --cc=yukeji@huawei.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).