From: "Michael Kerrisk (man-pages)" <mtk.manpages-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
To: Rich Felker <dalias-8zAoT0mYgF4@public.gmane.org>
Cc: Michael Kerrisk
<mtk.manpages-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>,
"linux-man-u79uwXL29TY76Z2rM5mHXA@public.gmane.org"
<linux-man-u79uwXL29TY76Z2rM5mHXA@public.gmane.org>
Subject: Re: Very bad advice in man 2 dup2
Date: Thu, 12 Jun 2014 20:53:38 +0200 [thread overview]
Message-ID: <CAKgNAkjdM2ZLY6B4==9Pz1kz4X48NTiq7rnSttbALwJV8ru79g@mail.gmail.com> (raw)
In-Reply-To: <CAKgNAkhFJ6-_RKfTFXYxAAU3jw-ij9ojgeO9G=V9L-WgKZQ62w-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
Another ping....
On Thu, Jun 5, 2014 at 2:54 PM, Michael Kerrisk (man-pages)
<mtk.manpages-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
> Rich, Ping!
>
> On Fri, May 30, 2014 at 11:15 AM, Michael Kerrisk (man-pages)
> <mtk.manpages-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
>> Hi Rich,
>>
>> For discussions like this, it really is very important to CC the list,
>> so that there's an archived record of the reasons for the change.
>> I've concatenated your two mails below.
>>
>> I agree that the page needs to be fixed; thanks for the report!
>> I am mulling over what the best fix is. One proposal below.
>>
>> On 05/29/2014 11:02 PM, Rich Felker wrote:
>>> Hi,
>>>
>>> The following text appears in the man page for dup2 and dup3:
>>>
>>> If newfd was open, any errors that would have been reported at
>>> close(2) time are lost. A careful programmer will not use dup2()
>>> or dup3() without closing newfd first.
>>>
>>> Such use of close is very bad advice, as it introduces a race
>>> condition during which the file descriptor could be re-assigned and
>>> subsequently clobbered by dup2/dup3.
>>
>> Agreed. A more fundamental problem in the man page is that
>> it does not mention the atomicity of dup2() (and dup3()),
>> and why that is needed to avoid the race condition.
>> I'll add some words on that.
>>
>>> This type of bug can lead to
>>> serious data leaks and/or data loss. The whole point of dup2/dup3 is
>>> to _atomically_ replace a file descriptor.
>>>
>>> For the most part there are no meaningful errors which close can
>>> return, probably only obscure NFS behavior with bad caching settings
>>> which are really not handlable by applications anyway, so I feel it
>>> would be best to just drop this text (or find a way to detect such
>>> errors without close, perhaps using fsync, and recommend that).
>>
>> On 05/30/2014 07:23 AM, Rich Felker wrote:
>> [...]
>>> Here is a proposed alternate text that was recommended to me by a user
>>> on Stack Overflow:
>>
>> It'd be great to add URLs when citing such discussions... With some
>> effort, I found
>> http://stackoverflow.com/questions/23440216/race-condition-when-using-dup
>>
>>> A careful programmer will first dup() the target descriptor, then
>>> use dup2()/dup3() to replace the target descriptor atomically, and
>>> finally close the initially duplicated target descriptor. This
>>> replaces the target descriptor atomically, but also retains a
>>> duplicate for closing so that close-time errors may be checked
>>> for. (In Linux, close() should only be called once, as the
>>> referred to descriptor is always closed, even in case of
>>> errno==EINTR.)
>>>
>>> I'm not sure this is the best wording, since it suggests doing a lot
>>> of work that's likely overkill (and useless in the case where the
>>> target descriptor was read-only, for instance). I might balance such
>>> text with a warning that it's an error to use dup2 or dup3 when the
>>> target descriptor is not known to be open unless you know the code
>>> will only be used in single-threaded programs. And I'm a little bit
>>> hesitant on the parenthetical text about close() since the behavior
>>> it's documenting is contrary to the requirements of the upcoming issue
>>> 8 of POSIX,
>>
>> Again citing the Issue 8 discussion would be helpful. Could
>> you tell me where it is? (It could be useful for the change log.)
>>
>>> and rather irrelevant since EINTR cannot happen in Linux's
>>> close() except with custom device drivers anyway.
>>
>> So, how about something like the following (code not
>> compile-tested...):
>>
>> If newfd was open, any errors that would have been reported at
>> close(2) time are lost. If this is of concern, then—unless the
>> program is single-threaded and does not allocate file descrip‐
>> tors in signal handlers—the correct approach is not to close
>> newfd before calling dup2(), because of the race condition
>> described above. Instead, code something like the following
>> could be used:
>>
>> /* Obtain a duplicate of 'newfd' that can subsequently
>> be used to check for close() errors; an EBADF error
>> means that 'newfd' was not open. */
>>
>> tmpfd = dup(newfd);
>> if (tmpfd == -1 && errno != EBADF) {
>> /* Handle unexpected dup() error */
>> }
>>
>> /* Atomically duplicate 'oldfd' on 'newfd' */
>>
>> if (dup2(oldfd, newfd) == -1) {
>> /* Handle dup2() error */
>> }
>>
>> /* Now check for close() errors on the file originally
>> referred to by 'newfd' */
>>
>> if (tmpfd != -1) {
>> if (close(tmpfd) == -1) {
>> /* Handle errors from close */
>> }
>> }
>>
>> Cheers,
>>
>> Michael
>>
>>
>> --
>> Michael Kerrisk
>> Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/
>> Linux/UNIX System Programming Training: http://man7.org/training/
>
>
>
> --
> Michael Kerrisk
> Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/
> Linux/UNIX System Programming Training: http://man7.org/training/
--
Michael Kerrisk
Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/
Linux/UNIX System Programming Training: http://man7.org/training/
--
To unsubscribe from this list: send the line "unsubscribe linux-man" in
the body of a message to majordomo-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
next prev parent reply other threads:[~2014-06-12 18:53 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <20140529210242.GP507@brightrain.aerifal.cx>
[not found] ` <20140529210242.GP507-C3MtFaGISjmo6RMmaWD+6Sb1p8zYI1N1@public.gmane.org>
2014-05-30 9:15 ` Very bad advice in man 2 dup2 Michael Kerrisk (man-pages)
[not found] ` <53884C27.7020207-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
2014-06-05 12:54 ` Michael Kerrisk (man-pages)
[not found] ` <CAKgNAkhFJ6-_RKfTFXYxAAU3jw-ij9ojgeO9G=V9L-WgKZQ62w-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2014-06-12 18:53 ` Michael Kerrisk (man-pages) [this message]
[not found] ` <CAKgNAkjdM2ZLY6B4==9Pz1kz4X48NTiq7rnSttbALwJV8ru79g-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2014-06-29 3:05 ` Rich Felker
[not found] ` <20140629030554.GW179-C3MtFaGISjmo6RMmaWD+6Sb1p8zYI1N1@public.gmane.org>
2014-06-29 7:46 ` Michael Kerrisk (man-pages)
[not found] ` <53AFC455.8080707-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
2014-06-29 15:07 ` Rich Felker
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='CAKgNAkjdM2ZLY6B4==9Pz1kz4X48NTiq7rnSttbALwJV8ru79g@mail.gmail.com' \
--to=mtk.manpages-re5jqeeqqe8avxtiumwx3w@public.gmane.org \
--cc=dalias-8zAoT0mYgF4@public.gmane.org \
--cc=linux-man-u79uwXL29TY76Z2rM5mHXA@public.gmane.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 NNTP newsgroup(s).