bug-gnulib@gnu.org mirror (unofficial)
 help / color / mirror / Atom feed
From: Florian Weimer <fweimer@redhat.com>
To: Bruno Haible <bruno@clisp.org>
Cc: Kamil Dudka <kdudka@redhat.com>, Paul Eggert <eggert@cs.ucla.edu>,
	bug-gnulib@gnu.org, Jeff Layton <jlayton@redhat.com>,
	Bernhard Voelker <mail@bernhard-voelker.de>,
	NeilBrown <neilb@suse.com>, Eric Blake <eblake@redhat.com>
Subject: Re: Why does close_stdout close stdout and stderr?
Date: Mon, 27 May 2019 14:00:40 +0200	[thread overview]
Message-ID: <875zpwjedz.fsf@oldenburg2.str.redhat.com> (raw)
In-Reply-To: <1981329.u6ql5j3YLz@omega> (Bruno Haible's message of "Sat, 25 May 2019 13:24:39 +0200")

* Bruno Haible:

> Florian Weimer wrote in
> <https://lists.gnu.org/archive/html/bug-gnulib/2019-05/msg00087.html>:
>> The relevant case is where there is no error, and we do not call _exit.
>> I'm worried that the current implementation introduces a use-after-free
>> bug under certain, quite reasonable circumstances.  All that is needed
>> is a shared object that tries to log something to stderr from an ELF
>> destructor.  I don't think that's something that can be ruled out, or
>> can be assumed to happen in development environments only.
>
> OK, now you have described the problem in an understandable way.
>
> Let me rephrase the dilemma:
>
>   1) POSIX guarantees that we can detect write errors [up to the file
>      system layer of the kernel - I'm not worried about I/O errors on
>      the actual device] through fclose(), and kernels implement this.
>      Neither POSIX nor Linux guarantees that we can detect write errors
>      without calling fclose().
>
>   2) POSIX says "After the call to fclose(), any use of stream results in
>      undefined behavior." [1]
>
> So, we need to call fclose(stderr) at a moment when we know that stderr
> will not be used any more.
>
> We have
>   * applications (like the coreutils programs), and
>   * environments which can modify the behaviour of these applications,
>     like shared objects added through LD_PRELOAD, or sanitizers [2].

This doesn't have to do anything with LD_PRELOAD.  It really depends
what kind of ELF destructors and atexit handlers are present in the
process image.

> The solution I would propose is to
>   - By default, assume that the environment does not modify the behaviour
>     of the application. That is, that the application executes its code
>     and nothing more.
>   - Let the environment tell the application (through an environment
>     variable) that it is modifying its behaviour.
>
> For the first case, the current 'closeout' module is perfect.
>
> For the other case, we can introduce, next to the !SANITIZE_ADDRESS test,
> tests for
>   getenv ("LD_PRELOAD") != NULL
>   getenv ("ASAN_OPTIONS") != NULL
>   getenv ("TSAN_OPTIONS") != NULL
>   getenv ("MSAN_OPTIONS") != NULL
>   getenv ("LSAN_OPTIONS") != NULL
> We can add more such environment variables as needed. getenv() lookups
> don't make system calls; so they are cheap.

The application may have called clearenv before that.

What's so bad about closing the underyling file descriptor after
duplicating it?  It's much more portable than the other stdio hacks the
gnulib code contains today.  It will not have the desired effect on some
platforms, and on others, it is redundant because the write system call
performs ENOSPC checks, even on NFS.  It fixes a real problem our users
reported on Linux.  Why can't we make this work correctly out of the
box?

Thanks,
Florian


  parent reply	other threads:[~2019-05-27 12:02 UTC|newest]

Thread overview: 34+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-04-29 19:45 Why does close_stdout close stdout and stderr? Florian Weimer
2019-04-29 19:49 ` Eric Blake
2019-04-29 20:26   ` Florian Weimer
2019-05-06 12:05     ` Florian Weimer
2019-05-06 14:56       ` Bernhard Voelker
2019-05-06 15:47         ` Florian Weimer
2019-05-06 19:14           ` Bernhard Voelker
2019-05-06 19:19             ` Florian Weimer
2019-05-09  6:20               ` Bernhard Voelker
2019-05-09  6:39                 ` Florian Weimer
2019-05-09  9:49                   ` Bernhard Voelker
2019-05-09 22:17                   ` Paul Eggert
2019-05-10  7:28                     ` Kamil Dudka
2019-05-10  7:31                       ` Florian Weimer
2019-05-13  1:09                       ` Paul Eggert
2019-05-13  7:00                         ` Florian Weimer
2019-05-25  1:41                           ` Paul Eggert
2019-05-25 10:58                             ` Bruno Haible
2019-05-27 11:56                             ` Florian Weimer
2019-05-25 11:24                           ` Bruno Haible
2019-05-25 19:23                             ` Paul Eggert
2019-05-27 12:00                             ` Florian Weimer [this message]
2019-05-27 21:13                               ` Bruno Haible
2019-05-06 18:53       ` Paul Eggert
2019-05-06 19:02         ` Jeff Layton
2019-05-06 22:32       ` Bruno Haible
2019-05-07  9:44         ` Assaf Gordon
2019-05-07  9:49           ` Assaf Gordon
2019-05-07 11:28           ` Bruno Haible
2019-05-08  0:43             ` NeilBrown
2019-05-08 11:00               ` Florian Weimer
2019-05-09  4:42                 ` Paul Eggert
2019-05-09  5:01                   ` Florian Weimer
2019-05-09  6:27                   ` NeilBrown

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

  List information: https://lists.gnu.org/mailman/listinfo/bug-gnulib

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

  git send-email \
    --in-reply-to=875zpwjedz.fsf@oldenburg2.str.redhat.com \
    --to=fweimer@redhat.com \
    --cc=bruno@clisp.org \
    --cc=bug-gnulib@gnu.org \
    --cc=eblake@redhat.com \
    --cc=eggert@cs.ucla.edu \
    --cc=jlayton@redhat.com \
    --cc=kdudka@redhat.com \
    --cc=mail@bernhard-voelker.de \
    --cc=neilb@suse.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).