git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Avery Pennarun" <apenwarr@gmail.com>
To: "Stephen R. van den Berg" <srb@cuci.nl>
Cc: "Johannes Sixt" <j.sixt@viscovery.net>,
	"Paolo Bonzini" <bonzini@gnu.org>,
	"Karl Chen" <quarl@cs.berkeley.edu>,
	"Git mailing list" <git@vger.kernel.org>,
	"Junio C Hamano" <gitster@pobox.com>
Subject: Re: [PATCH] be paranoid about closed stdin/stdout/stderr
Date: Wed, 27 Aug 2008 01:01:00 -0400	[thread overview]
Message-ID: <32541b130808262201v4d7c1aa5r781720a80b79fcd0@mail.gmail.com> (raw)
In-Reply-To: <20080826074044.GA22694@cuci.nl>

On 8/26/08, Stephen R. van den Berg <srb@cuci.nl> wrote:
> Well, in general the policy I've used in all the tools I created is that:
>
>  a. If it's a setuid tool, then you need to make sure that you don't step
>    on anything unintendedly.  I.e. for setuid-something programs this is
>    desirable and necessary in order to prevent securityleaks.
>
>  b. Anything else is started in an environment controlled by the user,
>    and if this environment is broken, then that is the user's fault.

In general I'd mostly agree with you, but fd 0/1/2 are super-special
and I've personally been bitten by insane, rare problems that occur
when programs are started with one or more of those fds closed.

The usual case is that you're writing a new daemon.  The generally
accepted behaviour for a daemon is to chdir("/') and then close all
unnecessary open fds, in order to minimize the chance that it will be
holding open any directories or files that would prevent unmounting a
filesystem.  On the other hand, if the daemon then needs to run git
for some reason (who knows! maybe it's a git auto-commit daemon as was
discussed earlier on the list), it needs to open file descriptors
instead.  Such a program might work 99% of the time when git doesn't
happen to print any output.  But if there's ever an error, git would
print to fd#2 on die(), and that could corrupt some random file that
the daemon *or* git was using.  Remember, the situations where the
daemon leaves fd#2 open pointing at *the wrong thing* aren't the real
problem - you could easily say that's the daemon leaving the
environment in an insane state.  The problem situation is when git
opened some random file, and it *happened* to get assigned fd#2, and
then git incorrectly assumed that writing to fd#2 would not corrupt a
file that it opened.

Does this sound rare?  It is!  But it's also hellish to debug when it
happens, precisely because of its rarity.  For example, in one case, I
had this problem because an sfdisk process started by my custom
/sbin/init ran into a minor warning, and printed it to fd#2.
Unfortunately, because /sbin/init had opened sfdisk with fds 0/1/2
closed, fd#2 ended up being the very disk it was partitioning.  The
boot sector ended up getting overwritten with a warning message in
something like 1 out of 100 cases, and the computer wouldn't boot.
ARGH.  Easy to debug, once you think to read the boot sector as
plaintext.  But that's not the first thing you think to do.

Anyway, I personally think that given how incredibly cheap this
operation is to do, and how startlingly painful it is to debug when it
*is* a problem, that it would be nice if every program just did this
by default.  I would personally feel fine if such a thing ended up in
libc or the kernel, although presumably that would violate POSIX.

Yes, it would also be fine to have every *daemon* make sure it opens
/dev/null instead of just closing fd 0/1/2.  But it's harmless to have
both.

(As for the non-builtin git commands, isn't this an advantage of
having everything get run through the main /usr/bin/git wrapper?)

Have fun,

Avery

  reply	other threads:[~2008-08-27  5:02 UTC|newest]

Thread overview: 37+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-08-25  8:28 [PATCH] Fix start_command() pipe bug when stdin is closed Karl Chen
2008-08-25 10:44 ` Johannes Sixt
2008-08-25 11:49   ` Paolo Bonzini
2008-08-25 12:00     ` [PATCH v2] fix start_command() " Paolo Bonzini
2008-08-25 13:12       ` Johannes Sixt
2008-08-25 13:37         ` [PATCH v2 properly indented] " Paolo Bonzini
2008-08-25 16:00           ` Karl Chen
2008-08-26  0:06             ` Junio C Hamano
2008-08-26  6:09           ` Junio C Hamano
2008-08-26  6:33             ` Johannes Sixt
2008-08-26  6:45             ` Paolo Bonzini
2008-08-26  6:48             ` [PATCH] be paranoid about closed stdin/stdout/stderr Paolo Bonzini
2008-08-26  6:57               ` Johannes Sixt
2008-08-26  7:40                 ` Stephen R. van den Berg
2008-08-27  5:01                   ` Avery Pennarun [this message]
2008-08-27  9:18                     ` Stephen R. van den Berg
2008-08-27 12:36                       ` Paolo Bonzini
2008-08-27 15:20                         ` [PATCH v4] make git-shell " Paolo Bonzini
2008-08-27 17:22                           ` Stephen R. van den Berg
2008-08-27 17:27                         ` [PATCH] be " Junio C Hamano
2008-08-28 13:17                           ` Paolo Bonzini
2008-08-28 13:58                             ` Stephen R. van den Berg
2008-08-27 18:22                       ` Avery Pennarun
2008-08-28 12:21                         ` Nick Andrew
2008-08-28 12:52                           ` Stephen R. van den Berg
2008-08-26 17:38                 ` Junio C Hamano
2008-08-26 18:33                   ` Paolo Bonzini
2008-08-26 22:42                     ` Junio C Hamano
2008-08-26 23:04                       ` Junio C Hamano
2008-08-26 23:10                         ` Stephen R. van den Berg
2008-08-27  3:05                         ` Karl Chen
2008-08-27  4:38                           ` Paolo Bonzini
2008-08-27  9:04                           ` Stephen R. van den Berg
2008-08-27  6:35                     ` Johannes Sixt
2008-08-27  8:20                       ` Paolo Bonzini
2008-08-27  2:04                   ` Nick Andrew
2008-08-25 15:56   ` [PATCH] Fix start_command() pipe bug when stdin is closed Karl Chen

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=32541b130808262201v4d7c1aa5r781720a80b79fcd0@mail.gmail.com \
    --to=apenwarr@gmail.com \
    --cc=bonzini@gnu.org \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=j.sixt@viscovery.net \
    --cc=quarl@cs.berkeley.edu \
    --cc=srb@cuci.nl \
    /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 NNTP newsgroup(s).