public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Martin Sebor <msebor@gmail.com>
To: Jonathan Wakely <jwakely@redhat.com>
Cc: libstdc++ <libstdc++@gcc.gnu.org>,
	 gcc-patches <gcc-patches@gcc.gnu.org>
Subject: Re: [patch] libstdc++/67173 Fix filesystem::canonical for Solaris 10.
Date: Wed, 16 Sep 2015 23:51:00 -0000	[thread overview]
Message-ID: <55F9FE71.1060901@gmail.com> (raw)
In-Reply-To: <20150916221727.GF2631@redhat.com>

On 09/16/2015 04:17 PM, Jonathan Wakely wrote:
> On 16/09/15 16:04 -0600, Martin Sebor wrote:
>>> Tested powerpc64le-linux, x86_64-dragonfly4.1 and x86_64-netbsd5.1,
>>> do you see any reason not to commit this for now?
>>
>> I see only a couple of potential problems: a missing test for
>> PATH_MAX in the unlikely event it's not defined (or is obscenely
>
> In the current patch _GLIBCXX_USE_REALPATH won't be defined unless:
>
>        #elif _XOPEN_VERSION >= 700 || defined(PATH_MAX)
>
> so if it's defined and _XOPEN_VERSION < 700 then we know PATH_MAX must
> be defined (otherwise _GLIBCXX_USE_REALPATH wouldn't be).
>
>> large), and a missing check to avoid infinite loops due to symlinks.
>
> I thought about keeping track of where I'd been while expanding
> symlinks, but then realised this will do it:
>
>   if (!exists(pa, ec))
>     {
>       fail(ENOENT);
>       return result;
>     }
>   // else we can assume no unresolvable symlink loops
>
> If there's a symlink loop then exists(pa) will fail with ELOOP, and we
> won't try to resolve it by hand.
>
> And then after each step in the while(!cmpts.empty()) loop I also have
> a check for !exists(result, ec), which should even handle the case
> where the filesystem changes after the initial exists() call so that a
> loop is introduced while we're canonicalising the path.

I obviously didn't read the patch carefully enough and missed
both the PATH_MAX check and the loop comment.

I see now the first exists test will detect symlink loops in
the original path. But I'm not convinced there isn't a corner
case that's subject to a TOCTOU race condition between the first
exists test and the while loop during which a symlink loop can
be introduced.

Suppose we call the function with /foo/bar as an argument and
the path exists and contains no symlinks. result is / and cmpts
is set to { foo, bar }. Just as the loop is entered, /foo/bar
is replaced with a symlink containing /foo/bar. The loop then
proceeds like so:

1. The first iteration removes foo from cmpts and sets result
to /foo. cmpts is { bar }.

2. The second iteration removes bar from cmpts, sets result to
/foo/bar, determines it's a symlink, reads its contents, sees
it's an absolute pathname and replaces result with /. It then
inserts the symlink's components { foo, bar } into cmpts. cmpts
becomes { foo, bar }. exists(result) succeeds.

3. The next iteration of the loop has the same initial state
as the first.

But I could have very easily missed something that takes care
of this corner case. If I did, sorry for the false alarm!

Martin

  reply	other threads:[~2015-09-16 23:42 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-09-11 14:23 Jonathan Wakely
2015-09-11 18:05 ` Martin Sebor
2015-09-12 10:39   ` Jonathan Wakely
2015-09-12 19:49     ` Martin Sebor
2015-09-12 22:00       ` Martin Sebor
2015-09-16 14:52       ` Jonathan Wakely
2015-09-16 16:05         ` Jonathan Wakely
2015-09-16 16:11           ` Jonathan Wakely
2015-09-16 17:38         ` Martin Sebor
2015-09-16 19:02           ` Jonathan Wakely
2015-09-16 22:17             ` Martin Sebor
2015-09-16 22:23               ` Jonathan Wakely
2015-09-16 23:51                 ` Martin Sebor [this message]
2015-09-17 11:31                   ` Jonathan Wakely
2015-09-17 11:33                     ` Jonathan Wakely
2015-09-17 14:38                     ` Jonathan Wakely
2015-09-17 15:40                     ` Martin Sebor
2015-09-23 12:14                       ` Jonathan Wakely
2015-09-16 23:42             ` Jonathan Wakely
2015-09-17 15:36               ` Jonathan Wakely
2015-09-17 19:27             ` Andreas Schwab
2015-09-17 22:23               ` Jonathan Wakely

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=55F9FE71.1060901@gmail.com \
    --to=msebor@gmail.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=jwakely@redhat.com \
    --cc=libstdc++@gcc.gnu.org \
    /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).